PyQt4工作线程,“QThread.started”丢失了

时间:2012-10-24 03:30:25

标签: python pyqt4 qthread

我正在尝试在我的PyQt4应用程序中正确设置工作线程,但由于某种原因,来自线程的启动信号没有传播给我的工作人员!

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

有什么想法吗?

谢谢!

更新:重命名工作人员后 - >按照Gary Hughes的自我工作我在崩溃前得到一个新的错误

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

更新#2 没关系!我的工作人员正在调用GUI代码并导致新错误。使用self.worker的原始修复是正确的。

1 个答案:

答案 0 :(得分:2)

尝试将worker重命名为self.worker

我认为worker在您致电syncThread.start()后被删除,因为它超出了范围。