Search code examples
c++qtqgraphicsitem

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.


Solution

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