PyQt5 - 信号:pyqtSignal没有方法连接

时间:2016-06-04 12:27:58

标签: python-3.x signals pyqt5

sourceforge上找到的此文档中的示例 我遇到了一个错误。

from PyQt5.QtCore import pyqtSignal, pyqtSlot

 def setSignal():        
     signal = pyqtSignal()
     signal.connect(self.myAction)

 @QtCore.pyqtSlot()
 def myAction():
     print("signal triggered")

结果

AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'

1 个答案:

答案 0 :(得分:10)

如您所链接的文档中所述,需要在类级别定义信号:

class Foo(QObject):

    signal = pyqtSignal()

    def connectSignal():        
        self.signal.connect(self.myAction)

    @QtCore.pyqtSlot()
    def myAction():
        print("signal triggered")