PDA

View Full Version : Model sorting vs. selection



VlJE
13th October 2006, 15:32
Hello! After the sorting of my model (derived from the abstract table model, my own file model) the selection on my table view remains on the (now false) place. The problem: my selection is on the view and my sorting is in the model.
I thought it should work automatically in the view to keep the selection of indexes and not of cells by sorting my model.
What can You recommend?



void FileModel::sort ( int column, Qt::SortOrder order )
{

bool (*compare_funk)(const BUFileInfo &a,const BUFileInfo &b)=0; //point to the compare-function
switch (column)
{
case 0:
compare_funk=(order)?BUFileInfo::biggerName:BUFile Info::lessName;
break;
case 1:
compare_funk=(order)?BUFileInfo::biggerSize:BUFile Info::lessSize;
break;
case 2:
compare_funk=(order)?BUFileInfo::biggerExt:BUFileI nfo::lessExt;
break;
case 3:
compare_funk=(order)?BUFileInfo::biggerDate:BUFile Info::lessDate;
break;
case 4:
compare_funk=(order)?BUFileInfo::biggerState:BUFil eInfo::lessState;
break;
}

if(compare_funk)
{
//std::list<BUFileInfo>::sort(fileList.begin(),fileList.end(),compare_fun k);
qSort(fileList.begin(),fileList.end(),compare_funk );
emit layoutChanged();

}

}

jpn
22nd October 2006, 11:24
What can You recommend?
I think Qt 4.2's QSortFilterProxyModel should be able to do what you want.

VlJE
25th October 2006, 17:46
I know QSortFilterProxyModel from 4.1.2. It really sorts and doesn't lose selection ( the current item can be lost) .
The Problem is that I can overwrite the methode lessThen() but not biggerThen().
I d'like to have the directory list above not dependent of the reverse flag.

My (nor very good) solution was to marry the View and the model
(

class FileModel: public QAbstractTableModel
{
public:
.....
bool setSelectionModel(QItemSelectionModel *selectionModel)
{
pSelectionModel= selectionModel;
return bool(selectionModel) ;
}
......
private:
.....
std::list<BUFileInfo> fileList;
QItemSelectionModel *pSelectionModel;

};
)
and save the selection positions in the data container at the beginning of my Sort methode
(
void FileModel::sort ( int column, Qt::SortOrder order )
{
//save the selection if the View is known
QVector<const BUFileInfo *> fiSelVector;
.........
)
and find the appropriate indexes and set the selection again after the sorting.