[PyQt] Re: Filter mouse move event

LF lucafasano at interfree.it
Mon Apr 6 08:56:28 BST 2009


Arnold Krille <arnold <at> arnoldarts.de> writes:

> 
> On Saturday 04 April 2009 20:07:35 Luca Fasano wrote:
> > Hi list,
> > I need to filter mouse moving event on a QGraphicsView. I created a
> > filter and install it to a QGraphicsView instance, but desired event
> > seems not to be captured.
> > Filter definition is:
> > class Filter(QtCore.QObject):
> > 	def eventFilter(self, obj, event):
> >     	print event.type()
> >     	return False
> 
> Is the indentation in your source-file correct?

The indentation *is* correct, but copying code it has been changed.
> > then I have installed an instance of this filter to graphicsview as
> > following:
> > 	self.graphicsview = QtGui.QGraphicsView(self.scene, self)
> >     	self.graphicsview.setMouseTracking(True)
> >     	self.filter = Filter(self.graphicsview)
> >     	self.graphicsview.installEventFilter(self.filter)
> >     	self.setCentralWidget(self.graphicsview)
> > ...but event seems not to be captured! Where I'm wrong??
> 
> While your Filter-class subclasses QObject, are you calling QObjects 
> constructor from your constructor? As far as I know the constructors of 
>parent 
> classes aren't called automaticly in python...

I think my class constructor is called automatically, but I avoid this problem
too. Following there is complete test file (hopying indentation is reported
correctly):

import sys

from PyQt4 import QtCore, QtGui

class Filter(QtCore.QObject):
    def __init__(self):
        super(QtCore.QObject, self).__init__()

    def eventFilter(self, obj, event):
        print event.type()
        return False

class MainWin(QtGui.QMainWindow):

    def __init__(self, parent=None, flags=QtCore.Qt.Widget):
        QtGui.QMainWindow.__init__(self, parent, flags)

        self.scene = QtGui.QGraphicsScene(self)
        self.graphicsview = QtGui.QGraphicsView(self.scene, self)
        self.graphicsview.setMouseTracking(True)
        self.filter = Filter()
        self.graphicsview.installEventFilter(self.filter)
        self.setCentralWidget(self.graphicsview)

def main():
    app = QtGui.QApplication(sys.argv)
    mainwin = MainWin()
    mainwin.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()







More information about the PyQt mailing list