我想这样做: 有一个名为' Main'的课程。还有另一个名为“aClass'”的课程。还有一个名为“线程”的课程。这是我们的线程类。 '主'是我们的主类,我们从Main类开始我们的Thread类。 当我们的Thread类启动时,它会从run()函数发出一个信号...... '主'和' aClass'课程试图捕捉这些信号。 '主' class能够捕获从Thread类发出的信号但是' aClass'因为我没有从'aClass'开始QThread,所以无法捕获相同的信号。我只在' aClass'。
中定义了它以下是代码:
#!/usr/bin/env python
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("Test")
self.aClass = aClass()
self.thread = Thread()
self.thread.printMessage.connect(self.write)
self.initUI()
def initUI(self):
self.button = QPushButton("Start Process", self)
self.button.clicked.connect(self.startProcess)
def startProcess(self):
self.thread.start()
def terminateProcess(self):
self.thread.terminate()
def write(self):
print "Main: hello world..."
class aClass(object):
def __init__(self):
print "aClass: I have been started..."
self.thread = Thread()
self.thread.printMessage.connect(self.write)
def write(self):
print "aClass: hello world..."
class Thread(QThread):
printMessage = pyqtSignal()
def __init__(self):
QThread.__init__(self)
print "Thread: I have been started..."
def run(self):
self.printMessage.emit()
print "Thread: I emitted the message."
if __name__ == "__main__":
app = QApplication(sys.argv)
root = Main()
root.show()
app.exec_()
结果: 程序启动时,输出为:
aClass: I have been started...
Thread: I have been started...
Thread: I have been started...
当我点击“开始流程”时按钮,输出为:
Thread: I emitted the message.
Main: hello world...
总产量:
aClass: I have been started...
Thread: I have been started...
Thread: I have been started...
Thread: I emitted the message.
Main: hello world...
点击“启动流程'
”后我想要获得的输出Thread: I emitted the message.
Main: hello world...
aClass: hello world...
我想要这个结果,但我不想使用来自' aClass'的自我.thread.start()命令。因为我只想运行Thread一次......
答案 0 :(得分:0)
您正在做的是在Thread
对象中创建第二个aClass
,该Thread
与Main
中的self.thread
不同。您需要将Main
中的write
信号与self.aClass
对象中的广告符class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("Test")
self.aClass = aClass()
self.thread = Thread()
self.thread.printMessage.connect(self.write)
self.thread.printMessage.connect(self.aClass.write)
...
class aClass(object):
def __init__(self):
print "aClass: I have been started..."
#self.thread = Thread() #This makes a new Thread
#self.thread.printMessage.connect(self.write)
def write(self):
print "aClass: hello world..."
self.initUI()
相关联。
你想这样做:
if (element.parentNode == divElement) { ... }