下面的示例应用程序旨在显示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()