PySide2 QTreeWidget DropEvent not firing

162 views
Skip to first unread message

Dilen Shah

unread,
Feb 12, 2020, 8:17:17 PM2/12/20
to Python Programming for Autodesk Maya
Hey guys,

I have been trying to get dropevent working in Pyside2. So my UI is built in QtDesigner with QMainWindow as main class and have QTreeWidget in that same class.

Is there a way i can fire up QTreeWidget dropEvent when the items are moved and the order has been changed.


If anyone can figure this out for me, would be greatly appreciated.

Thanks,
Dilen.

Justin Israel

unread,
Feb 13, 2020, 5:35:51 PM2/13/20
to python_in...@googlegroups.com
I'm not sure I fully understand the intention, from reading your code.
Is that a typo when you use "setDragDropMode" on the QMainWindow? Was
it meant to be set on the tree view?

What I am seeing is that you set the drop mode on the tree to
InternalMove, and you have drag/drop event methods defined on the main
window. Which drag/drop behaviour are you hoping to implement? Setting
InternalMove on the tree view means that it only allows moving the
tree view items around within itself, and no drag/drag from outside
the view. Then you have drag/drop on the main window, so are you
trying to accept external drops?
> --
> You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2ebb4f44-1d76-422f-af20-aeaf63793013%40googlegroups.com.

Dilen Shah

unread,
Feb 13, 2020, 11:44:49 PM2/13/20
to Python Programming for Autodesk Maya
Hey Justin,

Sorry about the confusion, looks like i pasted the wrong code.
This is what i wanted to paste: https://pastebin.com/8vxb3h9n

So my only issue currently is that when i move/re-order QTreeWidget i am not getting any prints from the dropEvent or dragEnterEvent.
Can't seem to figure it out why it's not firing. Currently i am only looking to make the re-order event work.

Hope this makes sense.

Thanks.

Russell Pearsall

unread,
Feb 14, 2020, 4:40:51 PM2/14/20
to Python Programming for Autodesk Maya
I am not in front of a computer right now, but I thought the itemview's viewport() was what handled drag/drop events. Have you tried installing an event filter on the view that handles drop events for the viewport?

Justin Israel

unread,
Feb 14, 2020, 9:02:44 PM2/14/20
to python_in...@googlegroups.com


On Sat, Feb 15, 2020, 10:40 AM Russell Pearsall <robo...@gmail.com> wrote:
I am not in front of a computer right now, but I thought the itemview's viewport() was what handled drag/drop events. Have you tried installing an event filter on the view that handles drop events for the viewport?

To expand on this, the original code isn't working as expected because it is just implementing drag and drop methods on the main window. There isn't a connection between the main window drag drop and the child tree view, unless the tree view decides to not handle the event and it manages to bubble up to the main window as a parent. 

So using installEventFilter() on the tree viewport and catching them in the eventFilter method on the main window is one approach. 
Or you can create a QTreeWidget subclass in your code and reimplement the drag drop handling in there so it is isolated from the main window logic. Up to you. 






On Thursday, February 13, 2020 at 8:44:49 PM UTC-8, Dilen Shah wrote:
Hey Justin,

Sorry about the confusion, looks like i pasted the wrong code.
This is what i wanted to paste: https://pastebin.com/8vxb3h9n

So my only issue currently is that when i move/re-order QTreeWidget i am not getting any prints from the dropEvent or dragEnterEvent.
Can't seem to figure it out why it's not firing. Currently i am only looking to make the re-order event work.

Hope this makes sense.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Dilen Shah

unread,
Feb 16, 2020, 10:48:20 PM2/16/20
to Python Programming for Autodesk Maya
Hey Russell and Justin,

Thank you for your replies. I will test the installEventFilter() and see how i can implement it and if i cant figure it out might put out my code again.

Thanks,
Dilen.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Dilen Shah

unread,
Feb 19, 2020, 10:49:29 PM2/19/20
to Python Programming for Autodesk Maya
Hey guys,

So i have managed to get it working using installEventFilter.

So now when i change the order i get the event and i can run some information in it.
Just out of curiosity is there a simpler way using installEventFilter to get the original row index of item before dragEnter and index after drop?

Thanks,
Dilen.

Justin Israel

unread,
Feb 19, 2020, 11:47:46 PM2/19/20
to python_in...@googlegroups.com
On Thu, Feb 20, 2020 at 4:49 PM Dilen Shah <dilen...@gmail.com> wrote:
Hey guys,

So i have managed to get it working using installEventFilter.

So now when i change the order i get the event and i can run some information in it.
Just out of curiosity is there a simpler way using installEventFilter to get the original row index of item before dragEnter and index after drop?

I've put this example together for you as one way of doing it:
https://gist.github.com/justinfx/b7cedc8cca240efa3412d98914fda265 

Using a QTreeWidget means you are dealing in terms of items as opposed to indexes. So you have to convert between them to get the row number.  
There are different ways of looking up what is being dragged. And in order to get the final drop position of the child you have to push through the drop event and then ask afterwards.
 

Thanks,
Dilen.

On Monday, February 17, 2020 at 2:18:20 PM UTC+10:30, Dilen Shah wrote:
Hey Russell and Justin,

Thank you for your replies. I will test the installEventFilter() and see how i can implement it and if i cant figure it out might put out my code again.

Thanks,
Dilen.

On Saturday, February 15, 2020 at 12:32:44 PM UTC+10:30, Justin Israel wrote:


On Sat, Feb 15, 2020, 10:40 AM Russell Pearsall <robo...@gmail.com> wrote:
I am not in front of a computer right now, but I thought the itemview's viewport() was what handled drag/drop events. Have you tried installing an event filter on the view that handles drop events for the viewport?

To expand on this, the original code isn't working as expected because it is just implementing drag and drop methods on the main window. There isn't a connection between the main window drag drop and the child tree view, unless the tree view decides to not handle the event and it manages to bubble up to the main window as a parent. 

So using installEventFilter() on the tree viewport and catching them in the eventFilter method on the main window is one approach. 
Or you can create a QTreeWidget subclass in your code and reimplement the drag drop handling in there so it is isolated from the main window logic. Up to you. 






On Thursday, February 13, 2020 at 8:44:49 PM UTC-8, Dilen Shah wrote:
Hey Justin,

Sorry about the confusion, looks like i pasted the wrong code.
This is what i wanted to paste: https://pastebin.com/8vxb3h9n

So my only issue currently is that when i move/re-order QTreeWidget i am not getting any prints from the dropEvent or dragEnterEvent.
Can't seem to figure it out why it's not firing. Currently i am only looking to make the re-order event work.

Hope this makes sense.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d1461bb6-46d1-4f44-b234-0c2d620b2081%40googlegroups.com.

Dilen Shah

unread,
Mar 2, 2020, 10:59:50 PM3/2/20
to Python Programming for Autodesk Maya
Hey Justin,

Thank you for the example and sorry for the late reply. I managed to make the installEventFilter work to how i wanted it to be.
I was able to query the event i wanted as well by printing it everytime i press anything in the QtreeWidget.

Mainly needed QEvent.UpdateLater, with this i was able to re-arrange my QtreeWidget.

Thank you all again.

Dilen.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages