PyQt5将辅助线程传递给QDialog类

时间:2018-07-03 17:08:48

标签: python multithreading pyqt pyqt5

将QThread类传递给QDialog的正确方法是什么?我的工作线程定义为:

class MasterLoader(QThread):
    totsignal = pyqtSignal(int)
    cntsignal = pyqtSignal(int)

    def __init__(self, filename, database, parent=None):
        super(MasterLoader, self).__init__(parent)
        self.threadactive = True
        self.filename = filename
        self.database = database
        self.data = list()
        self.commitsize = 100

    def run(self):
        # Running a loader loop here

    def stop(self):
        self.threadactive = False
        self.wait()

我的对话线程:

class FileLoader(QDialog):
    def __init__(self, title, worker, parent=None):
        super(FileLoader, self).__init__(parent)
        loadUi("FileLoader/fileloader.ui", self)
        self.setWindowTitle(title)
        self.loaderProgress.setValue(0)
        self.setWindowFlag(Qt.WindowCloseButtonHint, False)
        self.thread = worker
        self.thread.totsignal.connect(self.loaderProgress.setMaximum)
        self.thread.cntsignal.connect(self.updateProgress)
        self.thread.finished.connect(self.close)

    @pyqtSlot()
    def showEvent(self, e):
        self.thread.start()

    @pyqtSlot()
    def closeEvent(self, e):
        self.thread.stop()

    def updateProgress(self,n):
        self.loaderProgress.setValue(n)

从另一个班级叫来

class MasterBrowser(QWidget):
    def __init__(self,parent=None):
        super(MasterBrowser, self).__init__(parent)
        loadUi("masterbrowse.ui", self)
        self.database = self.parent().database

    @pyqtSlot()
    def on_btnReloadMaster_clicked(self):
        loader = FileLoader("Loading The Master File",MasterLoader("test.txt",self.database))
        loader.show()

在FileLoader类中,传递工作线程使应用程序崩溃,如果我分配self.thread = MasterLoader(...),则它将按预期工作。我正在尝试创建可用于所有文件处理操作的FileLoader类。

0 个答案:

没有答案