Development/Tutorials/KDE2/KHello/Tutorial 2

< Development‎ | Tutorials‎ | KDE2‎ | KHello
Jump to: navigation, search

Contents

Description

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>
  4.  
  5. class KHello : public KMainWindow
  6. {
  7.   Q_OBJECT
  8. public:
  9.   void closeEvent(QCloseEvent *);
  10. };
  1. /************* khello.cc ******************/
  2. #include "khello.moc"
  3.  
  4. void KHello::closeEvent(QCloseEvent *e)
  5. {
  6.   kapp->beep();
  7.   KMainWindow::closeEvent(e);
  8. }
  1. /************* main.cc ********************/
  2. #include "khello.h"
  3.  
  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);
  9.  
  10.   a.setMainWidget( w );
  11.   w->show();
  12.   return a.exec();
  13. }

Explaining the code

There are a few commands that need explaining:

Q_OBJECT

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.

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

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


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