QTableView sorting when using a model
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 :
Code:
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
Re: QTableView sorting when using a model
Use the three parameter version of qSort from QtAlgorithms.
Re: QTableView sorting when using a model
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)