i attempting create tree widget allow user view various breakdowns of data , have option delete items. in order want have check boxes associated each top level item , each child user can select top level items (and children of top level item) delete. or specific children delete. give better idea i've created example [x] represents checked check box , [ ] represents empty checkbox:
>beverages allowed in stadium [ ] soda [ ] water [ ] tea [ ] spirits [x] ale [ ] >tickets [x] row [x] row b [x] row c [x] lawn [x] any suggestions how implement this? don't know if makes difference far difficulty, have allocated separate column check box.
in addition answer provided, can simplify logic using itemistristate flag on parent elements.
from pyqt4.qtcore import * pyqt4.qtgui import * import sys def main(): app = qapplication (sys.argv) tree = qtreewidget () headeritem = qtreewidgetitem() item = qtreewidgetitem() in xrange(3): parent = qtreewidgetitem(tree) parent.settext(0, "parent {}".format(i)) parent.setflags(parent.flags() | qt.itemistristate | qt.itemisusercheckable) 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 3 important lines of code are:
parent.setflags(parent.flags() | qt.itemistristate | qt.itemisusercheckable) this 1 sets parent element 3 state check box.
child.setflags(child.flags() | qt.itemisusercheckable) child.setcheckstate(0, qt.unchecked) these set child selectable , set default unchecked. if child's checkbox isn't given state, checkbox element not appear.
the code above builds simple tree.

however, if check check box on parent element, children automatically selected:

if, wish unselect single child, parent enters partially selected (tri-state):

if children unselected, parent automatically unselected. if parent unselected, children automatically unselected well.
Comments
Post a Comment