PDA

View Full Version : Sorting in QListView class



Yayati.Ekbote
14th April 2010, 08:55
Hi guys,

How can i disable sorting of QListView class if i want the items to be placed in the ListView as they are.???
For example-


QDir dir("/root");

dir.setSorting(QDir::DirsFirst);

const QFileInfoList *dirList=dir.entryInfoList();
QFileInfoListIterator it(dirList);
QFileInfo *fi;

while(fi=it.current())
{
QListVIewItem *dirItem=new QListViewItem(ListView);
dirItem->setText(0,fi->fileName());
ListView->insertItem(dirItem);
++it;
}

In this case the items which i want to show are file/folder items which i already sort in QDir class setSorting option. Now i don't want ListView to resort the items according to ascending or descending order..
QListView class gives sorting option as QListView::setSorting(int,bool ascending=True);

if i set int(column)=-1 the sorting is disabled but the items are always viewed in reverse order. where as i want my items to be viewed as Directories first and FIles later. Plz guide!!!

wysota
14th April 2010, 10:53
You can add items to the list in reverse order or reimplement QListView::sort() to perform sorting according to the rules you want.

aamer4yu
14th April 2010, 11:24
where as i want my items to be viewed as Directories first and FIles later.
That itself implies you want custom sort ;)

Yayati.Ekbote
14th April 2010, 12:21
But my list is already sorted using QDir::DIrsFirst;
Once the dir.entryInfoList is sorted. the iterator it reads the fileName and assigns the item name respectively. Its just that QListView again resorts the items ascending or descending by name precedence. I just want to switch off QListView class's sorting option which it does default.
As Wysota suggested, i need to reimplement the QListView::sort() method....
What parameters should i reimplement...the code for implementation of QListView class is too big....to read and understand easily for me!!!Any simpler way???

wysota
14th April 2010, 12:45
As Wysota suggested, i need to reimplement the QListView::sort() method....
What parameters should i reimplement...the code for implementation of QListView class is too big....to read and understand easily for me!!!Any simpler way???
I'd try to leave the implementation of sort() empty, you shouldn't have any sorting at all then. But I looked at your code again and I think the problem is not with sorting but with the fact that you are inserting items to the beginning of the list instead of the end of the list. There is a QListViewItem constructor that lets you specify where to insert the item.

Yayati.Ekbote
14th April 2010, 13:26
Thanks, I got the problem. Ya i was inserting items at the beginning. I realized that and made the following change.

QListViewItem *dirItem=new QListViewItem(ListView,dirItem);

i.e. i inserted items after the dirItem..

Thanks very much.