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. Model/View - How to deal with insert/remove simulatinously.

Model/View - How to deal with insert/remove simulatinously.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 574 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,

    I have a view that shows a QTreeModel.
    There is one action that makes something like this

    • parent
      • child
      • child
      • child

    is converted to:

    • parent
      • child
      • child
    • parent
      • child
      • child

    The conversion happens inside a lib, so i cannot split it into two actions.
    Now my question is how to call beginInsertRows() / endInsertRows() beginRemoveRows() / endRemoveRows() here, since it happens both at once.

    Can i cascade them like

    beginRemoveRows()
    beginInsertRows()
    //do the action
    endInsertRows()
    endRemoveRows()
    

    or what would be the right way to deal with this?

    kshegunovK 1 Reply Last reply
    0
    • gde23G gde23

      Hello,

      I have a view that shows a QTreeModel.
      There is one action that makes something like this

      • parent
        • child
        • child
        • child

      is converted to:

      • parent
        • child
        • child
      • parent
        • child
        • child

      The conversion happens inside a lib, so i cannot split it into two actions.
      Now my question is how to call beginInsertRows() / endInsertRows() beginRemoveRows() / endRemoveRows() here, since it happens both at once.

      Can i cascade them like

      beginRemoveRows()
      beginInsertRows()
      //do the action
      endInsertRows()
      endRemoveRows()
      

      or what would be the right way to deal with this?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      It's sort of an abuse of the API, but you could delay the notifications for after the change has occurred (assuming it's happening synchronously):

      // Do the action without a care in the world
      
      beginRemoveRows()
      endRemoveRows()
      
      beginInsertRows()
      endInsertRows()
      

      As for your question, I don't believe it's possible to nest these calls the way you want to ...

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @kshegunov said in Model/View - How to deal with insert/remove simulatinously.:

        but you could delay the notifications for after the change has occurred (assuming it's happening synchronously)

        This will not work as begin() accesses the model and assumes the old structure.

        The only way is to fix the library. When it changes the model it also must emit the signals.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        kshegunovK 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @kshegunov said in Model/View - How to deal with insert/remove simulatinously.:

          but you could delay the notifications for after the change has occurred (assuming it's happening synchronously)

          This will not work as begin() accesses the model and assumes the old structure.

          The only way is to fix the library. When it changes the model it also must emit the signals.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @Christian-Ehrlicher said in Model/View - How to deal with insert/remove simulatinously.:

          This will not work as begin() accesses the model and assumes the old structure.

          Ah, the second begin** you mean ... yes, indeed, that's that may be a problem.
          Well there's always beginResetModel and endResetModel if nothing else helps ...

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by
            #5

            Thanks for the quick replies, The problem is, that the library does not have anything to do with Qt at all, so I cannot emit the signals there. (I have made a wrapper for that wraps the model in the lib inside a QAbstractItemModel).

            Also "fixing" the lib does not really make sense, since the action that is performed is something like splitting an element into two parts which could maybe somehow be seperated into two actions but is not really what you would expect in the interface.

            Resetting the model always has the disadvantage that the whole tree expansion state will collapse, which is kind of annoying.

            I'll think a bit further about it and probably need to come up with some kind of dirty hack then.

            kshegunovK 1 Reply Last reply
            0
            • gde23G gde23

              Thanks for the quick replies, The problem is, that the library does not have anything to do with Qt at all, so I cannot emit the signals there. (I have made a wrapper for that wraps the model in the lib inside a QAbstractItemModel).

              Also "fixing" the lib does not really make sense, since the action that is performed is something like splitting an element into two parts which could maybe somehow be seperated into two actions but is not really what you would expect in the interface.

              Resetting the model always has the disadvantage that the whole tree expansion state will collapse, which is kind of annoying.

              I'll think a bit further about it and probably need to come up with some kind of dirty hack then.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              I took a peek at the current implementation and from the quick glance it does seem your original idea may work. So I suggest you try it out. Here's a relevant piece of Qt's code:
              https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN18QAbstractItemModel15beginRemoveRowsERK11QModelIndexii

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • gde23G Offline
                gde23G Offline
                gde23
                wrote on last edited by
                #7

                @kshegunov
                Tried it, but unfortunately it crashes.
                Thanks anyhow. I will go with resetting the model and maybe look for a solution some other time.
                Its really not that much important.

                1 Reply Last reply
                1

                • Login

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