Skip to Content
Programming with Qt, 2nd Edition
book

Programming with Qt, 2nd Edition

by Matthias Kalle Dalheimer
January 2002
Beginner to intermediate
522 pages
14h 36m
English
O'Reilly Media, Inc.
Content preview from Programming with Qt, 2nd Edition

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.

Adding Menus

In this section, you will learn how to work with menus. Qt menus mainly consist of two classes: QMenuBar and QPopupMenu. Both are derived from a common base class, QMenuData. 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 QPopupMenu. These menus can have other submenus, which are also objects of type QPopupMenu. 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ GUI Programming with Qt 4

C++ GUI Programming with Qt 4

Jasmin Blanchette, Mark Summerfield
C++ GUI Programming with Qt 4

C++ GUI Programming with Qt 4

Jasmin Blanchette, Mark Summerfield

Publisher Resources

ISBN: 0596000642Supplemental ContentErrata Page