Nominate our 2022 Qt Champions!

QTreeView sorts but not QTableView

  • I've got a QSqlTableModel subclass that is pulling from a SQLite database.

    When I push it through a QTreeView and set "view->setSortingEnabled(true);" it will allow the user to click on the columns and the view will sort.

    However, QTreeView doesn't work well with QSqlTableModel (it removes the expand and collapse buttons) so I'm trying to get it to sort with QTableView.

    I'm having zero luck so far. The only thing that tells me that its seeing the property set to true is the up-caret ^ symbol is displaying in the QTableView.

    I've tried using a QSortFilterProxyModel subclass and rewrote its lessThan method, but that didn't work either. The questions regarding sorting on google focuses on sorting only one column of the table or are in regards to sorting QStandardItemModel instances.

    I want all of the columns sortable, individually, in a QTableView.


  • This works for me:

    tableModel = new QSqlTableModel(this, db);

    //Set the tableModel in a proxyModel and set one column of the proxyModel in a listView
    proxyModel = new QSortFilterProxyModel;


  • I have basically the same as your example. One difference is I'm doing QSqlTableModel::OnFieldChange instead of OnManualSubmit.

    Another is that I am not telling it the QSqlDatabase to look at, I am just calling this;

    EDIT: I realized the following didn't contain enough.
    const QString databaseFile = "./data/cashflow.db";

    BalanceTab::BalanceTab(QWidget *parent) : QWidget(parent) {
    bool isRunningOkay = true;

    // check for the database file
    if (!QFile::exists(databaseFile)) {
    , qApp->tr("Database file not found")
    , qApp->tr(
    "Unable to find the database file.\n\nClick Cancel to exit.")
    , QMessageBox::Cancel);
    isRunningOkay = false;

    QSqlTableModel *balanceModel = new QSqlTableModel(parent); // note no second parameter

    I'm also using a read-only SQL view with a trigger that pushes certain field changes to my table, which in turn updates the read-only SQL view, then the QSqlTableModel, and finally the QTableView. (I did test this issue using only the table, but no luck.)

    Like I said the ^ symbol is showing on my first column so it looks like it wants to sort, but clicking on any of the columns does nothing.

    I can change the values by selecting (or clicking) and editing (and pressing enter). It propagates back to my database table.

    I'm not sure if its a SQLite thing or not. Are you using (or would you use) SQLite with your example?

    By the way, thanks for the initial reply! If nothing else, at least I know I'm on the right path.

  • The second parameter is completely optional. This does not have any impact, otherwise you would not see any data at all.

    All I can add, is that I tested my code and it works.

  • Thanks for confirming, Volker. That gives me some hope that I'm close to getting it working.

    As a side note, a friend from work said that when he worked on ADO and VB6 back in the day the "view"-type thing that they use would predicate its sorting/filtering characteristics on the backend source (the view/table in my case.)

    By the way, what version of the Qt libs are you running? I'm running 4.7.1-2.

  • Although you can sort in the background, this would not affect the proxy approach. The proxy model sorts the rows independently from the underlying actual model. I run 4.7.3 on a Mac, but it works on Windows and with older versions too.

  • The following lines were causing the issue:
    // set the header settings
    Qt::Orientation orientation(balanceView->horizontalHeader()->orientation());
    QHeaderView *headerView = new QHeaderView(orientation, this);

    It's interesting that setting the header view to its same settings (even if I comment out only the setHighlightSections and setMovable line) will still cause it not to sort.

    I just discovered the following will work instead, so I'm good with the movable and sortable columns.

    // set the header settings

    These properties have been in the code for a while so I forgot that they were there. Sorry I didn't try this earlier.

    Thanks for your assistance and info!

Log in to reply