Recently, I wanted that QPushButton
can emit a signal, when the mouse pointer enters. How can I make it?
I know that QPushButton has some already defined signal, such as clicked()
, pressed()
, destory()
and so on. But no signal like hover(), enter(), ...
I looked some information about it: Someone said it can be done by css. I don't understand. Can you give me some advice ? Thank you!
Make sure to add ':' after the public keyword
Although @Exa has answered this question, I want to show another solution which does not need to subclass QPushButton and is flexible in use! ( That's what I need in my project)
Step 1/2 : Overriding eventFilter.
LoginWindow.h:
LoginWindow.cpp:
Step 2/2 : Installing eventFilter on target widgets.
If I remember correctly, you need to enable mouse tracking for the button (Qt documentation) and override
QWidget::onEnter()
andQWidget::onLeave()
.You will need to create a custom button class inheriting from QPushButton. You can define signals for mouseEnter and mouseLeave in your custom class and emit them from the
onEnter()
andonLeave()
methods that you need to override.You can use QWidget::enterEvent ( QEvent * event ) for this.
You override this event and send a custom defined signal when ever this event occurs.
First you have to enable mouse tracking for this widget (
setMouseTracking(true)
in the constructor for example).Header file:
Source file:
Where you use your button: