Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - Enable/Disable QTreeWidget from updating

Is it possible to enable/disable a QTreeWidget from updating?

I want to add rows to my tree and update it manually with a button. Obviously when i add a row to the TreeWidget it will be shown in the table. Is there a way to disable this so i can add like 100 rows and than update it once? If not is there a solution with a TreeView?

import sys
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QTreeWidget, QTreeWidgetItem

class Table(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(800, 600)
        self.setMinimumSize(QSize(800, 600))

        self.table = QTreeWidget()
        self.table.setHeaderLabels(["Description", "Price"])
        self.setCentralWidget(self.table)

        self.car = QTreeWidgetItem(["Car"])
        self.house = QTreeWidgetItem(["House"])
        self.table.addTopLevelItem(self.car)
        self.table.addTopLevelItem(self.house)

        toolbar = QToolBar("Toolbar")
        carAction = QAction("Car", self)
        carAction.triggered.connect(self.addToCar)
        houseAction = QAction("House", self)
        houseAction.triggered.connect(self.addToHouse)
        updateAction = QAction("Update", self)
        updateAction.triggered.connect(self.update)

        toolbar.addAction(carAction)
        toolbar.addAction(houseAction)
        toolbar.addAction(updateAction)
        self.addToolBar(toolbar)

    def addToCar(self):
        child =  QTreeWidgetItem(["Audi", str(25000)])
        self.car.addChild(child)

    def addToHouse(self):
        child =  QTreeWidgetItem(["Villa", str(500000)])
        self.house.addChild(child)

    def update(self):
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Table()
    win.show()
    sys.exit( app.exec_() )
question from:https://stackoverflow.com/questions/65559792/enable-disable-qtreewidget-from-updating

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I do not know of a way to "suspend" tree updates, but what about using lists to store the children until one decides to update? See the modified example below (modifications are commented):

import sys
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QTreeWidget, QTreeWidgetItem

class Table(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(800, 600)
        self.setMinimumSize(QSize(800, 600))

        self.table = QTreeWidget()
        self.table.setHeaderLabels(["Description", "Price"])
        self.setCentralWidget(self.table)

        self.car = QTreeWidgetItem(["Car"])
        self.house = QTreeWidgetItem(["House"])
        self.table.addTopLevelItem(self.car)
        self.table.addTopLevelItem(self.house)

        # initialize empty lists which will keep track of generated children
        self.car_list, self.house_list = [], []

        toolbar = QToolBar("Toolbar")
        carAction = QAction("Car", self)
        carAction.triggered.connect(self.addToCar)
        houseAction = QAction("House", self)
        houseAction.triggered.connect(self.addToHouse)
        updateAction = QAction("Update", self)
        updateAction.triggered.connect(self.update)

        toolbar.addAction(carAction)
        toolbar.addAction(houseAction)
        toolbar.addAction(updateAction)
        self.addToolBar(toolbar)

    def addToCar(self):
        child = QTreeWidgetItem(["Audi", str(25000)])
        # instead of adding them directly to the tree, keep them in the list
        self.car_list.append(child)


    def addToHouse(self):
        child =  QTreeWidgetItem(["Villa", str(500000)])
        # instead of adding them directly to the tree, keep them in the list 
        self.house.addChild(child)

    def update(self):
        # update the tree and reset the lists
        self.car.addChildren(self.car_list)
        self.house.addChildren(self.house_list)
        self.car_list, self.house_list = [], []


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Table()
    win.show()
    sys.exit( app.exec_() )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...