Chris Aung
Chris Aung

Reputation: 9492

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 (i.e range (0,0)) using a QThread class.

I am using mysignal = QtCore.pyqtSignal() to create my signal. And after that I use mysignal.emit() to transmit that signal. 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). 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

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()

Upvotes: 1

Views: 1144

Answers (1)

sebastian
sebastian

Reputation: 9696

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:

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()

Upvotes: 2

Related Questions