About calling the super() function

59 views
Skip to first unread message

illunara

unread,
Feb 28, 2013, 12:18:32 PM2/28/13
to python_in...@googlegroups.com
Hi everybody
I'm using QtDesign to create my own UI and convert it to python version. So after subclass the UI python file, i had written some function to implement mouseEvent for QGraphicsView. Just one small question. How can i call the super() function for the QGraphicsView?

class RigModuleUi(QtGui.QMainWindow,Ui_RiggingModuleUI):
def __init__(self,parent = None):
super(RigModuleUi,self).__init__(parent = parent)
self.GraphicsView.mousePressEvent = self.qView_mousePressEvent

def qView_mousePressEvent(self,event):
if event.button() == QtCore.Qt.LeftButton:
super(RigModuleUi,self).mousePressEvent(event)

Look like the super(RigModuleUi,self).mousePressEvent(event)will return the mouseEvent for QMainWindow, not QGraphicsView. So all other option for mouse like rubberBand will lost.

I try this too, but no go at all.

super(QtGui.QGraphicsView,self.GraphicsView).mouseMoveEvent(event)

Thanks

Justin Israel

unread,
Feb 28, 2013, 2:27:37 PM2/28/13
to python_in...@googlegroups.com
I first started out by doing the same thing, and replacing the bound method with another one. Nowadays I find it a bit messy and prefer the event filter approach if I am not subclassing the target object.

Here are two example of the way you are trying to do it, followed by the event filter approach...

###
## Example 1 - just call the class directly
class RigModuleUi(QtGui.QMainWindow,Ui_RiggingModuleUI):
    def __init__(self,parent = None):
        super(RigModuleUi,self).__init__(parent = parent)
        self.setupUi(self)

        self.GraphicsView.mousePressEvent = self.qView_mousePressEvent

    def qView_mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            view = self.GraphicsView
            # directly call the class and pass the instance
            QtGui.QGraphicsView.mousePressEvent(view, event)

## Example 2 - save the original bound method
    def __init__(self,parent = None):
        ...
        view = self.GraphicsView
        view._mousePressEvent = view.mousePressEvent
        view.mousePressEvent = self.qView_mousePressEvent

    def qView_mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            view = self.GraphicsView
            view._mousePressEvent(event)

## Example 3 - Use an event filter to manage composed objects and route their actions
class RigModuleUi(QtGui.QMainWindow,Ui_RiggingModuleUI):
    def __init__(self,parent = None):
        super(RigModuleUi,self).__init__(parent = parent)
        self.setupUi(self)

        self.GraphicsView.mousePressEvent = self.qView_mousePressEvent

    def eventFilter(self, obj, event):
        if obj is self.GraphicsView:
            # If its not a left button then we wanted to do
            # some custom handling, so we call our own handler
            # and return True which means that the event is filtered
            # and the original object will not ever see this event.
            if event.button() != QtCore.Qt.LeftButton:
                self.qView_mousePressEvent(event)
                return True

        return super(RigModuleUi, self).eventFilter(obj, event)

    def qView_mousePressEvent(self, event):
        event.accept()
        view = self.GraphicsView
        # do something custom with the view and event

####
I like the event filter approach because I don't have to replace the bound methods at all. I simply catch the events before the objects ever see them. You can route multiple objects in that event filter and send them to other methods.

-- 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 post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Tuan Nguyen

unread,
Feb 28, 2013, 3:01:48 PM2/28/13
to python_in...@googlegroups.com
Maybe i had done something funny when reimplement mouseEvent, so the result after calling class out still wrong. I will check the script again, also thank for show me how to use eventFilter. I try to go with it at first place, but since time is short, i chose replace the bounding method instead :D

Thank for your reply, Justin

Tuan Nguyen

unread,
Apr 19, 2013, 2:36:54 AM4/19/13
to python_in...@googlegroups.com
Sorry to dig this up, at that time, i were using the first way you told me, it works fine so i think i will left it like that for a while and come back after had finished other part. But somehow, i think the eventFilter is not working at all,


self.GraphicsView.mousePressEvent = self.qView_mousePressEvent

def eventFilter(self, obj, event):
        if obj is self.GraphicsView:
            if event.button() != QtCore.Qt.LeftButton:
                self.qView_mousePressEvent(event)
                return True

in the mousePressEvent i had a case when event.button() == QtCore.Qt.LeftButton, it will return the QGraphicsView,mousePressEvent. And if i remove it, the leftMousePressEvent is gone too

I also try install the eventFilter on GraphicsView but no luck :(

http://pastebin.com/SbJfsHTG

Justin Israel

unread,
Apr 19, 2013, 2:52:16 AM4/19/13
to python_in...@googlegroups.com
Did you confirm that the eventFilter() is being called? Put a print statement right after:
    if obj is self.GraphicsView

Also remember to do   event.accept()  if you are handling the event yourself so that it doesn't bubble up.



Tuan Nguyen

unread,
Apr 19, 2013, 3:35:25 AM4/19/13
to python_in...@googlegroups.com
yes, i try it too, the print statement. But nothing is print out, so i guess its not working?



--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/rUzeXlRweDI/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.

Tuan Nguyen

unread,
Apr 19, 2013, 3:53:24 AM4/19/13
to python_in...@googlegroups.com
oh and i'm sorry. The script in pastebin  missing the event.accept() line, but still, the eventFilter is not working :(

Justin Israel

unread,
Apr 20, 2013, 3:19:24 AM4/20/13
to python_in...@googlegroups.com
If you are installing the event filter on an object and you have defined the eventFilter() method, then I am not sure why it would not get called. Please post a small, concise, fully working example of the problem, so we can test it for you.

Tuan Nguyen

unread,
Apr 20, 2013, 1:21:39 PM4/20/13
to python_in...@googlegroups.com
Ah, about that. I just got it work, i really really sorry, that was my mistake :(
However can i ask another question? Can i use the eventFilter on QGraphicsItem? they are not widget .... and i have to make an except on it when call the QGraphicsView's keyPressEvent :(

Justin Israel

unread,
Apr 20, 2013, 4:51:30 PM4/20/13
to python_in...@googlegroups.com

Glad you got the problem resolved.

For the other question, don't forget to check the docs!
http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#events

Tuan Nguyen

unread,
Apr 21, 2013, 9:39:11 AM4/21/13
to python_in...@googlegroups.com
Thanks
But those events drive my crazy :( i don't know why it keep jump up to Maya's keyEvent, even i put an eventFilter on.  When i reimplement QGraphicsView's keyPressEvent, i have to skip calling events super class, else Maya's hotkey still working as well.

Justin Israel

unread,
Apr 21, 2013, 2:57:50 PM4/21/13
to python_in...@googlegroups.com

If you call the super class method then of course it will pass the event up.
The way to prevent it from bubbling up is to do event.accept() and return True from the eventFilter.

Tuan Nguyen

unread,
Apr 21, 2013, 11:34:50 PM4/21/13
to python_in...@googlegroups.com
Ah, everything work as expect :D thank a ton, David XD

Tuan Nguyen

unread,
Apr 21, 2013, 11:35:26 PM4/21/13
to python_in...@googlegroups.com
Ah, everything work as expect, thank a ton Justin :3

Justin Israel

unread,
Apr 22, 2013, 1:02:16 AM4/22/13
to python_in...@googlegroups.com

No problem, David :-)

Tuan Nguyen

unread,
Apr 22, 2013, 2:48:33 AM4/22/13
to python_in...@googlegroups.com
eh, sorry about that. I though i delete the last one, and send a new one :D

Justin Israel

unread,
Apr 22, 2013, 4:00:35 AM4/22/13
to python_in...@googlegroups.com

Haha I am just teasing. You can't really delete messages off the message group ;-)

Tuan Nguyen

unread,
Apr 22, 2013, 4:22:12 AM4/22/13
to python_in...@googlegroups.com
I feel bad about it, just type in on a whim :( .
And, there are 2 last thing about eventFilter i'm confusing about, can you confirm it for me? :D

def eventFilter(self,obj,event):
.....if obj is item:

This is quite a pain since eventFilter can only chose one specify item to filter on. I try change the method, like in, isinstance but no luck :(

The second thing is, sceneEventFilterEvent not working on itself, and its child either?

:3

Justin Israel

unread,
Apr 22, 2013, 5:33:39 AM4/22/13
to python_in...@googlegroups.com
The eventFilter does not just operate on a single object. You can set something to be an event filter to 100 objects if you want. But you will usually want to check what the object is to ensure you handle it in the appropriate way:

# check for a specific instance
if obj is foo:

# check for a number of instance types:
if isinstance(obj, (str, int, float, list, tuple, dict)):

# duck typing
if hasattr(obj, "someAttribute"):

You can set any QObject subclass to be an event filter of any other QObject
Reply all
Reply to author
Forward
0 new messages