PDA

View Full Version : QTableView sorting when using a model



steg90
4th June 2008, 09:45
Hi,

Having to do some modifications to some code someone did well over 10 months ago. Part of this is to sort some data in ascending order within a QTableView. Now I noticed it uses a model ( QAbstractTableModel ). The data seems to be stored within a QList. The objects that are stored in this QList are of the following :



class ASignal
{
public:
ASignal() { }
char name[256];
char unit[256];
char strECU[20];
double dmaximum;
double dminimum;
double dfactor;
double doffset;
UINT istartbit;
UINT ilength;
double realvalue;
long rawvalue;
double byteorder;
};


I want to sort this QList<ASignal*> on the member name. What is the best and quickest way of achieving this? Before the data is inserted into the QList should I do some checking and use swap etc?

Kind regards,
Steve

jacek
4th June 2008, 14:39
Use the three parameter version of qSort from QtAlgorithms.

Conel
12th June 2008, 13:13
I would suggest not sorting data in model if you need only to display sorted data in QTableView.

There is another good solution: use QSortFilterProxyModel as a proxy between your model and QTableView. If you want to have some specific sorting then you only need to re-implement one virtual method

bool QSortFilterProxyModel::lessThan

But in most case its default implementation will be enough (see Qt help & examples on QSortFilterProxyModel)