Stjepan
Stjepan

Reputation: 111

Python new style signals and slot between thread and gui app

I am newbie to OOP and python. I am trying to emit signal from Qthread to Qt GUI main window using new style signals and slots.

This is the thread. Inside I will emit signals for updating message dialog in GUI after clicking RUN button in GUI and 3 seconds after that. I am not sure if the inheritance is defined OK or is the signal defined in the right manner.

class OptimThread (QtCore.QThread):

    signalUpdateMessageDialog = QtCore.SIGNAL("updateMessageDialog(PyQt_PyObject,QString)")

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

    def run(self):

        start = time.time()

        self.emit(self.signalUpdateMessageDialog, time.time() - start, 'Initialising...')     

        time.sleep(3)

        self.emit(self.signalUpdateMessageDialog, time.time() - start, 'You waited 3 seconds...')

The main class and the app part is like this (I omitted the other probably unrelevant code).

class Main(QtGui.QMainWindow, Ui_MainWindow):    

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

    def updateMessageDialog(self, times, dialog):  

        hours = str(datetime.timedelta(seconds=int(times)))

        self.MessageDialog.insertHtml('<tt>' + hours + ':</tt> ' + dialog + '<br>')

        return
    def clickRun(self):


        self.optimThread = OptimThread()

        self.connect(self.optimThread, QtCore.SIGNAL("updateMessageDialog(PyQt_PyObject,QString)"), self.updateMessageDialog)

        #self.optimThread.signalUpdateMessageDialog.connect(self.updateMessageDialog)

        self.optimThread.start()

if __name__ == '__main__':
    app=QtGui.QApplication(sys.argv)
    window=Main(None)
    app.setActiveWindow(window)
    window.show()
    sys.exit(app.exec_()) # Exit from Python

If evertyhing is written like this, It works. Yet if I want to use new style for connecting in Main:

self.optimThread.signalUpdateMessageDialog.connect(self.updateMessageDialog)

It says:

self.optimThread.signalUpdateMessageDialog.connect(self.updateMessageDialog) AttributeError: 'str' object has no attribute 'connect'

I appreciate your advices (related to the subject and related to the style) and apologize for not making an MWE.

Upvotes: 4

Views: 1659

Answers (1)

ekhumoro
ekhumoro

Reputation: 120598

The structure of your example is more or less right: but you are mixing up the old-style signal-slot syntax with the new-style.

The signal definition should look like this:

class OptimThread(QtCore.QThread):
    signalUpdateMessageDialog = QtCore.pyqtSignal(int, str)

The signal should be emitted like this:

    self.signalUpdateMessageDialog.emit(
        time.time() - start, 'Initialising...')

And this is how the signal should be connected:

    self.optimThread.signalUpdateMessageDialog.connect(
        self.updateMessageDialog)

With the new-style syntax, there is never any need to use SIGNAL() or SLOT(), and it is never necessary to specify the C++ signature.

For further details, see New-style Signal and Slot Support in the PyQt4 reference.

Upvotes: 2

Related Questions