Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    7
    2534
    Loading More Posts
    • 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
      Moschops last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • M
          Moschops last edited by

          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 Reply Quote 0
          • M
            Moschops last edited by

            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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 0
              • jerome_isAviable
                jerome_isAviable last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • First post
                    Last post