How to remove border around QGraphicsItem when selected?
Asked Answered
G

3

5

Pretty basic question but I couldn't find a solution through google. In QT when a graphics item is selected, there's a border around it. I was wondering how I can set this border to be invisible. Thanks.

Gong answered 11/6, 2012 at 17:48 Comment(0)
E
18

There's no interface to disable the drawing of the selection border for the build-in QGraphicsItems. The only way I can think of is derive your own items from the build-in ones and override the paint() function:

void MyRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QStyleOptionGraphicsItem myOption(*option);
    myOption.state &= ~QStyle::State_Selected;
    QGraphicsRectItem::paint(painter, &myOption, widget);
}

It's not tested but basically you make a copy of option passed and clear the selection state flag before passing it to the actual paint().

Etienne answered 11/6, 2012 at 19:22 Comment(2)
Stephen. I will try this when I get home. Thanks!Gong
Doesn't work in Qt 5.15, but myOption.state = QStyle::State_None; seems to do the trick.Fourth
L
8

If your QGraphicsItem is derived from QAbstractGraphicsShapeItem then you can simply disable its pen, example:

myShape->setPen(Qt::NoPen);
Louden answered 28/9, 2012 at 18:20 Comment(0)
P
2

for those trying to figure it out using python:

def paint(self, painter, option, a):
    option.state = QStyle.State_None
    return super(MyClassName, self).paint(painter,option)
Pietrek answered 21/7, 2021 at 13:58 Comment(1)
PyQt6: option.state = QStyle.StateFlag.State_NoneLaster

© 2022 - 2024 — McMap. All rights reserved.