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. Drag and drop in QTreeView - where do I get the dragged data?

Drag and drop in QTreeView - where do I get the dragged data?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.2k Views
  • 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.
  • M Offline
    M Offline
    Moschops
    wrote on last edited by
    #1

    I've got a QTreeView. I set some values to enable drag and drop within the QTreeView (so the user can move things around).
    @
    ui->generatedDataTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->generatedDataTreeView->setDragDropMode(QAbstractItemView::InternalMove);
    ui->generatedDataTreeView->setDragEnabled(true);
    ui->generatedDataTreeView->setAcceptDrops(true);
    ui->generatedDataTreeView->setDropIndicatorShown(true);@

    Now, the user can drag an item. When they release it, insert rows is called on the model. The call contains enough information for me to work out exactly where I need to insert the item. But I don't have the actual item that has been dragged. How do I know which item has been dragged so that I can copy that item's data and insert it as a new row?

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

      Hi,

      Since you are doing an internal move you don't need to copy any data or are you trying to do something different ?

      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
      0
      • M Offline
        M Offline
        Moschops
        wrote on last edited by
        #3

        No, I'm just trying to do an internal move.

        Maybe I've missed the point of this entirely. I have some items in the treeview, and I click one and drag it somewhere else and let go of the mouse button, and the next function that I see being called is my model's override of "insertRows".

        What I need to do is, in my model, identify the item that was dragged, remove it from its parent's list of children, and put it on some new item's list of children (i.e. the suitable new parent where the user dropped it)

        But I don't know what item the user dragged. The insertRows function is being called, but how do I know what the dragged item was?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Moschops
          wrote on last edited by
          #4

          I can't make head nor tail of the documentation on this one. There seem to be so many different options being described by drag and drop. I've just got the simplest (?) case; dragging something in a QTreeView, to somewhere else in that QTreeView.

          This page:
          http://qt-project.org/doc/qt-4.8/model-view-programming.html#using-drag-and-drop-with-item-views

          says:

          bq. The standard views automatically support internal drag and drop, where items are moved around to change the order in which they are displayed. bq.

          but I can't see how to "turn on" that automatic support.

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

            You don't have to turn it on, it's in automatically.

            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
            0
            • jerome_isAviableJ Offline
              jerome_isAviableJ Offline
              jerome_isAviable
              wrote on last edited by
              #6

              if you want to play with drag and drop, you need to implement a model class and add member function need to do it... for flags and also for handle your datas type droped inside... it is not really simple (for my level).
              if you need an exemple of a code model (QStandardItemModel) for a qtreeview that use drag and drop, i can pastebin it for you... but again, it is not really simple to use.

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

                InternalMove is not as complicated as the other modes

                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
                0

                • Login

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