Chapter 3. Learning More About Qt
By now, you know the basics of programming with Qt, but we have only begun. In this chapter, we continue to work on our painting program by adding features such as menus and file access.
In this section, you will learn how to work with menus. Qt
menus mainly consist of two classes:
Both are derived from a common base class,
Thus, working with menu bars and pop-up menus
is very similar. A menu bar serves as a container for its menus,
which are just objects of the type
menus can have other submenus, which are also objects of type
In addition, you can also use pop-up menus (also called contextual menus) directly,
for example, when the user presses the right mouse button.
For our painting program, we will define three menus: a File menu that will contain only a Quit entry, a Color menu that will allow the user to choose the painting color, and a Help menu that will contain only an About entry. We’ll add more items to these menus later.
Again, we first present you with the complete code and tell you how it works afterwards. The code is in Example 3-1; its output is in Figure 3-1. The new or changed lines have been set in boldface for your convenience.
Example 3-1. qtscribble2.cpp: Adding a menu bar to our painting application
#include <qapplication.h> #include <qmenubar.h> #include <qmessagebox.h> #include <qpainter.h> #include <qpixmap.h> #include <qpopupmenu.h> #include ...