Chapter 5. Graphics with Qt

When it comes to graphics, we have so far been using only ready-made widgets for the user interface, which resulted in the crude approach of using buttons for a tic-tac-toe game. In this chapter, you will learn about much of what Qt has to offer with regard to custom graphics. This will let you not only create your own widgets, incorporating content that is entirely customized, but also integrate multimedia in your programs. You will also learn about employing your OpenGL skills to display fast 3D graphics. If you are not familiar with OpenGL, this chapter should give you a kick-start for further research in this topic. By the end of the chapter, you will be able to create 2D and 3D graphics for your games using classes offered by Qt and integrate them with the rest of the user interface.

When it comes to graphics, Qt splits this domain into two separate parts. One of them is raster graphics (used by widgets, for example). This part focuses on using high-level operations (such as drawing lines or filling rectangles) to manipulate colors of a grid of points that can be visualized on different devices, such as images or the display of your computer device. The other is vector graphics, which involves manipulating vertices, triangles, and textures. This is tailored for maximum speed of processing and display using hardware acceleration provided by modern graphics cards. Qt abstracts graphics by using the concept of a surface that it draws on. The surface (represented by the QSurface class) can be of one of two types—RasterSurface or OpenGLSurface. The surface can be further customized using the QSurfaceFormat class, but we will talk about that later as it is not important right now.