gpt4 book ai didi

python - 从 Qrunnable 连接一个 pyqtSignal

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:43 34 4
gpt4 key购买 nike

我正在尝试制作一个监控硬件的应用程序,我想使用 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com