1

我正在尝试在我的 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 重命名 worker -> self.worker 后,我在崩溃前收到一个新错误

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 的原始修复是正确的。

4

1 回答 1

2

尝试重命名workerself.worker.

在我看来,worker您打电话后就被删除syncThread.start()了,因为它超出了范围。

于 2012-10-24T11:03:08.600 回答