Install event filter for drop event on the main window

1,106 views
Skip to first unread message

johan Borgström

unread,
Feb 3, 2017, 12:06:31 PM2/3/17
to Python Programming for Autodesk Maya
Hi,

I am trying to install an event filter on the main maya window to process a drop event. In the snippet below I can get the key press event, but the DragEnter and Drop event never gets called. Instead a function called "performFileDropAction" gets called.

How would I do this?

Thanks,
/ J

from PySide2 import QtGui, QtCore, QtWidgets
from maya import OpenMayaUI as omui
from shiboken2 import wrapInstance


class MyEventFilter(QtCore.QObject):
    
    def eventFilter(self, obj, event):
                     
        if event.type() is event.KeyPress:
            print("Ate key press", event.key())
            event.accept()
            return True
            
        elif event.type() is event.DragEnter:
            print('DragEnter')
            event.accept()
                 
        elif event.type() is event.Drop:
            print('Drop')
            event.accept()
        
        else:
            # standard event processing
            return QtCore.QObject.eventFilter(self, obj, event)
                               
main_win = wrapInstance(long(omui.MQtUtil.mainWindow()), QtWidgets.QWidget) 
my_filter = MyEventFilter()
main_win.installEventFilter(my_filter)
#main_win.removeEventFilter(my_filter)


johan Borgström

unread,
Feb 3, 2017, 5:18:03 PM2/3/17
to Python Programming for Autodesk Maya
I am using Maya 2017.

/J

Justin Israel

unread,
Feb 3, 2017, 5:18:05 PM2/3/17
to Python Programming for Autodesk Maya
If you search the forum you will see previous questions about adding a drag and drop handler to Maya. 


This one in particular shows an approach that works in 2015 for sure, but possibly not anymore in 2017 under PySide2. But it may be because of changes to Maya and which widgets are handling events. If in the past, the viewport never directly handled the drops, then it would make sense to be able to catch them on the main window. But if Maya has changed to where the event is handled on the viewport or some child widget of the main window, then you won't be able to catch it from the main window. It would have to be installed on the viewport widget. 

Events work by first going through the QApplication, and then getting sent directly to the target widget for the event. If the target widget decides not to handle it, then it bubbles up to parents, which is why you can catch things on the main window like a viewport drag and drop I'm Maya 2015

Here is the qt 4.8 reference about how the event system works:

Justin 


--
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/180b8d68-8f17-4dd2-a96e-068027de2e77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

johan Borgström

unread,
Feb 3, 2017, 6:50:56 PM2/3/17
to Python Programming for Autodesk Maya
Thanks for your answer Justin,

I tried to install the event filter directly on the perspective view panel using snippet below. Got the same result, key press events captured but not the drop. I guess that means that the event is processed "higher up"
I will continue to look for a solution, but if anyone have any ideas that would be great!

Best,
Johan

ptr = MQtUtil.findControl('modelPanel4')
model_panel_4 = wrapInstance(long(ptr), QtWidgets.QWidget)

my_filter = MyEventFilter()
model_panel_4.installEventFilter(my_filter)
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Feb 3, 2017, 7:39:10 PM2/3/17
to Python Programming for Autodesk Maya


On Sat, Feb 4, 2017, 12:50 PM johan Borgström <jo...@petfactory.se> wrote:
Thanks for your answer Justin,

I tried to install the event filter directly on the perspective view panel using snippet below. Got the same result, key press events captured but not the drop. I guess that means that the event is processed "higher up"

It could be higher up or lower down because whatever widget or filter receiving the drop is handling it and not bubbling it up 


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/a8a12ecb-4104-4454-a0ce-3d73e42b0da9%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages