Domanda

I'm writing a C++ application that uses Qt classes to work with certain data models. For that purpose I inherited from QAbstractItemModel:

// the following is a class that represents the actual data used in my application
class EventFragment
{
....
private:
    qint32 address;
    QString memo;
    QDateTime dateCreated;
    QVector<EventFragment*> _children;
....
};

// the following is the model representation that used by my application to display the actual details to the user
class EventModel : public QAbstractItemModel
{
     Q_OBJECT
public:
     explicit EventModel (QObject *parent = 0);
....
private:
     // the following is the root item within the model - I use a tree-like presentation to show my data
     EventFragment* _rootFragment;
};

At some point I needed a sort/filter option in my application so I also created a class that inherits from QSortFilterProxyModel

class EventProxyModel : public QSortFilterProxyModel
{
     Q_OBJECT
public:
     explicit EventProxyModel (QObject *parent = 0);
...
public:
     // I had to add my custom implementation in the 'lessThan' method to achieve a
     // more complex sort logic (not just comparing the actual values but using
     // additional conditions to compare the two indexes)
     virtual bool lessThan ( const QModelIndex & left, const QModelIndex & right ) const;
...
};

To achieve sorting, I used the default QSortFilterProxyModel::sort() method (I haven't reimplemented it in my proxy model class) and for a time it seemed to work.

At some point though, I noticed that the actual QSortFilterProxyModel::sort() method sorts the entire model and what I need is to sort only the immediate children of a certain index.

I tried to reimplement the sort() method of the EventModel class, but after a while I realized that QSortFilterProxyModel::sort() is not referring to it at all. On the other hand, I'm not sure how to rearrange the indexes in a safe way so that the view which displays the model does not crash.

I think there must be a way to sort only the immediate children of a certain QModelIndex, but I haven't found it yet.

Is there any tutorial/example that demonstrates a possible solution to my case, or some guidelines on how to do it?

Regards

È stato utile?

Soluzione

If you want an optimized solution that doesn't do comparisons at all for the indexes you don't want to sort, I think you'd have to reimeplement your own QAbstractProxyModel, which is a non-trivial task. However, if you're fine with a non-optimized solution, I'd try this:

bool EventProxyModel::lessThan( const QModelIndex & left, const QModelIndex & right ) const {
    if ( left.parent() == isTheOneToSortChildrenFor ) {
        ...apply custom comparison
    } else {
        return left.row() < right.row();
    }
}

Comparing the rows in the source should leave everything other then indexes with that specific parent as they are.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top