I am creating a small PyQt application and got stuck up in MouseOver effect.
I have a QMainWindow which has three buttons named createProfileButton, downloadPackagesButton and installPackagesButton. All these are of type QPushButton
Now I have created a Label which will hold the text when someone hovers the mouse over any of these button. I checked the documentation and came to know that it can be handled using over-riding
- focusInEvent(self, QFocusEvent)
- focusOutEvent(self, QFocusEvent)
methods of the button. Now this means that I have to extend QPushButton for each of the three buttons and each one of them have to an object for one class. I tried hunting for the signal which is emitted when mouse is hovered or taken away from the button, but in vain. All help I got on the net was to implement these two methods.
Isnt extending a class and creating one of each an overkill? A signal would be neat, unfortunately, I couldn’t find any signal.
- http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpushbutton.html – Has no signals
- http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qabstractbutton.html – Has clicked, released, pressed and toggled signals
- http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html – Has only one method
So I checked the whole inheritance hierarchy and found no signal for FocusIn and FocusOut
As you stated, no signals exist for this functionality. You have two basic options.
Option 1 – Subclass:
You can then connect to that signal in your client code. Also, if necessary, you can use designer’s
Promote Tofeature to promote each button to theFocusEmittingButtontype. You’ll only need to subclass once, and then make sure the different buttons are all of the same type.Option 2 – Use
QApplication.focusChangedYou can also leverage
QApplication.focusChanged(oldQWidget, newQWidget). By doing so, you won’t need to subclass and override the focus events. Instead, you connect to theQApplication.focusChangedsignal and then respond in a handler.