QTreeView & Model help. Drag & Drop inside both view and model

178 views
Skip to first unread message

Benjam901

unread,
May 26, 2022, 11:54:03 AM5/26/22
to Python Programming for Autodesk Maya
Hello all,

I am facing a new issue with my drag and drop features inside my treeview, so as it stands you can move tree items to new parents by dragging and dropping which works great.

You drag a tree item (which represents a file on your system) to a new directory and BAM, it moves it in the view and on the file system, life is good and the user is happy.


...however, and here is the tricky part. I need to allow for users to drag a directory from the windows explorer or Mac finder into the view and drop it into the view (not on a model item).

I can do this in the view by setting acceptDrops to true and ovverriding dragEnterEvent and dropEvent.

When I reimplement the dragEnterEvent and dropEvent in the view itself, the drag and drop functionality within the model no longer works... files are not moved, nothing happens other than the print statements I have inside the dropEvent in the view class.

Is this normal behaviour for the view to override the drop code inside my model? Or should I let the view handle the drop? If the latter, then I would need to find the index of the item dropped to from the view.

Here is the shortnened code for my tree model:

If anyone can help me with this I would be very grateful :)

Peace!

Ben

Justin Israel

unread,
May 26, 2022, 3:14:59 PM5/26/22
to python_in...@googlegroups.com


On Fri, 27 May 2022, 3:54 am Benjam901, <benandr...@gmail.com> wrote:


When I reimplement the dragEnterEvent and dropEvent in the view itself, the drag and drop functionality within the model no longer works... files are not moved, nothing happens other than the print statements I have inside the dropEvent in the view class.

Is this normal behaviour for the view to override the drop code inside my model? Or should I let the view handle the drop? If the latter, then I would need to find the index of the item dropped to from the view.

Here is the shortnened code for my tree model:

Hey Ben. It's because your overrides for the event handlers are always calling accept() on the event. You need to check the mimetype and if you decide its not something you are going to handle then don't call accept so that the event can propagate. But you should probably also call the super() event handler as well if you want the original view handler code to be able to try default drop behavior. 




If anyone can help me with this I would be very grateful :)

Peace!

Ben

--
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/59cc4287-d876-4774-9116-14a9cc2185bcn%40googlegroups.com.

Benjam901

unread,
May 26, 2022, 3:24:12 PM5/26/22
to Python Programming for Autodesk Maya
I was wondering if that might be the way to go also, but if both instances of the drop event require that a file url be present then both would need to call accept() right?

Do you have any examples of this behaviour you could point me toward? Also, I am not 100% clear on the super() event handler

// Ben

Benjam901

unread,
May 26, 2022, 4:36:44 PM5/26/22
to Python Programming for Autodesk Maya
Ok, after you pointed me towards super I looked into it and stumbled across all the right solutions to do this.

Inside my treemodel I am adding an additional custom mime data type and checking for that inside the view. If its custom I call super and if not I call the normal event accept and run the other code I need to drop in a directory from the system :)

Justin Israel

unread,
May 26, 2022, 4:46:23 PM5/26/22
to python_in...@googlegroups.com
On Fri, May 27, 2022 at 8:36 AM Benjam901 <benandr...@gmail.com> wrote:
Ok, after you pointed me towards super I looked into it and stumbled across all the right solutions to do this.

Inside my treemodel I am adding an additional custom mime data type and checking for that inside the view. If its custom I call super and if not I call the normal event accept and run the other code I need to drop in a directory from the system :)

Ya that's right. Calling the super() dropEvent method allows the original superclass logic to run, if you haven't manually handled the drop logic yourself. 
I was looking for example links to share when I saw you just figured it out. So that saves me time :-)


On Thursday, 26 May 2022 at 21:24:12 UTC+2 Benjam901 wrote:
I was wondering if that might be the way to go also, but if both instances of the drop event require that a file url be present then both would need to call accept() right?

Do you have any examples of this behaviour you could point me toward? Also, I am not 100% clear on the super() event handler

// Ben

On Thursday, 26 May 2022 at 21:14:59 UTC+2 justin...@gmail.com wrote:


On Fri, 27 May 2022, 3:54 am Benjam901, <benandr...@gmail.com> wrote:


When I reimplement the dragEnterEvent and dropEvent in the view itself, the drag and drop functionality within the model no longer works... files are not moved, nothing happens other than the print statements I have inside the dropEvent in the view class.

Is this normal behaviour for the view to override the drop code inside my model? Or should I let the view handle the drop? If the latter, then I would need to find the index of the item dropped to from the view.

Here is the shortnened code for my tree model:

Hey Ben. It's because your overrides for the event handlers are always calling accept() on the event. You need to check the mimetype and if you decide its not something you are going to handle then don't call accept so that the event can propagate. But you should probably also call the super() event handler as well if you want the original view handler code to be able to try default drop behavior. 




If anyone can help me with this I would be very grateful :)

Peace!

Ben

--
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/59cc4287-d876-4774-9116-14a9cc2185bcn%40googlegroups.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.
Reply all
Reply to author
Forward
0 new messages