Results 1 to 5 of 5

Thread: QTableWidget Sorting

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableWidget Sorting

    I would like to sort the rows of a QTableWidget. The cell I want to sort on contains an icon indicating either an online or offline device (along with accompanying tooltip). I don't want to put visible text into that cell (I want the cell to have a small width) but would like to sort the rows based upon online status.

    First question, can there be hidden text in a cell?
    If so, can a sort be accomplished using hidden text?

    [IMG]C:\Documents and Settings\mclark\My Documents\My Pictures\online.jpg[/IMG]

  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 Sorting

    Quote Originally Posted by mclark View Post
    First question, can there be hidden text in a cell?
    Sure. You can use a custom role (different than Qt::DisplayRole) to store the text.
    If so, can a sort be accomplished using hidden text?
    Yes, but you can sort according to an image too. You have to reimplement QTableWidgetItem::operator<() to return the proper order of items. As you probably want to sort according to sort order selected in the table widget, you'll have to access the table through QTableWidgetItem::tableWidget() and get to the proper column by accessing appropriate header and its sortIndicatorSection. You can then compare pixmaps somehow (you can't do it directly as QPixmap doesn't have equality operator defined).

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Sorting

    Thanks for the quick reply. I'll give your suggestions a try.

  4. #4
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Sorting

    I created the QTableWidgetItem using the Qt::UserRole type.
    Qt Code:
    1. setItem(nRow, COL_Status, new QTableWidgetItem("Offline", Qt::UserRole));
    To copy to clipboard, switch view to plain text mode 
    The text always appears. Did I read your response incorrectly or is there more to this?

  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: QTableWidget Sorting

    Quote Originally Posted by mclark View Post
    Did I read your response incorrectly
    Yes, read it again and read the docs again - you're using Qt::DisplayRole because of incorrect use of the constructor (the second parameter doesn't determine the role but the type of item). It won't sort according to UserRole anyway... You'll still have to subclass and reimplement the operator and if you do, you don't need the additional role (and even if you don't want to sort images, you'll probably want to use the ToolTipRole anyway).

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

    mclark (27th October 2006)

Similar Threads

  1. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  2. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05
  3. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16
  4. Replies: 6
    Last Post: 5th March 2006, 21:05
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 22:34

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.