PDA

View Full Version : How to replace image on hover



JimDaniel
24th September 2007, 17:40
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!

marcel
24th September 2007, 17:47
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.

wysota
24th September 2007, 18:16
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.

JimDaniel
24th September 2007, 18:55
Thanks a lot for the suggestions! I'll see if I can get it working.

JimDaniel
24th September 2007, 19:39
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...



void HeaderBar::enterEvent(QEvent * event)
{
logout->setIcon(*logout_hover);
}

void HeaderBar::leaveEvent(QEvent * event)
{
logout->setIcon(*logout_default);
}


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

marcel
24th September 2007, 20:13
I guess that's OK, but if you have higher quality/bigger resolution pictures then you'll hsvr yo paint them in paint event.

wysota
24th September 2007, 20:19
I don't see a point in using a pointer to a pixmap. Pixmaps are implicitly shared, so using a pointer yields no advantage.