Development/Tutorials/KDE2/KHello/Tutorial 1: Difference between revisions

From KDE TechBase
< Development‎ | Tutorials‎ | KDE2‎ | KHello
m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">")
m (Text replace - "</code>" to "</syntaxhighlight>")
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
==Source code==
==Source code==
The source code is very simple:
The source code is very simple:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
/************* khello.cc *******************/
/************* khello.cc *******************/
#include <kapp.h>
#include <kapp.h>
Line 19: Line 19:
}
}
/************* end of file *****************/
/************* end of file *****************/
</code>
</syntaxhighlight>
== Explaining the code==
== Explaining the code==
Here is the commands explained:
Here is the commands explained:
Line 25: Line 25:
#include <kapp.h>
#include <kapp.h>
#include <kmainwindow.h>
#include <kmainwindow.h>
</code>
</syntaxhighlight>
The kapp.h file contain some basic code needed by all programs, and the kmainwindow.h file contains the class which should be used for all main windows in KDE programs.
The kapp.h file contain some basic code needed by all programs, and the kmainwindow.h file contains the class which should be used for all main windows in KDE programs.
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
KApplication a(argc, argv);
KApplication a(argc, argv);
KMainWindow *w = new KMainWindow();
KMainWindow *w = new KMainWindow();
</code>
</syntaxhighlight>
Create a KApplication object and a KMainWindow object. The KApplication object will contain all the basic code our program needs, and the KMainWindow object will be our main window.
Create a KApplication object and a KMainWindow object. The KApplication object will contain all the basic code our program needs, and the KMainWindow object will be our main window.
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
w->setGeometry(100,100,200,100);
w->setGeometry(100,100,200,100);
</code>
</syntaxhighlight>
Move and resize the window. It is moved to coordinates (100, 100), and the size is changed to 200x100 (width x height).
Move and resize the window. It is moved to coordinates (100, 100), and the size is changed to 200x100 (width x height).
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
a.setMainWidget( w );
a.setMainWidget( w );
</code>
</syntaxhighlight>
Our program needs to know where to find the main window.
Our program needs to know where to find the main window.
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
w->show();
w->show();
</code>
</syntaxhighlight>
Make the main window visible.
Make the main window visible.
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
return a.exec();
return a.exec();
</code>
</syntaxhighlight>
Execute the program.
Execute the program.
==Compiling==
==Compiling==

Latest revision as of 20:52, 29 June 2011

Description

This is, as far as I know, the simplest possible KDE program.

Source code

The source code is very simple:

/************* khello.cc *******************/
#include <kapp.h>
#include <kmainwindow.h>

int main( int argc, char **argv )
{
  KApplication a( argc, argv, "khello" );
  KMainWindow *w = new KMainWindow();
  w->setGeometry(100,100,200,100);

  a.setMainWidget( w );
  w->show();
  return a.exec();
}
/************* end of file *****************/

Explaining the code

Here is the commands explained:

#include <kapp.h>
#include <kmainwindow.h>

The kapp.h file contain some basic code needed by all programs, and the kmainwindow.h file contains the class which should be used for all main windows in KDE programs.

KApplication a(argc, argv);
KMainWindow *w = new KMainWindow();

Create a KApplication object and a KMainWindow object. The KApplication object will contain all the basic code our program needs, and the KMainWindow object will be our main window.

w->setGeometry(100,100,200,100);

Move and resize the window. It is moved to coordinates (100, 100), and the size is changed to 200x100 (width x height).

a.setMainWidget( w );

Our program needs to know where to find the main window.

w->show();

Make the main window visible.

return a.exec();

Execute the program.

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 khello.cc
g++ -L$KDEDIR/lib -L$QTDIR/lib -lkdeui -lkdecore -lqt -ldl -o khello khello.o