[SOLVED] Hide leafs in QTreeView
-
Hi all,
I'm working in QTreeView data representation and I would like to show a TreeModel (inherited from QStandardItemModel) that only show the nodes but not the leafs of them. An example could be:
- ROOT
** NODE 1
*** LEAF
*** NODE 1.1
** NODE 2
*** NODE 2.1
*** NODE 2.2
*** LEAF
And represented with the custom TreeModel like:
- ROOT
** NODE 1
*** NODE 1.1
** NODE 2
*** NODE 2.1
*** NODE 2.2
I have tried overloading rowCount() of the model, overloading the return of the childCount() or rowCount() of each item but I have not accomplished what I want. I know how many childs are leafs so I can return the total value decreasing the total of leafs.
One problem is that in the TreeModel::data() I need some way to show the following node in case that the current index is a leaf (due to the childCount() reports only the total of nodes with child).
- ROOT
-
Have you checked out the QSortFilterProxy class? It might suit your needs. Then the data function is still the same, but the sort filter proxy is placed between the view and the model to customize the way it is shown in every different view.
Greetz -
Thanks! I will take a look and I will post the results!
-
Hi again!
Here I post how to hide the leafs of a TreeModel. My model is based on QStandardItemModel, so I need to cast from it to evaluate if the node is a leaf or not:
@bool TreeFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &index) const
{
if (index.isValid())
{
TreeModel model = dynamic_cast<TreeModel>(sourceModel());if (model) { TreeItem *item = model->itemFromIndex(index); if (item and item->child(source_row)->hasChildren()) return true; } } else if (source_row == 0) return true; return false;
}@
-
you could simply avoid the casting when you hold a pointer to your source model as member.