# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
from PyQt4.QtGui import *
from PyQt4.Qt import *
from PyQt4.QtCore import *
import sys
class Tree(QTreeView):
def __init__(self,parnet=None,*args):
super(Tree,self).__init__(parnet,*args)
QTextCodec.setCodecForTr(QTextCodec.codecForName(‘utf-8‘))
mode=QStandardItemModel(4,2)
mode.setHeaderData(0,Qt.Horizontal,‘id‘)
mode.setHeaderData(1,Qt.Horizontal,‘name‘)
item1=QStandardItem(‘01‘)
item1.setIcon(QIcon(r‘i/a.png‘))
item2=QStandardItem(‘02‘)
item2.setIcon(QIcon(r‘i/b.png‘))
mode.setItem(0,1,item1)
mode.setItem(1,0,item2)
item5=QStandardItem(‘abc‘)
item2.appendRow(item5)
parent=QModelIndex()
for i in range(3):
parent1=mode.index(0,0,parent)
mode.insertRows(0,1,parent)
mode.insertColumns(0,0,parent)
index=mode.index(0,0,parent)
mode.setData(index,i)
self.setModel(mode)
app =QApplication(sys.argv)
x = Tree()
x.setEditTriggers(QAbstractItemView.NoEditTriggers)
x.header().setResizeMode(QHeaderView.ResizeToContents)
x.show()
sys.exit(app.exec_())
如图:
pyqt treeview基础学习,布布扣,bubuko.com