Painting text
Drawing text using QPainter
deserves a separate explanation, not because it is complicated, but because Qt offers much flexibility in this regard. In general, painting text takes place by calling QPainter::drawText()
or QPainter::drawStaticText()
. Let's focus on the former first, which allows the drawing of generic text.
The most basic call to paint some text is a variant of this method, which takes x and y coordinates and the text to draw:
painter.drawText(10, 20, "Drawing some text at (10, 20)");
The preceding call draws the given text at position 10 horizontally and places the baseline of the text at position 20 vertically. The text is drawn using the painter's current font and pen. The coordinates can alternatively be passed as QPoint
instances, instead of being given x and y values separately. The problem with this method is that it allows little control over how the text is drawn. A much more flexible variant is one that lets us give a set of flags and expresses the position...