Qt Example Source 

Sourcecode for this example you can find here:

You need Qt (I installed Qt at D:\QT you may have to change the path for the moc forwarding to your install dir to compile)

 Qt Example 

Qt Example
Date: 16-11-03
Author: Michael Hielscher
Homepage: www.genesis-x7.de

How to use a Toolbar, Menubar and Statusbar in Qt

1. Creating a QtApplication and Mainwindow
First we need a main.cpp or something like this where we start building our Qt project. In this file we will only write the int main () function and nothing else. Cause we going to use the Qt library we need some header files:

// Qt Sample Application
//   16-11-03 by Moongate
#include <qapplication.h> // requires qt/include is in your path
#include "mainwindow.h"
int main( int argc, char **argv ) {

    QApplication a( argc, argv ); // Creates a QT Application
    MainWindow mw;                // get an instance of MainWindow
    mw.resize( 400, 280 );        // set dimensions of the window
    a.setMainWidget( &mw );       // set the MainWindow as main QT Widget
    mw.setCaption("Qt Example");  // set Window Caption
    mw.show();                    // show the window
    return a.exec();              // start the message loop
Now we need the mainwindow.h header file. In that file we include the rest of the qt library files which we are going to use. The MainWindow class we will inherit from the QMainWindow base class. Cause nearly everything is already defined in that base class we only need to add a few lines for our own actions and widgets:

mainwindow.h (part of it)
class MainWindow : public QMainWindow {
  Q_OBJECT  // Important to make a Qt Object


    QToolButton*  btn1; // some Toolbuttons
    QToolButton*  btn2;
    QToolButton*  btn3;
    QLabel*       statuslabel; // Label for Statusbar

protected slots:
    void Event1(); // some Events for the buttons
    void Event2();
    void Event3();
I used Visual C++ 6.0 to compile these files so if you want to do the same you need to send this headerfile to moc.exe which generates some missing functions (those for the Q_OBJECT macro). The generated file you need to add to your project otherwise you get some link errors. To send your header file automaticly to moc.exe do the following:
Right click on mainwindow.h in the filelist and select setup. There you can enter a commandline like this: D:\Qt\bin\moc mainwindow.h -o moc\moc_mainwindow.cpp

You can look in my sample project how this is done.

Finaly we need the mainwindow.cpp file:

#include "mainwindow.h"
#include "btnimages.cpp"
MainWindow::MainWindow() : QMainWindow() {

    // create a MainMenu //
    QPopupMenu *file = new QPopupMenu( this ); // create a Tab for menu
    menuBar()->insertItem( "&File", file );    // create the menu

    file->insertItem(QPixmap( btn1_xpm ),"Kunden", this,

    file->insertItem(QPixmap( btn2_xpm ),"Drucken",this,


    file->insertItem(QPixmap( btn3_xpm ),"Beenden",this,

    // create Tool Bar with 3 buttons //
    QToolBar *toolbar = new QToolBar("Menu",this, DockTop , TRUE ,0);

    btn1 = new QToolButton( QPixmap( btn1_xpm ),
                            "Kunden", QString::null,
                             this, SLOT( Event1() ), toolbar, "Kunden" );

    btn2 = new QToolButton( QPixmap( btn2_xpm ),
                            "Drucken", QString::null,
                             this, SLOT( Event2() ), toolbar, "Drucken" );

    btn3 = new QToolButton( QPixmap( btn3_xpm ),
                            "Beenden", QString::null,
                             this, SLOT( Event3() ), toolbar, "Beenden" );

    // create Status Bar //
    statuslabel = new QLabel( statusBar() );
    statuslabel->setText  ("Program is running...");
    statusBar()->addWidget( statuslabel, 1, TRUE  );
// Events (SLOTS)
void MainWindow::Event1 () {
    // Show a message
    QMessageBox::information( this, "Qt Example","Hello World!");
void MainWindow::Event2 () {
    // tonggle label on button //
void MainWindow::Event3 () {
    // Close the programm
As you can see, we build the whole window content in the constructor of MainWindow. The event functions are linked with the buttons with the SLOT declaration.
2. MainMenu, Toolbar and Statusbar
We already created MainMenu, Toolbar and Statusbar within the constructor of MainWindow. So a few explenations about this theme here.
The functions menuBar() and statusBar() create the matching bars easily. You can add other widgets to them like a QLabel or QPopupMenu.
You can create an instance of QToolBar like any other class - with new. When creating the buttons you need to set the parent parameter to the toolbar where the button will appear on. When creating a Toolbutton you can specify a label and a hint for it (the 2 string values) as well as a image.
I used some QPixmaps the get some color on the buttons and menus. These are some special char arrays with picture data. I stored them in a separate file called btnimages.cpp.