-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.cpp
51 lines (45 loc) · 1.2 KB
/
button.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "button.h"
#include <QGraphicsTextItem>
#include <QBrush>
Button::Button(QString name, QGraphicsItem *parent)
:QGraphicsRectItem(parent)
{
//draw the rect
setRect(0,0,200,50);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkRed);
setBrush(brush);
//draw Text
text = new QGraphicsTextItem(name,this);
int xPos = rect().width()/2 - text->boundingRect().width()/2;
int yPos = rect().height()/2 - text->boundingRect().height()/2;
text->setPos(xPos,yPos);
text->setDefaultTextColor(Qt::white);
//Allow responding to hover
setAcceptHoverEvents(true);
}
void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event)
emit clicked();
}
void Button::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
//change color
if(event){
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::red);
setBrush(brush);
}
}
void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
//change color
if(event){
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkRed);
setBrush(brush);
}
}