Difference between revisions of "Development/Tutorials/KDE2/KHello/Tutorial 2"

< Development‎ | Tutorials‎ | KDE2‎ | KHello
Jump to: navigation, search
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
m (Text replace - "</code>" to "</syntaxhighlight>")
 
Line 14: Line 14:
 
   void closeEvent(QCloseEvent *);
 
   void closeEvent(QCloseEvent *);
 
};
 
};
</code>
+
</syntaxhighlight>
 
<syntaxhighlight lang="cpp-qt" line>
 
<syntaxhighlight lang="cpp-qt" line>
 
/************* khello.cc ******************/
 
/************* khello.cc ******************/
Line 24: Line 24:
 
   KMainWindow::closeEvent(e);
 
   KMainWindow::closeEvent(e);
 
}
 
}
</code>   
+
</syntaxhighlight>   
 
<syntaxhighlight lang="cpp-qt" line>
 
<syntaxhighlight lang="cpp-qt" line>
 
/************* main.cc ********************/
 
/************* main.cc ********************/
Line 39: Line 39:
 
   return a.exec();
 
   return a.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
==Explaining the code==
 
==Explaining the code==
Line 45: Line 45:
 
<syntaxhighlight lang="cpp-qt">
 
<syntaxhighlight lang="cpp-qt">
 
Q_OBJECT
 
Q_OBJECT
</code>
+
</syntaxhighlight>
 
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.
 
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">
 
<syntaxhighlight lang="cpp-qt">
 
void closeEvent(QCloseEvent *);
 
void closeEvent(QCloseEvent *);
</code>
+
</syntaxhighlight>
 
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.
 
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">
 
<syntaxhighlight lang="cpp-qt">
 
#include "khello.moc"
 
#include "khello.moc"
</code>
+
</syntaxhighlight>
 
The meta object compiler produces a moc file, which you must include. The moc file is an extended version of your header file.
 
The meta object compiler produces a moc file, which you must include. The moc file is an extended version of your header file.
 
==Compiling==
 
==Compiling==

Latest revision as of 20:52, 29 June 2011

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