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. QTableView, QSortFilterProxyModel row selection
Forum Updated to NodeBB v4.3 + New Features

QTableView, QSortFilterProxyModel row selection

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a QTableView this contains records from a database. I've implemented column sorting using QSortFilterProxyModel.

    Before QSortFilterProxyModel, to get a row selection I used:

    QItemSelectionModel* pobjModel(mptvSDrecs->selectionModel());
    QModelIndexList objIndexes(pobjModel->selectedIndexes());
    

    This worked, however now this doesn't return the correct row. Should I have changed something else?

    This is how I added the filter model:

    mptvSDrecs = new QTableview(this);
    mpsiSDmodel = new QStandardItemModel(this);
    mpsfpSDmodel = new QSortFilterProxyModel(this);
    mptvSDrecs->setSelectionBehavior(QAbstractItemView::SelectRows);
    mptvSDrecs->setStyleSheet(DataSets::mscszTableViewStyle);
    mpsfpSDmodel->setSourceModel(mpsiSDmodel);
    mptvSDrecs->setModel(mpsfpSDmodel);
    mptvSDrecs->setSortingEnabled(true);
    

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @SGaist said in QTableView, QSortFilterProxyModel row selection:

      QItemSelectionModel::selection

      Thank you, is there an example which demonstrates how to use these?

      Found https://stackoverflow.com/questions/18128722/convert-qsqlquerymodel-data-into-qvectors/18130955#18130955, do I need to sub-class QAbstractProxyModel ?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #4

      @SPlatten said in QTableView, QSortFilterProxyModel row selection:

      is there an example which demonstrates how to use these?

      const QItemSelection proxySelect = mptvSDrecs->selectionModel()->selection();
      QModelIndexList objIndexes = mpsfpSDmodel->mapSelectionToSource(proxySelect)->indexes();
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        What you have selected there are the indexes from the proxy model.

        From the top of my head you need to map that selection to the source model. Check QSortFilterProxyModel::mapSelectionToSource and QItemSelectionModel::selection.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        SPlattenS 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          What you have selected there are the indexes from the proxy model.

          From the top of my head you need to map that selection to the source model. Check QSortFilterProxyModel::mapSelectionToSource and QItemSelectionModel::selection.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #3

          @SGaist said in QTableView, QSortFilterProxyModel row selection:

          QItemSelectionModel::selection

          Thank you, is there an example which demonstrates how to use these?

          Found https://stackoverflow.com/questions/18128722/convert-qsqlquerymodel-data-into-qvectors/18130955#18130955, do I need to sub-class QAbstractProxyModel ?

          Kind Regards,
          Sy

          VRoninV 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @SGaist said in QTableView, QSortFilterProxyModel row selection:

            QItemSelectionModel::selection

            Thank you, is there an example which demonstrates how to use these?

            Found https://stackoverflow.com/questions/18128722/convert-qsqlquerymodel-data-into-qvectors/18130955#18130955, do I need to sub-class QAbstractProxyModel ?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #4

            @SPlatten said in QTableView, QSortFilterProxyModel row selection:

            is there an example which demonstrates how to use these?

            const QItemSelection proxySelect = mptvSDrecs->selectionModel()->selection();
            QModelIndexList objIndexes = mpsfpSDmodel->mapSelectionToSource(proxySelect)->indexes();
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3

            • Login

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