[PyQt] QGraphicsView rubberband selection

Antonio Valentino antonio.valentino at tiscali.it
Fri Oct 30 13:50:44 GMT 2009


Hi Alexander,


Il giorno Fri, 30 Oct 2009 11:44:31 +0100
Alexander Borghgraef <alexander.borghgraef.rma at gmail.com> ha scritto:

> On Fri, Oct 30, 2009 at 9:44 AM, Antonio Valentino <
> antonio.valentino at tiscali.it> wrote:
> 
> >
> > I have the same problem. At the moment I have a solution based on
> > event filters but I'm very interested in a solution exploiting
> > built-in features so please keep updated.
> >
> 
> Interesting to see that I'm not alone in this :-) I tried looking
> into event filters, but I don't grasp how they work yet. I tried the
> following code to capture the selection position, but it doesn't
> behave like I expected, maybe you can shed some light on this?
> 
> class Dialog(QWidget):
> >   def __init__(self, parent=None):
> >     QWidget.__init__(self, parent)
> >     self.gs = QGraphicsScene()
> >     self.gv = QGraphicsView(self.gs)
> >     self.gv.installEventFilter(self)
> >     self.gv.show()
> >
> >   def eventFilter(self, object, event):
> >     if event.type() == QEvent.MouseButtonPress:
> >       self.p0 = self.mapFromGlobal(event.globalPos())
> 
>       return True
> >     elif event.type() == QEvent.MouseButtonRelease:
> >       self.p1 = self.mapFromGlobal(event.globalPos())
> >       return True
> >     else:
> >       return False
> >
> 
> So I attach the eventfilter to the QGraphicsView instance, and
> capture the position at the mouse button press and release. When I
> print p0 and p1, only p0 (the button press one) responds, the release
> does nothing. Any ideas why?

Try to install the event filter in the scene

self.gs.installEventFilter(self)

and then

        if event.type() == QEvent.GraphicsSceneMouseRelease:
            self.p0 = event.buttonDownScenePos(Qt.LeftButton)
            self.p1 = event.scenePos()


This should work for every view attached to the scene.

To hold the selection one could add a rectangle in a "special"
GraphicsItemGroup.


> For the other readers, I'm still interested in how to get to the
> QGraphicsView's rubberband selection data, as I think that would be
> the most elegant solution.


ciao

-- 
Antonio Valentino


More information about the PyQt mailing list