PyQt5 线程、信号和槽。连接错误

PyQt5 Threads, Signal, and Slot. Connect Error

我是 PyQt5 的新手,我似乎无法连接我的 pyqtSignal 和 pyqtSlot。弹出错误 "TypeError: connect() failed between worker.newIcon[object] and updateIcon()"。任何人都可以指导我走上正确的道路吗?

from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QAction, QMenu
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot


class worker(QThread):
    newIcon = pyqtSignal(object)

    def run(self):
        while True:
            self.newIcon.emit(QIcon("shield-off.png"))
            QThread.msleep(1000)


class systemTray():
    def start_tray(self):
        self.app = QApplication([])
        self.app.setQuitOnLastWindowClosed(False)

        icon = QIcon("shield-on.png")
        self.tray = QSystemTrayIcon()
        self.tray.setIcon(icon)
        self.tray.setVisible(True)

        self.menu = QMenu()
        self.quit = QAction("Exit")
        self.quit.triggered.connect(self.app.quit)
        self.menu.addAction(self.quit)
        self.tray.setContextMenu(self.menu)

        self.thread = QThread()
        self.worker = worker()
        self.worker.moveToThread(self.thread)
        self.worker.newIcon.connect(self.updateIcon)
        self.thread.started.connect(self.worker.run)
        self.thread.start()

        # Run tray
        self.app.exec_()

    @pyqtSlot(object)
    def updateIcon(self, icon):
        self.tray.setIcon(icon)


if __name__ == "__main__":
    be_tray = systemTray()
    be_tray.start_tray()

pyqtSlot 装饰器旨在用于 QObject 个子类,否则将无法正常工作。

所以,解决方法很简单,只需要继承QObject即可:

class systemTray(<b>QObject</b>):
    def start_tray(self):
        # ...

另一方面,很少需要使用 pyqtSlot 装饰器,并且仅在特定情况下使用(参见 this related answer。在您的情况下,似乎没有必要。

此外,由于您已经从 QThread 继承了子类,因此您可以只使用它自己的 start(),而无需在新的 moveToThread() 上使用。