PyQt Tree Widget,添加动态移除复选框 [英] PyQt Tree Widget, adding check boxes for dynamic removal

查看:26
本文介绍了PyQt Tree Widget,添加动态移除复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个树形小部件,它基本上允许用户查看数据的各种细分,并可以选择删除某些项目.为了做到这一点,我希望有与每个顶级项目和每个子项相关联的复选框,以便用户可以选择要删除的顶级项(以及该顶级项的所有子项).或者删除哪些特定的子项.为了给您一个更好的主意,我创建了一个示例,其中 [x] 表示选中的复选框,而 [ ] 表示一个空的复选框:

I am attempting to create a tree widget that will essentially allow the user to view various breakdowns of data and have the option to delete certain items. In order to do this I want to have check boxes associated with each top level item and each child so the user can select which top level items (and thus all the children of that top level item) to delete. Or which specific children to delete. To give you a better idea I've created an example where [x] represents a checked check box and [ ] represents an empty checkbox:

>Beverages Allowed in Stadium [ ]
    Soda                      [ ]
    Water                     [ ]
    Tea                       [ ]
    Spirits                   [X]
    Ale                       [ ]

>Tickets                      [X]
    Row A                     [X]
    Row B                     [X]
    Row C                     [X]
    Lawn                      [X]

有什么建议如何实施吗?我不知道这是否对难度有影响,但我为复选框分配了一个单独的列.

Any suggestions how to implement this? I don't know if it makes a difference as far as difficulty, but i have allocated a separate column for the check box.

推荐答案

除了您提供的 answer 之外,您可以通过使用 ItemIsTristate 父元素上的标志.

In addition to the answer you provided, you can simplify your logic by using the ItemIsTristate flag on the parent elements.

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
import sys

def main(): 
    app     = QApplication (sys.argv)
    tree    = QTreeWidget ()
    headerItem  = QTreeWidgetItem()
    item    = QTreeWidgetItem()

    for i in xrange(3):
        parent = QTreeWidgetItem(tree)
        parent.setText(0, "Parent {}".format(i))
        parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
        for x in xrange(5):
            child = QTreeWidgetItem(parent)
            child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
            child.setText(0, "Child {}".format(x))
            child.setCheckState(0, Qt.Unchecked)
    tree.show() 
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

<小时>

最重要的三行代码是:


The three most important lines of code are:

parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)

此设置将父元素设置为三态复选框.

This one sets up the parent element to be a three state check box.

child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
child.setCheckState(0, Qt.Unchecked)

这些将子项设置为可选择并将默认设置为未选中.如果孩子的复选框没有被赋予状态,则复选框元素不会出现.

These set up the child to be selectable and set the default to unchecked. If the child's checkbox isn't given a state, the checkbox element does not appear.

上面的代码构建了一个非常简单的树.

The code above builds a very simple tree.

但是,如果我选中父元素上的复选框,则会自动选择所有子元素:

However, if I check a check box on the parent element, all the children are automatically selected:

如果我想取消选择单个孩子,则父级进入部分选择(三态):

If, I wish to unselect a single child, the parent enters the partially selected (Tri-State):

如果未选择所有子项,则自动取消选择父项.如果未选择父项,则所有子项也会自动取消选择.

If all children are unselected, the parent is automatically unselected. If the parent is unselected, all children are automatically unselected as well.

这篇关于PyQt Tree Widget,添加动态移除复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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