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. Order QTreeView rows when using a QSortFilterProxyModel with filter applied
Forum Updated to NodeBB v4.3 + New Features

Order QTreeView rows when using a QSortFilterProxyModel with filter applied

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 991 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.
  • N Offline
    N Offline
    nwoki
    wrote on last edited by
    #1

    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 QSortFilterProxy

    void 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 the filterModel in getSelectedElements()

        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

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

      Hi,

      Did you try invalidating the filter after the move is ended ?

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

      N 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Did you try invalidating the filter after the move is ended ?

        N Offline
        N Offline
        nwoki
        wrote on last edited by
        #3

        @SGaist said in Order QTreeView rows when using a QSortFilterProxyModel with filter applied:

        Hi,

        Did you try invalidating the filter after to move is end ?

        I need to move the row with the filter applied.

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

          Invalidating the filter means that it will be re-applied not that it will be cleared.

          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

          • Login

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