QPushButton FocusIn 生成哪个信号?
我正在创建一个小型 PyQt 应用程序,并陷入了 MouseOver 效果。
我有一个 QMainWindow,它有三个按钮,分别名为 createProfileButton、downloadPackagesButton 和 installPackagesButton。所有这些都是 QPushButton 类型
现在我创建了一个标签,当有人将鼠标悬停在这些按钮上时,它将保存文本。我检查了文档,发现可以使用
- 重写focusInEvent(self, QFocusEvent)
- focusOutEvent(self, QFocusEvent)
按钮的 方法来处理它。现在,这意味着我必须为三个按钮中的每一个扩展 QPushButton,并且每个按钮都必须为一个类的一个对象。我尝试寻找当鼠标悬停或离开按钮时发出的信号,但徒劳。我在网上得到的所有帮助都是实现这两种方法。
扩展一个类并分别创建一个类不是一种矫枉过正吗?如果有信号就好了,不幸的是,我找不到任何信号。
- http://www.riverbankcomputing.co.uk/static/ Docs/PyQt4/html/qpushbutton.html - 没有信号
- http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qabstractbutton.html - 单击、释放、按下和切换信号
- http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html< /a> - 只有一种方法
所以我检查了整个继承层次结构,发现没有 FocusIn
和 FocusOut
的信号
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,此功能不存在任何信号。您有两个基本选择。
选项 1 - 子类:
然后您可以在客户端代码中连接到该信号。此外,如有必要,您可以使用设计器的
Promote To
功能将每个按钮提升为FocusEmittingButton
类型。您只需要子类化一次,然后确保不同的按钮都是相同的类型。选项 2 - 使用
QApplication.focusChanged
您还可以利用
QApplication.focusChanged(oldQWidget, newQWidget)
。通过这样做,您将不需要子类化和覆盖焦点事件。相反,您连接到 QApplication.focusChanged 信号,然后在处理程序中进行响应。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 To
feature to promote each button to theFocusEmittingButton
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 theQApplication.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: