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.
How to remove border around QGraphicsItem when selected?
Asked Answered
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()
.
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 If your QGraphicsItem is derived from QAbstractGraphicsShapeItem then you can simply disable its pen, example:
myShape->setPen(Qt::NoPen);
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)
PyQt6:
option.state = QStyle.StateFlag.State_None
–
Laster © 2022 - 2024 — McMap. All rights reserved.