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
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
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?
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
[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.)
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...
Qt 4.2 introduced QTreeView::expandAll(). Ain't that neat?And the feature reject mentions it BTW...
![]()
Current Qt projects : QCodeEdit, RotiDeCode
The point is that it's very expensive to call it for large models.
Now, consider a dynamically changing large model. You don't want to call QTreeView::expandAll() again and again, do you?Warning: if the model contains a large number of items, this function will be take time to execute.![]()
J-P Nurmi
Qt Code:
tree->setExpanded(parentIndex, expand); for (qint32 rowNum = 0; rowNum < treeModel->rowCount(parentIndex); ++rowNum) { tree->setExpanded(childIndex, expand); expandNode(childIndex); } }To copy to clipboard, switch view to plain text mode
Bookmarks