PDA

View Full Version : Simple way to expand all nodes on a QTreeView?



cboles
16th March 2006, 03:28
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

ball
16th March 2006, 05:23
how about recursion

jpn
16th March 2006, 08:14
This might not be the most efficient way..


QModelIndexList indexes = model->match(model->index(0,0), Qt::DisplayRole, "*", -1, Qt::MatchWildcard|Qt::MatchRecursive);
foreach (QModelIndex index, indexes)
tree->expand(index);

cboles
16th March 2006, 19:43
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

Pieter from Belgium
28th August 2007, 17:24
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?

jpn
28th August 2007, 17:32
Currently there is no way to do this automatically. This has been suggested (http://trolltech.com/developer/task-tracker/index_html?method=entry&id=98668) but unfortunately it got rejected. Connecting to QAbstractItemModel::rowsInserted() sounds like a good idea to me.

Pieter from Belgium
28th August 2007, 17:36
[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.)

Pieter from Belgium
28th August 2007, 17:40
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...

fullmetalcoder
28th August 2007, 18:25
Qt 4.2 introduced QTreeView::expandAll() (http://doc.trolltech.com/latest/qtreeview.html#expandAll). Ain't that neat? :) And the feature reject mentions it BTW... ;)

jpn
28th August 2007, 20:07
Qt 4.2 introduced QTreeView::expandAll() (http://doc.trolltech.com/latest/qtreeview.html#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? ;)

evgeniy
12th April 2014, 16:54
void MainWindow::expandNode(const QModelIndex &parentIndex, bool expand) {
tree->setExpanded(parentIndex, expand);
for (qint32 rowNum = 0; rowNum < treeModel->rowCount(parentIndex); ++rowNum) {
QModelIndex childIndex = treeModel->index(rowNum, 0, parentIndex);
tree->setExpanded(childIndex, expand);
expandNode(childIndex);
}
}