Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect view of custom hierarchical model when set proxy model that swap columns

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.

like image 748
typbl4 Avatar asked Oct 09 '22 09:10

typbl4


1 Answers

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.

like image 123
Silas Parker Avatar answered Oct 13 '22 09:10

Silas Parker