我正在尝试制作一个监视硬件的应用程序,我想使用Qthreadpool而不是每个小部件都有一个线程,并且在连接信号以显示输出时遇到麻烦。我也没有任何错误。

我的旧代码运行良好,并使用QThread显示了应该显示的内容:

import sys
from PyQt4 import QtCore, QtGuiimport ui
import time
import mem_stats


class MainClass(QtGui.QMainWindow, ui.Ui_MainWindow):
        def __init__(self, parent= None):
        super(MainClass, self).__init__(parent)
        self.setupUi(self)
        self.mem_worker = MemWorker()
        self.mem_worker.start()
        self.connect(self.mem_worker, QtCore.SIGNAL('MEM_STATS'),self.show_mem_stats)

    def show_mem_stats(self, free_mem):
        self.free_memory_lcd.display(free_mem)


class MemWorker(QtCore.QThread):
    def __init__(self):
    super(MemWorker, self).__init__()

    def run(self):
        while True:
            free_mem = mem_stats.free_mem()
            self.emit(QtCore.SIGNAL('MEM_STATS'), free_mem)
            time.sleep(1)


if __name__ == '__main__':
    a = QtGui.QApplication(sys.argv)
    app = MainClass()
    app.show()
    sys.exit(a.exec_())


但是,当我将MemWorker类更改为QRunnable时,代码运行没有错误,但QlcdNumber不显示任何内容。

这是我的新代码:

import sys
from PyQt4 import QtCore, QtGui
import ui
import time
import mem_stats


class WorkerSignals(QtCore.QObject):
    mem_signal = QtCore.pyqtSignal(int)


class MainClass(QtGui.QMainWindow, ui.Ui_MainWindow):
    def __init__(self, parent= None):
        super(MainClass, self).__init__(parent)
        self.setupUi(self)
        self.mem_worker = MemWorker()
        self.thread_pool = QtCore.QThreadPool()
        self.thread_pool.setMaxThreadCount(2)
        self.connect(self.mem_worker.signal, QtCore.SIGNAL('MEM_STATS'), self.show_mem_stats)

    def show_mem_stats(self, free_mem):
        self.free_memory_lcd.display(free_mem)
        self.thread_pool.start(self.mem_worker)


class MemWorker(QtCore.QRunnable):
    def __init__(self):
        super(MemWorker, self).__init__()
        self.signal = WorkerSignals()

    def run(self):
        while True:
            free_mem = mem_stats.free_mem()
            self.signal.mem_signal.emit(QtCore.SIGNAL('MEM_STATS'), free_mem)
            time.sleep(1)


if __name__ == '__main__':
    a = QtGui.QApplication(sys.argv)
    app = MainClass()
    app.show()
    sys.exit(a.exec_())

最佳答案

问题是您没有启动QRunnable,而是要在插槽中启动它,并且如果QRunnable没有启动,它将永远不会被调用。另一方面,当前建议使用the new connection syntax,最后建议使用@pyqtSlot decorator从C ++端建立连接,从而以更少的资源加快连接速度。

from PyQt4 import QtCore, QtGui
import ui
import time
import mem_stats

class WorkerSignals(QtCore.QObject):
    mem_signal = QtCore.pyqtSignal(int)


class MainClass(QtGui.QMainWindow, ui.Ui_MainWindow):
    def __init__(self, parent= None):
        super(MainClass, self).__init__(parent)
        self.setupUi(self)
        self.mem_worker = MemWorker()
        self.thread_pool = QtCore.QThreadPool()
        self.thread_pool.setMaxThreadCount(2)
        self.mem_worker.signal.mem_signal.connect(self.show_mem_stats)
        self.thread_pool.start(self.mem_worker)

    @QtCore.pyqtSlot(int)
    def show_mem_stats(self, free_mem):
        self.free_memory_lcd.display(free_mem)


class MemWorker(QtCore.QRunnable):
    def __init__(self):
        super(MemWorker, self).__init__()
        self.signal = WorkerSignals()

    def run(self):
        while True:
            ree_mem = mem_stats.free_mem()
            self.signal.mem_signal.emit(free_mem)
            time.sleep(1)


if __name__ == '__main__':
    import sys
    a = QtGui.QApplication(sys.argv)
    app = MainClass()
    app.show()
    sys.exit(a.exec_())

关于python - 从Qrunnable连接pyqtSignal,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53053569/

10-11 00:51