aarelovich
23rd July 2009, 21:29
Hi:
I need I want implement a basic button because I'm building an application that only uses QGraphicsItems. I need one button so here is the code I wrote to get started:
Button::Button(int w, int h, QString cap){
caption = cap;
width = w;
height = h;
color.setRed(40);
color.setGreen(40);
color.setBlue(40);
pen.setColor(color.lighter(130));
pen.setWidth(2);
}
QRectF Button::boundingRect() const{
double margin = 2;
return QRectF(0,0,width+margin,height+margin);
}
void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
Q_UNUSED(widget);
if (option->state & QStyle::State_Sunken){
qDebug() << "I'm being pressed";
painter->setRenderHints(QPainter::Antialiasing|QPainter::Te xtAntialiasing);
QBrush brush(color);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawRoundedRect(QRect(0,0,width,height),20.0,20.0, Qt::RelativeSize);
}
else{
painter->setRenderHints(QPainter::Antialiasing|QPainter::Te xtAntialiasing);
QBrush brush(color);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawRoundedRect(QRect(0,0,width,height),20.0,20.0, Qt::RelativeSize);
}
}
void Button::mousePressEvent(QGraphicsSceneMouseEvent *event){
if (event->button() != Qt::LeftButton){
event->ignore();
return;
}
qDebug() << "press";
QGraphicsItem::mousePressEvent(event);
update();
}
void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
qDebug() << "release";
emit(clicked());
QGraphicsItem::mouseReleaseEvent(event);
}
The problem is that when I press the button all I get is the message "press" and I don't get "release" or the "I'm being pressed" message. I've compared it over and over to the elastic node's example and I can't find any differences that would make mine not work. What's even more bizarre I allready have another implemented class that also uses de the mouseRelease and mousePress events to change cursors and it works perfectly.
Any one has any idea of what I could be doing wrong?
Thanks for any help.
I need I want implement a basic button because I'm building an application that only uses QGraphicsItems. I need one button so here is the code I wrote to get started:
Button::Button(int w, int h, QString cap){
caption = cap;
width = w;
height = h;
color.setRed(40);
color.setGreen(40);
color.setBlue(40);
pen.setColor(color.lighter(130));
pen.setWidth(2);
}
QRectF Button::boundingRect() const{
double margin = 2;
return QRectF(0,0,width+margin,height+margin);
}
void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
Q_UNUSED(widget);
if (option->state & QStyle::State_Sunken){
qDebug() << "I'm being pressed";
painter->setRenderHints(QPainter::Antialiasing|QPainter::Te xtAntialiasing);
QBrush brush(color);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawRoundedRect(QRect(0,0,width,height),20.0,20.0, Qt::RelativeSize);
}
else{
painter->setRenderHints(QPainter::Antialiasing|QPainter::Te xtAntialiasing);
QBrush brush(color);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawRoundedRect(QRect(0,0,width,height),20.0,20.0, Qt::RelativeSize);
}
}
void Button::mousePressEvent(QGraphicsSceneMouseEvent *event){
if (event->button() != Qt::LeftButton){
event->ignore();
return;
}
qDebug() << "press";
QGraphicsItem::mousePressEvent(event);
update();
}
void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
qDebug() << "release";
emit(clicked());
QGraphicsItem::mouseReleaseEvent(event);
}
The problem is that when I press the button all I get is the message "press" and I don't get "release" or the "I'm being pressed" message. I've compared it over and over to the elastic node's example and I can't find any differences that would make mine not work. What's even more bizarre I allready have another implemented class that also uses de the mouseRelease and mousePress events to change cursors and it works perfectly.
Any one has any idea of what I could be doing wrong?
Thanks for any help.