Drawing basic shapes on screen
In this section, we will learn how to draw simple vector shapes (line, rectangle, circle, and so on) and display text on the main window using the QPainter
class. We will also learn how to change the drawing style of the vector shapes using the QPen class.
How to do it…
First, let's create a new Qt Widgets Application project:
Open up
mainwindow.uiand remove the menu bar, main tool bar, and status bar so that we get a clean, empty main window. Right-click on the bar widgets and select Remove Menu Bar from the pop-up menu:
Then, open up
mainwindow.hand add the following code to include theQPainterheader file:#include <QMainWindow> #include <QPainter>Then, declare the
paintEvent()event handler below the class destructor:public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); virtual void paintEvent(QPaintEvent *event);Next, open up
mainwindow.cppand define thepaintEvent()event handler:void MainWindow::paintEvent(QPaintEvent *event...