PyQt5:如何 "collect" 或 "receive" 发射信号?
PyQt5: How do I "collect" or "receive" an emitted signal?
在我的代码中,我在 2 个单独的文件中有 2 个 类。我有一个信号 testSignal
,一个按钮 pushButton
,我这样连接按钮:
testSignal = QtCore.pyqtSignal()
pushButton.pressed.connect(buttonPressed)
def buttonPressed(self):
testSignal.emit()
现在我想做的是“接收”class/file 中发出的信号,但我对 emit() 的实际工作原理有些不知所措。有没有人有 emit() 函数指南的任何链接或可以提供帮助?
谢谢
[Py]Qt 信号 必须 在 class 级别声明,因为只有在创建新的 class 实例时它们才会“激活” .此外,它们只能用于继承自 QObject 的 classes(包括 QWidget subclasses),并与标准 python object
class 一起使用es 不会工作。
class SomeWindow(QtWidgets.QWidget):
<b>testSignal = QtCore.pyqtSignal()</b>
def __init__(self):
super().__init__()
# ...
pushButton.pressed.connect(self.buttonPressed)
# connect the custom signal with an *instance method*
<b>self.testSignal.connect(self.someInstanceFunction)</b>
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
<b>window.testSignal.connect(someAnonymousFunction)</b>
sys.exit(app.exec_())
显然上面的示例没有多大意义,因为自定义信号通常用于在(可能不同的)classes 的实例之间进行通信,但这只是为了解释目的。此外,您可以“连接”信号(只要它们的签名兼容):
class SomeWindow(QtWidgets.QWidget):
<b>testSignal = QtCore.pyqtSignal()</b>
def __init__(self):
super().__init__()
# ...
# emit the testSignal when the button emits its pressed signal
<b>pushButton.pressed.connect(self.testSignal)</b>
self.testSignal.connect(self.someInstanceFunction)
# ...
请注意,如果您想在用户 单击 按钮时执行某些操作,您应该使用 clicked()
,而不是 pressed()
和区别非常重要:通常的惯例是,只有当用户按下 并且 仍在按钮区域时释放鼠标按钮时,才认为按钮被单击,这样,如果鼠标按钮在 之外释放,则可以避免“点击”。如果您连接到 pressed()
,只要鼠标按钮 按下 按钮就会立即发出信号,这不是标准行为。
在我的代码中,我在 2 个单独的文件中有 2 个 类。我有一个信号 testSignal
,一个按钮 pushButton
,我这样连接按钮:
testSignal = QtCore.pyqtSignal()
pushButton.pressed.connect(buttonPressed)
def buttonPressed(self):
testSignal.emit()
现在我想做的是“接收”class/file 中发出的信号,但我对 emit() 的实际工作原理有些不知所措。有没有人有 emit() 函数指南的任何链接或可以提供帮助?
谢谢
[Py]Qt 信号 必须 在 class 级别声明,因为只有在创建新的 class 实例时它们才会“激活” .此外,它们只能用于继承自 QObject 的 classes(包括 QWidget subclasses),并与标准 python object
class 一起使用es 不会工作。
class SomeWindow(QtWidgets.QWidget):
<b>testSignal = QtCore.pyqtSignal()</b>
def __init__(self):
super().__init__()
# ...
pushButton.pressed.connect(self.buttonPressed)
# connect the custom signal with an *instance method*
<b>self.testSignal.connect(self.someInstanceFunction)</b>
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
<b>window.testSignal.connect(someAnonymousFunction)</b>
sys.exit(app.exec_())
显然上面的示例没有多大意义,因为自定义信号通常用于在(可能不同的)classes 的实例之间进行通信,但这只是为了解释目的。此外,您可以“连接”信号(只要它们的签名兼容):
class SomeWindow(QtWidgets.QWidget):
<b>testSignal = QtCore.pyqtSignal()</b>
def __init__(self):
super().__init__()
# ...
# emit the testSignal when the button emits its pressed signal
<b>pushButton.pressed.connect(self.testSignal)</b>
self.testSignal.connect(self.someInstanceFunction)
# ...
请注意,如果您想在用户 单击 按钮时执行某些操作,您应该使用 clicked()
,而不是 pressed()
和区别非常重要:通常的惯例是,只有当用户按下 并且 仍在按钮区域时释放鼠标按钮时,才认为按钮被单击,这样,如果鼠标按钮在 之外释放,则可以避免“点击”。如果您连接到 pressed()
,只要鼠标按钮 按下 按钮就会立即发出信号,这不是标准行为。