我有一个我的执行将在线程中发生的要求,我想从该线程向主类发出信号。下面是我的示例代码片段,在其中创建了MainClass
的实例并将其分配给temp
。其中MainClass
内部连接到线程Thread1
,实际工作将在该线程中进行。当从Thread1
发出信号时,我要执行从signalcalled
连接的temp
定义
代码段
class Thread1(QtCore.QThread):
ThreadSignal = QtCore.pyqtSignal(str)
While True:
#some statments
if Condition:
ThreadSignal.emit('Yes')
else:
ThreadSignal.emit('No')
class MainClass(QtCore.QObject):
MainSignal = QtCore.pyqtSignal(str)
Testinstance = Thread1()
def signalcalled(s):
print s
if __name__=='__main__':
app = AtGui.QApplication(sys.argv)
temp = MainClass()
temp.MainSignal.connect(signalcalled)
sys.exit(app.exec()_)
最佳答案
这应该工作:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui
class myThread(QtCore.QThread):
threadSignal = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(myThread, self).__init__(parent)
def run(self):
while True:
if True:
self.threadSignal.emit('Yes')
else:
self.threadSignal.emit('No')
break
class myMainClass(QtCore.QObject):
mainSignal = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(myMainClass, self).__init__(parent)
testInstance = myThread(self)
testInstance.threadSignal.connect(self.mainSignal.emit)
testInstance.start()
def signalcalled(s):
print s
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
main = myMainClass()
main.mainSignal.connect(signalcalled)
sys.exit(app.exec_())
关于python - 如何从一个类中发出Qtsignal导致从pyqt中其父类中发出Qtsignal,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13898767/