Qt 增加QGraphicsTextitem中的选择轮廓宽度

Qt 增加QGraphicsTextitem中的选择轮廓宽度,qt,qgraphicsitem,Qt,Qgraphicsitem,我有一个带有文本交互的QGraphicsTextitem,用户可以在其中编辑当前文本并添加新文本。但我的新要求是增加选择轮廓的宽度,并且可以由QSlider控制。是否可以增加QGraphicsTextItem的虚线选择宽度。 我想增加文本周围选择框的笔厚或大小 在图像中,一条虚线将文本装订起来。是否可以增加虚线笔的大小或厚度。这个问题由来已久,但我会尽我的公民责任尝试回答: 有几件事你必须做 子类化您的QGraphicsTextItem 覆盖paint方法,并在其中删除默认选择样式,然后绘制自己

我有一个带有文本交互的QGraphicsTextitem,用户可以在其中编辑当前文本并添加新文本。但我的新要求是增加选择轮廓的宽度,并且可以由QSlider控制。是否可以增加QGraphicsTextItem的虚线选择宽度。 我想增加文本周围选择框的笔厚或大小


在图像中,一条虚线将文本装订起来。是否可以增加虚线笔的大小或厚度。

这个问题由来已久,但我会尽我的公民责任尝试回答:

有几件事你必须做

  • 子类化您的
    QGraphicsTextItem

  • 覆盖
    paint
    方法,并在其中删除默认选择样式,然后绘制自己的样式



  • 您可能需要覆盖
    boundingRect
    opaqueArea
    shape
    函数来解释您的尺寸增加

  • 选择宽度应根据文本大小自动调整大小。抱歉,我的问题不清楚。。事实上,我在问钢笔的宽度或画框的厚度我在问题中又加了更清楚的描述。。
    void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        QStyleOptionGraphicsItem opt(*option);
    
        // Remove the selection style state, to prevent the dotted line from being drawn.
        opt.state = QStyle::State_None;
    
        // Draw your fill on the item rectangle (or as big as you require) before drawing the text
        // This is where you can use your calculated values (as member variables) from what you did with the slider
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::green);
        painter->drawRect(whateverRectangle());
    
        // Call the parent to do the actual text drawing 
        QGraphicsTextItem::paint(painter, &opt, widget);
    
        // You can use these to decide when you draw
        bool textEditingMode = (textInteractionFlags() & Qt::TextEditorInteraction);
        bool isSelected = (option->state & QStyle::State_Selected);
    
        // Draw your rectangle - can be different in selected mode or editing mode if you wish
    
        if (option->state & (QStyle::State_Selected))
        {
            // You can change pen thickness for the selection outline if you like
            painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine)); 
            painter->setBrush(Qt::magenta);
            painter->drawRect(whateverRectangle());
        }
    }