Search code examples
c++qtqgraphicsview

QPixmap of a QGraphicsTextItem


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


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");