[<< Contents] [<< Prev] [Next >>]
The QMainWindow class provides many useful facilities that make this task very easy. It could be accomplished by adding a few lines to the main routine in main.cpp, however to give us more flexibility for the future we will instead add these few lines to a simple new class derived from QMainWindow.
The new class will be called "MainWindow" and is added to our project by using the KDevelop Project menu "New Class..." wizard. Enter the class name and press "OK".
To make the code rather easier to follow and to better fit our purposes, we will again replace the template code provided by KDevelop with our own new code shown below.
As previously mentioned our new class is derived from QMainWindow to inherit the excellent Qt functionality. Initially we keep things very simple with only a constructor.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
/*************************************************************************************/
/*********************** Main application window for QSimulate ***********************/
/*************************************************************************************/
class MainWindow : public QMainWindow
{
public:
MainWindow(); // constructor
};
#endif // MAINWINDOW_H
|
The #ifndef #define at the beginning of the file and #endif at the end of the file, is the standard C++ construction to avoid errors if a header file happens to be included more than once. If you don't use it already, it is a very good habit to develop.
In the constructor we use the menuBar() and statusBar() functions inherited from QMainWindow to add our menus to the menu bar and to display a message on the status bar.
#include "mainwindow.h"
#include <QMenuBar>
#include <QStatusBar>
/*************************************************************************************/
/*********************** Main application window for QSimulate ***********************/
/*************************************************************************************/
/************************************ constuctor *************************************/
MainWindow::MainWindow() : QMainWindow()
{
// add drop down menus (currently empty)
menuBar()->addMenu("&File");
menuBar()->addMenu("&Edit");
menuBar()->addMenu("&View");
menuBar()->addMenu("&Simulate");
menuBar()->addMenu("&Help");
// add status bar message
statusBar()->showMessage("QSimulate has started");
}
|
Five drop-down menus are created (File, Edit, View, Simulate, and Help) currently with no entries. We will add entries later. Note that placing an ampersand in the menu title tells Qt to underline the next character and to create an automatic keyboard shortcut. The status bar, like the main menu bar, is created automatically by QMainWindow when we start to use it.
Now that we have created our new "MainWindow" class, we need to edit main.cpp to use it instead of QMainWindow.
Remove the include statement for the Qt class QMainWindow and replace with a new include statement for the header file of our new class.
#include "mainwindow.h" |
And edit the line that creates the main application window to use our new class MainWindow instead of the Qt class QMainWindow.
MainWindow window; |
The new code will be compiled automatically when you attempt to re-run the application using KDevelop.
[<< Contents] [<< Prev] [Next >>]