-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Qt5 C++ GUI Programming Cookbook
By :
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.
First, let's create a new Qt Widgets Application project:
Open up mainwindow.ui and 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.h and add the following code to include the QPainter header 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.cpp and define the paintEvent() event handler:
void MainWindow::paintEvent(QPaintEvent *event...