【问题标题】:Enable/Disable QTreeWidget from updating启用/禁用 QTreeWidget 更新
【发布时间】:2021-04-10 01:49:07
【问题描述】:

是否可以启用/禁用 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_() )

【问题讨论】:

    标签: python pyqt qtreewidget


    【解决方案1】:

    我不知道“暂停”树更新的方法,但是在决定更新之前使用列表来存储子节点怎么样?见下面的修改示例(修改有注释):

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

    【讨论】:

      猜你喜欢
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      相关资源
      最近更新 更多