如何删除 QGraphicsItem 周围的边框

How to remove border around QGraphicsItem when selected?

本文关键字:QGraphicsItem 周围 边框 删除 何删除      更新时间:2023-10-16

非常基本的问题,但我无法通过谷歌找到解决方案。在 QT 中,当选择图形项时,它周围有一个边框。我想知道如何将此边框设置为不可见。谢谢。

没有接口可以禁用内置 QGraphicsItems 的选择边框的绘制。我能想到的唯一方法是从内置项目派生您自己的项目并覆盖 paint() 函数:

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

它没有经过测试,但基本上你复制了传递的选项,并在将其传递给实际paint()之前清除了选择状态标志。

如果你的QGraphicsItem派生自QAbstractGraphicsShapeItem,那么你只需禁用它的笔,例如:

myShape->setPen(Qt::NoPen);

对于那些试图使用python弄清楚的人:

def paint(self, painter, option, a):
    option.state = QStyle.State_None
    return super(MyClassName, self).paint(painter,option)