Qt GUI Widgets

This chapter provides an overview of the GUI building blocks, called widgets, that are part of the Qt library and includes some simple examples of how to use them.

11.1

Widget Categories

239

11.2

QMainWindow and QSettings

240

11.3

Dialogs

244

11.4

Images and Resources

248

11.5

Layout of Widgets

251

11.6

QActions, QMenus, and QMenuBars

260

11.7

QActions, QToolbars, and QActionGroups

262

11.8

Regions and QDockWidgets

270

11.9

Views of a QStringList

272

Widgets, objects of classes derived from QWidget, are reusable building blocks with a visual representation on the screen. The common features of a QWidget are shown in Figure 11.1.

Figure 11.1. QWidget's Heritage

QWidget is a class that uses multiple inheritance (see Section 23.3). A QWidget is a QObject, and thus can have parents, signals, slots, and managed children. A QWidget is a QPaintDevice, the base class of all objects that can be "painted."

QWidgets interact with their children in interesting ways. A widget that has no parent is called a window. If one widget is a parent of another widget, the boundaries of the child widget lie completely within the boundaries of the parent. The contained child widget is displayed according to layout rules (see Section 11.5).

A QWidget can handle events by responding to signals from various entities in the window system (e.g., mouse, keyboard, other processes, etc.). It can paint its own rectangular image on the screen. It can remove itself from the screen in a way that respects whatever else is on the screen at the moment.

A typical desktop GUI application can contain many (hundreds is not unusual) different QWidget-derived objects, deployed according to parent-child relationships and arranged according to the specifications of the applicable layouts.

A QWidget is considered to be the simplest of all GUI classes, because it is rendered as an empty box. The class itself is quite complex, containing hundreds of functions. When you reuse QWidget and its subclasses you are standing on the shoulders of giants, because every QWidget is built on top of layers of Qt code, which in turn are built on top of different layers of native widget libraries, depending on your platform (X11 in Linux, Cocoa on MacOS, and Win32 in Windows).

In fact, QWidget is a Façade for the widget classes from each of these native window libraries (see Section 16.3).

Widget Categories

Категории