c++ - 如何在 QTreeView 中隐藏孙子

标签 c++ qt model-view-controller

我使用树/表模型(继承自 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/

相关文章:

c++ - QT 信号和插槽无法工作并给出错误消息

unit-testing - 使用适当的抽象时,我们是否需要对 GUI 进行单元测试?

angularjs - 如何通过指令和 Controller 中的代码交叉来构建 Angular web 应用程序?

php - Controller 与模型——需要解释

c++ - 初学者有什么实际使用的好项目

c++ - Qt:获取线程成员函数的运行时间

c++ - 如何在 C++ 中的原始线程中执行回调?

c++ - 程序跳过用户输入

c++ - 如果类型在命名空间中,QMetaObject::newInstance 会失败

c++ - 我应该使用 QScopedPointer 还是 std::unique_ptr?