Das K Desktop Environment

Kapitel 5. Configuring Menubars and Toolbars

Menubars and toolbar s are one of the most important parts of an application to provide methods to work with a document structure. As a general rule, you should make all functions available by the menubar. Those methods that should not be available at a current stage of the application process should be disabled.

Further, an application can only contain one menubar, but several toolbar s. Toolbars on the other hand should contain only the most frequently used commands by pixmap icons or provide quick access methods like combos to select values.

5.1. How does it work ?

Each entry, may it be a menuentry or a toolbar item, has a resource ID which is an integer value. As these values can't be used twice, those are set by macros, where the numeric values are replaced by a descriptive ID name that can be used in your sources then.

All resource ID's are collected in the file resource.h, where you can keep an overview over the used values. Anyway, the compiler will inform you if you've used a value twice for constructing entries. Also, the resource file should contain all menu accelerators by IDK macro replacements. An example:

   1 (resource.h)
   2 
   3 #define ID_VIEW_TOOLBAR             12010
   4 
   5 
   6 (kscribble.cpp)
   7 
   8 // menu entry Toolbar in the "view" menubar menu
   9 view_menu->insertItem(i18n("&&;Toolbar"), ID_VIEW_TOOLBAR);

This inserts the entry Toolbar to the View popup menu of the menubar in the kscribble application. The resource ID's name is held to contain the menu name and the action's name visible. The ampersand is set in front of the letter that functions as a keyboard accelerator and the entry itself is embraced by the i18n() internationalization macro.

On activating the menu item, the commandCallback() switch is called with the ID number. There, you have to add an according comparator value with the method you want to execute on activating the menuentry:

   1     case ID_VIEW_TOOLBAR:
   2     	slotViewToolBar();
   3     	break;

Anmerkung

Note: you don't have to use the ID system. If no ID is given, the menu gets numbered automatically. The KDevelop framework uses this as it allows accessing menu and toolbar ID's to create switch statements that select the slot to call on activated() for menus, clicked() for toolbar buttons. The connection can also be made directly using the provided methods of the classes providing menus and toolbar s.