2016-05-23 6 views
0

Ich benutze Baum/Tabellen-Modell (von QStandardItemModel geerbt) und ein paar Ansichten für verschiedene Zwecke.So verstecken Sie Enkelkinder in QTreeView

Einige Zeilen des Modells haben Kinder-Zeilen, und einige von ihnen können auch Kinder und so weiter haben.

In QTreeView möchte ich nur Zeilen der obersten Ebene und ihre "First-Level-Kinder" zeigen - Enkel und ihre Kinder sollten ausgeblendet werden.

Wie kann ich es tun?

Antwort

2

Sie müssen QSortFilterProxyModel verwenden.

Blick Beispiel

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); 
} 
+0

Vielen Dank, es funktioniert! – paws

0

Meine Arbeitslösung, basierend auf Vladislav Mikitich antworten:

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

} 
Verwandte Themen