Does QPushButton FocusIn generate a signal?

I am building a small PyQt application and am stuck in the MouseOver effect.

I have a QMainWindow that has three buttons named createProfileButton , downloadPackagesButton and installPackagesButton . All of them are of type QPushButton

Now I have created a shortcut that will contain text when someone hovers over any of these buttons. I checked the documentation and found out that it can be processed by reinstalling

  • focusInEvent (self, QFocusEvent)
  • focusOutEvent (self, QFocusEvent)

button methods. Now this means that I have to extend the QPushButton for each of the three buttons, and each of them must have an object for one class. I tried to find the signal that is emitted when the mouse freezes or moves away from the button, but in vain. All the help I received on the network was to implement these two methods.

Doesn't extend the class and create one of them redundant? The signal would be accurate, unfortunately, I could not find any signal.

So, I checked the entire inheritance hierarchy and did not find a signal for FocusIn and FocusOut

+4
source share
2 answers

As you pointed out, there are no signals for this function. You have two main options.

Option 1 - Subclass:

 class FocusEmittingButton(QPushButton): #... def focusInEvent(self, event): # emit your signal 

Then you can connect to this signal in the client code. Additionally, if necessary, you can use the Promote To constructor to promote each button to a FocusEmittingButton type. You will only need to subclass once, and then make sure that the different buttons are of the same type.

Option 2 - use QApplication.focusChanged

You can also use QApplication.focusChanged(oldQWidget, newQWidget) . By doing so, you do not need to subclass and override focus events. Instead, you connect to the QApplication.focusChanged signal, and then respond in the handler.

+4
source

There is another way to get focus signals in parent widgets. You can add an event filter:

 class MyParent(QWidget): def __init__(self): #... self.mybutton.installEventFilter() def eventFilter(self, obj, event): if obj is self.mybutton and event.type() == QEvent.FocusOut: #... return QWidget.eventFilter(self, obj, event) 
+1
source

Source: https://habr.com/ru/post/1300213/


All Articles