pythoncheckboxpyqtqtreeviewqtreewidgetitem

PyQt Tree Widget, adding check boxes for dynamic removal


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.


Solution

  • 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.

    Simple Tree

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

    Parent Selected

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

    TriState

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