Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Creating GUI in Qt

As described in Chapter 1Introduction to Qt, Qt consists of multiple modules. In this chapter, you will learn how to use the Qt Widgets module. It allows you to create classic desktop applications. The user interface (UI) of these applications consists of widgets.

A widget is a fragment of the UI with a specific look and behavior. Qt provides a lot of built-in widgets that are widely used in applications: labels, text boxes, checkboxes, buttons, and so on. Each of these widgets is represented as an instance of a C++ class derived from QWidget and provides methods for reading and writing the widget's content. You may also create your own widgets with custom content and behavior.

The base class of QWidget is QObject—the most important Qt class that contains multiple useful features. In particular, it implements parent–child relationships between objects, allowing you to organize a collection of objects in your program. Each object can have a parent object and an arbitrary number of children. Making a parent–child relationship between two objects has multiple consequences. When an object is deleted, all its children will be automatically deleted as well. For widgets, there is also a rule that a child occupies an area within the boundaries of its parent. For example, a typical form includes multiple labels, input fields, and buttons. Each of the form's elements is a widget, and the form is their parent widget.

Each widget has a separate coordinate system that is used for painting and event handling within the widget. By default, the origin of this coordinate system is placed in its top-left corner. The child's coordinate system is relative to its parent.

Any widget that is not included into another widget (that is, any top-level widget) becomes a window, and the desktop operating system will provide it with a window frame, which usually usually allows the user to drag around, resize, and close the window (although the presence and content of the window frame can be configured).