Results 1 to 11 of 11

Thread: Simple way to expand all nodes on a QTreeView?

  1. #1
    Join Date
    Mar 2006
    Posts
    46
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Simple way to expand all nodes on a QTreeView?

    I have a small model displayed in a QTreeView that I would like to have all of the nodes expanded by default when it is first displayed. It seems like there is no simple way to do this, or even a simple way to walk the all nodes and expand them. Does anyone have some suggestions?

    Colby

  2. #2
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    how about recursion

  3. #3
    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: Simple way to expand all nodes on a QTreeView?

    This might not be the most efficient way..
    Qt Code:
    1. QModelIndexList indexes = model->match(model->index(0,0), Qt::DisplayRole, "*", -1, Qt::MatchWildcard|Qt::MatchRecursive);
    2. foreach (QModelIndex index, indexes)
    3. tree->expand(index);
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to jpn for this useful post:

    cboles (16th March 2006)

  5. #4
    Join Date
    Mar 2006
    Posts
    46
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    Thanks for the example. I agree it may not be the most efficent, but it definitely works, and since I only need to do it once initially, the performance hit is negiligible.

    Colby

  6. #5
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    But still this is not satisfactory in all cases: suppose the model evolves during the application, and you want to expand all nodes by default.

    Is there an easy way to achieve this, or should to proposed code fragment be placed in a hook method that gets called upon each model change?

  7. #6
    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: Simple way to expand all nodes on a QTreeView?

    Currently there is no way to do this automatically. This has been suggested but unfortunately it got rejected. Connecting to QAbstractItemModel::rowsInserted() sounds like a good idea to me.
    J-P Nurmi

  8. #7
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    [Answering to my self, but also to the original asker:]

    Maybe a more elegant solution for keeping rows expanded by default is by reimplementing the following method:

    void QAbstractItemView::rowsInserted ( const QModelIndex & parent, int start, int end )

    In the reimplementation, besides the super class effect, explicitly expand all collapsed rows that were inserted [as well as their children that may already be present]...

    (However, it would be nice if in a future version of Qt, automatically managing such properties would be present -- like also automatically resizing columns. This of course unless explicitly overruled by the user (e.g. reducing the column with interactively, or collapsing an item interactively.)

  9. #8
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    Hehe, thinking the same thing at the same time . However I must admit you put it more concise, and with the correct references.

    QTreeView::itemsExpandable [as referred to in the feature rejection] does not help in this issue, as it can only be used to prevent the default collapsed item to be expanded...

  10. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    Qt 4.2 introduced QTreeView::expandAll(). Ain't that neat? And the feature reject mentions it BTW...
    Current Qt projects : QCodeEdit, RotiDeCode

  11. #10
    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: Simple way to expand all nodes on a QTreeView?

    Quote Originally Posted by fullmetalcoder View Post
    Qt 4.2 introduced QTreeView::expandAll(). Ain't that neat? And the feature reject mentions it BTW...
    The point is that it's very expensive to call it for large models.
    Warning: if the model contains a large number of items, this function will be take time to execute.
    Now, consider a dynamically changing large model. You don't want to call QTreeView::expandAll() again and again, do you?
    J-P Nurmi

  12. #11
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple way to expand all nodes on a QTreeView?

    Qt Code:
    1. void MainWindow::expandNode(const QModelIndex &parentIndex, bool expand) {
    2. tree->setExpanded(parentIndex, expand);
    3. for (qint32 rowNum = 0; rowNum < treeModel->rowCount(parentIndex); ++rowNum) {
    4. QModelIndex childIndex = treeModel->index(rowNum, 0, parentIndex);
    5. tree->setExpanded(childIndex, expand);
    6. expandNode(childIndex);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 28th February 2007, 08:34
  2. [QT4] QTreeView and expandable nodes
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2006, 16:52

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.