Results 1 to 3 of 3

Thread: Drawing in a QPushButton: Clicked(), Pressed(), Released()

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drawing in a QPushButton: Clicked(), Pressed(), Released()

    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?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Drawing in a QPushButton: Clicked(), Pressed(), Released()

    Try reimplementing mousePressEvent() & mouseReleaseEvent().
    Qt Code:
    1. void MyPushButton::paintEvent(QPaintEvent *e){
    2. . . .
    3. painter.setBrush(m_color);
    4. . . .
    5. }
    6.  
    7.  
    8. void MyPushButton::mousePressEvent(QMouseEvent *e){
    9. m_color=Qt::green;
    10. QPushButton::mousePressEvent(e);
    11. }
    12.  
    13. void MyPushButton::mouseReleaseEvent(QMouseEvent *e){
    14. m_color=Qt::white;
    15. QPushButton::mouseReleaseEvent(e);
    16. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    bruceariggs (26th October 2011)

  4. #3
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing in a QPushButton: Clicked(), Pressed(), Released()

    Thanks Norobro! It works beautifully now.

Similar Threads

  1. Replies: 4
    Last Post: 16th September 2011, 03:10
  2. Replies: 2
    Last Post: 12th September 2011, 16:40
  3. Replies: 5
    Last Post: 26th January 2011, 20:12
  4. Removing pressed effect from a QPushButton
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2010, 12:43
  5. Raise key pressed event when button is clicked?
    By newstead in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2009, 14:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.