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. How to Accept/Reject an InternalMove in a QTreeView

How to Accept/Reject an InternalMove in a QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 527 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.
  • S Offline
    S Offline
    sdbrown
    wrote on last edited by sdbrown
    #1

    I have a QTreeView that contains N parent items and each parent has one or more children. I want to support internal moves (reordering) of children within each parent but not between parents. I could add a unique mime-type to each class of children and do a full drag/drop setup that would allow me to accept/reject a drop based on that mime-type. If I have to do a full drag/drop setup, what exactly does a "move" look like in terms of events and can I stop that chain of events once it has begun with something like an event filter, or would I have a lot of state to restore?

    I feel like there should be an easier way that includes using QAbstractItemView::InternalMove, but I just can't seem to see it. All I need to check is if the moving child's parent model index agrees with the accepting parent model index.

    Many thanks for any suggestions.

    S 1 Reply Last reply
    0
    • S sdbrown

      I have a QTreeView that contains N parent items and each parent has one or more children. I want to support internal moves (reordering) of children within each parent but not between parents. I could add a unique mime-type to each class of children and do a full drag/drop setup that would allow me to accept/reject a drop based on that mime-type. If I have to do a full drag/drop setup, what exactly does a "move" look like in terms of events and can I stop that chain of events once it has begun with something like an event filter, or would I have a lot of state to restore?

      I feel like there should be an easier way that includes using QAbstractItemView::InternalMove, but I just can't seem to see it. All I need to check is if the moving child's parent model index agrees with the accepting parent model index.

      Many thanks for any suggestions.

      S Offline
      S Offline
      sdbrown
      wrote on last edited by
      #2

      I should clarify that I'm backending with just QStandardItemModel at this point.

      One of my ideas is to derive a model from QStandardItemModel and then reimplement moveRows(), which I assume is called on an "internal move" type operation. In there I can evaluate the source and destination of the move and if it is acceptable, call the parent class implementation. If not, return false.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sdbrown
        wrote on last edited by sdbrown
        #3

        It doesn't look like moveRows() is ever called. Upon some further reading and then some code experimentation it appears an "internal move" is implemented as an insertRows() and removeRows() combination, which are both virtual. So I can grab the item being moved in an override insertRows(), evaluate the validity during removeRows() and then decide if the remove and insert should happen. If so, I will just call the QStandardItemModel functions to perform the operation.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sdbrown
          wrote on last edited by
          #4

          Yeah, the code is ballooning quickly to use this route. Everything I have tried as made the total code for this model/view 5x bigger and far more likely to contain bugs.

          Here's hoping that someone has an elegant solution.

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

            Hi and welcome to devnet,

            Since it's GUI related, you should implement a custom tree view. There you have the logic that will check whether the index under the mouse is a valid target.

            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
            1

            • Login

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