Results 1 to 6 of 6

Thread: Sorting in QListView class

  1. #1
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Sorting in QListView class

    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-

    Qt Code:
    1. QDir dir("/root");
    2.  
    3. dir.setSorting(QDir::DirsFirst);
    4.  
    5. const QFileInfoList *dirList=dir.entryInfoList();
    6. QFileInfoListIterator it(dirList);
    7.  
    8. while(fi=it.current())
    9. {
    10. QListVIewItem *dirItem=new QListViewItem(ListView);
    11. dirItem->setText(0,fi->fileName());
    12. ListView->insertItem(dirItem);
    13. ++it;
    14. }
    To copy to clipboard, switch view to plain text mode 

    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!!!
    Last edited by wysota; 14th April 2010 at 09:48. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sorting in QListView class

    You can add items to the list in reverse order or reimplement QListView::sort() to perform sorting according to the rules you want.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting in QListView class

    where as i want my items to be viewed as Directories first and FIles later.
    That itself implies you want custom sort

  4. #4
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sorting in QListView class

    But my list is already sorted using QDir:IrsFirst;
    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???

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sorting in QListView class

    Quote Originally Posted by Yayati.Ekbote View Post
    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.

  6. #6
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sorting in QListView class

    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.

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 19:34
  2. QListView sorting
    By fear in forum Qt Programming
    Replies: 14
    Last Post: 21st August 2007, 13:08
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. How can I disable the QListView Sorting?
    By darpan in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2006, 10:36
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.