代码之家  ›  专栏  ›  技术社区  ›  Ivan

如何将子行添加到由特定列值找到的QTreeView行?

  •  1
  • Ivan  · 技术社区  · 6 年前

    下面的示例应用程序旨在显示QTreeView,用4行填充它,然后再添加12行,将它们作为子行随机分布在最初添加的4行之间。父级必须通过其UUID列(不能是行中的第一列)的值进行选择。所有UUID都是唯一的。

    我正在尝试使用 self.findItems(parent_uuid, Qt.MatchExactly, 1) (在哪里 parent_uuid: str

    如何更改代码以实现所需的行为?

    我使用Python3.7和Qt5.11。UI代码是使用pyuic5从使用QtDesigner生成的Qt UI XML文件生成的。

    完整的应用程序源代码:

    #!/usr/bin/env python
    
    
    import random
    import sys
    import uuid
    
    from PyQt5 import QtCore, QtWidgets
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QStandardItemModel, QStandardItem
    from PyQt5.QtWidgets import QApplication, QMainWindow
    
    LETTERS = 'abcdefghijklm'
    
    
    def main():
        application = QApplication(sys.argv)
        main_window = MainWindow()
        main_window.show()
        sys.exit(application.exec_())
    
    
    class TreeViewModel(QStandardItemModel):
        def __init__(self, *args, **kwargs):
            super(TreeViewModel, self).__init__(*args, **kwargs)
    
        def populate(self):  # This is where the work is done
            self.clear()
            self.setHorizontalHeaderLabels(['Name', 'UUID', 'Attr1'])
    
            parents_uuids = []
    
            for i in range(16):
                name = random.choice(LETTERS) \
                       + random.choice(LETTERS) \
                       + random.choice(LETTERS)
                uuid_ = str(uuid.uuid4())
                attr1 = str(random.random())
    
                row = [QStandardItem(name),
                       QStandardItem(uuid_),
                       QStandardItem(attr1)]
    
                if len(parents_uuids) < 4:
                    self.appendRow(row)
                    parents_uuids.append(uuid_)
                else:
                    # TODO: Fix this else clause so it'd make the new row a child
                    # The parent row must be found by its UUID column str value
                    parent_uuid = random.choice(parents_uuids)
                    parent = self.findItems(parent_uuid, Qt.MatchExactly, 1)
                    parent.appendRow(row)  # Error
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.actionPopulateTree.triggered \
                .connect(self.on_action_populate_tree)
            self.ui.treeView.setModel(TreeViewModel(self))
    
        def on_action_populate_tree(self):
            self.ui.treeView.model().populate()
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(320, 240)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName("gridLayout")
            self.treeView = QtWidgets.QTreeView(self.centralwidget)
            self.treeView.setObjectName("treeView")
            self.gridLayout.addWidget(self.treeView, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.toolBar = QtWidgets.QToolBar(MainWindow)
            self.toolBar.setObjectName("toolBar")
            MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
            self.actionPopulateTree = QtWidgets.QAction(MainWindow)
            self.actionPopulateTree.setObjectName("actionPopulateTree")
            self.toolBar.addAction(self.actionPopulateTree)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
            self.actionPopulateTree.setText(_translate("MainWindow", "PopulateTree"))
    
    
    if __name__ == "__main__":
        main()
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Yonghwan Shin    6 年前

    findItems返回与搜索匹配的项目列表,因此在您的情况下,您只需要获取第一个项目,但仍然无法添加项目,因为您必须将第0列中的项目作为父项,但您已从第1列获取该项目,因此其他逻辑部分将获取该项目:

    parent_uuid = random.choice(parents_uuids)
    items = self.findItems(parent_uuid, Qt.MatchExactly, 1)
    if items:
        ix = self.indexFromItem(items[0])
        ix_col_0 = self.sibling(ix.row(), 0, ix)
        it_col_0 = self.itemFromIndex(ix_col_0)
        it_col_0.appendRow(row)
    

    enter image description here