< Development‎ | Tutorials‎ | KDE2‎ | KHello
Revision as of 20:32, 29 June 2011 by Neverendingo (talk | contribs) (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")

Development/Tutorials/KDE2/KHello/Tutorial 2


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

Source code

Now there are three files <syntaxhighlight lang="cpp-qt" line> /************* khello.h *******************/

  1. include <kapp.h>
  2. include <kmainwindow.h>

class KHello : public KMainWindow {



 void closeEvent(QCloseEvent *);

}; <syntaxhighlight lang="cpp-qt" line> /************* khello.cc ******************/

  1. include "khello.moc"

void KHello::closeEvent(QCloseEvent *e) {


} <syntaxhighlight lang="cpp-qt" line> /************* main.cc ********************/

  1. include "khello.h"

int main( int argc, char **argv ) {

 KApplication a( argc, argv, "khello" );
 KHello *w = new KHello();
 a.setMainWidget( w );
 return a.exec();


Explaining the code

There are a few commands that need explaining: <syntaxhighlight lang="cpp-qt"> 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.

<syntaxhighlight lang="cpp-qt"> 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.

<syntaxhighlight lang="cpp-qt">

  1. 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 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 edited on 29 June 2011, at 20:52. Content is available under Creative Commons License SA 4.0 unless otherwise noted.