Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

QListWidget - Keep track of moved rows



  • I have a class that inherits from QListWidget that has method connected to QAbstractItemModel::rowsMoved signal.

    When I move the rows I need to track their new positions.

    void PlaylistPage::onRowsMoved(const QModelIndex &, int start, int end, const QModelIndex &, int row)
    {
            
    }
    

    Let's say the table has 9 rows and I move the rows 1,2,3 to position 6 and I want to know where the row in position 2 will be when rows are dropped. The rows are moved through drag and drop.



  • @hbatalha
    So 2 will end up being new 5?



  • @JonB it will be 4.



  • @hbatalha
    Upon reflection you are quite right! I was mentally placing them after 6, but of course they get inserted at 6 to come before:

    4
    5
    1
    2
    3
    6
    ...
    


  • @JonB So I came up with a solution. I use a variable to store the difference between the position of the row I want to keep track and the last row position out of the selected rows.

    //mRowsDifference - holds the position of the row I want to keep track of
    
    void PlaylistPage::onRowsMoved(const QModelIndex &, int start, int end, const QModelIndex &, int row)
    {    
         mCurrentPlayingIndex = this->selectedIndexes().last().row() - mRowsDifference;  // after the last row is moved mCurrentPlayingIndex will be holding the new position
    }
    
    void PlaylistPage::dragMoveEvent(QDragMoveEvent *event)
    {
         mRowsDifference = this->selectedIndexes().last().row() - mCurrentPlayingIndex;
            
         QListWidget::dragMoveEvent(event);
    }
    

    I will be making more tests but until it seems to be working


Log in to reply