首页 > 代码库 > pyqt QTreeWidget例子学习

pyqt QTreeWidget例子学习

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

# python:2.x

__author__ = ‘Administrator‘

from PyQt4.QtGui import  *

from PyQt4.Qt import *

from PyQt4.QtCore import *

import sys

class Tree(QMainWindow):

    def __init__(self,parnet=None):

        super(Tree,self).__init__(parnet)

        self.setWindowTitle(‘TreeWidget‘)

        self.tree = QTreeWidget()

        self.tree.setColumnCount(2)

        self.tree.setHeaderLabels([‘Key‘,‘Value‘])

        root= QTreeWidgetItem(self.tree)

        root.setText(0,‘root‘)

        child1 = QTreeWidgetItem(root)

        child1.setText(0,‘child1‘)

        child1.setText(1,‘name1‘)

        child2 = QTreeWidgetItem(root)

        child2.setText(0,‘child2‘)

        child2.setText(1,‘name2‘)

        child3 = QTreeWidgetItem(root)

        child3.setText(0,‘child3‘)

        child4 = QTreeWidgetItem(child3)

        child4.setText(0,‘child4‘)

        child4.setText(1,‘name4‘)

        self.tree.addTopLevelItem(root)

        self.setCentralWidget(self.tree) 

app =QApplication(sys.argv)

x = Tree()

x.show()

sys.exit(app.exec_())

如图:

注:由网友提供的内容,地址:http://blog.sina.com.cn/s/blog_4b5039210100h6co.html