QPushButton FocusIn 生成哪个信号?

发布于 2024-08-20 07:21:45 字数 1239 浏览 5 评论 0原文

我正在创建一个小型 PyQt 应用程序,并陷入了 MouseOver 效果。

我有一个 QMainWindow,它有三个按钮,分别名为 createProfileButton、downloadPackagesButton 和 installPackagesButton。所有这些都是 QPushButton 类型

现在我创建了一个标签,当有人将鼠标悬停在这些按钮上时,它将保存文本。我检查了文档,发现可以使用

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

按钮的 方法来处理它。现在,这意味着我必须为三个按钮中的每一个扩展 QPushButton,并且每个按钮都必须为一个类的一个对象。我尝试寻找当鼠标悬停或离开按钮时发出的信号,但徒劳。我在网上得到的所有帮助都是实现这两种方法。

扩展一个类并分别创建一个类不是一种矫枉过正吗?如果有信号就好了,不幸的是,我找不到任何信号。

所以我检查了整个继承层次结构,发现没有 FocusInFocusOut 的信号

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.

So I checked the whole inheritance hierarchy and found no signal for FocusIn and FocusOut

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-08-27 07:21:45

正如您所说,此功能不存在任何信号。您有两个基本选择。

选项 1 - 子类:

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

然后您可以在客户端代码中连接到该信号。此外,如有必要,您可以使用设计器的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:

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.

你在我安 2024-08-27 07:21:45

还有另一种方法可以在父窗口小部件中接收焦点信号。您可以添加事件过滤器:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文