2021年4月3日星期六

QTreeWidget Refresh Without Duplicating the nodes

i have below code, which reads the data from a csv file, convert into pandas frame. It is having job name and each job is having multiple task. I am trying to list down all job name as node and their task as respective children's. I am able to do it, with below code. But my csv files gets updated on regular interval hence i need to refresh the data on refresh button. The below code is duplicating the data. It is keeping the tree which was created at the first execution and as soon as I hit refresh it again bring existing job names along with new job name. 1. is there any way to remove the existing tree on GUI and build new tree every time i hit refresh button ? 2. or only refresh incremental data.

import pandas as pd  from PyQt5 import QtCore, QtGui, QtWidgets      class logTree(QtWidgets.QWidget):      def __init__(self, parent=None):                super(logTree, self).__init__(parent)          self.lay = QtWidgets.QVBoxLayout(self)            self.pushButton = QtWidgets.QPushButton()          self.pushButton.setMaximumSize(QtCore.QSize(25, 25))          self.pushButton.setStyleSheet("color: rgb(0, 0, 0);")          self.pushButton.setText("Refresh")          self.lay.addWidget(self.pushButton)          self.treeWidget = QtWidgets.QTreeWidget()          self.lay.addWidget(self.treeWidget)          self.treeWidget.setMinimumSize(QtCore.QSize(0, 700))          self.treeWidget.setStyleSheet("color: rgb(255, 170, 0); color: rgb(0, 0, 0);")          self.treeWidget.setFrameShape(QtWidgets.QFrame.NoFrame)          self.treeWidget.setFrameShadow(QtWidgets.QFrame.Plain)          self.treeWidget.setLineWidth(1)          self.treeWidget.setMidLineWidth(0)          self.treeWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)          self.treeWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)          self.treeWidget.setUniformRowHeights(True)          self.treeWidget.setColumnCount(6)          self.treeWidget.setObjectName("treeWidget")          self.treeWidget.header().setDefaultSectionSize(150)          self.treeWidget.header().setMinimumSectionSize(100)                self.icon1 = QtGui.QIcon()          self.icon1.addPixmap(QtGui.QPixmap(":/repository.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)          self.rootnode = QtWidgets.QTreeWidgetItem(self.treeWidget)          self.rootnode.setIcon(0, self.icon1)          self.production = QtWidgets.QTreeWidgetItem(self.rootnode)          self.production.setIcon(0, self.icon1)            _translate = QtCore.QCoreApplication.translate          self.treeWidget.topLevelItem(0).setText(0, _translate("frame", "Terminal" ))          self.treeWidget.topLevelItem(0).child(0).setText(0, _translate("frame", "env"))          self.treeWidget.expandAll()            self.data = pd.read_csv(r"C:/job_log.csv")          self.convert_dict = {'duration': str}          self.df = self.data.astype(self.convert_dict)            self.task = self.df.groupby('wa_job')[['wa_task', 'duration','wtl_started', 'wtl_completed',    'wtl_run_status', 'wtl_return_msg']].apply(lambda g: g.values.tolist()).to_dict()          self.jobs = self.data.wa_job.unique()            self.pushButton.clicked.connect(self._logs_)          def _logs_(self):              for i in self.jobs:                  self.parent = QtWidgets.QTreeWidgetItem(self.production)                  self.parent.setText(0, i)                            for key, value in self.task.items():                      if i == key:                          for p in value:                                  self.child = QtWidgets.QTreeWidgetItem(p)                                  self.child.setIcon(0, self.icon1)                               self.parent.addChild(self.child)      if __name__ == '__main__':      import sys        app = QtWidgets.QApplication(sys.argv)      w = logTree()      w.show()      sys.exit(app.exec_())  
https://stackoverflow.com/questions/66937679/qtreewidget-refresh-without-duplicating-the-nodes April 04, 2021 at 11:04AM

没有评论:

发表评论