Drawing basic shapes on the screen
In this section, we will learn how to draw simple vector shapes (a line, a rectangle, a 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 these vector shapes using the QPen
class.
How to do it…
Let’s follow the steps listed here to display basic shapes in our Qt window:
- First, let’s create a new Qt Widgets Application project.
- Open up
mainwindow.ui
and remove themenuBar
,mainToolBar
, andstatusBar
objects 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:
Figure 4.1 – Removing the menu bar from the main window
- Then, open up the
mainwindow.h
file and add the following code to include theQPainter
header file:#include <QMainWindow> #include <QPainter>
- Then, declare the
paintEvent()
event handler...