Results 1 to 20 of 21

Thread: QTableWidget case sensitive sort

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question QTableWidget case sensitive sort

    Hi all,
    I have to sort a QTableWidget, and I achieve this with QTableView::setSortingEnabled(bool); but this sorting seems to be case sensitive, so the table is ordered in this way:
    a....zA.....Z and so on.

    Is there a way to make it NOT case sensitive? Maybe a flag or something like that...
    Last edited by Raccoon29; 3rd April 2008 at 12:29. Reason: wrong namespace
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  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: QTableWidget case sensitive sort

    Yes, reimplement operator< in QTableWidgetItem.

  3. The following user says thank you to wysota for this useful post:

    Raccoon29 (4th April 2008)

  4. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget case sensitive sort

    Quote Originally Posted by wysota View Post
    Yes, reimplement operator< in QTableWidgetItem.
    Thank you, brilliant idea
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  5. #4
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Red face Re: QTableWidget case sensitive sort

    oops... where should I overload it?
    Have I to inherit from QTableWidget and replace the existing table (i'm using QtDesigner)?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. #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: QTableWidget case sensitive sort

    No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.

  7. The following user says thank you to wysota for this useful post:

    Raccoon29 (5th April 2008)

  8. #6
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget case sensitive sort

    Quote Originally Posted by wysota View Post
    No, you have to subclass QTableWidgetItem, reimplement the operator and use your subclass instead of QTableWidgetItem.
    Ok, you are right.
    Thank you again
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  9. #7
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Re: QTableWidget case sensitive sort

    Ahi, my custom items seem to be ignored...
    Here is some code:
    CTableWidgetItem.h
    Qt Code:
    1. #include <QtGui>
    2.  
    3. /** Subclass definition **/
    4. class CTableWidgetItem: public QTableWidgetItem
    5. {
    6. public:
    7. CTableWidgetItem();
    8. bool operator<(const CTableWidgetItem &item);
    9. };
    To copy to clipboard, switch view to plain text mode 

    CTableWidgetItem.cpp
    Qt Code:
    1. #include "CTableWidgetItem.h"
    2.  
    3. CTableWidgetItem::CTableWidgetItem()
    4. {
    5. }
    6.  
    7. bool CTableWidgetItem::operator<(const CTableWidgetItem &item)
    8. {
    9. if(text().toUpper()<item.text().toUpper())
    10. return true;
    11. else
    12. return false;
    13. }
    To copy to clipboard, switch view to plain text mode 

    then I used my CTableWidgetItem instead of QTableWidgetItem in the table, like this:
    Qt Code:
    1. int row=0;
    2. ...
    3. CTableWidgetItem *idvar=new CTableWidgetItem;
    4. idvar->setText(id);
    5. ui.tbwvariations->setItem(row,0,idvar); //tbwvariations is a GUI QTableWidget
    To copy to clipboard, switch view to plain text mode 
    when I click the table's headbar they get sorted case insensitive like always.
    I tried a MessageBox in the operator method, but it get no ever called, so application never passes through it.
    What is the mistake...?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  10. #8
    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: QTableWidget case sensitive sort

    You have to reimplement the existing operator< (the one that takes QTableWidgetItem), not implement yours that takes your item.

  11. The following user says thank you to wysota for this useful post:

    Raccoon29 (7th April 2008)

  12. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget case sensitive sort

    Also, notice the constness of the method.
    J-P Nurmi

  13. The following user says thank you to jpn for this useful post:

    Raccoon29 (7th April 2008)

  14. #10
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget case sensitive sort

    ...right... now it works...

    what a stupid mistake... Bjarne Stroustrup would hurt me...
    Well, thank you both for the Nth time
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  15. #11
    Join Date
    Feb 2008
    Posts
    60
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget case sensitive sort

    the above posts in this thread helped me to do sorting in case sensitive manner.
    Thanks for this.

    If an item contains both text and an icon,
    is there any way to do sorting based on icon and also the text?
    (In my application, i am displaying system files and folders. so i need to sort folders first, then files)

    can anyone help me to do this.

    I am using Qt4.4.0 in windows

  16. #12
    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: QTableWidget case sensitive sort

    The contents of the comparison operator is totally up to you, so you can sort based on any criteria you choose. In your case you need a mechanism to decide which icon is "smaller".

Similar Threads

  1. Custom sort with QTableWidget
    By nicolas44 in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 00:47
  2. QTableWidget won't sort cellwidgets!!!
    By Arsenic in forum Qt Programming
    Replies: 7
    Last Post: 21st July 2007, 10:41

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.