KHello #4 - Adding a Menu




Description

Now we will make our users very happy by adding a menu!

Source code

/************* khello.h *******************/
#include <kapp.h>
#include <ktmainwindow.h>
#include <qpushbutton.h>
#include <kmenubar.h>
#include <qpopupmenu.h>

class KHello : public KTMainWindow
{
  Q_OBJECT
public:
  KHello();
  void closeEvent(QCloseEvent *);
public slots:
  void slotHello();
  void slotExit();
private:
  QPushButton *btnHello;
  QPushButton *btnExit;
  KMenuBar *menu;
  QPopupMenu *file, *help;
};


/************* khello.cc ******************/
#include "khello.moc"
#include <kmsgbox.h>

KHello::KHello() : KTMainWindow()
{
  btnHello = new QPushButton("Hello", this);
  btnHello->setGeometry(45,30,50,25);
  btnHello->show();
  connect(btnHello, SIGNAL(clicked()), this, SLOT(slotHello()));
  
  btnExit = new QPushButton("Exit", this);
  btnExit->setGeometry(105,30,50,25);
  btnExit->show();
  connect(btnExit, SIGNAL(clicked()), this, SLOT(slotExit()));  
  
  file = new QPopupMenu();
  file->insertItem("&Hello", this, SLOT(slotHello()));
  file->insertItem("&Exit", this, SLOT(slotExit()));
  
  help = kapp->getHelpMenu(TRUE, "KHello\nby Daniel Marjamäki");
  
  menu = new KMenuBar(this);
  menu->insertItem("&File", file);
  menu->insertItem("&Help", help);
}

void KHello::closeEvent(QCloseEvent *)
{
  kapp->quit();
}

void KHello::slotHello()
{
  KMsgBox::message(0,"Important","Hello World!");
}

void KHello::slotExit()
{
  close();
}

Important code

This code was simple and didn't need any explaining. However, I think one line was important, and will comment it.
help = kapp->getHelpMenu(TRUE, "KHello\nby Daniel Marjamäki");
KApplication has a built in function which generates a help menu. Use it, in order to make your application consistent to other KDE programs.

Compiling

     g++ -c -I$KDEDIR/include -I$QTDIR -fno-rtti main.cc
     moc khello.h -o khello.moc
     g++ -c -I$KDEDIR/include -I$QTDIR -fno-rtti khello.cc
     g++ -L$KDEDIR/lib -lkdecore -lkdeui -lqt -o khello main.o khello.o

[<< Prev]


Daniel Marjamäki
2000-02-01