Results 1 to 2 of 2

Thread: Updating checkboxes in QTreeView programmatically

  1. #1
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Updating checkboxes in QTreeView programmatically

    Hi All

    I have a QTreeView with a custom model.
    The behaviour I'm looking for is that a user can check or uncheck bottom level items (ie. items that have no children) and when all the siblings have been checked for a particular parent, the parent gets checked.

    So far I have no problem with getting the correct bottom level items to check and uncheck but the parent won't update until either it is selected or the QTreeView loses focus.

    The reimplemented code for the flags is:

    Qt Code:
    1. else if (index.column() == columnHeadingsList.indexOf("Complete"))
    2. {
    3. if ((aNode->children).count() == 0)
    4. {
    5. flags |= Qt::ItemIsUserCheckable;
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    The code for data is:
    Qt Code:
    1. else if (role == Qt::CheckStateRole)
    2. {
    3. if (index.column() == columnHeadingsList.indexOf("Complete"))
    4. {
    5. if (aNode->getIsComplete())
    6. return Qt::Checked;
    7. else
    8. return Qt::Unchecked;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    And the code for setData is:
    Qt Code:
    1. else if (index.column() == columnHeadingsList.indexOf("Complete"))
    2. {
    3. if (role == Qt::CheckStateRole)
    4. {
    5. // set this node's isComplete
    6. aNode->setIsComplete(value.toBool());
    7.  
    8. // set parent node's isComplete depending on it's children being complete
    9. JBModelRow* aParentNode = aNode->parent;
    10. if (aParentNode)
    11. {
    12. bool aNodeComplete = true;
    13. for (int i = 0; i < (aParentNode->children).count(); i++)
    14. {
    15. if (!aParentNode->children[i]->getIsComplete())
    16. {
    17. aNodeComplete = false;
    18. i = (aParentNode->children).count();
    19. }
    20. }
    21. aParentNode->setIsComplete(aNodeComplete);
    22.  
    23. QModelIndex parentIndex = createIndex(aParentNode->getRow(),
    24. aParentNode->getCol(),
    25. aParentNode);
    26.  
    27. emit dataChanged(parentIndex, parentIndex);
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Any help appreciated.

    Jeff

  2. #2
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Updating checkboxes in QTreeView programmatically

    Have worked out what was wrong.
    I had the wrong column when creating the QModelIndex parentIndex. It should have been the index.col

    Cheers
    Jeff

Similar Threads

  1. Replies: 1
    Last Post: 16th April 2010, 21:59
  2. QTreeView and CheckBoxes
    By FreeG in forum Qt Programming
    Replies: 2
    Last Post: 9th December 2009, 09:36
  3. qtreeview + checkboxes
    By lamera in forum Newbie
    Replies: 9
    Last Post: 6th September 2008, 22:10
  4. Replies: 3
    Last Post: 25th July 2008, 14:30
  5. QTreeView with checkboxes
    By shad in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2006, 13:29

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.