Results 1 to 13 of 13

Thread: QTableView sorting

  1. #1
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView sorting

    Hi,

    I have a QTableView with some columns, one column is with numbers only. If I sorting this column, it's 1, 11, 12 ... 2, 21, 22 ... but I need 1, 2, 3 ... 10, 11, 12 ...

    How can I do this?

    Thx!
    Chris

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView sorting

    Hi Chris, you need to make sure your database column is a number type, then it would sort correctly. It looks to me like it's a character or varchar type and that's why it is sorting like that.

  3. #3
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting

    Ok, but how can I do that?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView sorting

    What is the model underlying the view? Is it a QStandardItemModel, QSqlTableModel or something else?

  5. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView sorting

    Are doing this from a database? If so, how are you defining the column spec when you create the table?

  6. #6
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting

    Qt Code:
    1.  
    2. ...
    3.  
    4. QList<QStandardItem *> column1, column2, column3, column4, column5;
    5.  
    6. foreach (...) {
    7. column1.append(new QStandardItem("data1..."));
    8. column2.append(new QStandardItem("data2..."));
    9. ...
    10. }
    11.  
    12. model->appendColumn(column1);
    13. model->appendColumn(column2);
    14. ...
    15.  
    16. model->setHeaderData(0, Qt::Horizontal, tr("Column1"));
    17. model->setHeaderData(1, Qt::Horizontal, tr("Column2"));
    18. ...
    19.  
    20. ui->tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    column5 data are numbers only! "79962", "35776", "140480", "120384" ... but all QStrings!
    Last edited by realdarkman71; 1st December 2010 at 10:30.

  7. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView sorting

    Well, that is the problem then. Even if strings contain only numbers, they will still sort order as strings, like you are seeing.
    What happens if you load column5 like:

    column5.append(new QStandardItem(1234)); ?

  8. #8
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting

    ...this doesn't work unfortunately, the constructor allows no numbers!

    Qt Code:
    1. QStandardItem::QStandardItem ( const QIcon & icon, const QString & text )
    2. QStandardItem::QStandardItem ( int rows, int columns = 1 )
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting

    I try to subclass QStandardItem, I think, this is the solution!

    Qt Code:
    1. class NumberItem : public QStandardItem {
    2.  
    3. public:
    4. NumberItem(const int number);
    5.  
    6. private:
    7. int value;
    8.  
    9. public:
    10. int type() const;
    11. QStandardItem *clone() const;
    12. QVariant data(int role = Qt::UserRole + 1) const;
    13. };
    14.  
    15. NumberItem::NumberItem(const int number) : QStandardItem() {
    16. value = number;
    17. }
    18.  
    19. int NumberItem::type() const {
    20. return QStandardItem::UserType;
    21. }
    22.  
    23. QStandardItem *NumberItem::clone() const {
    24. return new NumberItem(*this);
    25. }
    26.  
    27. QVariant NumberItem::data(int) const {
    28. return QVariant(number);
    29. }
    To copy to clipboard, switch view to plain text mode 

    than I append this to the model:

    Qt Code:
    1. column5.append(new NumberItem(12345));
    To copy to clipboard, switch view to plain text mode 

    It work's, the numbers are in the column 5 of tableView, but all number cells are checkable and disabled!? I don't know, why! setCheckable(false) and setEnabled(true) doesn't work!

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView sorting

    Quote Originally Posted by realdarkman71 View Post
    ...this doesn't work unfortunately, the constructor allows no numbers!

    Qt Code:
    1. QStandardItem::QStandardItem ( const QIcon & icon, const QString & text )
    2. QStandardItem::QStandardItem ( int rows, int columns = 1 )
    To copy to clipboard, switch view to plain text mode 
    So create the item with the default constructor and then use QStandardItem::setData() to set a QVariant of type QVariant::Int (or Uint, LongLong etc.) as the data for the Qt::DisplayRole. Alternatively, set the integer data on the EditRole and use setSortRole on the model to set the sort role to the EditRole.

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

    realdarkman71 (1st December 2010)

  12. #11
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting

    Quote Originally Posted by ChrisW67 View Post
    So create the item with the default constructor and then use QStandardItem::setData() to set a QVariant of type QVariant::Int (or Uint, LongLong etc.) as the data for the Qt:isplayRole.
    Ok, this works!

    Qt Code:
    1. item->setData(QVariant(12345), Qt::DisplayRole);
    2. column5.append(item);
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by ChrisW67 View Post
    Alternatively, set the integer data on the EditRole and use setSortRole on the model to set the sort role to the EditRole.
    How can I do this? Have you a code snippet for me?

  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView sorting

    If setting the Qt::DisplayRole is working then just use that. However:
    Qt Code:
    1. item->setData(QVariant(12345), Qt::EditRole);
    2. column5.append(item);
    To copy to clipboard, switch view to plain text mode 
    and, elsewhere:
    Qt Code:
    1. model->setSortRole(Qt::EditRole);
    To copy to clipboard, switch view to plain text mode 
    This makes all the columns in the model sort on the edit role.

  14. The following user says thank you to ChrisW67 for this useful post:

    realdarkman71 (1st December 2010)

  15. #13
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting

    Ok, big thanks for your help!

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 18:13
  2. QTableView sorting problem
    By noktus in forum Newbie
    Replies: 11
    Last Post: 23rd April 2008, 11:20
  3. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 14:22
  4. QTableView sorting
    By Bojan in forum Newbie
    Replies: 2
    Last Post: 28th September 2006, 09:11
  5. Sorting QTableView
    By Jimmy2775 in forum Qt Programming
    Replies: 7
    Last Post: 9th February 2006, 17:47

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.