Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView sorts but not QTableView

QTreeView sorts but not QTableView

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swivelhead
    wrote on last edited by
    #1

    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.

    Thanks.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      This works for me:

      @
      tableModel = new QSqlTableModel(this, db);
      tableModel->setTable("qdntest");
      tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
      tableModel->select();

      //Set the tableModel in a proxyModel and set one column of the proxyModel in a listView
      proxyModel = new QSortFilterProxyModel;
      proxyModel->setSourceModel(tableModel);
      
      tableView->setModel(proxyModel);
      tableView->setSortingEnabled(true);
      

      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        swivelhead
        wrote on last edited by
        #3

        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.
        [code]
        const QString databaseFile = "./data/cashflow.db";

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

        // check for the database file
        if (!QFile::exists(databaseFile)) {
        QMessageBox::critical(
        0
        , 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
        [/code]

        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?

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • S Offline
            S Offline
            swivelhead
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                swivelhead
                wrote on last edited by
                #7

                The following lines were causing the issue:
                [code]
                // set the header settings
                Qt::Orientation orientation(balanceView->horizontalHeader()->orientation());
                QHeaderView *headerView = new QHeaderView(orientation, this);
                headerView->setHighlightSections(true);
                headerView->setMovable(true);
                balanceView->setHorizontalHeader(headerView);
                [/code]

                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.

                [code]
                // set the header settings
                balanceView->horizontalHeader()->setHighlightSections(true);
                balanceView->horizontalHeader()->setMovable(true);
                [/code]

                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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved