qt之treeview例子

# -*- coding: utf-8 -*-

# python:2.x

__author__ = 'Administrator'

from PyQt4.Qt import *

from PyQt4.QtCore import *

from PyQt4.QtGui 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,'Path')

        mode.setHeaderData(1,Qt.Horizontal,'')

        item1=QStandardItem(u'收藏夹')

        mode.setItem(0,0,item1)

        item1_0=QStandardItem(u'下载')

        item1_1=QStandardItem(u'桌面')

        item1.appendRow(item1_0)

        item1.appendRow(item1_1)

        item2=QStandardItem(u'')

        item2_0=QStandardItem(u'视频')

        item2_1=QStandardItem(u'文档')

        item2.appendRow(item2_0)

        item2.appendRow(item2_1)

        mode.setItem(1,0,item2)

        item3=QStandardItem(u'硬盘')

        item3_0=QStandardItem(u'C')

        item3_1=QStandardItem(u'D')

        item3_2=QStandardItem(u'E')

        item3_3=QStandardItem(u'F')

        item3.appendRow(item3_0)

        item3.appendRow(item3_1)

        item3.appendRow(item3_2)

        item3.appendRow(item3_3)

        mode.setItem(2,0,item3)

        self.setModel(mode)

 

app =QApplication(sys.argv)

x = Tree()

x.setEditTriggers(QAbstractItemView.NoEditTriggers)

x.header().setResizeMode(QHeaderView.ResizeToContents)

x.show()

sys.exit(app.exec_())

 

 截图:qt之treeview例子_第1张图片

你可能感兴趣的:(qt之treeview例子)