2012-10-24 25 views
1

我想在我的PyQt4應用程序中正確設置工作線程,但由於某種原因線程的啓動信號沒有傳播給我的工作人員!PyQt4工作線程,「QThread.started」失蹤

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

任何想法?

謝謝!

更新:重命名後的工人 - >每加里·休斯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的原始修復程序是正確的。

回答

2

嘗試重命名workerself.worker

在我看來,worker在您撥打syncThread.start()之後立即被刪除,因爲它超出了範圍。

+0

D'Oh!我還沒有嘗試過,但這完全有道理。在我的代碼中有一個PyQt的工作對象池嗎?這裏似乎有很多鍋爐板,現在我需要爲每個線程添加參考。 (如果我在第一次運行時再次嘗試運行該工作,突然,self.worker = ...不夠好!) – Sandro

+0

因此,重命名worker - > self.worker會在崩潰前提示bizaar錯誤:「QObject :: setParent:不能設置父,新的父母在不同的線程「 – Sandro

+0

沒關係,我的工作人員正在調用GUI代碼...謝謝! – Sandro