我使用树/表模型(继承自 QStandardItemModel)和几个用于不同目的的 View 。
模型的某些行有子行,其中一些也可能有子行等。
在 QTreeView 中,我只想显示顶级行和他们的“第一级 child ”——应该隐藏孙辈和他们的 child 。
我该怎么做?
最佳答案
您需要使用 QSortFilterProxyModel。
看例子
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);
}
关于c++ - 如何在 QTreeView 中隐藏孙子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392469/