最近开发一个项目,需要用到界面,遇到界面不能实时更新的问题,看到网上很多用槽函数,但是大多都是些button的,并不是我需要的,要么就是整数的,后来自己进行尝试,写了一个自定义的槽函数处理treewidget,特地分享出来。
背景:用QT Designer设计了一个界面,在程序运行中间调用界面,并随着程序的运行会更新treewidget。
问题1:如果在界面Class中写运行程序,那么结果会等到程序运行结束,最后界面出来,但是此时界面上已经是最终结果
解答:因为会等到程序结束才会加入界面mainloop中,执行到最后一句话,界面才会显示,所以解决办法就是将中间需要处理的代码放到线程中,这样就不会妨碍程序往下走了。
问题二:在线程中代码会需要更新界面,但是界面往往卡顿,只有鼠标点cmd窗口,在点回界面时,数据才刷新,不能更好的看到程序运行的结果。
解答:PyQt5中不友好支持子线程中对界面的设置,需要使用槽函数,信号触发的方式来更新界面。
为了更好的演示,这里先介绍一下把界面和代码分离的方法
现在开始自定义槽函数。
先看分离后的函数:
treeview4_test.py
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog from PyQt5 import QtCore, QtGui, QtWidgets import treeview4 import treeview_data import sys class MainCode(QMainWindow, treeview4.Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) treeview4.Ui_MainWindow.__init__(self) self.setupUi(self) # 设置列宽 self.treeWidget.setColumnWidth(0, 200) _translate = QtCore.QCoreApplication.translate self.initial_tree_view(_translate) self.treeWidget.expandAll() def initial_tree_view(self, _translate): list_objects = [] test1 = treeview_data.DataCollection() test1.set_module({"Test1": "waiting"}) test1.add_item({"a1": "waiting"}) test1.add_item({"b1": "waiting"}) test1.add_item({"c1": "waiting"}) list_objects.append(test1) test2 = treeview_data.DataCollection() test2.set_module({"Test2": "waiting"}) test2.add_item({"a2": "waiting"}) test2.add_item({"b2": "waiting"}) test2.add_item({"c2": "waiting"}) test2.add_item({"d2": "waiting"}) list_objects.append(test2) test3 = treeview_data.DataCollection() test3.set_module({"Test3": "waiting"}) test3.add_item({"a2": "waiting"}) test3.add_item({"b2": "waiting"}) test3.add_item({"c2": "waiting"}) test3.add_item({"d2": "waiting"}) list_objects.append(test3) for object_num, object_module in enumerate(list_objects): item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(176, 165, 172)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) for module_name, module_result in object_module.module.items(): brush = QtGui.QBrush(QtGui.QColor(0, 255, 0)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) self.treeWidget.topLevelItem(object_num).setText(0, _translate("MainWindow", module_name)) self.treeWidget.topLevelItem(object_num).setText(1, _translate("MainWindow", module_result)) for item_num, item_dic in enumerate(object_module.items): for item_name, item_result in item_dic.items(): item_1 = QtWidgets.QTreeWidgetItem(item_0) self.treeWidget.topLevelItem(object_num).child(item_num).setText(0, _translate("MainWindow", item_name)) self.treeWidget.topLevelItem(object_num).child(item_num).setText(1, _translate("MainWindow", item_result)) if __name__ == "__main__": app = QApplication(sys.argv) md = MainCode() md.show() app.exec_() # sys.exit(app.exec_())
然后调用子线程进行刷新:
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog from PyQt5 import QtCore, QtGui, QtWidgets import treeview4 import treeview_data import sys import threading import time class MainCode(QMainWindow, treeview4.Ui_MainWindow): list_result_update = QtCore.pyqtSignal([int, int, str]) def __init__(self): QMainWindow.__init__(self) treeview4.Ui_MainWindow.__init__(self) self.setupUi(self) # 设置列宽 self.treeWidget.setColumnWidth(0, 200) _translate = QtCore.QCoreApplication.translate self.initial_tree_view(_translate) self.treeWidget.expandAll() # 用列表触发槽函数 self.list_result_update.connect(self.modified_treewidget) # 线程函数 self.run() def run(self): th = threading.Thread(target=self.go_update) th.setDaemon(True) th.start() def initial_tree_view(self, _translate): list_objects = [] test1 = treeview_data.DataCollection() test1.set_module({"Test1": "waiting"}) test1.add_item({"a1": "waiting"}) test1.add_item({"b1": "waiting"}) test1.add_item({"c1": "waiting"}) list_objects.append(test1) test2 = treeview_data.DataCollection() test2.set_module({"Test2": "waiting"}) test2.add_item({"a2": "waiting"}) test2.add_item({"b2": "waiting"}) test2.add_item({"c2": "waiting"}) test2.add_item({"d2": "waiting"}) list_objects.append(test2) test3 = treeview_data.DataCollection() test3.set_module({"Test3": "waiting"}) test3.add_item({"a2": "waiting"}) test3.add_item({"b2": "waiting"}) test3.add_item({"c2": "waiting"}) test3.add_item({"d2": "waiting"}) list_objects.append(test3) for object_num, object_module in enumerate(list_objects): item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(176, 165, 172)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) for module_name, module_result in object_module.module.items(): brush = QtGui.QBrush(QtGui.QColor(0, 255, 0)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) self.treeWidget.topLevelItem(object_num).setText(0, _translate("MainWindow", module_name)) self.treeWidget.topLevelItem(object_num).setText(1, _translate("MainWindow", module_result)) for item_num, item_dic in enumerate(object_module.items): for item_name, item_result in item_dic.items(): item_1 = QtWidgets.QTreeWidgetItem(item_0) self.treeWidget.topLevelItem(object_num).child(item_num).setText(0, _translate("MainWindow", item_name)) self.treeWidget.topLevelItem(object_num).child(item_num).setText(1, _translate("MainWindow", item_result)) def modified_treewidget(self, num_one, num_two, str_one): _translate = QtCore.QCoreApplication.translate self.treeWidget.topLevelItem(num_one).child(num_two).setText(1, _translate( "MainWindow", str_one)) def go_update(self): time.sleep(1) # 更新treeview self.list_result_update.emit(1, 1, "aaaaaaa") if __name__ == "__main__": app = QApplication(sys.argv) md = MainCode() md.show() app.exec_() # sys.exit(app.exec_())
现在就完成了自定义槽函数了。