简体   繁体   English

Python PyQt4发出和接收自定义信号

[英]Python PyQt4 Emitting and Receiving Custom signals

I am aware that there are several questions about this topic. 我知道有关此主题有几个问题。 I have look through most of them but still can't figure out why I am having this problem. 我已经浏览了大多数,但仍然无法弄清楚为什么我会遇到这个问题。

Basically, what I am trying to do is: to show a busy indication progress bar (ie range (0,0) ) using a QThread class. 基本上,我想做的是:使用QThread类显示繁忙的指示进度栏(即range (0,0) )。

I am using mysignal = QtCore.pyqtSignal() to create my signal. 我正在使用mysignal = QtCore.pyqtSignal()创建我的信号。 And after that I use mysignal.emit() to transmit that signal. 之后,我使用mysignal.emit()传输该信号。 The signal is used to inform the main thread to stop the progress bar action. 该信号用于通知主线程停止进度条操作。

This is how I connect that signal to one of my function mysignal.connect(myfunction) . 这就是我将该信号连接到函数mysignal.connect(myfunction) But whenever I run my script, I have this error message. 但是,每当我运行脚本时,都会出现此错误消息。

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

What is likely the cause of my problem? 我的问题可能是什么原因造成的?

FYI: I am using windows 8.1 with python 2.7.5 仅供参考:我在Windows 8.1和python 2.7.5中使用

EDIT: 编辑:

Simplify version of my code below: 下面简化我的代码版本:

class Main(QtGui.QMainWindow):
    .......
    .......
    self.progressBar = QtGui.QProgressBar(self)
    self.progressBar.setRange(0,1)
    button = QtGui.QPushButton('button')
    button.clicked.connect(self.onStart())

    def onStart(self):
        self.progressBar.setRange(0,0)
        self.LoadPage = LoadingThread()
        self.LoadPage.taskFinished.connect(self.onFinished)
        self.LoadPage.Load()

    def onFinished(self):
        self.progressBar.setRange(0,1)


class LoadingThread(QtCore.QThread):
    def __init__(self):   
        QtCore.QThread.__init__(self)    
        self.taskFinished = QtCore.pyqtSignal()
    def Load(self):
        #My stuffs here (to be executed duing the progress bar busy)
        time.sleep(5)
        self.taskFinished.emit()

This smells like you're adding the signal to the class instance, rather than the class, which is not possible. 闻起来就像您将信号添加到类实例而不是类上,这是不可能的。 Tried this myself just a few hours ago, getting the same error message... 几个小时前我自己做了一次尝试,收到了同样的错误消息...

Make sure the signal is defined on the class: 确保在类上定义了信号:

class Foo(QObject):

    mysignal = QtCore.pyqtSignal()

This does not work: 这不起作用:

foo = Foo()
foo.mysignal = QtCore.pyqtSignal()

EDIT : 编辑:

In your case, change LoadingThread to: 在您的情况下,将LoadingThread更改为:

class LoadingThread(QtCore.QThread):

    taskFinished = QtCore.pyqtSignal()

    def __init__(self):   
        QtCore.QThread.__init__(self)    

    def Load(self):
        #My stuffs here (to be executed duing the progress bar busy)
        time.sleep(5)
        self.taskFinished.emit()

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

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