首页 > 解决方案 > 具有多列的 PyQt5 树视图如何将信息获取到其他列

问题描述

我正在尝试在 nPyQt5 中创建一个树视图,并且我希望在树中拥有列表项以及数据类型和长度,树的父级将没有此信息。我让树工作,但一切都在树的一列中,我想将信息添加到其他 2 列。

import sys
from PyQt5.QtWidgets import QDialog,QApplication,QFileDialog,QAbstractItemView
from PyQt5.QtGui import QStandardItemModel,QStandardItem
from PyQt5.QtCore import Qt
from treeview import *
import lxml.etree as etree

features = {('POLYGON', 'SLPR'): [('ONE WAY', ['NO', 'YES'], 'List', 3), ('CLASS', ['INTERSTATE', 'PRIMARY', 'RESIDENTIAL', 'SECONDARY', 'SERVICE', 'STATE HWY', 'TERTIARY', 'TRACK', 'US HWY'], 'List', 11)], ('POINT', 'CALC FLD'): [('NAME', [], 'TEXT', '50'), ('SURFACE', ['BLACK TOP', 'BRICK', 'CALICHE', 'CALICHE AND GRAVEL', 'CINDER', 'CONCRETE', 'DIRT', 'GRASS', 'GRAVEL', 'LIMESTONE', 'OILED', 'PAVED ASPHALT', 'ROCK', 'SAND', 'SAND AND GRAVEL', 'SCORIA', 'SHELL', 'SHELL & OIL', 'SLAG'], 'List', 18)], ('POINT', 'RKDH'): [('TYPE', ['COUNTY', 'DO NOT USE', 'ENGINEERED', 'IMPROVED', 'PRIMITIVE', 'TEMPLATE', 'TEMPORARY ACCESS'], 'List', 16)]}
class Window(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButtonFile.clicked.connect(self.dispFolder)
        self.ui.pushButtonLoad.clicked.connect(self.loadXml)
        self.show()
    def dispFolder(self):
        fname = QFileDialog.getOpenFileName(self,'Open File','/home')
        if fname[0]:
            self.ui.lineEditFile.setText(fname[0])
    def loadXml(self):
        print(features)
        model = QStandardItemModel(0,3,self.ui.treeView)
        model.setHeaderData(0,Qt.Horizontal,"CODE")
        model.setHeaderData(1,Qt.Horizontal,"DATA TYPE")
        model.setHeaderData(2,Qt.Horizontal,"LENGTH")
        self.ui.treeView.setModel(model)
        i=0
        for k,featuretype in features.items():
            parent1 = QStandardItem('{}'.format(k[1]))
            for item in featuretype:
                child = QStandardItem(item[0])
                if len(item[1])>0:
                    for listitem in item[1]:
                        gchild=QStandardItem(listitem)
                        child.appendRow(gchild)
                parent1.appendRow(child)
            model.setItem(i,0,parent1)
            self.ui.treeView.setFirstColumnSpanned(i,self.ui.treeView.rootIndex(),True)
            i+=1


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

这是我在 Designer 中创建的 ui 的 treeview.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.pushButtonFile = QtWidgets.QPushButton(Dialog)
        self.pushButtonFile.setGeometry(QtCore.QRect(310, 20, 75, 23))
        self.pushButtonFile.setObjectName("pushButtonFile")
        self.lineEditFile = QtWidgets.QLineEdit(Dialog)
        self.lineEditFile.setGeometry(QtCore.QRect(70, 20, 231, 20))
        self.lineEditFile.setObjectName("lineEditFile")
        self.pushButtonLoad = QtWidgets.QPushButton(Dialog)
        self.pushButtonLoad.setGeometry(QtCore.QRect(250, 250, 75, 23))
        self.pushButtonLoad.setObjectName("pushButtonLoad")
        self.treeView = QtWidgets.QTreeView(Dialog)
        self.treeView.setGeometry(QtCore.QRect(70, 50, 256, 192))
        self.treeView.setObjectName("treeView")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButtonFile.setText(_translate("Dialog", "File"))
        self.pushButtonLoad.setText(_translate("Dialog", "Load"))

在此处输入图像描述

因此,对于 POINT,DATA TYPE 为 double,LENGTH 为 None,LOCATION 为 LIST DATA TYPE,LENGTH 为字段长度

所有这些信息都在 features 字典中,它是我拼凑的 xml 转换的一部分。字典中的列表实际上是原始代码中的一个 xml 元素。

标签: pythonpython-3.xtreeviewpyqt5

解决方案


由于标准项目模型是用 3 列实例化的,因此行可以附加一个或多个项目,因此

parent1.appendRow([item1,item2,item3])

将填充所有 3 列,为父级填充第二列只需将模型中的项目设置为所需的索引

model.setItem(row,col,item)

在此处输入图像描述


推荐阅读