Delete Event after run once

82 views
Skip to first unread message

Arvid Schneider

unread,
Mar 31, 2014, 6:43:59 AM3/31/14
to python_in...@googlegroups.com
Hey Group,

I have a command which returns click and release coordinates of the mouse pointer. I should only run once though, and I cant figure out how to do that.
Can someone give me an insight please. 
The wrapinstance method is a custom one (it just checks if PySide or PyQt4 ist running)

class Region(QtCore.QObject):
    """ Class contains methods to provide information for a specific selected region """

    def eventFilter(self, obj, event):

        if event.type() == QtCore.QEvent.MouseButtonPress:

            print QtGui.QCursor().pos()
            press = QtGui.QCursor().pos()

            return False
        if event.type() == QtCore.QEvent.MouseButtonRelease:

            print QtGui.QCursor().pos()
            release = QtGui.QCursor().pos()

            return False
        # Call Base Class Method to Continue Normal Event Processing
        return super(Region, self).eventFilter(obj, event)

def regionDataOnSetup():

        global filter
        filter = Region()
        view = omui.M3dView.active3dView()
        viewWidget = wrapinstance(long(view.widget()), QtCore.QObject)
        viewWidget.installEventFilter(filter)       


 

Marcus Ottosson

unread,
Mar 31, 2014, 1:47:57 PM3/31/14
to python_in...@googlegroups.com
A class attribute should do the trick.

class Region(QtCore.QObject):
  clicked = False

Set to true in any of the events and it will persist across runs. Assuming that, by "once" you mean once per run, and not once ever, as that would involve storing clicked somewhere where it could last across sessions, like on disk.




--
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/f4b50bee-75d5-4359-b6e8-c6c24ab065c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Mar 31, 2014, 2:24:20 PM3/31/14
to python_in...@googlegroups.com

If you only want it to capture the mouse click once after you have told it to attach to the widget, then you can try calling removeEventFilter(obj) in the eventFilter() after you have handled the mouse release. That way it will only fire once and not continue processing events.

Some other suggestions though. Use a constructor and set up some attributes like your clicked and released values. Then store them from your eventFilter like: 
self.released = value

Don't use QCursor to get the positions. The proper values are already there on the event object that is being delivered.  Once you have checked the type, then you know it is a QMouseEvent: http://qt-project.org/doc/qt-4.8/qmouseevent.html

--

Arvid Schneider

unread,
Apr 1, 2014, 2:02:32 PM4/1/14
to python_in...@googlegroups.com
Hey Guys, thanks for the Input. I am still struggling. 

here is my complete code:


REGION_COORDINATES = ['rStart_x', 'rStart_y', 'rEnd_x', 'rEnd_y']

class Region(QtCore.QObject):
    """ Class contains methods to provide information for a specific selected region """

    global REGION_COORDINATES

    def stop(self, value):
        self.stopEvent = value

    def setCoordinateData(self, coordinateValue, place):
        """ stores the x and y coordinate in our global variable """

        REGION_COORDINATES[place] = coordinateValue

    def eventFilter(self, obj, event):
        """ setup the event filter for mouse button press
            and mouse button release
        """

        if event.type() == QtCore.QEvent.MouseButtonPress:
            self.setCoordinateData(event.globalX(), int(0))
            self.setCoordinateData(event.globalY(), int(1))

            return False
        if event.type() == QtCore.QEvent.MouseButtonRelease:
            self.setCoordinateData(event.globalX(), int(2))
            self.setCoordinateData(event.globalY(), int(3))
            self.stop(value = True)

            return False

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

def installRegion():

        global filter
        filter = Region()

        filter.stop(value = False)
        stopEvent = filter.stopEvent
        
        view = omui.M3dView.active3dView()
        viewWidget = wrapinstance(long(view.widget()), QtCore.QObject)
        viewWidget.installEventFilter(filter)

This my code, all I want to do now is kill the event when stopEvent jumps to True.
But how do I accomplish that?

Justin Israel

unread,
Apr 1, 2014, 3:06:02 PM4/1/14
to python_in...@googlegroups.com
I didn't have time to really test this, but it would be something like this:

It gets rid of the globals inside of Region and just uses my previous suggestion of storing the coordinates locally on the region instance. And it disables itself my removing the event filter after the mouse release. In the few seconds I had to test it, I was seeing something odd, that the event object coming into the event filter was actually just a plain QEvent and not a QMouseEvent for some reason. So the calls to globalX() was erroring out. 




--
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.

Arvid Schneider

unread,
Apr 2, 2014, 3:32:04 AM4/2/14
to python_in...@googlegroups.com
Hi Justin!
Thanks, now I finally got it to work with the constructor. 
Your help is much appreciated.
Arvid
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