Development/Tutorials/KDE2/KHello/Tutorial 2

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

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



In this step I describe how a window class is created.

Source code

Now there are three files

  1. /************* khello.h *******************/
  2. #include <kapp.h>
  3. #include <kmainwindow.h>
  5. class KHello : public KMainWindow
  6. {
  7.   Q_OBJECT
  8. public:
  9.   void closeEvent(QCloseEvent *);
  10. };
  1. /************* ******************/
  2. #include "khello.moc"
  4. void KHello::closeEvent(QCloseEvent *e)
  5. {
  6.   kapp->beep();
  7.   KMainWindow::closeEvent(e);
  8. }
  1. /************* ********************/
  2. #include "khello.h"
  4. int main( int argc, char **argv )
  5. {
  6.   KApplication a( argc, argv, "khello" );
  7.   KHello *w = new KHello();
  8.   w->setGeometry(100,100,200,100);
  10.   a.setMainWidget( w );
  11.   w->show();
  12.   return a.exec();
  13. }

Explaining the code

There are a few commands that need explaining:


This is a command to the meta object compiler, which is included in the developer files for QT. You must start each KDE class with this line.

void closeEvent(QCloseEvent *);

This function is called when your window is being closed. We sound a beep when that happens and then call the original closeEvent() function. The original closeEvent() function takes care to exit the application when the last window gets closed.

#include "khello.moc"

The meta object compiler produces a moc file, which you must include. The moc file is an extended version of your header file.


[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
moc khello.h -o khello.moc
g++ -c -I$KDEDIR/include -I$QTDIR/include -fno-rtti
g++ -L$KDEDIR/lib -L$QTDIR/lib -lkdeui -lkdecore -lqt -ldl -o khello main.o khello.o

[if moc is not in your path try $QTDIR/bin/moc]

This page was last modified on 29 June 2011, at 20:52. This page has been accessed 2,023 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