Since we have learned so much about the QPainter
class and how to use it to display graphics on screen, I guess it's time for us to do something fun so that we can put our knowledge into practice.
In this recipe, we will learn how to make a basic paint program that allows us to draw lines on a canvas with different brush sizes and colors. We will also learn how to use the QImage
class and the mouse events in order to construct the paint program.
Let us start our fun project through the following steps:
Again, we start by creating a new Qt Widgets Application project and removing the tool bar and status bar. We will keep the menu bar this time.
After that, set up the menu bar like so:
We will leave the menu bar as it is for the moment and let's proceed to
mainwindow.h
. First, include the following header files as it's required for the project:#include <QPainter> #include <QMouseEvent> #include <QFileDialog>