Results 1 to 6 of 6

Thread: QTreeWidgetItem Check Boxes

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QTreeWidgetItem Check Boxes

    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

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QTreeWidgetItem Check Boxes

    I'm not sure whether I've understood what you're asking because the answer seems to obvious:
    Qt Code:
    1. item->setCheckedState(column, Qt::Unchecked);
    2. item->setText(column, QString());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,309
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeWidgetItem Check Boxes

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem Check Boxes

    Quote Originally Posted by Infinity View Post
    I'm not sure whether I've understood what you're asking because the answer seems to obvious:
    Qt Code:
    1. item->setCheckedState(column, Qt::Unchecked);
    2. item->setText(column, QString());
    To copy to clipboard, switch view to plain text mode 
    Unfortunately, this doesn't remove the checkbox, so it is visible even with the flags set to not include ItemIsUserCheckable.


    Added after 5 minutes:


    Quote Originally Posted by d_stranz View Post
    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:

    Qt Code:
    1. Structure
    2. Item 1
    3. Structure
    4. Item 2
    5. Item 3
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by ToddAtWSU; 24th September 2015 at 19:26.

  5. #5
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QTreeWidgetItem Check Boxes

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,309
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeWidgetItem Check Boxes

    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.

Similar Threads

  1. Check boxes
    By lupo5 in forum Newbie
    Replies: 2
    Last Post: 11th May 2014, 11:56
  2. Replies: 11
    Last Post: 3rd March 2012, 22:21
  3. QAbstractItemView and tri state check boxes
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 3rd June 2011, 07:54
  4. Qt tree widget with check boxes iteration
    By denton17 in forum Newbie
    Replies: 1
    Last Post: 9th November 2010, 14:24
  5. List withembedded check boxes
    By bruccutler in forum Newbie
    Replies: 3
    Last Post: 7th March 2007, 18:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.