Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: QTableWidget case sensitive sort

  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 13: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,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 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,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 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,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 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,518 Times in 1,388 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,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 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".

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

    Default Re: QTableWidget case sensitive sort

    Thanks a lot for your reply.

    I have implemented this. and working fine.

    can anyone help me to sort all other item except the first item ( I am having '..' as the first item).

    after sorting, it is stayed in the first. but
    problem is:
    when the sorted column is again clicked, this comes to the bottom.

  18. #14
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget case sensitive sort

    Quote Originally Posted by jay View Post
    Thanks a lot for your reply.

    I have implemented this. and working fine.

    can anyone help me to sort all other item except the first item ( I am having '..' as the first item).

    after sorting, it is stayed in the first. but
    problem is:
    when the sorted column is again clicked, this comes to the bottom.
    For item '..' operator < must always return true

  19. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 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, if the sort order is reversed, it has to be the greatest item of all, so it has to return false. I think it's best to reimplement QTableWidget::sortItems().

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

    I guess QTableWidget::sortItems() is not virtual, and cant be reimplemented , isnt it ??

    Also for sorting icons, one can associate some role with the item and do sorting based on that role for the icon.

  21. #17
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget case sensitive sort

    Quote Originally Posted by wysota View Post
    No, if the sort order is reversed, it has to be the greatest item of all, so it has to return false. I think it's best to reimplement QTableWidget::sortItems().
    Oops, of course Wysota You are right.

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

    Default Re: QTableWidget case sensitive sort

    Quote Originally Posted by aamer4yu View Post
    I guess QTableWidget::sortItems() is not virtual, and cant be reimplemented , isnt it ??
    Hmm... right. Somehow I was sure there was a "virtual" keyword when I checked the method before posting. Anyway, knowing the item, you can ask it for its QTableWidgetItem::tableWidget() which then can be asked for its QTableWidget::horizontalHeader() which in turn can be asked for QHeaderView::sortIndicatorOrder() and QHeaderView::sortIndicatorSection(). Based on that info you can implement the sorting function in the item.

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

    Default Re: QTableWidget case sensitive sort

    Dear wysota,

    Thanks you sooo... much.

    tableWidget ()->horizontalHeader () ->sortIndicatorOrder ()
    - this helps me a lot to do proper sorting.

    Thank you.

  24. #20
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableWidget case sensitive sort

    this topic really help me,
    I think post the modified code may help more.
    Qt Code:
    1. //.h file
    2. class CTableWidgetItem: public QTableWidgetItem
    3. {
    4. public:
    5. CTableWidgetItem();
    6. CTableWidgetItem(const QString &text,int type= Type):QTableWidgetItem( text,type){};
    7. virtual bool operator<(const QTableWidgetItem &item) const;
    8. };
    9. //.cpp file
    10. CTableWidgetItem::CTableWidgetItem()
    11. {
    12. }
    13.  
    14. bool CTableWidgetItem::operator<(const QTableWidgetItem &item) const
    15. {
    16. if(text().toUpper()<item.text().toUpper())
    17. return true;
    18. else
    19. return false;
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Custom sort with QTableWidget
    By nicolas44 in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 01:47
  2. QTableWidget won't sort cellwidgets!!!
    By Arsenic in forum Qt Programming
    Replies: 7
    Last Post: 21st July 2007, 11: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.