Search code examples
qtuser-interfaceqtguimousehover

Qt Hovering over a button and triggering a function?


What is the proper way of triggering a function if the mouse cursor hovers over a QButton?

To be more precise I have a label called statusLabel which should show message whenever I hover my mouse cursor over a button and should revert back to empty string whenever the mouse is not over it.


Solution

  • You have to create your own class, derived of QPushButton (or whichever class you want to capture hover events in).

    In this class, you can override QWidget::enterEvent() to detect when the mouse hovers your widget and call your function. What you can for example do is emit a custom signal that will trigger the erasing of your label.