MistyD
MistyD

Reputation: 17223

How to add string/numerical sorting to QSortFilterProxyModel derived class

I have a class derived from QSortFilterProxyModel, however when I click on the tabs of the table view for sorting nothing happens. Any suggestions on how I can add sorting feature to my custom class that inherits from QSortFilterProxyModel?

Upvotes: 1

Views: 209

Answers (1)

mhcuervo
mhcuervo

Reputation: 2660

It is well explained in the Qt documentation. This link points to the Qt 4.8 online reference.

The sorting feature is achieved either by implementing sort() in your model, or by using a QSortFilterProxyModel to wrap the model.

According to the question you're using the second approach. The class QSortFilterProxyModel provides a generic sort() reimplementation and also allows to achieve custom sorting behavior by subclassing QSortFilterProxyModel and reimplementing the lessThan() method.

My suggestions:

1- Make sure the proxy model is acting like a proxy. (it is sitting in the middle of the view and the real model).

QTreeView *treeView = new QTreeView;
MyItemModel *sourceModel = new MyItemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
treeView->setModel(proxyModel);

2- Make sure to enable sorting in the view (the default value is false).

treeView->setSortingEnabled(true);

3- If you needed to reimplement a member, make sure you have done it correctly.

I hope this helps.

Upvotes: 1

Related Questions