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. QAbstractItemModel beginRemoveRows() - how to deal when selection is not next to each other?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.9k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    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?

    JKSHJ VRoninV 2 Replies Last reply
    0
    • D Dariusz

      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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      D 1 Reply Last reply
      4
      • D Dariusz

        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?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        @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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • JKSHJ JKSH

          @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
          D Offline
          D Offline
          Dariusz
          wrote on last edited by JKSH
          #4

          @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!

          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