[Qt-interest] QAbstractItemView and tri state check boxes
Ross Bencina
rossb-lists at audiomulch.com
Thu Jun 2 13:24:25 CEST 2011
Hi Graham
flags() need to include
Qt::ItemIsTristate
then data() for Qt::CheckStateRole can return:
Qt::Unchecked, Qt::Checked or Qt::PartiallyChecked
Obviously if the data state changes you need to emit the appropriate dataChanged signal (eg if a child change causes the parent's state to change you need to emit dataChanged for the parent as well as the child).
Exactly how best to compute the tristate of the parent based on children state changes is up to you. The naive method would be to scan the whole subtree whenever a parent's state is requested (ie whenever data() is called). a more efficient approach might involve caching intermediate values and invalidating them when necessary. Another approach might be to keep a counter of "number of checked children" and transition the parent when this counter changes between 0 and non-zero and between childCount and less than childCount.
In one case where I've implemented this I have relatively small child lists and a shallow tree, so I just do a for loop across all children to determine the check state of the parent every time it is requested.
HTH
Ross.
----- Original Message -----
From: Graham Labdon
To: Qt-interest at qt.nokia.com
Sent: Thursday, June 02, 2011 8:55 PM
Subject: [Qt-interest] QAbstractItemView and tri state check boxes
Hello
I have a model/view that allowed me to display a check box for each item in my tree and allowed the user to check or uncheck the item.
What I want, however, is that if an item has subitems and these are not all checked then the parent should be partially checked, if they are all checked the parent should be checked and if none are checked then the parent should be unchecked. This should of course be applied to the whole tree.
I am struggling to implement this and would be grateful if anyone has any suggestions on how I can achieve this
Thanks
Graham
------------------------------------------------------------------------------
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110602/6ff3b7ad/attachment.html
More information about the Qt-interest-old
mailing list