Search code examples
pythonpyqtdrag-and-droppyside2qtreeview

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


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.

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.


Solution

  • They have solved it for me in the Qt forums.

    I have connected custom signals by adding the parameter QtCore.Qt.QueuedConnection in connect.

    Here the solution:
    https://forum.qt.io/topic/114716/treeview-wait-until-the-drop-is-complete-to-perform-another-action/3

    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