我有自定义的分层模型,继承自QAbstractModelItem.此外,我实现了从QSortFilterProxyModel子类化的MySortFilterProxyModel.MySortFilterProxyModel可以删除和交换列.如果MySortFilterProxyModel中的第一列对应模型中的第一列,则一切正常.但如果它在代理模型中转换,则视图存在一些问题:MySortFilterProxyModel :: hasChildren工作正常,所以在顶层我有"+"附近有子节点的元素.但是当我尝试展开它时 - 不会显示任何子项.以下是一些MySortFilterProxyModel方法:
bool MySortFilterProxyModel::hasChildren(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return false;
QModelIndex source_parent = mapToSource(parent);
return sourceModel()->hasChildren( source_parent.sibling(source_parent.row(), 0) );
}
int MySortFilterProxyModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return 0;
QModelIndex source_parent = mapToSource(parent);
return sourceModel()->rowCount( source_parent.sibling(source_parent.row(), 0) );
}
Run Code Online (Sandbox Code Playgroud)
在degugging期间,我发现MySortFilterProxyModel :: rowCount返回正确的数据.但我也注意到MyModel :: rowCount不是通过MySortFilterProxyModel :: rowCount调用的,而是来自QSortFilterProxyModel :: index().Peharps这是问题吗?
所以特别的问题是在层次模型中交换和关闭列的实现代理模型的正确方法是什么?
请帮我解决问题.谢谢.