Question

I am trying to get a picture of a string (in Qt), i.e. make a QImage from a QString (std::string is fine too), preferably for a given resolution. While the question seems trivial, I can't figure out the correct way to do it.

The obvious idea of the constructor QImage(const QString& string) clearly doesn't work, as this reads the file described by string. So what would be the correct way to do it?

Was it helpful?

Solution

Create a QPainter and pass it the QImage object in its constructor. Then call the QPainter's drawText function to draw the text to the image.

So, assuming you've created your QImage object called img...

QPainter painter(img);
painter.drawText(xPos, yPos, text);

You can also set things on the QPainter like the font and brush that you want, to define what the text will look like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top