Development/Tutorials/KDE2/KHello/Tutorial 4

< Development‎ | Tutorials‎ | KDE2‎ | KHello
Revision as of 21:52, 29 June 2011 by Neverendingo (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Description

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

Source code

  1. /************* khello.h *******************/
  2. // From KHello3
  3. #include <kapp.h>
  4. #include <kmainwindow.h>
  5. #include <kmessagebox.h>
  6. #include <qpushbutton.h>
  7. // end From KHello3
  8. #include <kmenubar.h>
  9. #include <kpopupmenu.h>
  10.  
  11. // From KHello3
  12. class KHello : public KMainWindow
  13. {
  14.   Q_OBJECT
  15. public:
  16.   KHello();
  17.   void closeEvent(QCloseEvent *);
  18. public slots:
  19.   void slotHello();
  20.   void slotExit();
  21. private:
  22.   QPushButton *btnHello;
  23.   QPushButton *btnExit;
  24. // end From KHello3
  25.   KMenuBar *menu;
  26.   KPopupMenu *file, *help;
  27. };
  1. /************* khello.cc ******************/
  2. // From KHello3
  3. #include "khello.moc"
  4. #include <kmessagebox.h>
  5.  
  6. KHello::KHello() : KMainWindow()
  7. {
  8.   btnHello = new QPushButton("Hello", this);
  9.   btnHello->setGeometry(45,30,50,25);
  10.   btnHello->show();
  11.   connect(btnHello, SIGNAL(clicked()), this, SLOT(slotHello()));
  12.  
  13.   btnExit = new QPushButton("Exit", this);
  14.   btnExit->setGeometry(105,30,50,25);
  15.   btnExit->show();
  16.   connect(btnExit, SIGNAL(clicked()), this, SLOT(slotExit()));
  17. // end From KHello3
  18.   file = new KPopupMenu();
  19.   file->insertItem("&Hello", this, SLOT(slotHello()));
  20.   file->insertItem("&Exit", this, SLOT(slotExit()));
  21.  
  22.   help = helpMenu("KHello\nby Daniel Marjamäki\nUpdated by David Leimbach");
  23.  
  24.   menu = menuBar();
  25.   menu->insertItem("&File", file);
  26.   menu->insertItem("&Help", help);
  27. }
  28. // From KHello3
  29. void KHello::closeEvent(QCloseEvent *e)
  30. {
  31.   kapp->beep();
  32.   KMainWindow::closeEvent(e);
  33. }
  34.  
  35. void KHello::slotHello()
  36. {
  37.   KMessageBox::information(this, "Hello World!", "Important");
  38. }
  39.  
  40. void KHello::slotExit()
  41. {
  42.   close();
  43. }
  44. // end From KHello3

Important code

This code was simple and didn't need any explaining. However, I think one line was important, and will comment it.

help = helpMenu("KHello\nby Daniel Marjamäki\nUpdated by David Leimbach");

KMainWindow has a built in function which generates a help menu. Use it, in order to make your application consistent to other KDE programs.

Compiling

[on linux where KDEDIR and QTDIR contain the paths to where KDE and Qt are installed respectively] [FreeBSD 5.x users may omit the -ldl]

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

This page was last modified on 29 June 2011, at 21:52. This page has been accessed 1,701 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal