Drawing vector shapes using QPainter

First, create another Qt project by going to File | New File or Project. Then select Qt Widget Application under the Application category. After the project has been created, open up mainwindow.h and add in the QPainter header:

#include <QMainWindow> 
#include <QPainter> 

After that, we also declare a virtual function called paintEvent(), which is a standard event handler in Qt that gets called whenever there is something that needs to be painted, be it a GUI update, a window resize, or when the update() function is being called manually:

public:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();    virtual void paintEvent(QPaintEvent *event); 

Then, open up mainwindow.cpp and add the paintEvent() function: ...

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.