Main menu and toolbars
As you may remember, any widget that has no parent will be displayed as a window. However, when we created our main window, we selected QMainWindow as the base class. If we had selected QWidget instead, we would still be able to do everything we did up to this point. However, the QMainWindow class provides some unique functionality that we will now use.
A main window represents the control center of an application. It can contain menus, toolbars, docking widgets, a status bar, and the central widget that contains the main content of the window, as shown in the following diagram:
If you open the mainwindow.ui file and take a look at the object tree, you will see the mandatory centralWidget that actually contains our form. There are also optional menuBar, mainToolBar, and statusBar that were added automatically when Qt Creator generated the form.
The central widget part doesn't need any extra explanation; it is a regular widget like any other. We will also not focus on dock widgets or the status bar here. They are useful components, but you can learn about them yourself. Instead, we will spend some time mastering menus and toolbars. You have surely seen and used toolbars and menus in many applications, and you know how important they are for a good user experience.
The main menu has a bit of unusual behavior. It's usually positioned in the top part of the window, but in macOS and some Linux environments, the main menu is separated from the window and displayed in the top area of the screen. Toolbars, on the other hand, can be moved freely by the user and docked horizontally or vertically to the sides of the main window.
The main class shared by both these concepts is QAction, which represents a functionality that can be invoked by a user. A single action can be used in multiple places—it can be an entry in a menu (the QMenu instances) or in a toolbar (QToolBar), a button, or a keyboard shortcut (QShortcut). Manipulating the action (for example, changing its text) causes all its incarnations to update. For example, if you have a Save entry in the menu (with a keyboard shortcut bound to it), a Save icon in the toolbar, and maybe also a Save button somewhere else in your user interface and you want to disallow saving the document (for example, a map in your dungeons and dragons game level editor) because its contents haven't changed since the document was last loaded. In this case, if the menu entry, toolbar icon, and button are all linked to the same QAction instance, then, once you set the enabled property of the action to false, all the three entities will become disabled as well. This is an easy way to keep different parts of your application in sync—if you disable an action object, you can be sure that all entries that trigger the functionality represented by the action are also disabled. Actions can be instantiated in code or created graphically using Action Editor in Qt Creator. An action can have different pieces of data associated with it—a text, tooltip, status bar tip, icons, and others that are less often used. All these are used by incarnations of your actions.