QTableView setModel (7)

从别的地方复制过来的:

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

演示效果:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值