[PySide] How do I hide a QGraphicsItem's "focus outline"?

Purma Jukka jukka.purma at aalto.fi
Thu Aug 11 09:07:16 CEST 2016


> Ch'Gans <chgans at gna.org> kirjoitti 11.8.2016 kello 2.54:
> 
> On 11 August 2016 at 10:38, Kevin Cole <dc.loco at gmail.com> wrote:
>> When my QGraphicsItem receives a mousePressEvent, it draws a
>> dotted-line rectangle around the item. Is there a way to suppress
>> that? (I want the item selected / focused, but I don't want to see the
>> dotted line.
>> 
>> I'm actually working with a QGraphicSvgItem, but I don't think that
>> will make a difference.
> 
> If i understand your problem correctly,you're talking about the
> QGraphicSvgItem's paint function that draws outline when the item is
> selected or not.
> If that's the case, you'll have to implement your own SVG item (or
> override it), which might not be as complicated as it first looks.
> But since you're using python, that might not be the way you would be kin to go.
> 
> My 2 cents,
> Chris
> 

Have you enabled some of these flags? http://doc.qt.io/qt-5/qgraphicsitem.html#GraphicsItemFlag-enum 
If e.g. ItemIsSelectable is on, QGraphicsSvgItem will try to see if isSelected() is True in paint function, and draw accordingly.  

If you think you don’t need Qt’s support for selecting graphicsitems, you can a) toggle of that ItemIsSelectable or b) override isSelected -function to return False. Don’t know if paint function relies on this public function, though.

If neither works, the easiest way to implement your own SVG item would be to make a new QGraphicsItem subclass that contains QGraphicsSvgItem as a child item. Basically when you create your QGraphicsItem, in it init it creates QGraphicsSvgItem and sets the QGraphicsItem to be its parentItem. QGraphicsItem’s boundingRect and paint should be implemented as empty — the contained object is drawn & included automatically. Parent QGraphicsItem would be the one to capture all mouse events, be selected, set the position on scene etc, child QGraphicsSvgItem would just draw the SVG, untouched by other events. 

One thing that I forgot to mention in your last example is that you should avoid accidentally overwriting Qt functions when naming class variables, your last example had Image.scene that overrides inherited QGraphicsItem’s scene() -function and can cause odd behaviour along the way. Usually when working with GraphicsItems it is enough to check if nothing in http://doc.qt.io/qt-5/qgraphicsitem.html gets overridden.

Jukka


> 
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/pyside
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside



More information about the PySide mailing list