0

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.

Joe Carr
  • 445
  • 1
  • 10
  • 23

1 Answers1

4

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.

dydil
  • 949
  • 7
  • 20
  • I just found that the QWidget has a Hover trigger, any idea on how to use it? – Joe Carr Dec 12 '16 at 13:19
  • @JoeCarr `QWidget` is a C++ class. What you mean by "has a hover trigger"? You need to read about event filters. It is a good answer. – Dmitry Sazonov Dec 12 '16 at 13:46
  • "ui->nucButton->setAttribute(Qt::WA_Hover, true);" is what I meant by the trigger and the issue I have with it is that I do not know how to use it. Tried it as an if statement checking whether the true value is really true but it didn't work. – Joe Carr Dec 12 '16 at 13:50
  • Look at the documentation for `setAttribute` and `WA_Hover`. It is not related to what you are trying to do. – dydil Dec 12 '16 at 14:06
  • This is true. I just cannot picture it in my ind, how it would work though by overriding enterEven();.... – Joe Carr Dec 12 '16 at 14:16
  • This is basic C++, you will easily find information on your favorite search engine. [This example](http://www.informit.com/articles/article.aspx?p=1405227&seqNum=2) is for Qt 4 but is still applicable (it overrides mousePressEvent instead of enterEvent but the principle is the same). – dydil Dec 12 '16 at 14:42