PDA

View Full Version : About QAbstractProxyModel::sort(...) method



zgulser
13th December 2013, 16:33
Hi,

As you know there is method in both QAbstractProxyModel & QAbstractItemModel classes. What I want to know is - what is the first parameter of the sort method. For example;


myFilterModel->sort(0, Qt::AscendingOrder);

0 refers to column here but according to what? I mean I have custom objects in my data model with various members/fields. How '0' in the sort method associated with my highlighted custom object's field?

thomas@itest
13th December 2013, 17:26
Hi,

not exactly : QAbstractItemModel has the sort method but QAbstractProxyModel has the sort method only by inheritance from QAbtractItemModel ...

AND the docs says :



void QAbstractItemModel::sort (int column , Qt::SortOrder order=Qt::AscendingOrder ) [virtual]
Sorts the model by column in the given order.
The base class implementation does nothing.


So, it's up to you to do what you want with the "int column" parameter by reimplementing this method ...
(It would be a good idea that the column parameter refers to the same entity than the one returned by "int QModelIndex::column() const" ...)

anda_skoa
13th December 2013, 17:28
Your model decides which field it maps into which column.

That's what models do: they provide an abstract interface to data. their main job is to define what a row means and what a column means in terms of the data they are working on.

Cheers,
_

zgulser
16th December 2013, 07:51
Your model decides which field it maps into which column.

That's what models do: they provide an abstract interface to data. their main job is to define what a row means and what a column means in terms of the data they are working on.

Cheers,

You're absolutely right but it would be much better if there is a sort method which takes a Role as a parameter.

ChrisW67
16th December 2013, 08:21
You mean like calling QSortFilterProxyModel::setSortRole() before sorting?

You are, of course, quite welcome to subclass either the model or the proxy to reimplement sort() however you want. The function is virtual for a reason.

anda_skoa
16th December 2013, 08:41
You're absolutely right but it would be much better if there is a sort method which takes a Role as a parameter.
It would still need a column.

The model can use whatever it wants for sorting, the sorting key doesn't even have to be accessible through a role.
Of course the model can always allow to set a sorting role as additional information if a single column can be sorted differently depending on what role one is using.

Cheers,
_