简体   繁体   English

PyQt5:我如何“收集”或“接收”发射的信号?

[英]PyQt5: How do I “collect” or “receive” an emitted signal?

In my code, I have 2 classes in 2 separate files.在我的代码中,我在 2 个单独的文件中有 2 个类。 I have a signal testSignal , a button pushButton and I connected the button like this:我有一个信号testSignal ,一个按钮pushButton ,我像这样连接按钮:

testSignal = QtCore.pyqtSignal()

pushButton.pressed.connect(buttonPressed)

def buttonPressed(self):
    testSignal.emit()

Now what I want to do is "receive" the emitted signal in the class/file, but I'm somewhat in the dark on how emit() actually works.现在我想要做的是“接收”类/文件中发出的信号,但我对 emit() 的实际工作原理有些不知所措。 Does anyone have any links for guides for the emit() function or can help somehow?有没有人有任何关于 emit() function 指南的链接或者可以以某种方式提供帮助?

Thanks谢谢

[Py]Qt signals must be declared at the class level, as they become "active" only whenever a new class instance is created. [Py]Qt 信号必须在 class 级别声明,因为它们只有在创建新的 class 实例时才会“激活”。 Also, they can only be used for classes that inherit from QObject (including QWidget subclasses), and using them with standard python object classes will not work.此外,它们只能用于从 QObject 继承的类(包括 QWidget 子类),并且将它们与标准 python object类一起使用将不起作用

class SomeWindow(QtWidgets.QWidget):
    testSignal = QtCore.pyqtSignal()
    def __init__(self):
        super().__init__()
        # ...
        pushButton.pressed.connect(self.buttonPressed)
        # connect the custom signal with an *instance method*
        self.testSignal.connect(self.someInstanceFunction)

    def buttonPressed(self):
        # emit the signal
        self.testSignal.emit()

    def someInstanceFunction(self):
        print('hello from the instance!')


def someAnonymousFunction():
    print('hello from outside!')


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = SomeWindow()
    # connect the signal with an anonymous function
    window.testSignal.connect(someAnonymousFunction)
    sys.exit(app.exec_())

Obviously the example above doesn't make a lot of sense, as custom signals are normally used to communicate between instances of (possibly different) classes, but that's just for explanation purposes.显然,上面的示例没有多大意义,因为自定义信号通常用于在(可能不同的)类的实例之间进行通信,但这仅用于解释目的。 Also, you could "concatenate" signals (as long as their signature is compatible):此外,您可以“连接”信号(只要它们的签名兼容):

class SomeWindow(QtWidgets.QWidget):
    testSignal = QtCore.pyqtSignal()
    def __init__(self):
        super().__init__()
        # ...
        # emit the testSignal when the button emits its pressed signal
        pushButton.pressed.connect(self.testSignal)
        self.testSignal.connect(self.someInstanceFunction)

    # ...

Note that if you want to do something when the user clicks a button, you should use clicked() , not the pressed() one and the difference is very important: it's common convention that a button is considered clicked only when the user presses and releases the mouse button while still in the button area, so that the "click" can be avoided if the mouse button is released outside it.请注意,如果您想在用户单击按钮时执行某些操作,则应该使用clicked() ,而不是pressed() ,并且区别非常重要:通常的约定是,仅当用户按下按钮时才认为按钮被单击并且在按钮区域内释放鼠标按钮,这样可以避免在按钮区域释放鼠标按钮时“点击”。 If you connect to pressed() , the signal will be emitted as soon as the mouse button is just pressed down on the button, which is not considered a standard behavior.如果您连接到pressed() ,只要鼠标按钮按下按钮,就会发出信号,这不被认为是标准行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM