Results 1 to 7 of 7

Thread: QPushButton and mouse

  1. #1
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPushButton and mouse

    Hi everybody,
    I'm absolutely a Qt4 newbie.
    I have the following problem: I have a QPushButton, with a icon image (iconSize: 90x90px) I would like to associate another icon image when I pass the mouse over this button, without clicking.
    I tried with style sheet but it doesn't works (in the designer it works but the image is not in 90x90px, but in the application it not works). I tried that but nothing:

    css Code:
    1. QPushButton: hover {
    2. background-image: url("mypicturefromresources.png");
    3. background-position: center;
    4. width: 90px;
    5. height: 90px;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Is there another suggestion to do it in a easy way?
    Thank you very much.
    Last edited by jpn; 25th January 2009 at 17:15. Reason: added [highlight] tags

  2. #2
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QPushButton and mouse

    Hi,

    if I undesrtood you correctly then reimplement the void QWidget::mouseMoveEvent ( QMouseEvent * event ) shoudl help you.

    Radek

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

    lixo1 (28th January 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QPushButton and mouse

    Or perhaps better QWidget::enterEvent

  5. The following user says thank you to drhex for this useful post:

    lixo1 (28th January 2009)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushButton and mouse

    You are setting the push button to be 90x90 which will probably not happen if it is inside a layout. The icon doesn't occupy the whole area of the button (for instance there are margins involved).

    By the way, check out the "Common mistakes" section of Qt Style Sheets reference.

  7. The following user says thank you to wysota for this useful post:

    lixo1 (28th January 2009)

  8. #5
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and mouse

    Hi everyboy,
    First of all thank you very much for all helps.
    I think that the best way is overriting the mouseMoveEvent or maybe the enterEvent, but I'm substancially new, so I tried that:

    Qt Code:
    1. void MyMainWindow::enterEvent(QEvent *){ //it will overrite the default enterEvent
    2. //for example
    3. ui->pushbutton_calculate->setIcon(QIcon("...my icon"));
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 
    This example will works everytime I pass the mouse over my MainWindow, so, how to determine if the mouse is over my pushbutton_calculate?
    What I have to do?
    Thank you another time.

  9. #6
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and mouse

    It's because you didn't tell the program, that this event is for your pushbutton. Recently I made something similar in my program, but I used QGraphicsview, and HoverEvent.

    I don't find this event at QPushButton.

    Maybe you should do: write over the enterEvent(QEvent* event) of your pushbutton with explicit conversation to QHoverEvent*. To do this, you must make a subclass of QPushButton.

  10. The following user says thank you to greenvirag for this useful post:

    lixo1 (28th January 2009)

  11. #7
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and mouse

    Hi everybody,
    So thank you very much, for yours opinions and explanations, now I really understand the Qt mechanism!
    Cheers.

Similar Threads

  1. QPushButton not calling repaint when needed?
    By Enygma in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2010, 17:03
  2. Changing QPushButton text colour with mouseMoveEvent
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 16:53
  3. Determining when mouse over widget without events
    By Kimmo in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2007, 10:48
  4. QPushButton:: Handle right mouse button events. Easyway?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 28th February 2007, 16:56
  5. QPushButton mouse events
    By Twey in forum Newbie
    Replies: 5
    Last Post: 15th January 2006, 16:41

Tags for this Thread

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.