QSortFilterProxyModel class indexes don't mapping with source model indexes
-
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 misunderstandThis is unsorted view (as you can see number of below widget (Номер пакета: 1) is same as number in the row (1) )
This is sorted view (as you can see number of below widget left unchanged, but visually view changed the number of row to 6 )
-
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 misunderstandThis is unsorted view (as you can see number of below widget (Номер пакета: 1) is same as number in the row (1) )
This is sorted view (as you can see number of below widget left unchanged, but visually view changed the number of row to 6 )
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.
-
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.
So, my code very simple. I have
QList<FrameInfo>
where i inserting my parsed packets. This action is performed inQThread
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 subclassedQAbstractListModel
as model to it.In the
MainWindow()
constructor i initialize view,
initializeQSortFilterProxyModel
subclass namedSortingProxyModel
and setting model to it. Then, i assignSortingProxyModel
instance as model to view viasetModel()
sortingModel = new SortingProxyModel(); sortingModel->setDynamicSortFilter(false); sortingModel->setSourceModel(captureManager->getModel()); ui->PacketView->setModel(sortingModel); ui->PacketView->setSortingEnabled(true);
-
So, my code very simple. I have
QList<FrameInfo>
where i inserting my parsed packets. This action is performed inQThread
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 subclassedQAbstractListModel
as model to it.In the
MainWindow()
constructor i initialize view,
initializeQSortFilterProxyModel
subclass namedSortingProxyModel
and setting model to it. Then, i assignSortingProxyModel
instance as model to view viasetModel()
sortingModel = new SortingProxyModel(); sortingModel->setDynamicSortFilter(false); sortingModel->setSourceModel(captureManager->getModel()); ui->PacketView->setModel(sortingModel); ui->PacketView->setSortingEnabled(true);
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. -
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.@Christian-Ehrlicher
I want to synchronizeQSortFilterProxyModel
indexes with my source model indexes:
-
@Christian-Ehrlicher
I want to synchronizeQSortFilterProxyModel
indexes with my source model indexes:
@Mansur-Galimov And what's the problem? That's what QSFPM::mapToSource() is for.
-
@Mansur-Galimov And what's the problem? That's what QSFPM::mapToSource() is for.
@Christian-Ehrlicher
And when to call mapToSource()? -
@Mansur-Galimov And what's the problem? That's what QSFPM::mapToSource() is for.
@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.))))