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. QSortFilterProxyModel class indexes don't mapping with source model indexes
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel class indexes don't mapping with source model indexes

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 478 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.
  • M Offline
    M Offline
    Mansur Galimov
    wrote on last edited by
    #1

    Hello, I would like to solve the problem that arises with sorting a model using the QSortFilterProxyModel class. I want the indexes of the unsorted model to be mapped to the sorted model. For example, if in order 1,2,3,4 were written into the view, and I clicked on the 4 (view), then the data was taken from array index 3 (model), and after sorting, for example, it turned out 4,2,3,1, then I want the index to be 4 (view) remained as array index 3!. The current behavior is provided below. Please help me correct this misunderstand

    This is unsorted view (as you can see number of below widget (Номер пакета: 1) is same as number in the row (1) )
    bf3b3dbc-cc32-4e64-9680-0c01640d8456-image.png

    This is sorted view (as you can see number of below widget left unchanged, but visually view changed the number of row to 6 )

    939c9ab0-1759-4c66-ba74-ee74439fb469-image.png

    Christian EhrlicherC 1 Reply Last reply
    0
    • M Mansur Galimov

      Hello, I would like to solve the problem that arises with sorting a model using the QSortFilterProxyModel class. I want the indexes of the unsorted model to be mapped to the sorted model. For example, if in order 1,2,3,4 were written into the view, and I clicked on the 4 (view), then the data was taken from array index 3 (model), and after sorting, for example, it turned out 4,2,3,1, then I want the index to be 4 (view) remained as array index 3!. The current behavior is provided below. Please help me correct this misunderstand

      This is unsorted view (as you can see number of below widget (Номер пакета: 1) is same as number in the row (1) )
      bf3b3dbc-cc32-4e64-9680-0c01640d8456-image.png

      This is sorted view (as you can see number of below widget left unchanged, but visually view changed the number of row to 6 )

      939c9ab0-1759-4c66-ba74-ee74439fb469-image.png

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see what should be wrong here. Especially because you don't provide any minimal code example to show what you're actually doing. It's the first row in the view so '1' seems to be correct.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        I don't see what should be wrong here. Especially because you don't provide any minimal code example to show what you're actually doing. It's the first row in the view so '1' seems to be correct.

        M Offline
        M Offline
        Mansur Galimov
        wrote on last edited by Mansur Galimov
        #3

        @Christian-Ehrlicher

        So, my code very simple. I have QList<FrameInfo> where i inserting my parsed packets. This action is performed in QThread in the cycle.

        int res;
            pcap_pkthdr *hdr;
            const uint8_t * data;
        
        
            static int f_num;
            f_num = 1;
            while ((res = pcap_next_ex(*handle, &hdr, &data)) >= 0)
            {
                if (QThread::currentThread()->isInterruptionRequested())
                {
                    break;
                }
        
                if (res == 0) continue;
        
                FrameInfo * frame = new FrameInfo;
                const char *m_ptr = (const char*)data;
        
                frame->alt_time = hdr->ts.tv_usec;
                frame->copy = QByteArray(m_ptr, hdr->caplen);
                frame->total_length = hdr->len;
                frame->cap_len = hdr->caplen;
                frame->f_num = f_num;
                frame->recv_time = hdr->ts.tv_sec;
                frame->p_ref = new Packet{};
                ParseFrame(frame, data);
                framesModel->append(*frame);
                f_num++;
        
                delete frame;
            }
        

        I'm using QTableView with subclassed QAbstractListModel as model to it.

        In the MainWindow() constructor i initialize view,
        initialize QSortFilterProxyModel subclass named SortingProxyModel and setting model to it. Then, i assign SortingProxyModel instance as model to view via setModel()

        sortingModel = new SortingProxyModel();
            sortingModel->setDynamicSortFilter(false);
            sortingModel->setSourceModel(captureManager->getModel());
        
        
            ui->PacketView->setModel(sortingModel);
            ui->PacketView->setSortingEnabled(true);
        
        Christian EhrlicherC 1 Reply Last reply
        0
        • M Mansur Galimov

          @Christian-Ehrlicher

          So, my code very simple. I have QList<FrameInfo> where i inserting my parsed packets. This action is performed in QThread in the cycle.

          int res;
              pcap_pkthdr *hdr;
              const uint8_t * data;
          
          
              static int f_num;
              f_num = 1;
              while ((res = pcap_next_ex(*handle, &hdr, &data)) >= 0)
              {
                  if (QThread::currentThread()->isInterruptionRequested())
                  {
                      break;
                  }
          
                  if (res == 0) continue;
          
                  FrameInfo * frame = new FrameInfo;
                  const char *m_ptr = (const char*)data;
          
                  frame->alt_time = hdr->ts.tv_usec;
                  frame->copy = QByteArray(m_ptr, hdr->caplen);
                  frame->total_length = hdr->len;
                  frame->cap_len = hdr->caplen;
                  frame->f_num = f_num;
                  frame->recv_time = hdr->ts.tv_sec;
                  frame->p_ref = new Packet{};
                  ParseFrame(frame, data);
                  framesModel->append(*frame);
                  f_num++;
          
                  delete frame;
              }
          

          I'm using QTableView with subclassed QAbstractListModel as model to it.

          In the MainWindow() constructor i initialize view,
          initialize QSortFilterProxyModel subclass named SortingProxyModel and setting model to it. Then, i assign SortingProxyModel instance as model to view via setModel()

          sortingModel = new SortingProxyModel();
              sortingModel->setDynamicSortFilter(false);
              sortingModel->setSourceModel(captureManager->getModel());
          
          
              ui->PacketView->setModel(sortingModel);
              ui->PacketView->setSortingEnabled(true);
          
          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          This code has nothing to do with the error you describe above. It is just showing that you're using a QSortFilterProxyModel and probably access gui elements from outside the main thread which is not allowed (but this has nothing to do with your question).
          Please provide a minimal, compilable example of your issue.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            This code has nothing to do with the error you describe above. It is just showing that you're using a QSortFilterProxyModel and probably access gui elements from outside the main thread which is not allowed (but this has nothing to do with your question).
            Please provide a minimal, compilable example of your issue.

            M Offline
            M Offline
            Mansur Galimov
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I want to synchronize QSortFilterProxyModel indexes with my source model indexes:
            312f0a40-d15a-493e-8861-c806cbafb27f-image.png

            Christian EhrlicherC 1 Reply Last reply
            0
            • M Mansur Galimov

              @Christian-Ehrlicher
              I want to synchronize QSortFilterProxyModel indexes with my source model indexes:
              312f0a40-d15a-493e-8861-c806cbafb27f-image.png

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Mansur-Galimov And what's the problem? That's what QSFPM::mapToSource() is for.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 2 Replies Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Mansur-Galimov And what's the problem? That's what QSFPM::mapToSource() is for.

                M Offline
                M Offline
                Mansur Galimov
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                And when to call mapToSource()?

                1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Mansur-Galimov And what's the problem? That's what QSFPM::mapToSource() is for.

                  M Offline
                  M Offline
                  Mansur Galimov
                  wrote on last edited by Mansur Galimov
                  #8

                  @Christian-Ehrlicher
                  Thank you for help! Solution was found. I just missed in my garbage code, and i did't totally understand what i was doing for a lot of time.))))

                  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