gpt4 book ai didi

qt - 如何更改 QGraphicsTextItem 的背景?

转载 作者:行者123 更新时间:2023-12-05 01:26:16 49 4
gpt4 key购买 nike

我想添加一个 QGraphicsTextItem 并且我想更改背景的颜色。我的意思是我希望包含文本的 boundingRect 具有特定颜色。一种方法是创建一个 QGraphicsRectItem 并将其放在文本的背面,但我想知道是否有另一种方法可以做到这一点?

谢谢你的帮助!

最佳答案

我会子类 QGraphicsTextItem , 例如:

class QGraphicsTextItemWithBackgroundColorOfMyChoosing : public QGraphicsTextItem
{
public:
QGraphicsTextItemWithBackgroundColorOfMyChoosing(const QString &text) :
QGraphicsTextItem(text) { }

void paint( QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *w) {
painter->setBrush(Qt::red);
painter->drawRect(boundingRect());
QGraphicsTextItem::paint(painter, o, w);
}
};

关于qt - 如何更改 QGraphicsTextItem 的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684277/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com