Question

How do you convert/paint a QGraphicsTextItem into a QPixmap?

Was it helpful?

Solution

You can add it to a QGraphicsScene (if it's not already inside one) and then render() the scene to a QPixmap using a QPainter

QPixmap pix(100, 100);
QPainter paint(&pix);
scene.render(&paint);

Or, you can save yourself the trouble and just use QPainter::drawText() after changing the current font of the painter. it should provide the same capabilities.

Maybe something like this-

QPixmap pix(100, 100);
QPainter paint(&pix);
paint.drawText(0, 0, "Hello World");

OTHER TIPS

The QGraphicsTextItem::document() function is the back door you're looking for:

// pItem is a QGraphicsTextItem *
QPixmap srcPixmap(pItem->boundingRect().size().toSize());

QPainter tmpPainter(&srcPixmap);
pItem->document()->drawContents(&tmpPainter);
tmpPainter.end()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top