Results 1 to 2 of 2

Thread: Display a string in a QTableView but sort using an associated integer value

  1. #1
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Display a string in a QTableView but sort using an associated integer value

    Hi everyone,

    I'm using a QAbstractTableModel and a QSortFilterProxyModel with a tableview like this :

    Qt Code:
    1. MyTableModel *model = new MyTableModel(searcher);
    2. MyQSortFilterProxyModel *proxyModel = new MyQSortFilterProxyModel();
    3. proxyModel->setSourceModel(model);
    4. ui->tableView->setModel(proxyModel);
    To copy to clipboard, switch view to plain text mode 

    When i click on a column header it is sorted but it's using the default comparison operator of the types used in the model.
    I have a field representing an age in number of days but i display it in the tableview like "2 years".
    I'm trying to get the proxy model to sort this column using the integer value and the tableview to display it using its string representation.

    What would be the easiest way to obtain this behavior ?

    Thank you.

  2. #2
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Display a string in a QTableView but sort using an associated integer value

    Well i eventually managed to do it using a custom role in the tablemodel, subclassing the proxymodel and re-implementing the lessThan method.
    For people interested this is my lessThan methode in the proxymodel :

    Qt Code:
    1. bool MyQSortFilterProxyModel::lessThan(const QModelIndex &left,
    2. const QModelIndex &right) const
    3. {
    4. QVariant leftData = sourceModel()->data(left);
    5. QVariant rightData = sourceModel()->data(right);
    6.  
    7. if(left.data(Qt::UserRole).type() == QVariant::Double)
    8. {
    9. return (left.data(Qt::UserRole).toDouble() < right.data(Qt::UserRole).toDouble());
    10. }
    11. else if(leftData.type() == QVariant::String)
    12. {
    13. return (leftData.toString() < rightData.toString());
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. String to Integer / Integer to String
    By hesam in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2010, 12:59
  2. wrong sort in qtableview
    By toem in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2009, 10:30
  3. qtableview sort question?
    By yunpeng880 in forum Qt Programming
    Replies: 2
    Last Post: 3rd March 2009, 09:28
  4. QTableView does not display time string correctly
    By ad5xj in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2007, 20:35
  5. converting string to unsigned integer
    By mgurbuz in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2006, 09:46

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.