Drawing text

Additionally, you can also draw text using the QPainterclass. All you need to do is to call QPainter::setFont() to set the font properties before calling QPainter::drawText(), like so:

QPainter painter; 
painter.begin(this); 
 
// Draw Text 
painter.setFont(QFont("Times", 14, QFont::Bold)); 
painter.drawText(QPoint(20, 30), "Testing"); 
 
// Draw Line 
painter.drawLine(QPoint(50, 60), QPoint(100, 100)) 

The setFont() function is optional as you will get a default font if you don't specify it. Once you're done, build and run the program. You should see the word Hello World! displayed in the window:

As you can see here, the vector shapes ...

Get Hands-On GUI Programming with C++ and Qt5 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.