PyQt5树视图具有多列,如何将信息获取到其他列中

问题描述 投票:0回答:1

我正在尝试在nPyQt5中创建树视图,我希望树中具有列表项以及数据类型和长度,树的父级将没有此信息。我有树在工作,但是所有内容都在树的一列中,我想在其他两列中添加信息。

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

这是我的treeview.py,其中包含我在Designer中创建的用户界面

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"))

enter image description here

因此,对于POINT,DATA TYPE为双精度,而LENGTH将为None,LOCATION将为LIST DATA TYPE,而LENGTH将为字段长度

所有这些信息都在功能字典中,这是我组装在一起的一部分xml转换。字典中的列表实际上是原始代码中的xml元素。

python python-3.x treeview pyqt5
1个回答
0
投票

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

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

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

model.setItem(row,col,item)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.