【问题标题】:Python. Connecting custom class to QThreadPython。将自定义类连接到 QThread
【发布时间】:2017-06-06 09:28:14
【问题描述】:

在我的应用程序中,我的控件类定义如下:

class Controller(QtGui.QWidget, Ui_myUI):

在按钮点击动作的这个类中,我想将 QThread 的finished() 动作连接到Controller 类中定义的done 方法,如下所示:

def applyAction(self):
    self.myTread = MyTestTread()
    self.connect(self.myTread, QtCore.SIGNAL("finished()"), self.done)
    self.myTread.start()

但此代码返回以下错误:

AttributeError: 'Controller' object has no attribute 'connect'

我在这里做错了什么?

【问题讨论】:

    标签: python pyqt4 signals-slots qthread


    【解决方案1】:

    您尝试使用的connect 函数是QObject 的静态方法。这个例子来自docs

    button = QtGui.QPushButton("Call someFunc")
    QtCore.QObject.connect(button, QtCore.SIGNAL ('clicked()'), someFunc)
    

    在我看来,您想将applyAction 中的第二行替换为:

    QtCore.QObject.connect(self.myTread, QtCore.SIGNAL("finished()"), self.done)
    

    请注意,这种语法早已被弃用(自 Qt 4.5 起)。新方法是:

    self.myTread.finished.connect(self.done)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2015-11-12
      相关资源
      最近更新 更多