Reputation: 5556
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?
Thanks for any help!
Upvotes: 3
Views: 6443
Reputation: 3461
This might be too little, too late, but the following worked for me, without having to sub-class or re-implement something.
item->setHtml(QString("<div style='background:rgba(255, 255, 255, 100%);'>" + QString("put your text here") + QString("</div>") );
Upvotes: 3
Reputation: 6584
You can write HTML into a QGraphicsTextItem using setHtml(), so you can fill the background with e.g.
item->setHtml("<div style='background-color:#666666;'>" + yourText + "</div>");
Upvotes: 8
Reputation: 17535
I would subclass QGraphicsTextItem
, for example:
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);
}
};
Upvotes: 13