Reader small image

You're reading from  Application Development with Qt Creator - Third Edition

Product typeBook
Published inJan 2020
Reading LevelBeginner
Publisher
ISBN-139781789951752
Edition3rd Edition
Languages
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Drawing with Qt

While many applications can be built using only the built-in widgets, others require the ability to perform custom drawing—for example, when you need a custom widget or two, or maybe you're doing offscreen rendering to programmatically create images in graphics files, or else you're interested in building a radically different user interface. Qt provides support for all of these scenarios in C++, in addition to what you can do with Qt Quick.

In this chapter, we will see what is needed to know for general drawing in Qt. We begin by discussing QPainter, and how it uses QPaintDevice instances to abstract drawing functionality. We will see how this works in general terms, and then give concrete examples for offscreen drawing to bitmaps, as well as creating custom widgets that interoperate with Qt Widgets. In the last half of the chapter, we will turn...

Technical requirements

Starting to draw in Qt

All the material we cover in this chapter depends on the Qt GUI module, available as part of Qt. Even if you're writing a command-line tool (say, to process image files), you need to include that module in your project by adding the following code to your .pro file:

QT += gui widgets 

Of course, in your C++ implementation, we also need to include the header files for the classes we're using. For example, if we're using QImage, QBitmap, and QPainter, be sure to include these headers at the top of your C++ file, like this:

#include <QImage> 
#include <QPainter> 
#include <QBitmap> 

As Qt's painting implementation uses the underlying windowing system, any application that performs graphics operations must be built using QGuiApplication, which initializes the windowing system as part of its startup.

As we have added the...

Drawing with QPainter on QPaintDevice instances

At its core, graphics painting requires two things: something that knows how to paint, and something that can be painted on. Qt defines the QPainter class as the former, and the QPaintDevice as the interface for classes for the latter. You'll seldom instantiate each, but you use both of these classes a lot if you're doing graphics programming; typically, you'll have an instance of a subclass of QPaintDevice, ask it for its associated QPainter, and then use QPainter to perform your drawing. This can happen when you're writing a widget; you'll be passed a QPainter subclass, for example, when you need to paint the widget's contents.

There are several subclasses of QPaintDevice, as follows:

  • QWidget: This class and its subclasses are used by the widget hierarchy.
  • QImage: This is a container class for offscreen...

Drawing off screen

There are a number of reasons why you might like to draw off screen: you might want to compose a collection of images and show them one after another (this is called double-buffering, which you can do to avoid screen painting flicker when you draw on screen), or write a program that generates image files directly.

As I mentioned in the previous section, Qt provides several classes for offscreen drawing, each with different advantages and disadvantages. These classes are QImage, QPixmap, QBitmap, and QPicture. In normal circumstances, you need to choose between QImage and QPixmap.

QImage is the class best suited for general-purpose drawing, where you're interested in loading the image from or saving the image to a file. If you're working with resources, combining multiple images, and doing a bit of drawing, QImage is the class you want to use.

On the...

Creating custom widgets

Painting with a custom widget is, at its heart, no different than offscreen painting; all you need is a widget subclass and a painter pointing to the widget, and you're all set. Yet, how do you know when to paint?

Qt's QWidget class defines an interface used by the rendering system to pass events to your widget: Qt defines the QEvent class to encapsulate the data about an event, and the QWidget class defines an interface that Qt's rendering system uses to pass events to your widget for processing. Qt uses this event system not just to indicate things such as mouse movements and keyboard input, but also for requests to paint the screen as well.

Let's look at painting first. QWidget defines the paintEvent method, which Qt's rendering system invokes, passing a QPaintEvent pointer. The QPaintEvent pointer includes the region that needs...

Introducing the Graphics View framework

Qt provides a separate view framework, the Graphics View framework, to draw hundreds or thousands of relatively lightweight customized items at once. You will choose the Graphics View framework if you're implementing your own widget set from scratch (although you might want to consider Qt Quick for this as well), or if you have a large number of items to display on the screen at once, each with their own position and data. This is especially important for applications that process and display a great deal of data, such as geographic information systems or computer-aided design applications.

In the Graphics View framework, Qt defines the scene, responsible for providing a fast interface to a large number of items. (If you remember our discussion of Model-View-Controller (MVC) from the previous chapter, you can think of the scene as the...

Summary

In this chapter, we learned how to draw graphics on screen and off screen, using the QPainter class. We also learned how to create our own custom widget in Qt. Then, we explored the Graphics View framework and created a simple game.

Throughout this chapter, we have learned how Qt provides the QPaintDevice interface and QPainter class to perform graphics operations. Using QPaintDevice subclasses such as QWidget, QImage, and QPixmap, you can perform onscreen and offscreen drawing. We also saw how Qt provides a separate viewable object hierarchy for large numbers of lightweight objects through the Graphics View framework, supported by the classes QGraphicsView and QGraphicsScene, and the QGraphicsItem interface.

In the next chapter, we turn from Qt's support for GUIs in C++ to that of Qt Quick. We'll learn about the fundamental Qt Quick constructs, performing animations...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Application Development with Qt Creator - Third Edition
Published in: Jan 2020Publisher: ISBN-13: 9781789951752
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng