Solved QToolButton growing and shrinking like heartbeat vs. leaveEvent
-
Hello Qt community!
Long story short, I made a QToolButton and added it into my QGraphicsScene. When the mouse enters that button, its QSize grows from
size
to2*size
(both directions), then is continuously varying betweensize*5/2
andsize*3/2
(thanks to QPropertyAnimations) until the mouse leaves the Button. It looks as if the button was a beating heart and that's very nice.Additional information (not sure if useful but here it is): I had to set that QToolButton's maximum QSize to
(size*5/2, size*5/2)
, otherwise Qt would set the maximum QSize to(200, 100)
for no reason. Either value is by far exceedingsize*5/2
, and it would be annoying for me to position the button exactly where I want it to be.Problem: since the maximum QSize is
(size*5/2, size*5/2)
, the mouse isn't considered to have left the button until it is completely out of the biggest square: that is, the square(size*5/2, size*5/2)
. So, even if the cursor hovers a place that is sometimes inside the button, and sometimes outside (because of its size periodically changing), the mouse is never considered to have left the button. That's what I want to fix. I'd like Qt to runleaveEvent
whenever the cursor is out of the button, regardless of the maximum size the button can grow to.Is there an easy way to solve this? Thanks a lot!
-
@Pippin
Hello,
In short: no, not an easy way, although it depends on how you define easy.
As a longer answer, you can do it like that:- You put a transparent overlay window/widget on top of your button, which has a fixed size that matches the maximum size the button might have.
- You capture the events from the overlay and dispatch them to the button manually.
Kind regards.
-
@kshegunov Thanks a lot! Quick question: Once its size is settled, how exactly do I make my overlay widget transparent color-wise? Do I need to use a QToolButton with transparent icon?
Edit: Never mind:
QPalette p; p.setColor(QPalette::Background, Qt::red); w->setAutoFillBackground(true); w->setPalette(p);
works.
-
New problem: since my heartbeating QToolButton is inside a QGraphicsScene, even if I re-implement
QWidget::mouseMoveEvent(QMouseEvent*)
, that function is never run. How can I fix that?Edit: In the end, I subclassed QGraphicsScene and re-implemented
QGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent*)
. It's perfect now :) -
Just a thought. For something like this it might be better not to continuously resize the widget but to have a button subclass with a custom paint event and draw it with different sizes in the available area of the widget. You won't have problems with mouse events in this case as the widget size will not actually change. It should be more efficient too since redrawing and resizing will require more work then a custom paint event (resizing a widget can affect all other widgets in a layout).