Results 1 to 5 of 5

Thread: UserCheckable property in QTreeWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Smile UserCheckable property in QTreeWidget

    Hi,

    I am using QTreeWidget with one parent and one child QTreeWidgetItem, both are user checkable items.

    if the user checked the parent item, the children item also be checked. How to do this ?

    Thanks,
    **pc**

  2. #2
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UserCheckable property in QTreeWidget

    for this I wrote a slot which emits when the treewidget is clicked.
    but I can find any method to set check children of a QTreeWidgetItem.

    How to set check all child when theparent item is checked ?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UserCheckable property in QTreeWidget

    Quote Originally Posted by npc View Post
    How to set check all child when theparent item is checked ?
    Just iterate over children and check them too. You will need QTreeWidgetItem::childCount() and QTreeWidgetItem::child().

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: UserCheckable property in QTreeWidget

    The problem is that there is no simple and easy way to know when particularly the check state has changed. The Trolls were suggested to add QTreeWidget::itemCheckStateChanged() signal but unfortunately the idea got rejected.

    Here's a workaround:
    Qt Code:
    1. //
    2. // MyTreeWidgetItem
    3. //
    4. class MyTreeWidgetItem : public QTreeWidgetItem
    5. {
    6. public:
    7. // <contructors>
    8.  
    9. // reimplemented QTreeWidgetItem::setData(), as suggested
    10. virtual void setData(int column, int role, const QVariant& value);
    11. };
    12.  
    13. void MyTreeWidgetItem::setData(int column, int role, const QVariant& value)
    14. {
    15. if (role == Qt::CheckStateRole)
    16. {
    17. Qt::CheckState newState = static_cast<Qt::CheckState>(value.toInt());
    18. Qt::CheckState oldState = static_cast<Qt::CheckState>(data(column, role).toInt());
    19.  
    20. QTreeWidgetItem::setData(column, role, value);
    21.  
    22. if (newState != oldState)
    23. {
    24. MyTreeWidget* tree = qobject_cast<MyTreeWidget*>(treeWidget());
    25. if (tree)
    26. {
    27. emit tree->itemCheckStateChanged(this);
    28. }
    29. }
    30. }
    31. else
    32. {
    33. QTreeWidgetItem::setData(column, role, value);
    34. }
    35. }
    36.  
    37. //
    38. // MyTreeWidget
    39. //
    40. class MyTreeWidget : public QTreeWidget
    41. {
    42. Q_OBJECT
    43. friend class MyTreeWidgetItem;
    44.  
    45. public:
    46. ...
    47.  
    48. signals:
    49. void itemCheckStateChanged(MyTreeWidgetItem* item);
    50. };
    To copy to clipboard, switch view to plain text mode 

    Just notice that the checkable items must be instances of MyTreeWidgetItem, otherwise the signal wont get emitted. QTreeWidgetItemIterator might be handy for iterating the children..
    J-P Nurmi

  5. #5
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UserCheckable property in QTreeWidget

    It is all in how you write the code.

    Make a method that auto. (un)checks your child boxes like so:
    Qt Code:
    1. void ConfigurationForm::checkGroupChildren(QTreeWidgetItem* pItem, enum Qt::CheckState eCheckState)
    2. {
    3. if (pItem)
    4. {
    5. int nChildCount = pItem->childCount();
    6. for (int i = 0; i < nChildCount; i++)
    7. {
    8. pItem->child(i)->setCheckState(0,eCheckState);
    9. }
    10.  
    11. checkSiblingsSetParent(pItem); // If it is a child, set the parent check state accordingly
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    The next thing you need to do is send the right signal... itemClicked().
    Qt Code:
    1. connect(ui.treewMode, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(modeItemChecked(QTreeWidgetItem*,int)));
    To copy to clipboard, switch view to plain text mode 

    To get this to work as intended you need to write modeItemChecked() like this:
    Qt Code:
    1. void ConfigurationForm::modeItemChecked(QTreeWidgetItem* pItem, int nColumn )
    2. {
    3. if (pItem != NULL && nColumn != -1)
    4. {
    5. enum Qt::CheckState eCheckState = pItem->checkState(0);
    6. checkGroupChildren(pItem, eCheckState); // Check Items Children
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    So the theory is that when ever you click the text of the item... it will call this method. In that case it will not matter... nothing will change unless your children are not of the same check state. That is the main draw back of this design... it is not that bad though. When you do (un)check the check box... it will pick it up and (un)check the children. This is a lot easier solution than the one give above... especially if you are using Qt Designer.

    Hope that helps.

    EDITED:

    I also had to add this function:
    Qt Code:
    1. void ConfigurationForm::checkSiblingsSetParent(QTreeWidgetItem* pItem)
    2. {
    3. QTreeWidgetItem* pParentItem = NULL;
    4. enum Qt::CheckState eCheckState = Qt::Unchecked;
    5.  
    6. if (pItem != NULL) // Is a valid Item
    7. {
    8. pParentItem = pItem->parent();
    9. if (pParentItem != NULL) // Item was a child
    10. {
    11. int nChildCount = pParentItem->childCount();
    12. for (int i = 0; i < nChildCount; i++)
    13. {
    14. // See if any siblings are checked
    15. if (pParentItem->child(i)->checkState(0) == Qt::Checked)
    16. {
    17. eCheckState = Qt::Checked;
    18. break;
    19. }
    20. }
    21.  
    22. pParentItem->setCheckState(0, eCheckState); // Set parent according to children status
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    This way... when you (un)check a child box... the parent will reflect the correct change. I modified my original post code to use this function. It has been tested and works great.
    Last edited by bpetty; 4th April 2007 at 23:53.

Similar Threads

  1. FrameShape property for Q3GroupBox
    By user_mail07 in forum Qt Tools
    Replies: 3
    Last Post: 16th February 2007, 14:41
  2. QPushbutton Flat property and icon appearance.
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2006, 08:30
  3. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  4. QTreeView / QTreeWidget
    By morty in forum Qt Programming
    Replies: 4
    Last Post: 8th October 2006, 00:40
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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.