Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TreeView - Wait until the drop is complete to perform another action



  • Hello, I have been trying to fix this for more than a week, I do not sleep.

    I am using TreeView with a MyTreeModel (which inherits from QAbstractItemModel). This model is fed by a tree structure with nodes as it appears in the documentation.
    https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
    https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
    https://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views

    I have the whole subject of drag and drop between elements of the tree perfectly implemented. As the doc says well, I have reimplemented mimeData, mimeTypes and dropMimeData for it.

    Video of it:
    https://mega.nz/#!bkoTDQSI!YgVn6jIAPJ86fhexzsGySbhrPwQAK5IwlTTeb5L-sQ4

    The problem is that I want to do actions programmatically right after the drop in relation to the item that was dropped.

    I want to expand the item after the drop and I want to select it. The view on the right depends on the currentIndex.

    I have tried everything. Make the selection after:

    • rowsInserted
    • dataChanged
    • currentChanged
    • changeEvent
    • dropEvent
    • eventFilter

    Nothing works because if I try to change the selection after those methods, I break Qt inside causing the not terminated drops not to work well. I need to wait for all the signals derived from the drop to finish to execute the select and expand command.

    I am desperate, I have watched how TreeWidget behaves and has the same problem as me: when the drop is made the row selected is wrong and the node collapsed.

    What I can do?

    Thank you very much.



    1. I'm not sure why anything should break. Run your model throught the model test to make sure it's implemented 100% according to Qt's expectation
    2. adding Qt::QueuedConnection as the 5th argument of your connect ststement should ensure your code gets executed after the move is completed and control is given back to Qt's event loop


  • @VRonin You are a god. Finally I have finished the implementation thanks to your second option. I have tried (all possible combinations) and it works.
    I didn't know about Qt::QueuedConnection and the option to concatenate signals to the Qt event queue. This is going to be very useful to me.

    You have saved me and it is not the first time, I have seen your answers in other threads and they have helped me too.

    Thanks from Spain and sorry if my English is bad.


Log in to reply