本文介绍了如何更改QGraphicsTextItem的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add a QGraphicsTextItem and I want to change the color of the background. By this I mean i want the boundingRect that contains the text to have a specific color. One way to do this would be to create a QGraphicsRectItem and put it on the back on the text, but I was wondering If there was another way to do this?

感谢您的帮助!

推荐答案

我会子类化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); 
        }   
};

这篇关于如何更改QGraphicsTextItem的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:13