Sandro
Sandro

Reputation: 2259

PyQt4 Worker thread, a "QThread.started" goes missing

I am trying to properly setup a worker thread in my PyQt4 application, but for some reason the start signal from the thread is no propagating to my worker!

    syncThread = QtCore.QThread()
    self._syncThread = syncThread
    worker = SyncWorker(self.async_sync)
    worker.moveToThread(syncThread)
    syncThread.started.connect(self.progress.show) #This dialog appears!
    syncThread.started.connect(worker.work)  # This seems to be a no-op
    worker.finished.connect(syncThread.quit)
    worker.finished.connect(worker.deleteLater)
    syncThread.finished.connect(worker.deleteLater)
    syncThread.finished.connect(syncThread.deleteLater)
    syncThread.start() 

class SyncWorker(QtCore.QObject):

    # Emitted whenever done
    finished = QtCore.pyqtSignal()

    def __init__(self, delegate):
        QtCore.QObject.__init__(self)

    @QtCore.pyqtSlot()
    def work(self):
        print("Worker gonna work") #This never prints!
        self.finished.emit()  

Any ideas?

Thank you!

Update: After renaming worker -> self.worker as per Gary Hughes I get a new error before a crash

QObject::setParent: Cannot set parent, new parent is in a different thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.
Segmentation fault

Update #2 Nevermind! My worker was calling GUI code and that caused the new error. The original fix of using self.worker is correct.

Upvotes: 1

Views: 847

Answers (1)

Gary Hughes
Gary Hughes

Reputation: 4510

Try renaming worker to self.worker.

It looks to me like worker is being deleted right after you call syncThread.start() because it's out of scope.

Upvotes: 2

Related Questions