-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.cpp
41 lines (35 loc) · 1.12 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 <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::darkGreen);
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::green);
setBrush(brush);
}
void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
// change color to dark cyan
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush);
}