3

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!

aarelovich
  • 5,140
  • 11
  • 55
  • 106

3 Answers3

12

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); 
        }   
};
Chris
  • 17,119
  • 5
  • 57
  • 60
  • Thanks for the answer. That would work but I was looking for something less complex. If no other choices arrive I'll use yours. – aarelovich Mar 28 '13 at 14:43
  • 2
    This is a pretty common thing to do, what's so complex about it? – Chris Mar 28 '13 at 15:15
  • I agree with you. However, I need to create an entire new class. I've done it before. But is much less code and I get the same result (considering that all I want to do is show a table) if I just add a rectagle and the text in the QGraphicsScene and that is it. Thank you for your answer though. I was looking for something like the e.d's answer but It did not work as I expected. – aarelovich Mar 28 '13 at 17:27
7

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>");
ssc
  • 9,528
  • 10
  • 64
  • 94
Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
  • This didn't do what I expected as it only colored barely around the text and not the entire bounding box. But it was worth a shot I ended up doing the draw a rectangle on the background thing. Thank you! – aarelovich Mar 28 '13 at 17:28
2

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>") );
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43
  • How is this different than the [solution](https://stackoverflow.com/a/15684862/5366641), proposed by @RomhaKorev three years earlier? – scopchanov Sep 04 '20 at 15:10
  • 1
    This is now 4 years after I posted this. To the best of my memory, the solution what Romha proposed did not work for me without the additional `QString`, for whatever reason. I cannot say today, what version of Qt it was (probably something around 5.4). It was under Debian 7. (p.s. I still upvoted Romha's answer!). Feel free to flag for deletion or whatever. I thought it might help someone else in a similar predicament (which apparently it did, as I see here two upvotes.) – Duck Dodgers Sep 04 '20 at 18:22