class Worker(QObject): mySignal = pyqtSignal() def init(self): super().init() def run(self): self.mySignal.emit() class MyRunnable(QRunnable): def init(self, func,*param): super().init() self.func = func self.param = param def run(self): self.func(self.param[0][0],self.param[0][1]) class test(QWidget): def init(self): super().init() self.name = 'name' self.setWindowTitle('测试') self.initUI() def initUI(self): self.label = QLabel(self) thread = QThread() worker = Worker() worker.moveToThread(thread) worker1 = Worker() worker1.moveToThread(thread) worker2 = Worker() worker2.moveToThread(thread) threadPool = QThreadPool() task1 = MyRunnable(self.func1, (1, 2)) task2 = MyRunnable(self.func2, (2, 2)) task3 = MyRunnable(self.func3, (3, 2)) #thread.started.connect(task1) worker.mySignal.connect(task1.run) worker1.mySignal.connect(task2.run) worker2.mySignal.connect(task3.run) threadPool.start(worker) threadPool.start(worker1) threadPool.start(worker2) threadPool.waitForDone() def func1(self,x,y): print(x+y) def func4(): self.label.setText(str(x+y)) func4() time.sleep(1) def func2(self, x,y): print(x + y) self.label.setText(str(x + y)) time.sleep(1) def func3(self, x,y): print(x + y) self.label.setText(str(x + y)) time.sleep(1) app = QApplication(sys.argv) main = test() main.show() sys.exit(app.exec_())报警:TypeError: arguments did not match any overloaded call: start(self, QRunnable, priority: int = 0): argument 1 has unexpected type 'Worker' start(self, Callable[[], None], priority: int = 0): argument 1 has unexpected type 'Worker'怎么解决?
时间: 2023-12-09 14:04:59 浏览: 99
school_scheduler_see:===-开源
在这段代码中,你使用了错误的类型将 `worker`、`worker1` 和 `worker2` 对象传递给 `threadPool.start()` 方法。正确的类型应该是 `MyRunnable` 对象,因为 `QThreadPool` 的 `start()` 方法需要一个 `QRunnable` 对象作为参数。
为了解决这个问题,你需要将三个 `Worker` 对象分别替换为对应的 `MyRunnable` 对象。修改代码如下:
```
task1 = MyRunnable(self.func1, (1, 2))
task2 = MyRunnable(self.func2, (2, 2))
task3 = MyRunnable(self.func3, (3, 2))
threadPool.start(task1)
threadPool.start(task2)
threadPool.start(task3)
```
这样就可以将 `MyRunnable` 对象传递给 `QThreadPool` 的 `start()` 方法了。
阅读全文