我使用树/表模型(从QStandardItemModel继承)和几个视图用于不同目的。

模型的某些行具有子行,其中一些行也可能具有子行,依此类推。

在QTreeView中,我只想显示顶级行及其“一级子级”-应隐藏其子级及其子级。

我该怎么做?

最佳答案

您需要使用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/

10-11 18:44