Creating a Painter GUI

Learn how we can use the QPainter class to create a painting application.

Creating the GUI

The paintEvent() method of QLabel can be subclassed for easier management of drawing on label objects. A pixmap is created and passed to setPixmap(). Using a pixmap makes handling the painting and displaying of pixels simpler because QPixmap can be used as a QPaintDevice. QPixmap enables us to use fill to specify a default backdrop color. We must deal with the events that occur when the mouse button is pressed, released, or moved because the user will be using the mouse to draw in the GUI window. To maintain track of the mouse pointer and return its coordinates in mouseMoveEvent(), we use setMouseTracking() at line 20.

Get hands-on with 1200+ tech skills courses.