Reputation: 317
I use tree/table model (inherited from QStandardItemModel) and a couple of views for different purposes.
Some rows of the model have children-rows, and some of them may also have children and so on.
In QTreeView I would like to show only top-level rows and theirs "first-level children" - grandchildren and their children should be hidden.
How can I do it?
Upvotes: 0
Views: 637
Reputation: 317
My working solution, based on Vladislav Mikitich reply:
bool ArchiveQSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (source_parent == qobject_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem()->index())
{
// always accept children of rootitem, since we want to filter their children
return true;
}
if (source_parent.parent() == qobject_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem()->index())
{
return true;
}
if (source_parent.parent().parent() == qobject_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem()->index())
{
return false;
}
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
Upvotes: 0
Reputation: 485
You need to use QSortFilterProxyModel.
Look example
bool YourQSortFilterProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{
if (source_parent == qobject_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem()->index())
{
// always accept children of rootitem, since we want to filter their children
return true;
}
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
Upvotes: 2