Results 1 to 3 of 3

Thread: Sort a QListWidget with Qt::AscendingOrder

  1. #1
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Sort a QListWidget with Qt::AscendingOrder

    Hi,

    QListWidget::sortItems ( Qt::SortOrder order = Qt::AscendingOrder ) works well, but I don't like the Qt::AscendingOrder policy. Indeed the function first sorts words beginning with an uppercase character and then sorts the other words starting with a lowercase character. Then it concatenates both results together.

    For example if you use sortItems on QListWidget containing the following items:

    a
    India
    Abercrombie
    iPad
    Queen
    iPhone
    iPod
    papa
    ISO

    You get the following result:

    Abercrombie
    India
    ISO
    Queen
    a
    iPad
    iPhone
    iPod
    papa

    ...And I would like this result instead:

    a
    Abercrombie
    India
    iPad
    iPhone
    iPod
    ISO
    papa
    Queen

    which is an ascending order that ignores cases.

    Is there a way to customize the sort policy ?

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sort a QListWidget with Qt::AscendingOrder

    Disclaimer: I have not tested this...

    I believe that you must create your own version of QListWidgetItem and then override operator<.

  3. #3
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Sort a QListWidget with Qt::AscendingOrder

    Hi All,

    I try to do something similar, but I have some issues when trying to overload the operator<
    The problem is that I can't recover my original item from the list, I think that I'm wrong in the way I'm extracting it frm the list because I have to use a lots of const_cast, and is suspicious.
    Here is the overload code for the operator

    struct CustomListWidgetItem : QListWidgetItem
    {
    CustomListWidgetItem(QListWidget *view =0,int type = Type) : QListWidgetItem(view,type){}
    bool operator< ( const QListWidgetItem & other ) const
    {
    QListWidget * list1 = this->listWidget();
    QListWidget * list2 = other.listWidget();

    QWidget * w1 = list1->itemWidget(const_cast<CustomListWidgetItem *>(this));
    QWidget * w2 = list2->itemWidget(const_cast<QListWidgetItem *>(&other));

    Item item1 = (reinterpret_cast<InCallItem*>(w1))->getItem();
    Item item2 = (reinterpret_cast<InCallItem*>(w2))->getItem();

    return item1 < item2;
    }
    };

    Note:
    - list1 == list2 (both items are in the same list)
    - w2 == NULL (Why? is correct the way I'm getting w2? should it be of the custom type? perhaps a dynamic_cast?)
    - Is there any problem if two items are equivalent? Item1 == Item2 ?


    thanks in advence
    Last edited by JuanMO; 2nd August 2010 at 17:27.

Similar Threads

  1. model.sort(col, Qt::AscendingOrder); does not work.
    By kaushal_gaurav in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 07:18
  2. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  3. How to sort QMap
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 10:11
  4. Sort datatable
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2006, 08:43
  5. When sort the lists?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 14th April 2006, 16:30

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.