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 ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.