开发者

QPushButton FocusIn generates which signal?

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开发者_如何学C 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:

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

You can then connect to that signal in your client code. Also, if necessary, you can use designer's Promote To feature to promote each button to the FocusEmittingButton type. You'll only need to subclass once, and then make sure the different buttons are all of the same type.

Option 2 - Use QApplication.focusChanged

You 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 the QApplication.focusChanged signal and then respond in a handler.


There is another way to receive focus signals in a parent widget. 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜