from PyQt5.QtWidgets importQApplication, QMainWindow, QFileDialogfrom PyQt5 importQtCore, QtGui, QtWidgetsimporttreeview4importtreeview_dataimportsysimportthreadingimporttimeclassMainCode(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()defrun(self):
th= threading.Thread(target=self.go_update)
th.setDaemon(True)
th.start()definitial_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 inenumerate(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 inobject_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 inenumerate(object_module.items):for item_name, item_result initem_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))defmodified_treewidget(self, num_one, num_two, str_one):
_translate=QtCore.QCoreApplication.translate
brush= QtGui.QBrush(QtGui.QColor(0, 255, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
self.treeWidget.topLevelItem(num_one).child(num_two).setBackground(1, brush)
self.treeWidget.topLevelItem(num_one).child(num_two).setText(1, _translate("MainWindow",
str_one))defgo_update(self):
time.sleep(1)#更新treeview
self.list_result_update.emit(1, 1, "aaaaaaa")
time.sleep(1)#关闭窗口
self.close()if __name__ == "__main__":
app=QApplication(sys.argv)
md=MainCode()
md.show()
app.exec_()#sys.exit(app.exec_())