I have custom hierarchical model, inherited from QAbstractModelItem. Also, I implement MySortFilterProxyModel subclassed from QSortFilterProxyModel. MySortFilterProxyModel can remove and swap columns. If first column in MySortFilterProxyModel corresponds the first column in model everything works fine. But if it is swaped in proxy model there are some troubles with view: MySortFilterProxyModel::hasChildren works fine, so on the top level i have "+" near the elements that has children. But when i try expand it - no child items are displayed. Here are some MySortFilterProxyModel methods:
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) );
}
During degugging i found out that MySortFilterProxyModel::rowCount returns correct data. But also i notice that MyModel::rowCount is called not through the MySortFilterProxyModel::rowCount, but from QSortFilterProxyModel::index(). Peharps it's the problem?
So the particular question is What is the right way to implement proxy model for swapping and switching off columns in hierarchical model?
Help me please to solve the problem. Thank you.
Try using the Qt Model Tester to get more information.
This will fail with an assertion if something is wrong with the models. When an assertion fails, look in the model test code at the comment above that assertion to find out what went wrong.
Make sure you attach a model tester to the proxy model and the source model, otherwise an error in the source model may cause a proxy to fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With