代码:
# -*- coding: utf-8 -*-
"""
@author: Taar
"""
#conversion of https://github.com/openwebos/qt/tree/master/examples/tutorials/modelview/6_treeview
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt as qt
ROWS = 2
COLS = 3
class MainWindow(QtWidgets.QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
treeView = QtWidgets.QTreeView(self)
self.setCentralWidget(treeView)
standardModel = QtGui.QStandardItemModel()
preparedRow = self.prepareRow('first','second','third')
item = standardModel.invisibleRootItem()
item.appendRow(preparedRow)
secondRow = self.prepareRow('111','222','333')
preparedRow[0].appendRow(secondRow)
treeView.setModel(standardModel)
treeView.expandAll()
def prepareRow(self,first,second,third):
rowItems = [QtGui.QStandardItem(first),
QtGui.QStandardItem(second),
QtGui.QStandardItem(third)]
return rowItems
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app= QtWidgets.QApplication(sys.argv)
w = MainWindow(None)
w.show()
w.resize(400, 200)
app.exec_()
演示效果:

本示例展示如何使用PyQt5创建一个树视图,并填充数据。通过继承QtWidgets.QMainWindow类,实现了一个窗口,其中包含一个树视图组件。树视图的数据模型由QtGui.QStandardItemModel提供,示例中准备了两行数据,每行有三列,第一行作为根节点,第二行作为第一行的第一个子节点。
2197

被折叠的 条评论
为什么被折叠?



