KDE programming tutorial using KDevelop

Part 3: Adding a menu bar and a status bar


[<< Contents] [<< Prev] [Next >>]

Adding a menu bar and status bar

As the main window class "KSimulate" is derived from KMainWindow, we have inherited many useful facilities that make this task very easy to accomplish.

ksimulate.cpp

First we need to include three new class headers; kstatusbar.h for access to the status bar functionality, kmenubar.h for the main menu bar that will run across the top of our main window, and kpopupmenu.h for the drop-down menus.

#include <kstatusbar.h>
#include <kpopupmenu.h>
#include <kmenubar.h>

Then inside our KSimulate constructor we can add the following code to create the menus and to put a message on the status bar.

  // create drop-down menus
  KPopupMenu *menuFile = new KPopupMenu( this );
  KPopupMenu *menuEdit = new KPopupMenu( this );
  KPopupMenu *menuSim  = new KPopupMenu( this );
  
  // add drop-down menus to main menu bar
  menuBar()->insertItem( "&File", menuFile );
  menuBar()->insertItem( "&Edit", menuEdit );
  menuBar()->insertItem( "&Simulate", menuSim );
  menuBar()->insertItem( "&Help", helpMenu() );

  statusBar()->message("KSimulate has started.");  

Three drop-down menus are created (for File, Edit and Simulate) currently with no entries. We will add entries later. The drop-down menus are then attached to the main menu bar provided by KMainWindow together with the special Help drop-down menu also provided by KMainWindow. Finally we display a message on the status bar. The status bar, like the main menu bar, is created automatically by KMainWindow when we start to use it.

Compile and run

The new code will be compiled automatically when you next attempt to re-run the application using KDevelop. Investigate how the menus behave.


[<< Contents] [<< Prev] [Next >>]


Last updated 04-Dec-2004