如何解决使用线程和信号与 Python 生成 QTreeView
我正在尝试创建一个应用程序,该应用程序将使用 PySide6 通过 API 获取数据并将它们呈现在 QTreeView 中。由于获取数据需要很长时间,我想使用 QThread 检索它们。我的示例代码如下:
import logging
import time
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",level=logging.INFO)
class MainWindow(QMainWindow):
gettingData = False
counting = False
def __init__(self,*args,**kwargs):
super(MainWindow,self).__init__(*args,**kwargs)
self.counter = 0
layout = QVBoxLayout()
self.b = QPushButton("Start Counting with a Thread")
self.bt1 = QPushButton("Generate tree with Thread")
self.bt2 = QPushButton("Generate tree without a Thread")
self.tree = QTreeView()
layout.addWidget(self.b)
layout.addWidget(self.bt1)
layout.addWidget(self.bt2)
layout.addWidget(self.tree)
self.b.clicked.connect(self.startCounting)
self.bt1.clicked.connect(self.startGenerating)
self.bt2.clicked.connect(self.startGenerating2)
w = QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
self.show()
self.threadpool = QThreadPool()
print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())
def startCounting(self):
thread = Thread(self.counting)
thread.signal.signal.connect(self.something)
self.threadpool.start(thread)
def something(self,signal):
if signal == "False":
logging.info("Counting is Complete")
else:
logging.info(signal)
def startGenerating(self):
thread = DataThread()
thread.signals.signal_QStandardItemModel.connect(self.treeAction)
self.threadpool.start(thread)
def treeAction(self,signal):
self.tree.setModel(signal)
def startGenerating2(self):
start = time.perf_counter()
siteTreeViewModel = QStandardItemModel()
rootNode = siteTreeViewModel.invisibleRootItem()
aItem = TreeStandardItem("A")
bItem = TreeStandardItem("B")
cItem = TreeStandardItem("C")
rootNode.appendRow(aItem)
time.sleep(3)
rootNode.appendRow(bItem)
time.sleep(3)
bItem.appendRow(cItem)
self.tree.setModel(siteTreeViewModel)
end = time.perf_counter() - start
logging.info("Generated tree in "+str(end))
class Thread(QRunnable):
def __init__(self,counting):
super(Thread,self).__init__()
self.counting = counting
self.signal = Signal()
def run(self):
for x in range(10):
self.signal.signal.emit(str(x))
time.sleep(.1)
counting = False
self.signal.signal.emit(str(counting))
class Signal(QObject):
signal = Signal(str)
class DataThread(QRunnable):
def __init__(self):
super(DataThread,self).__init__()
self.signals = WorkerSignals()
def run(self):
logging.info("Start generating the tree")
start = time.perf_counter()
siteTreeViewModel = QStandardItemModel()
rootNode = siteTreeViewModel.invisibleRootItem()
aItem = TreeStandardItem("A")
bItem = TreeStandardItem("B")
cItem = TreeStandardItem("C")
rootNode.appendRow(aItem)
time.sleep(2)
rootNode.appendRow(bItem)
time.sleep(2)
bItem.appendRow(cItem)
end = time.perf_counter() - start
self.signals.signal_QStandardItemModel.emit(siteTreeViewModel)
logging.info("Generated tree in "+str(end))
self.signals.finished.emit()
class TreeStandardItem(QStandardItem):
def __init__(self,txt=''):
super().__init__()
self.setEditable = False
self.setForeground(QColor(255,255,255))
self.setText(txt)
class WorkerSignals(QObject):
signal_QStandardItemModel = Signal()
finished = Signal()
app = QApplication([])
window = MainWindow()
app.exec()
第一个计数按钮按预期工作,创建了一个线程。 在没有线程的情况下生成树,确认我尝试使用线程运行的代码正在工作 一旦我尝试使用线程运行树生成,我就会收到以下错误:
Traceback (most recent call last):
File "/home/giunz/code/tutorial/threaded-tree-01.py",line 61,in startGenerating
thread.signals.signal_QStandardItemModel.connect(self.treeAction)
TypeError: PySide6.QtCore.QObject.connect(): not enough arguments
更新 1: 稍微更改了代码,导致出现新错误。该错误似乎与 signal_QStandardItemModel 无法返回对象有关。 有什么建议吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。