Before constructing the menubar, you need to create a QPopupMenu for each of the pull-down menus. In KSimpleApp you create one QPopupMenu for the File menu. Line 23 adds the entry "Reposition" Text to the File menu. The object reposition, used on line 23 and created on lines 14–17 is an action (an instance of KAction). It holds all of the information needed to create a menu entry or toolbar entry (see the next section, "The Toolbar"). Actions are a convenient way of packaging application functions with the user interaction needed to describe and activate them. (KActions are discussed further in Chapter 5, "KDE User Interface Compliance.") Lines 14–17, for example, The ampersand before the letter R makes it so that when the menu is visible, the user can press R to activate this menu entry. This feature is made known to the user by the widget by underlining the R. The constants CTRL and Key_R are defined in qnamespace.h. Here they indicate that the Ctrl+R key combination will activate this menu entry whenever it is pressed by the user. These key combinations, called accelerators, allow the user to bypass the menubar/toolbar interface and access commonly used functions with simple keystrokes. The icon, specified by QIconSet(BarIcon ("idea")) (a light bulb), will be placed to the left of the menu entry. (see Figure 2.6). When a function appears on the toolbar (as this one does, see the next section, "The Toolbar"), it should also appear as an entry in the menubar with the same toolbar icon next to the entry. This makes the correspondence between the two functions clearer to the user. The class QIconSet takes the icon specified by BarIcon("idea") and creates different icons that might be needed by the GUI: a large icon, a small icon, and grayed-out "disabled"-look icons. This is all taken care of by the libraries with no further necessary interaction. The other two parameters to the KAction constructor: this and SLOT (slotRepositionText()) indicate that the method slotRepositionText(), which is a member of this instance of KSimpleApp, should be called whenever this action is activated. The details of just how such a feat can be accomplished—that feat being to call a method in a specific instance of a class seemingly arbitrarily—is discussed in Chapter 3. For now, note that this is accomplished with the Qt signal/slot mechanism. The next line appends a horizontal line to the pop-up menu. This is not an active GUI element; it simply serves to separate groups of functions. The KDE GUI design guidelines require this to be placed before the next entry in the menu, which is Quit. (These guidelines are discussed in Chapter 7.) | Use standard names for standard menu entries (Chapter 7 has details). Important: Quit is the last entry on the File menu, not Exit! |
The second argument passed to the KAction constructor when creating the Quit entry on lines 18-20 is KStdKeys::quit(), which is a static method of KStdAccel that returns the default accelerator key combination for the Quit action. All applications should have a Quit entry in the File menu, and Quit should always be associated with the accelerator key returned by KStdAccel::quit(). Using the same names and accelerators in all applications provides consistency, enabling the user to "Learn once and use everywhere" common application functions. Notice that I use the new operator to create a QPopupMenu. This allows the object to survive even after you leave the current scope (that is, the KSimpleApp constructor) so that it can continue to be accessed by the menubar. An object created in the following way: would be deleted after the constructor finished. It is deleted automatically, so you can forget about it unless you want to make changes to it later on. | Create your widgets with new. They will be deleted by their parents, so you won't need to delete them. |
Finally place the pop-up menu you created on the menubar with menuBar()creates a KMenuBar widget the first time it is called. KTMainWindow is also responsible for deleting the KMenuBar when it is no longer needed. This is the simplest method of handling a menubar. It is also possible to create a KMenuBar yourself and tell KTMainWindow to use it by calling This can be useful if you need to switch to a new menubar. |