Search code examples
pythonqtpyqtsignals-slots

SIGNAL emit works only in one function, but not in others?


I have a somewhat strange problem. I'm coding a multithreaded app and am using SIGNALS to communicate the QThread's data to the GUI class data. Without further ado, here is a simplified code.

class GUI(uiMainWindow.MainWindow):
    def __init__(self, parent=None):
        super etc
        self.thread = Thread()
        self.connect(self.thread, SIGNAL("changeStatus(QString)"), self.setStatus, Qt.QueuedConnection)

    def setStatus(self, status):
        self.statusBar.setText(status)


class Thread(QThread):
    def __init__(self, parent=None, create=True):
        super etc
        self.create = create

    def run(self):
        if self.create:
            create_data()
        if not self.create:
            upload_data()

    def create_data(self):
        self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")
        #rest of the code

    def upload_data(self):
        self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")

Pretty basic, right? However, here's the problem: the self.emit works only in create_data function, but not in upload_data (or for that matter, in any other function; I tried putting it in __init__ as well). I tried putting print "I got the status" + status in the setStatus function. Again, it works in the create_data() function, but not in the upload_data() function.

The differences between the two functions are relatively minor, and as far as I can tell, nothing is interfering with the self.emit function - in fact, in both cases, self.emit is only 4-5 lines "away" from the function definition.

This is really puzzling to me. Any help? Thanks in advance!

EDIT: again, as far as I can tell, the only difference between the two functions is in the run() - the first one is called if create parameter is True, and the second one if it is False.


Solution

  • I was right in my post. The difference between Thread() and Thread(create=False) was crucial. I had to define a new method (one was self.thread = Thread() and the other self.diff_thread = Thread(create=False)) and connect to different slots to make it work.