Results 1 to 7 of 7

Thread: How to replace image on hover

  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to replace image on hover

    Hi, I've been researching how to replace an image with another image when hovering over a widget, but I can't seem to figure it out (following the QStyle example) Does anyone have any pointers? A QPushButton is what I'm working with. Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to replace image on hover

    see QPushButton::enterEvent() and QPushButton::leaveEvent().

    Override them and on enter even set a QPixmap member in your QPushButton subclass to the image you want to paint.

    On leave event you have to revert to the old image.

    In paintEvent just paint the member QPixmap.

  3. #3
    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: How to replace image on hover

    Or activate hover events for your widget by setting an appropriate attribute and then reimplement paintEvent, check there whether the cursor is currently hovering over the widget and act accordingly.

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to replace image on hover

    Thanks a lot for the suggestions! I'll see if I can get it working.

  5. #5
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to replace image on hover

    Thanks a lot for the suggestions! I'll see if I can get it working.

    EDIT: ok, after fooling with the enter/leave events for a little while I came up with this, which works, but I wanted to see if you guys found anything wrong with it...

    Qt Code:
    1. void HeaderBar::enterEvent(QEvent * event)
    2. {
    3. logout->setIcon(*logout_hover);
    4. }
    5.  
    6. void HeaderBar::leaveEvent(QEvent * event)
    7. {
    8. logout->setIcon(*logout_default);
    9. }
    To copy to clipboard, switch view to plain text mode 

    ...where "logout" is my own QPushButton class.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to replace image on hover

    I guess that's OK, but if you have higher quality/bigger resolution pictures then you'll hsvr yo paint them in paint event.

  7. #7
    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: How to replace image on hover

    I don't see a point in using a pointer to a pixmap. Pixmaps are implicitly shared, so using a pointer yields no advantage.

Similar Threads

  1. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  2. Explanation to Image Formats
    By sincnarf in forum Qt Programming
    Replies: 13
    Last Post: 6th July 2007, 17:02
  3. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 09:24
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.