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. Sorting qtablewidget using proxymodel
Qt 6.11 is out! See what's new in the release blog

Sorting qtablewidget using proxymodel

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.0k Views 1 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.
  • A Offline
    A Offline
    an-01
    wrote on last edited by
    #1

    Hello!

    I have a qtablewidget which has a slot connected to it which is activated by the signal currentCellChanged. The slot loads information based on the cell row which is clicked.

    Now I would like sort my rows when I click on the horizontal header items. So I tried setSortingEnabled(true) after populating the qtablewidget with items. This works, but after sorting I still want the slot connected correctly, so I need to know the original row index of the clicked row.

    I read the documentation of qsortfilterproxymodel and I think this is the way to go. However I'm confused about the implementation since I did not use a table model or tableview. (Is it correct that qtablewidget is a tableview?)

    Can I set the proxymodel up as follows and how do I create the model from my qtablewidget?

    MyItemModel *sourceModel = new MyItemModel(this);
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(sourceModel);
    tableWidget->setModel(proxyModel);

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

      Hi,

      You can't replace a QTableWidget's model so you'll have to go with QTableView + QStandardItemModel (or custom model you need) + QSortFilterProxyModel

      Out of curiosity, why do you need to stay at the same row after you have your data sorted ?

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        an-01
        wrote on last edited by
        #3

        I figured out that the best solution indeed is to go with QTableView, QStandardItemModel and a QSortFilterProxyModel, because QTableWidget uses it's own internal model. Unfortunately, I already had a lot of slots etc connected to the table, so it was a little invconvenient to switch to a QTableView.

        I solved my issues by adding an extra column with the original row numbers, which I hid. Then I referred to this row indirectly. It worked, although it's not an elegant solution.

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

          I'm still curious about that fixed row when sorting

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by
            #5

            I’m still curious about that fixed row when sorting

            I see millions of reasons. Assume row has a file name you clicked in file manager sorted by extension. Then you sort by name to see all the files with the same name and different extension.

            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