Order QTreeView rows when using a QSortFilterProxyModel with filter applied
-
Let's say we have a model with a list of geometrical figures. A data filed is
number of sides
. Let's say we filter the model and show only rows with 3 number of sides.Now, I want to be able to move these rows and to do so i do:
filterModel
is the QSortFilterProxyvoid WorkSheetTableModel::moveElement(int elementRowIndex, int newElementRowIndex) { /** snip **/ // remove the element from its old position beginMoveRows(QModelIndex() , elementRowIndex , elementRowIndex , QModelIndex() , newElementRowIndex); d->workSheetRowObjects.swap(elementRowIndex, newElementRowIndex); endMoveRows(); } QList<int> WorkSheetTab::getSelectedElements() { QList<int> selectedIndexes; for (const QModelIndex &index : d->ui->tableView->selectionModel()->selectedRows()) { if (!selectedIndexes.contains(index.row())) { selectedIndexes.append(d->filterModel->mapToSource(index).row()); } } return selectedIndexes; } void WorkSheetTableModel::moveElementsDown(QList<int> elementIndexes) { // move the closest index to the bottom first qSort(elementIndexes.begin(), elementIndexes.end(), qGreater<int>()); for (int i = 0; i < elementIndexes.count(); ++i) { int destinationIndex = (rowCount() - (1+i)); // check we don't want to move the elements to their same position if (elementIndexes.at(i) == destinationIndex) { continue; } moveElement(elementIndexes.at(i), destinationIndex); } } void WorkSheetTab::onMoveDownButtonClicked() { d->tableModel->moveElementsDown(getSelectedElements()); }
This correctly moves the elements on the view but when i hit an index that's not in the filtered model, the row doesn't move up until i cycle through thouse elements not included in the filtered model and then it start moving again.
It's as if I'm still moving the elements on the default model and not the filtered one even though when i select the indexes i use those from thefilterModel
ingetSelectedElements()
for (const QModelIndex &index : d->ui->tableView->selectionModel()->selectedRows()) { if (!selectedIndexes.contains(index.row())) { selectedIndexes.append(d->filterModel->mapToSource(index).row()); } }
Any clues?
Hope I've managed to explain my problem
-
Hi,
Did you try invalidating the filter after the move is ended ?
-
Invalidating the filter means that it will be re-applied not that it will be cleared.