forked from Sicatriz/CppGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.cpp
41 lines (35 loc) · 1.09 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
#include "button.h"
#include "qbrush.h"
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::darkCyan);
setBrush(brush);
// draw the 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);
// allow responding to hover events
setAcceptHoverEvents(true);
}
void Button::mousePressEvent(QGraphicsSceneMouseEvent *event){
emit clicked();
}
void Button::hoverEnterEvent(QGraphicsSceneHoverEvent *event){
// change color to cyan
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::cyan);
setBrush(brush);
}
void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
// change color to dark cyan
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush);
}