Results 1 to 3 of 3

Thread: Hide children of QStandardItem in QTreeView

  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Hide children of QStandardItem in QTreeView

    I feel like I'm missing something really obvious, but is it possible to disable expansion of a particular QStandardItem within a QTreeView when it has children? My model has 4 root items uniquely subclassed from QStandardItem, each with a set of children of unknown quantity, but I only want two of them to actually show their children. Is this possible?

    I think QTreeWidgetItem::ChildIndicatorPolicy is a convenience function that does something like this, but I can't see anything similar in QStandardItem. Where in QStandardItem's implementation does it control the display of its children?

    In case it's relevant, I'm displaying them in a QTreeView.

  2. #2
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Hide children of QStandardItem in QTreeView

    if you have your own implementation of QAbstractItemModel or QStandardItemModel for your QTreeView you can return 0 from rowCount() for particular items
    or override function hasChildren () and returm false for particular items
    Last edited by folibis; 16th June 2012 at 05:43.

  3. The following user says thank you to folibis for this useful post:

    Phlucious (18th June 2012)

  4. #3
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hide children of QStandardItem in QTreeView

    That is exactly what I was looking for! Thank you.

    Here's my code:
    Qt Code:
    1. //myitemmodel.h
    2. class MyItemModel : public QStandardItemModel
    3. {
    4. Q_OBJECT
    5. public:
    6. explicit MyItemModel(QObject *parent = 0);
    7. explicit MyItemModel(int rows, int columns, QObject* parent = 0);
    8.  
    9. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    10. virtual bool hasChildren(const QModelIndex &parent) const;
    11. };
    12.  
    13. //myitemmodel.cpp
    14. #include "myitem.h"
    15. MyItemModel::MyItemModel(QObject *parent) :
    16. {}
    17.  
    18. MyItemModel::MyItemModel(int rows, int columns, QObject *parent) :
    19. QStandardItemModel(rows, columns, parent)
    20. {}
    21.  
    22. bool MyItemModel::hasChildren(const QModelIndex &parent) const
    23. {
    24. if(itemFromIndex(parent) != 0)
    25. {
    26. switch(itemFromIndex(parent)->type())
    27. {
    28. case MyItem::CUSTOMTYPE1: //QStandardItem::UserType +1
    29. case MyItem::CUSTOMTYPE3: //QStandardItem::UserType +3
    30. case MyItem::CUSTOMTYPE4: //QStandardItem::UserType +4
    31. /* force children to be hidden, no matter the view type */
    32. return false;
    33.  
    34. case MyItem::CUSTOMTYPE2: //QStandardItem::UserType +2
    35. default:
    36. /* proceed normally */
    37. break;
    38. }
    39. }
    40. return QStandardItemModel::hasChildren(parent);
    41. }
    42.  
    43. int SessionItemModel::rowCount(const QModelIndex &parent) const
    44. {
    45. if(itemFromIndex(parent) != 0)
    46. {
    47. switch(itemFromIndex(parent)->type())
    48. {
    49. case MyItem::CUSTOMTYPE1: //QStandardItem::UserType +1
    50. case MyItem::CUSTOMTYPE3: //QStandardItem::UserType +3
    51. case MyItem::CUSTOMTYPE4: //QStandardItem::UserType +4
    52. /* force children to be hidden, no matter the view type */
    53. return 0;
    54.  
    55. case MyItem::CUSTOMTYPE2: //QStandardItem::UserType +2
    56. default:
    57. /* proceed normally */
    58. break;
    59. }
    60. }
    61. return QStandardItemModel::rowCount(parent);
    62. }
    To copy to clipboard, switch view to plain text mode 

    This had the desired effect of particular item types appearing as if they had no children in my QTreeView. Note that only overriding QStandardItemModel::rowCount() made it so that the expansion button still appeared in the QTreeView even though its children were hidden, so I had to override QStandardItemModel::hasChildren(), too. It seems that overriding rowCount is optional in my case, but I did it anyway to be safe.

Similar Threads

  1. Deleting rows (children) from a qstandarditem
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 29th March 2012, 17:04
  2. Hide Parent and show only children in QTreeView
    By y.s.bisht in forum Qt Programming
    Replies: 8
    Last Post: 19th January 2012, 10:51
  3. Replies: 1
    Last Post: 16th April 2010, 22:59
  4. Replies: 0
    Last Post: 14th April 2010, 16:03
  5. Question on checkable QStandardItem in QTreeView
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2009, 01:30

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.