信号发射只在一个函数中有效,其他函数无效?
我遇到了一个有点奇怪的问题。我正在编写一个多线程的应用程序,并使用信号来将QThread的数据传递给GUI类的数据。下面是一个简化的代码示例。
class GUI(uiMainWindow.MainWindow):
def __init__(self, parent=None):
super etc
self.thread = Thread()
self.connect(self.thread, SIGNAL("changeStatus(QString)"), self.setStatus, Qt.QueuedConnection)
def setStatus(self, status):
self.statusBar.setText(status)
class Thread(QThread):
def __init__(self, parent=None, create=True):
super etc
self.create = create
def run(self):
if self.create:
create_data()
if not self.create:
upload_data()
def create_data(self):
self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")
#rest of the code
def upload_data(self):
self.emit(SIGNAL("changeStatus(QString)"), "Changing the statusbar text")
看起来很简单,对吧?但是,问题来了:self.emit 这个方法只在 create_data 函数中有效,而在 upload_data 函数中(或者说在其他任何函数中)都不行;我甚至试着把它放在 __init__
方法里,也没用。我还在 setStatus 函数里加了 print "I got the status" + status
,结果在 create_data() 函数里可以正常工作,但在 upload_data() 函数里却不行。
这两个函数之间的区别其实很小,按理说没有什么会干扰到 self.emit 函数——实际上,在这两种情况下,self.emit 距离函数定义只有4-5行代码的距离。
这让我感到很困惑。有没有人能帮帮我?谢谢!
补充说明:据我所知,这两个函数之间唯一的区别在于 run() 方法——第一个函数在 create 参数为 True 时调用,第二个函数在为 False 时调用。
1 个回答
1
我在我的帖子里说得没错。Thread()和Thread(create=False)之间的区别非常重要。我需要定义一个新的方法,一个是 self.thread = Thread()
,另一个是 self.diff_thread = Thread(create=False)
,然后把它们连接到不同的槽(也就是不同的功能)上,这样才能让它正常工作。