从主GUI窗口连接线程信号的正确方法是什么?

时间:2014-08-02 02:08:56

标签: python python-2.7 pyqt qthread

从MainWindow 连接到 DXF_Convert 线程信号的正确方法是什么,并在线程完成后在更新函数内显示消息?

我已经完成了这个但是消息没有显示(但是线程运行正常):

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from gui import Ui_MainWindow

    .
    .
    .

class MainWindow(QMainWindow, Ui_MainWindow):

    .
    .
    .  

    def DXF_convert(self):
        t = DXF_Convert(self)
        t.start()
    def PDF_print(self):
        t = PDF_Print(self)
        t.start()
    def update(self, message=''):
        QMessageBox.information('Done', message)
        self.updateui()
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.connect(PDF_Print(), SIGNAL('Done'), self.update)
        self.connect(DXF_Convert(), SIGNAL('Done'), self.update)

class DXF_Convert(QThread):

    def __init__(self, parent=None):
        super(DXF_Convert, self).__init__(parent)


    def run(self):
        global un, fl, sq, rv
        sp = Spool(un, fl, sq, rv)
        sp.dxf_convert('local')
        self.emit(SIGNAL('Done'), 'DXF conversion done!')

app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()

1 个答案:

答案 0 :(得分:0)

问题是你的代码符合这一点;

t = DXF_Convert(self)

self.connect(DXF_Convert(), SIGNAL('Done'), self.update)

原因是您必须使用相同的对象来连接您的信号。像这样;

t = DXF_Convert(self)

self.connect(t, SIGNAL('Done'), self.update)

所以,我没有QThread PyQt4的例子,希望这有帮助

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class QTestThread (QThread):
    def run(self):
        self.emit(SIGNAL('done'), 'Thread is end')

class MainWindow (QMainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.TestThreadInit()

    def TestThreadInit (self):
        self.myQTestThread = QTestThread(self)
        self.connect(self.myQTestThread, SIGNAL('done'), self.update)
        self.myQTestThread.start()

    def update (self, message):
        QMessageBox.information(self, 'Information', message)

app = QApplication(sys.argv)
myMainWindow= MainWindow()
myMainWindow.show()
app.exec_()

QMessageBox中的问题是QMessageBox.information中的错误参数 请阅读此参考资料;

参考http://pyqt.sourceforge.net/Docs/PyQt4/qmessagebox.html#information


此致