QAbstractItemModel beginRemoveRows() - how to deal when selection is not next to each other?



  • Hey

    When I look at docs, the beginRemoveRows shows this picture alt text

    This means I use beginRemoveRows(parent, 2, 3); so you move item at position 2 until position 3, so if u had selected 2/3/4/5 you would do beginRemoveRows (parent,2,5).

    This works great if the items selected are next to each other. But how do I deal with a function that I have to move items at 2, 4, 6,7,8, 10, 15? They are not next to each other, at least not all of them. Should I then just call beginRemoveRows for each item in a loop? so do 1 item move at a time?


  • Moderators

    @Dariusz said in QAbstractItemModel beginRemoveRows() - how to deal when selection is not next to each other?:

    how do I deal with a function that I have to move items at 2, 4, 6,7,8, 10, 15? They are not next to each other, at least not all of them. Should I then just call beginRemoveRows for each item in a loop? so do 1 item move at a time?

    Yes, that's right: If the rows are not next to each other, they need to be processed separately.

    You could do it in batches if you want:

    1. Remove row 15 alone
    2. Remove row 10 alone
    3. Remove rows 6, 7, 8 at the same time
    4. Remove row 4 alone
    5. Remove row 2 alone


  • @Dariusz said in QAbstractItemModel beginRemoveRows() - how to deal when selection is not next to each other?:

    I use beginRemoveRows(parent, 2, 3); so you move item

    beginRemoveRows is for removing rows, if you want to move items you have to call beginMoveRows

    This works great if the items selected are next to each other

    The model should not handle selections, that QItemSelectionModel's job so you should call removeRows from the outside



  • @JKSH said in QAbstractItemModel beginRemoveRows() - how to deal when selection is not next to each other?:

    @Dariusz said in QAbstractItemModel beginRemoveRows() - how to deal when selection is not next to each other?:

    how do I deal with a function that I have to move items at 2, 4, 6,7,8, 10, 15? They are not next to each other, at least not all of them. Should I then just call beginRemoveRows for each item in a loop? so do 1 item move at a time?

    Yes, that's right: If the rows are not next to each other, they need to be processed separately.

    You could do it in batches if you want:

    1. Remove row 15 alone
    2. Remove row 10 alone
    3. Remove rows 6, 7, 8 at the same time
    4. Remove row 4 alone
    5. Remove row 2 alone

    I had a feeling that this would be the case, does QT offer any sorting system for doing this for me ?

    For example, if I use mimeData(const QModelIndexList &indexes){}
    the indexes are unordered, so I run

        QModelIndexList models = indexes;
        qSort(models.begin(), models.end(), qGreater<QModelIndex>()); 
    

    To sort them all up. Now once they are sorted, is there a way to qGroup() or something like that that would return QVector/list<QVector/list<QModelIndex>> ?

    Or I have to write the sorting algorithm myself?

    Thanks in advance!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.