首页 > 解决方案 > 启用/禁用 QTreeWidget 更新

问题描述

是否可以启用/禁用 QTreeWidget 更新?

我想向我的树中添加行并使用按钮手动更新它。显然,当我向 TreeWidget 添加一行时,它将显示在表格中。有没有办法禁用它,所以我可以添加 100 行而不是更新一次?如果没有,是否有使用 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_() )

标签: pythonpyqtqtreewidget

解决方案


我不知道一种“暂停”树更新的方法,但是在决定更新之前使用列表来存储孩子呢?请参阅下面的修改示例(修改已注释):

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_() )

推荐阅读