Results 1 to 3 of 3

Thread: Sort a Table that is not the root of the model

  1. #1
    Join Date
    Jan 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Sort a Table that is not the root of the model

    I have a custom QAbstractItemModel that is basically a tree of tables and i use a QTableView for each table to show every table.
    when I try to sort a table by column using a standard QSortFilterProxyModel it only works for the first column.

    I suspect it works that way because the sort is applied recursively but not for all the children of the root but only those in the corresponding column.

    for exemple if i have the folowing model :

    A
    | 1 2 3
    | 3 1 1
    | 4 5 0
    B
    | a b c
    | a e d
    | f a a

    I want to sort the table "A" by the third column to see in the A tableView :

    4 5 0
    3 1 1
    1 2 3

    I thought of using the QStandardItem::sortChildren method but it is not compatible with the use of a proxy model so i am stuck here with simple solutions.

    If no such solution exist I will have to subclass QAbstractProxyModel but it seems rather non-trvial.

    thanks for the help.

    edit : here's a minmum example :

    Qt Code:
    1. #include <QtWidgets>
    2. #include <QStandardItemModel>
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QStandardItem* Atable = new QStandardItem(3,3);
    8. Atable->setData("A",Qt::EditRole);
    9. Atable->setChild(0,0,new QStandardItem("1"));
    10. Atable->setChild(0,1,new QStandardItem("2"));
    11. Atable->setChild(0,2,new QStandardItem("3"));
    12. Atable->setChild(1,0,new QStandardItem("3"));
    13. Atable->setChild(1,1,new QStandardItem("2"));
    14. Atable->setChild(1,2,new QStandardItem("1"));
    15. Atable->setChild(2,0,new QStandardItem("2"));
    16. Atable->setChild(2,1,new QStandardItem("1"));
    17. Atable->setChild(2,2,new QStandardItem("3"));
    18. model->appendRow(Atable);
    19.  
    20. proxy->setSourceModel(model);
    21. QTableView* view = new QTableView();
    22. view->setModel(proxy);
    23. view->setRootIndex(proxy->mapFromSource(Atable->index()));
    24. view->setSortingEnabled(true);
    25. view->show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Poca; 20th January 2015 at 17:44.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,310
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sort a Table that is not the root of the model

    I think you need to re-read the docs for QStandardItemModel::appendRow():

    When building a list or a tree that has only one column, this function provides a convenient way to append a single new item.
    I don't think you are building the model you think you are.

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

    Poca (21st January 2015)

  4. #3
    Join Date
    Jan 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Sort a Table that is not the root of the model

    When putting the entire model into a treeView the model looks like the one I thought I built.

    I tried using setItem instead of appendRow.
    It works but i need to increase the number of columns of the root item to be able to sort my table.

    Thank you d_stranz for the help.

Similar Threads

  1. Restore table sort order after app restart
    By aguayro in forum Qt Programming
    Replies: 0
    Last Post: 21st August 2012, 19:20
  2. Replies: 0
    Last Post: 2nd November 2011, 11:13
  3. Custom widget table sort
    By giantdragon in forum Qt Programming
    Replies: 10
    Last Post: 10th June 2011, 12:36
  4. how to SORT table of numbers with QT
    By tommy in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2009, 09:34
  5. sort mysql table
    By eleanor in forum Qt Programming
    Replies: 2
    Last Post: 10th October 2007, 16:42

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
  •  
Qt is a trademark of The Qt Company.