The Code

All the code we need will be in one file, main.cpp. Create that file with the code below:

 1 #include <KApplication>
 2 #include <KAboutData>
 3 #include <KCmdLineArgs>
 4 #include <KMessageBox>
 5 #include <KLocale>
 7 int main (int argc, char *argv[])
 8 {
 9     KAboutData aboutData(
10                          // The program name used internally.
11                          "tutorial1",
12                          // The message catalog name
13                          // If null, program name is used instead.
14                          0,
15                          // A displayable program name string.
16                          ki18n("Tutorial 1"),
17                          // The program version string.
18                          "1.0",
19                          // Short description of what the app does.
20                          ki18n("Displays a KMessageBox popup"),
21                          // The license this code is released under
22                          KAboutData::License_GPL,
23                          // Copyright Statement
24                          ki18n("(c) 2007"),
25                          // Optional text shown in the About box.
26                          // Can contain any information desired.
27                          ki18n("Some text..."),
28                          // The program homepage string.
29                          "",
30                          // The bug report email address
31                          "[email protected]");
33     KCmdLineArgs::init( argc, argv, &aboutData );
34     KApplication app;
35     KGuiItem yesButton( i18n( "Hello" ), QString(),
36                         i18n( "This is a tooltip" ),
37                         i18n( "This is a WhatsThis help text." ) );
38     KMessageBox::questionYesNo( 0, i18n( "Hello World" ),
39                                 i18n( "Hello" ), yesButton );
40     return 0;
41 }

The first KDE specific code we come across in this program is KAboutData. This is the class used to store information about the program such as a short description, authors or license information. Pretty much every KDE application should use this class.

Then we come to KCmdLineArgs. This is the class one would use to specify command line switches to, for example, open the program with a specific file. However, in this tutorial, we simply initialise it with the KAboutData object we created so we can use the --version or --author switches.

Then we create a KApplication object. This needs to be done exactly once in each program since it is needed for things such as i18n.

Now we've done all the necessary KDE setup, we can move on to doing interesting things with our application. We're going to create a popup box but we're going to customise one of the buttons. To do this customisation, we need to use a KGuiItem object. The first argument in the KGuiItem constructor is the text that will appear on the item (in our case, a button). Then we have an option of setting an icon for the button but we don't want one so we just give it QString(). We then set the tooltip (what appears when you hover over an item) and finally the "What's This?" (accessed through right-clicking or Shift-F1) text.

Now we have our item, we can create our popup. We call the KMessageBox::questionYesNo() function which, by default, creates a message box with a "Yes" and a "No" button. The second argument is the text that will appear in the message box above the buttons. The third is the caption the window will have and finally we set the KGuiItem for (what would normally be) the "Yes" button to the KGuiItem yesButton we created.

Note that all user-visible text is passed through the i18n() function; this is necessary for the UI to be translatable. More information on localization can be found in the localization tutorial.

We're all done as far as the code is concerned. Now to build it and try it out.

Creating the KAction object

The KAction is built up in a number of steps. The first is including the KAction library and then creating the KAction:

#include <KAction>
KAction* clearAction = new KAction(this);

This creates a new KAction called clearAction.

appnameui.rc File

Since the description of the UI is defined with XML, the layout must follow strict rules. This tutorial will not go into great depth on this topic, but for more information, see the detailed XMLGUI page (here is an older tutorial: [1]).


<?xml version="1.0" encoding="UTF-8"?>
<gui name="tutorial3"
               " >

    <Menu name="file" >
      <Action name="clear" />

  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="clear" />


The <Toolbar> tag allows you to describe the toolbar, which is the bar across the top of the window normally with icons. Here it is given the unique name mainToolBar and its user visible name set to Main Toolbar using the <text> tag. The clear action is added to the toolbar using the <Action> tag, the name parameter in this tag being the string that was passed to the KActionCollection with addAction() in mainwindow.cpp.

Besides having the action in the toolbar, it can also be added to the menubar. Here the action is being added to the File menu of the MenuBar the same way it was added to the toolbar.

Change the 'version' attribute of the <gui> tag if you changed .rc file since the last install to force a system cache update. Be sure it is an integer, if you use a decimal value, it will not work, but will not notify that it didn't. '

The version attribute must be an integer number, if you type in version="1.2" it will dispose of your kittens (but not eat them).

Some notes on the interaction between code and the .rc file: Menus appear automatically and should have a <text/> child tag unless they refer to standard menus. Actions need to be created manually and inserted into the actionCollection() using the name in the .rc file. Actions can be hidden or disabled, whereas menus can't.


You want to use CMake for your build environment. You provide a file CMakeLists.txt, cmake uses this file to generate all Makefiles out of it.


Create a file named CMakeLists.txt in the same directory as main.cpp with this content:

project (tutorial1)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
set(tutorial1_SRCS main.cpp)
kde4_add_executable(tutorial1 ${tutorial1_SRCS})
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})

The find_package() function locates the package that you ask it for (in this case KDE4) and sets some variables describing the location of the package's headers and libraries. In this case we will use the KDE4_INCLUDES variable which contains the path to the KDE4 header files.

In order to allow the compiler to find these files, we pass that variable to the include_directories() function which adds the KDE4 headers to the header search path.

Next we create a variable called tutorial1_SRCS using the set() function. In this case we simply set it to the name of our only source file.

Then we use kde4_add_executable() to create an executable called tutorial1 from the source files listed in our tutorial1_SRCS variable. Afterwards, we link our executable to the KDE4 kdeui library using target_link_libraries() and the KDE4_KDEUI_LIBS variable which was set by the find_package() function. The line starting with install writes a default "install" target into the Makefile.

Make And Run

You can invoke CMake and make manually:

mkdir build && cd build
cmake .. # Note the two dots - this is no ellipsis, but "parent directory".

Or, if you set up your environment as described in Getting Started/Build/Environemnt, you can compile this code with:


And launch it with:


Make And Run

You can invoke CMake and make manually:

mkdir build && cd build
cmake .. # Note the two dots - this is no ellipsis, but "parent directory".

Or, if you set up your environment as described in Getting Started/Build/Environemnt, you can compile this code with:


And launch it with:


This page was last edited on 27 June 2011, at 14:50. Content is available under Creative Commons License SA 4.0 unless otherwise noted.