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 17:04:59 浏览: 46
在这段代码中,你使用了错误的类型将 `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()` 方法了。
相关问题
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) worker1.mySignal.connect(task2) worker2.mySignal.connect(task3) 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_())报警:worker.mySignal.connect(task1) TypeError: argument 1 has unexpected type 'MyRunnable'怎么解决?
在这段代码中,`worker.mySignal` 是一个信号,它发出的信号将会触发 `task1` 的运行。但是你在连接信号和槽时,使用了错误的语法。正确的语法是将信号连接到槽,而你却在将信号连接到了 `MyRunnable` 对象。为了解决这个问题,你需要将 `worker.mySignal` 连接到 `task1.run` 方法。修改代码如下:
```
worker.mySignal.connect(task1.run)
worker1.mySignal.connect(task2.run)
worker2.mySignal.connect(task3.run)
```
这样就可以将信号和槽正确地连接起来了。
这个线程建的有问题吗 class Worker(QtCore.QThread): sinOut = pyqtSignal(object) def __init__(self, parent=None): super(Worker, self).__init__(parent) # 设置工作状态与初始num数值 self.working = True self.num = 0 #def __del__(self): # 线程状态改变与线程终止 #self.working = False #self.wait() def stop(self): #线程状态改变与线程终止 self.working = False self.wait() def run(self): self.working = True while self.working == True: #file_str = 'File index{0}'.format(self.num) self.num += 1 # 发射信号 #self.sinOut.emit(file_str) self.sinOut.emit('1') # 线程休眠2秒 self.sleep(5)
这个线程看起来没有明显的问题,但是需要注意以下几点:
1. `sinOut` 信号的参数类型是 `object`,这样会使得信号的参数类型不明确,建议根据实际情况设置参数类型。
2. 在 `stop()` 方法中,需要等待线程结束后再返回,因此应该调用 `self.wait()` 方法。
3. 在 `run()` 方法中,应该将 `self.working = True` 放在 `while` 循环的外面,否则当 `working` 的值为 `False` 时,线程不会结束。
4. 在 `run()` 方法中,可以考虑使用 `QThread.msleep()` 方法代替 `time.sleep()` 方法,因为前者是线程安全的。
5. 在 `run()` 方法中,可以考虑增加一个条件判断,当 `working` 的值为 `False` 时,跳出循环以结束线程。
最终代码如下:
```
class Worker(QtCore.QThread):
sinOut = pyqtSignal(str)
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self.working = True
self.num = 0
def stop(self):
self.working = False
self.wait()
def run(self):
while self.working:
self.num += 1
self.sinOut.emit('1')
self.msleep(5000)
if not self.working:
break
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)