【问题标题】:What's the right way to connect to the thread signal from the main GUI window?从主 GUI 窗口连接到线程信号的正确方法是什么?
【发布时间】:2014-09-25 07:00:16
【问题描述】:

连接到 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_()

【问题讨论】:

    标签: python python-2.7 pyqt qthread


    【解决方案1】:

    问题是你的代码在这行;

    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


    问候,

    【讨论】:

      猜你喜欢
      • 2010-10-08
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 2014-05-08
      • 2019-11-20
      • 1970-01-01
      • 2019-03-29
      相关资源
      最近更新 更多