How it works...

If you want to draw something onscreen using QPainter, all you need to do is tell it what type of graphics it should be drawing (as in text, a vector shape, an image, a polygon) with the desired position and size. The QPen class determines what the outline of the graphic should look like, such as its color, line width, line style (solid, dashed, or dotted), cap style, join style, and so on. On the other hand, QBrush sets the style of the background of the graphics, such as the background color, pattern (solid color, gradient, dense brush, and crossing diagonal lines) and pixmap.

The options for the graphics should be set before calling a draw function (such as drawLine(), drawRect(), or drawEllipse()). If your graphics do not appear onscreen and you see warnings such as QPainter::setPen: Painter not active and QPainter::setBrush: Painter not active appearing on the application output window in Qt Creator, it means that the QPainter class is not currently active and your program will not trigger its paint event. To solve this problem, set the main window as the parent of the QPainter class. Usually, if you're writing code in the mainwindow.cpp file, all you need to do is to put this in the bracket when initializing QPainter. For example, note the following:

QPainter linePainter(this);

QImage can load images from both the computer directories and from the program resources.