如何创建复选框的树状视图? [英] How to create a treeview of checkboxes?

查看:47
本文介绍了如何创建复选框的树状视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建树视图,而pyside中的每个项目都是复选框?树项是字典,如下例所示:

How to create a tree view, while each item on the is checkbox in pyside? The tree items are dictionary that look like the example below:

A: 1: 1.1: 1.1.1
           1.1.2
      1.2: 1.2.1
           1.2.2
   2: 2.1  2.1.1
           2.1.2
B ...

推荐答案

试试这个:

import PyQt4.QtGui as gui
import PyQt4.QtCore as core

dat = { 'A': 
            { '1': 
                {'1.1': ['1.1.1', '1.1.2'],
                 '1.2': ['1.2.1', '1.2.2']
                 },            
             '2': 
                {'2.1': ['2.1.1','2.1.2']}
            }
     }

def add(p,ch):
    if isinstance(ch,dict):
        for k,v in ch.iteritems():
            item = gui.QTreeWidgetItem(p)
            item.setText(0, k)
            item.setCheckState(0,core.Qt.Unchecked)
            item.setFlags(core.Qt.ItemIsUserCheckable | core.Qt.ItemIsEnabled)
            add(item,v)
            #p.addChild(item)        
    else:
        for txt in ch:
            item = gui.QTreeWidgetItem(p)
            item.setText(0, txt)
            item.setCheckState(0,core.Qt.Unchecked)
            item.setFlags(core.Qt.ItemIsUserCheckable | core.Qt.ItemIsEnabled)
            #p.addChild(item)        


app = gui.QApplication([])
tw = gui.QTreeWidget()

add(tw,dat)

tw.show()

app.exec_()

这篇关于如何创建复选框的树状视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆