从别的地方复制过来的:
This is an attempt at rewriting each example found at [/openwebos/qt/tree/master/examples/tutorials/modelview](https://github.com/openwebos/qt/tree/master/examples/tutorials/modelview) as a single Python3/PyQt5 file.
The examples work with Python 3.6.3, Qt 5.9.1 and PyQt5 5.9 on Windows.
The `__main__` code is tweaked to work well from Spyder.
For the accompanying official tutorial about Qt Model/View programming, see [there](http://doc.qt.io/archives/qt-4.8/modelview.html).
Other tutorials:
- [this one](http://neurochannels.blogspot.fr/2015/01/pyside-tree-0-building-simple-tree-in.html) by neuronet
代码:
# -*- 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()
rootNode = standardModel.invisibleRootItem()
#defining a couple of items
americaItem = QtGui.QStandardItem("America")
mexicoItem = QtGui.QStandardItem("Canada")
usaItem = QtGui.QStandardItem("USA")
bostonItem = QtGui.QStandardItem("Boston")
europeItem = QtGui.QStandardItem("Europe")
italyItem = QtGui.QStandardItem("Italy")
romeItem = QtGui.QStandardItem("Rome")
veronaItem = QtGui.QStandardItem("Verona")
#building up the hierarchy
rootNode.appendRow(americaItem)
rootNode.appendRow(europeItem)
americaItem.appendRow(mexicoItem)
americaItem.appendRow(usaItem)
usaItem.appendRow(bostonItem)
europeItem.appendRow(italyItem)
italyItem.appendRow(romeItem)
italyItem.appendRow(veronaItem)
#register the model
treeView.setModel(standardModel)
treeView.expandAll()
#selection changes shall trigger a slot
selectionModel= treeView.selectionModel()
selectionModel.selectionChanged.connect(self.selectionChangedSlot)
self.treeView = treeView
@QtCore.pyqtSlot(QtCore.QItemSelection,QtCore.QItemSelection) #decorator has same signature as the signal
def selectionChangedSlot(self,newSelection,oldSelection):
#get the text of the selected item
index = self.treeView.selectionModel().currentIndex()
selectedText = index.data(qt.DisplayRole)
#find out the hierarchy level of the selected item
hierarchyLevel=1
seekRoot = index
invalid = QtCore.QModelIndex()
while seekRoot.parent() != invalid:
seekRoot = seekRoot.parent()
hierarchyLevel += 1
showString = '{}, Level {}'.format(selectedText,hierarchyLevel)
self.setWindowTitle(showString)
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app= QtWidgets.QApplication(sys.argv)
w = MainWindow(None)
w.show()
w.resize(360, 300)
app.exec_()
演示效果: