这是一个有关pyqt5 QRunnable的简单程序。但是当我编码时出现了问题。主要是关于self.son = Son()
在此类class NoQObject(QRunnable)
中的位置。
正确的方法:
class NoQObject(QRunnable):
def __init__(self,x):
super().__init__()
self.x = x
pass
def run(self):
self.son = Son()
self.fa = Fa()
self.fa.finished.connect(self.son.output)
self.fa.start(self.x)
正确的输出:
错误的方式:
class NoQObject(QRunnable):
def __init__(self,x):
super().__init__()
self.x = x
self.son = Son()
pass
def run(self):
self.fa = Fa()
self.fa.finished.connect(self.son.output)
self.fa.start(self.x)
错误的输出:
完整的代码在这里:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class Son(QObject):
def __init__(self):
super().__init__()
@pyqtSlot(int)
def output(self, x):
print(x)
class Fa(QObject):
finished = pyqtSignal(int)
def __init__(self):
super().__init__()
def start(self, x):
self.finished.emit(x)
class NoQObject(QRunnable):
def __init__(self,x):
super().__init__()
self.x = x
pass
def run(self):
self.son = Son()
self.fa = Fa()
self.fa.finished.connect(self.son.output)
self.fa.start(self.x)
class MainUi(QObject):
start = pyqtSignal(int)
def __init__(self):
super().__init__()
self.son = Son()
self.pool = QThreadPool()
pass
def press(self):
for i in range(10):
self.noqobject = NoQObject(i)
self.pool.start(self.noqobject)
self.pool.waitForDone()
if __name__ == '__main__':
app = QApplication(sys.argv)
form = MainUi()
form.press()
sys.exit(app.exec())