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. Detecting sort completed on QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Detecting sort completed on QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5qtablewidgetsort
7 Posts 4 Posters 1.4k Views 3 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.
  • J Offline
    J Offline
    james b-s
    wrote on 3 Jan 2020, 19:11 last edited by
    #1

    I need to do something after a user sorts a QTableWidget. I need to be able to detect when the table has been resorted. I tried detecting a click on the header, but that occurs before the sort. I need to do something after the sort is completed.

    Someone suggested overriding QTableWidget::sortItems, but since that is not virtual, my method doesn't override anything and doesn't get called.

    Another suggestion that I found was to replace the QSortFilterProxyModel that is used by the QTableWidget, but the methods to do so are private.

    I presumably could stop using QTableWidget and use QTableView instead, but that seems like a lot of work to get something that should be fairly simple. There must be a better way.

    I also tried catching the rowsMoved signal of the default model for QTableWidget (another suggestion I found). I was unable to catch the signal. Not sure why.

    Here is the code that I used to try to catch the signal.

    QAbstractItemModel * qModel = m_qUI.calendarTableWidget->model();
      connect(qModel,   SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex, int)), this, SLOT(onCalendarRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
    

    To reiterate, I need to call a particular method of mine whenever a sort completes after someone clicks on the header of a QTableWidget.

    V 1 Reply Last reply 3 Jan 2020, 20:11
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Jan 2020, 19:19 last edited by
      #2

      Hi,

      Can you explain what you need to do after the sorting applied ?

      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
      1
      • J james b-s
        3 Jan 2020, 19:11

        I need to do something after a user sorts a QTableWidget. I need to be able to detect when the table has been resorted. I tried detecting a click on the header, but that occurs before the sort. I need to do something after the sort is completed.

        Someone suggested overriding QTableWidget::sortItems, but since that is not virtual, my method doesn't override anything and doesn't get called.

        Another suggestion that I found was to replace the QSortFilterProxyModel that is used by the QTableWidget, but the methods to do so are private.

        I presumably could stop using QTableWidget and use QTableView instead, but that seems like a lot of work to get something that should be fairly simple. There must be a better way.

        I also tried catching the rowsMoved signal of the default model for QTableWidget (another suggestion I found). I was unable to catch the signal. Not sure why.

        Here is the code that I used to try to catch the signal.

        QAbstractItemModel * qModel = m_qUI.calendarTableWidget->model();
          connect(qModel,   SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex, int)), this, SLOT(onCalendarRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
        

        To reiterate, I need to call a particular method of mine whenever a sort completes after someone clicks on the header of a QTableWidget.

        V Offline
        V Offline
        VRonin
        wrote on 3 Jan 2020, 20:11 last edited by VRonin 1 Mar 2020, 20:14
        #3

        @james-b-s said in Detecting sort completed on QTableWidget:

        I need to call a particular method of mine whenever a sort completes

        connect(tableWidget->model(),&QAbstractItemModel::layoutChanged,this,&MyClass::onCalendarRowsMoved);

        You will need to change the signature of the slot though

        "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
        4
        • J Offline
          J Offline
          james b-s
          wrote on 3 Jan 2020, 20:12 last edited by
          #4

          My code had a typo in it. It really looks like this

           QAbstractItemModel * qModel = m_qUI.calendarTableWidget->model();
            connect(qModel,   SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)), this, SLOT(onCalendarRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
          
          

          Still didn't work

          M 1 Reply Last reply 3 Jan 2020, 20:30
          0
          • J james b-s
            3 Jan 2020, 20:12

            My code had a typo in it. It really looks like this

             QAbstractItemModel * qModel = m_qUI.calendarTableWidget->model();
              connect(qModel,   SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)), this, SLOT(onCalendarRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
            
            

            Still didn't work

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 3 Jan 2020, 20:30 last edited by
            #5

            @james-b-s

            Works fine as @VRonin says

            for test i used

            connect(ui->tableWidget->model(), &QAbstractItemModel::layoutChanged,
                this, [](const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint) {
                qDebug() << "sorted";
                });
            

            and it seems to work.

            Note he uses other signal than your code.

            1 Reply Last reply
            3
            • J Offline
              J Offline
              james b-s
              wrote on 3 Jan 2020, 20:55 last edited by
              #6

              I am making related cells span together and I need to recalculate that when someone changes the sorting.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                james b-s
                wrote on 3 Jan 2020, 21:40 last edited by
                #7

                Thanks that worked

                1 Reply Last reply
                2

                1/7

                3 Jan 2020, 19:11

                • Login

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