Development/Tutorials/KDE2/KHello/Tutorial 2: Difference between revisions
Neverendingo (talk | contribs) m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">") |
Neverendingo (talk | contribs) m (Text replace - "</code>" to "</syntaxhighlight>") |
||
(One intermediate revision by the same user not shown) | |||
Line 3: | Line 3: | ||
==Source code== | ==Source code== | ||
Now there are three files | Now there are three files | ||
< | <syntaxhighlight lang="cpp-qt" line> | ||
/************* khello.h *******************/ | /************* khello.h *******************/ | ||
#include <kapp.h> | #include <kapp.h> | ||
Line 14: | Line 14: | ||
void closeEvent(QCloseEvent *); | void closeEvent(QCloseEvent *); | ||
}; | }; | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="cpp-qt" line> | ||
/************* khello.cc ******************/ | /************* khello.cc ******************/ | ||
#include "khello.moc" | #include "khello.moc" | ||
Line 24: | Line 24: | ||
KMainWindow::closeEvent(e); | KMainWindow::closeEvent(e); | ||
} | } | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="cpp-qt" line> | ||
/************* main.cc ********************/ | /************* main.cc ********************/ | ||
#include "khello.h" | #include "khello.h" | ||
Line 39: | Line 39: | ||
return a.exec(); | return a.exec(); | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Explaining the code== | ==Explaining the code== | ||
Line 45: | Line 45: | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
Q_OBJECT | Q_OBJECT | ||
</ | </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 *); | ||
</ | </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" | ||
</ | </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
/************* khello.h *******************/
#include <kapp.h>
#include <kmainwindow.h>
class KHello : public KMainWindow
{
Q_OBJECT
public:
void closeEvent(QCloseEvent *);
};
/************* khello.cc ******************/
#include "khello.moc"
void KHello::closeEvent(QCloseEvent *e)
{
kapp->beep();
KMainWindow::closeEvent(e);
}
/************* main.cc ********************/
#include "khello.h"
int main( int argc, char **argv )
{
KApplication a( argc, argv, "khello" );
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 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]