PDA

View Full Version : QTreeWidgetItem Check Boxes



ToddAtWSU
22nd September 2015, 21:54
I have a QTreeWidget with multiple top-level QTreeWidgetItems. One of the columns in my QTreeWidgetItem is a boolean value. These QTreeWidgetItems can have children. If the item has children, the user cannot check/uncheck the box for the parent, as they can only choose if the child is checked. The parent then checks itself if any of its children is checked. If any child is checked, the parent checks itself. If all the children are unchecked, then the parent unchecks itself. However, what I would like the parent to do is instead of showing an unchecked checkbox, just show an empty cell.

When I create the QTreeWidgetItem initially, if it's not checked, I just set it's text to "" and don't set a check state. Is there anyway to get it back to this state without deleting and re-creating the whole QTreeWidgetItem?

Thanks!
Todd

Infinity
22nd September 2015, 22:13
I'm not sure whether I've understood what you're asking because the answer seems to obvious:


item->setCheckedState(column, Qt::Unchecked);
item->setText(column, QString());

d_stranz
23rd September 2015, 00:55
I'm not sure I understand either, but maybe you should look at toggling the Qt::ItemFlag ItemIsUserCheckable. This should cause the checkbox to appear / disappear depending on whether the flag is set or not.

This would be rather strange behavior if this is what you really intend. Imagine a user's confusion when they accidentally uncheck a box and it disappears.

ToddAtWSU
24th September 2015, 20:26
I'm not sure whether I've understood what you're asking because the answer seems to obvious:


item->setCheckedState(column, Qt::Unchecked);
item->setText(column, QString());


Unfortunately, this doesn't remove the checkbox, so it is visible even with the flags set to not include ItemIsUserCheckable.

Added after 5 minutes:


I'm not sure I understand either, but maybe you should look at toggling the Qt::ItemFlag ItemIsUserCheckable. This should cause the checkbox to appear / disappear depending on whether the flag is set or not.

This would be rather strange behavior if this is what you really intend. Imagine a user's confusion when they accidentally uncheck a box and it disappears.

So inside my tree, I have the following:


Structure
Item 1
Structure
Item 2
Item 3

The only items that the user can check are the Items. However, if Item 2 is checked, I want both Structures to be checked so when the user first looks at the collapsed structures, they know something inside is checked. But if Item 2 becomes unchecked, and no other items are checked, I want the structures to display nothing in the column, not even an unchecked box because the user cannot check structures, only individual items. A user would confused if they saw an empty check box and couldn't check it. Right now, a user cannot interact with a checked structure, it is there for a quick look purpose to see if any sub-items are checked. Hopefully this helps clear up confusion.

It looks like I will have to recreate the structure but the biggest difficulty is remembering if the node is expanded. Working on that right now...

Infinity
25th September 2015, 14:16
I don't think it is a good idea to design the tree this way. However, letting check boxes appear and disappear can be done using the method d_stranz already described.

In my opinion it would be better to show check boxes in the parent nodes always (using Qt::PartiallyChecked when only some but not all child nodes are checked). The user should be able to check/uncheck all child nodes using the parent check box.

d_stranz
25th September 2015, 23:05
because the user cannot check structures, only individual items

Then you might have to use some combination of other ItemFlags (ItemIsEditable, ItemIsSelectable, ItemIsEnabled, or their inverse) so that the structure checkboxes are visible but can't be modified by the user. I can't tell you without experimentation which ones those would be. I suspect if you turn off ItemIsEnabled, it will prevent the user from expanding the tree at that point. I am guessing turning off ItemIsEditable might do it.