Congratulations to our 2022 Qt Champions!

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

  • I've got a QTreeView. I set some values to enable drag and drop within the QTreeView (so the user can move things around).

    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?

  • Lifetime Qt Champion


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

  • 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?

  • 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:


    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.

  • Lifetime Qt Champion

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

  • 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.

  • Lifetime Qt Champion

    InternalMove is not as complicated as the other modes

Log in to reply