PySide:向QLabel setText()发出信号

时间:2015-11-25 07:43:35

标签: python qt pyside qlabel

我试图通过从线程发出信号来从线程更新QLabel小部件,但它似乎无法正常工作。以下是代码段:

from PySide.QtCore import *
from PySide.QtGui import *
import sys
import mainGui
import os

class Program(QDialog, mainGui.Ui_Dialog):

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

        self.workerThread = WorkerThread()
        self.connect(self.updateButton, SIGNAL("clicked()"), self.getDists)
        ...some other signals here...
        self.connect(self.label, SIGNAL("changed(QString)"), self.updateLabel)
        self.connect(self.workerThread, SIGNAL("exception()"), self.threadDone)

    def getDists(self):
         self.workerThread.start()

    def updateLabel(self, text):
         self.label.setText(text)    

    def threadDone(self):
        QMessageBox.warning(self, "blaa", "blaa blaa") 

class WorkerThread(QThread):

    def __init__(self, parent=None ):

        super(WorkerThread, self).__init__(parent)

    def run(self):                      

        try:
           ..some code...           
           self.emit(SIGNAL("changed(QString)"), "Updating...")
           ..more code here..                     

        except OSError as e:
            self.emit(SIGNAL("exception()")) 

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

问题是GUI没有使用文本"更新..."更新QLabel小部件。当程序在try-block中时。但是,这样可以正常工作并更新GUI:

...
def run(self):                      

        try:
           ..some code...           
           form.label.setText("Updating...")
           #self.emit(SIGNAL("changed(QString)"), "Updating...")
           #and in this case, self.connect(self.label, SIGNAL("changed(QString)"), self.updateLabel) is also commented out
           ..more code here..

所以,我有点困惑为什么发出信号不起作用,因为在异常的情况下发出信号工作正常。

0 个答案:

没有答案