Results 1 to 3 of 3

Thread: QTableView sorting

  1. #1
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView sorting

    What's the most proper way of sorting a QTableView? QSortFilterProxyModel doesn't seem to work on QTableViews. At least I can't get it to work. For example if I take the sorting treeview example, and just change it so there is a QTableView, it doesn't work:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QFile file(":/default.txt");
    6. file.open(QIODevice::ReadOnly);
    7. TreeModel model(file.readAll());
    8. file.close();
    9.  
    10. QSortFilterProxyModel sortingModel;
    11. sortingModel.setSourceModel(&model);
    12.  
    13. QTableView sortedView;
    14. sortedView.setModel(&sortingModel);
    15. sortedView.setWindowTitle("Sorted Data");
    16. sortedView.setSelectionBehavior(QAbstractItemView::SelectRows);
    17. sortedView.horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
    18. sortedView.horizontalHeader()->setSortIndicatorShown(true);
    19. sortedView.horizontalHeader()->setClickable(true);
    20. sortedView.verticalHeader()->setVisible(false);
    21. sortedView.show();
    22.  
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    I would like to be able to sort the rows based on the column data in the clicked column. Qt 4.1. I don't need sample source code, just description of what to do, what things to connect, etc... Any advice is much appreciated.

    Bojan
    Last edited by Bojan; 9th March 2006 at 03:05.
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

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

    Default Re: QTableView sorting

    You should connect the sectionClicked signal from the header to sortByColumn slot in the view.

  3. #3
    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: QTableView sorting

    There is a method called QTableWidget::setSortingEnabled in Qt 4.1. This has been shifted to QTableView in Qt 4.2. Check it out to see what has to be done in order to enable sorting and disable selecting of rows/columns.

    Qt Code:
    1. // ...
    2. disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
    3. this, SLOT(selectColumn(int)));
    4. connect(horizontalHeader(), SIGNAL(sectionClicked(int)),
    5. this, SLOT(sortByColumn(int)));
    6. / ...
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. Refresh QTableView after sorting
    By araglin in forum Newbie
    Replies: 4
    Last Post: 18th December 2008, 22:13
  3. QTableView sorting problem
    By noktus in forum Newbie
    Replies: 11
    Last Post: 23rd April 2008, 10:20
  4. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 13:22
  5. Sorting QTableView
    By Jimmy2775 in forum Qt Programming
    Replies: 7
    Last Post: 9th February 2006, 16: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.