0
我用樹/表模型(從QStandardItemModel繼承),併爲不同的目的一對夫婦的意見。如何隱藏孫子QTreeView則
該模型的某些行有子行,其中有些行也可能有子行,等等。
在QTreeView則我想只顯示頂層行和他們的「第一級子」 - 孫子和他們的孩子應該被隱藏。
我該怎麼辦呢?
我用樹/表模型(從QStandardItemModel繼承),併爲不同的目的一對夫婦的意見。如何隱藏孫子QTreeView則
該模型的某些行有子行,其中有些行也可能有子行,等等。
在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);
}
我工作的解決方案,基於弗拉季Mikitich回覆:
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);
}
非常感謝,它的工作原理! – paws