简体   繁体   中英

How to remove border around QGraphicsItem when selected?

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.

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() .

如果您的 QGraphicsItem 是从 QAbstractGraphicsShapeItem 派生的,那么您可以简单地禁用它的笔,例如:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM