PDA

View Full Version : Drawing in a QPushButton: Clicked(), Pressed(), Released()



bruceariggs
26th October 2011, 00:31
I have a class that inherits from QPushButton, and I'm overriding the paint method to draw a triangle (drawPolygon).

That works fine.

I'm trying to make it where when the user clicks the mouse button down on the button, the triangle is green, and when the user releases the mouse button, the arrow goes back to white.

I tried doing a pressed() event turning it green, and a released() event turning it back to white, but I don't get the results I wanted. It turns green after I let go of the mouse button, and when I move the mouse off from over the button (not hovering anymore), it turns back to white.

What is the best way to go about having the button draw a green triangle while the mouse button is down, and a white triangle when the mouse button was released?

norobro
26th October 2011, 04:09
Try reimplementing mousePressEvent() & mouseReleaseEvent().

void MyPushButton::paintEvent(QPaintEvent *e){
. . .
painter.setBrush(m_color);
. . .
}


void MyPushButton::mousePressEvent(QMouseEvent *e){
m_color=Qt::green;
QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e){
m_color=Qt::white;
QPushButton::mouseReleaseEvent(e);
}

bruceariggs
26th October 2011, 15:41
Thanks Norobro! It works beautifully now.