繁体   English   中英

如何在QTreeView中隐藏子孙

[英]How to hide grandchildren in QTreeView

我使用树/表模型(从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);
}

基于弗拉迪斯拉夫·米基蒂奇的我的工作解决方案回复:

 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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM