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. Inconvenience when sorting a QTableView with QSortFilterProxyModel
Forum Updated to NodeBB v4.3 + New Features

Inconvenience when sorting a QTableView with QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 826 Views 2 Watching
  • 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.
  • K Offline
    K Offline
    KlodKrichen
    wrote on last edited by KlodKrichen
    #1

    I am using QSortFilterProxyModel to sort my QTableView which has a model that inherits from QAbstractTableModel. Visually the TableView is sorted properly but the entries of the model does not get sorted. As a result, whenever a specific row data changes, the wrong entry gets updated.
    Here is the code that I am using:

    ui->tableView->setSortingEnabled(true);
    QSortFilterProxyModel *proxyModel2 = new QSortFilterProxyModel(ui->tableView);
    proxyModel2->setSourceModel(&containertei);
    ui->tableView->setModel(proxyModel2);
    ui->tableView->sortByColumn(2, Qt::AscendingOrder);
    

    containertei is the model that inherits from QAbstractTableModel and it gets created before setting the QSortFilterProxyModel

    JonBJ 1 Reply Last reply
    0
    • K KlodKrichen

      I am using QSortFilterProxyModel to sort my QTableView which has a model that inherits from QAbstractTableModel. Visually the TableView is sorted properly but the entries of the model does not get sorted. As a result, whenever a specific row data changes, the wrong entry gets updated.
      Here is the code that I am using:

      ui->tableView->setSortingEnabled(true);
      QSortFilterProxyModel *proxyModel2 = new QSortFilterProxyModel(ui->tableView);
      proxyModel2->setSourceModel(&containertei);
      ui->tableView->setModel(proxyModel2);
      ui->tableView->sortByColumn(2, Qt::AscendingOrder);
      

      containertei is the model that inherits from QAbstractTableModel and it gets created before setting the QSortFilterProxyModel

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @KlodKrichen said in Inconvenience when sorting a QTableView with QSortFilterProxyModel:

      but the entries of the model does not get sorted.

      Which model? It will not sort the source model (containertei), it will sort the proxy model (proxyModel2).

      As a result, whenever a specific row data changes, the wrong entry gets updated.

      Meaning what exact code? Be careful in your code whether you want to refer to/get QModelIndexes from the source model or the proxy model....

      1 Reply Last reply
      1
      • K Offline
        K Offline
        KlodKrichen
        wrote on last edited by KlodKrichen
        #3

        I want to sort the containertei model at the same time the view gets sorted.
        I get exactly what you mean, so how do I explicitly get the QModelIndex from the source model rather then the proxy model?

        JonBJ 1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          When you want to change a specific row in the view then either go through proxy's setData that will internally translate to the right base model index and call its setData, or, manually translate the proxy index to the base model index with mapToSource and operate on that.

          1 Reply Last reply
          3
          • K KlodKrichen

            I want to sort the containertei model at the same time the view gets sorted.
            I get exactly what you mean, so how do I explicitly get the QModelIndex from the source model rather then the proxy model?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @KlodKrichen said in Inconvenience when sorting a QTableView with QSortFilterProxyModel:

            I want to sort the containertei model at the same time the view gets sorted.

            Why? In that case, why are you interposing a QSortFilterProxyModel at all? It's one or the other:

            • If you have a QSortFilterProxyModel, that does the sorting, the underlying model does not get sorted, else it's pointless.

            • If you want to directly sort the underlying model (for some unknown reason, do you really want to?) then do not interpose a QSortFilterProxyModel.

            Otherwise if you do have the proxy then read carefully what @Chris-Kawa has said.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KlodKrichen
              wrote on last edited by KlodKrichen
              #6

              Thanks for the suggestion seems clear enough. The reason why I want to keep the same order in the view and the source model is that I have a state for each table row entry, depending on that state (reviewed / in revision) I am permitting/or not the user to edit the specific row. So whenever the table gets sorted, the state of each row is not the appropriate one since I am calling the Data method of the source model. So maybe the solution is to go through proxy's setData and Data method as @Chris-Kawa suggested

              Chris KawaC 1 Reply Last reply
              0
              • K KlodKrichen

                Thanks for the suggestion seems clear enough. The reason why I want to keep the same order in the view and the source model is that I have a state for each table row entry, depending on that state (reviewed / in revision) I am permitting/or not the user to edit the specific row. So whenever the table gets sorted, the state of each row is not the appropriate one since I am calling the Data method of the source model. So maybe the solution is to go through proxy's setData and Data method as @Chris-Kawa suggested

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7

                @KlodKrichen This sounds like that state should be a custom user role in the base model and accessed through the data/setData methods, so indices get translated automatically when using a proxy model on top of it. Sorting the base model is unnecessary here. Whether a user can edit given row would then be managed simply by implementing the flags method of the model and checking what the state is for that index.

                1 Reply Last reply
                3
                • K Offline
                  K Offline
                  KlodKrichen
                  wrote on last edited by
                  #8

                  Thank you very much, thanks to your suggestion I managed to solve the issue without sorting the source model.

                  1 Reply Last reply
                  1

                  • Login

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