KHello #2 - Creating a window class




Description

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

Source code

Now there are three files
/************* khello.h *******************/
#include <kapp.h>
#include <ktmainwindow.h>

class KHello : public KTMainWindow
{
  Q_OBJECT
public:
  void closeEvent(QCloseEvent *);
};

/************* khello.cc ******************/
#include "khello.moc"

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

/************* main.cc ********************/
#include "khello.h"

int main( int argc, char **argv )
{
  KApplication a( argc, argv, "Hello World!" );
  KHello *w = new KHello();
  w->setGeometry(100,100,200,100);

  a.setMainWidget( w );
  w->show();
  return a.exec();
}

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 KDE program is closing.

#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

     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] [Next >>]


Daniel Marjamäki
2000-02-01