Tuesday, June 24, 2014

Hello World with KDE Frameworks 5

I have been wanting to learn KDE development for very long time but couldn't do much in the past due to various reasons. Now, with KDE Frameworks 5 beta releases are out, this is a great time to start.

I am documenting my learning process here for my reference and anybody else on the web who would like to have simple start point.

Note: I am documenting what I learn and what works for me so this may contain non-standard or non-optimized but working code . It should work for you as well, in theory. Also, I will be updating this post over the time as I learn more.

First of all we need to have KF5 installed. There are distros providing KF5 packages ready to be installed and used. You can find if your linux distro provides KF5 packages or not and if yes, then how to install them from here:

KDE Frameworks 5 - Binary Packages

Once we have KF5 installed, we can start with writing some code. For my learning process I decided to port existing KDE4 development examples to KDE5. KDE4 examples can be found on KDE Techbase - Development page.

Credit for code goes to original writer of the code on  KDE techbase site. I will be making changes in that code so that it can be compiled and run using KDE Frameworks 5 libraries. With that said, you can use code in this post for learning development with KF5 libraries.

I will not explain each line of code like it is explained in KDE4 tutorial, however you will find short comments within code which will give you some idea about what's being done. I plan to add explanation for code at later time. Also, I am not pointing out changes that are made for porting purpose because I am planning to do it in near future.

So let's start with the first example.

The techbase page for the first example says, "Your first program shall greet the world with a friendly 'Hello World', what else? For that, we will use a KMessageBox and customise one of the buttons."

Following this, we will be using KMessageBox and KGuiItem classes in our code.

Let's start with creating a folder named 'KF5'  and our project folder called 'HelloWorld' using command line. Run following 2 commands in command prompt.

mkdir KF5 && cd KF5
mkdir HelloWorld

Now, we have our project folder created.

Edit: All code updated to replace KLocale with KLocalizedString. Also removed KDELibs4Support to make it true port to KF5.

Open your text editor and write the code as below:

// Including KF5 Classes
#include <KAboutData>
#include <KLocalizedString>
#include <KGuiItem>
#include <KMessageBox>

// Including Qt Classes
#include <QApplication>

int main(int argc, char **argv)
{
    // Configure the data related to our application 
    KAboutData about("helloworld", i18n("helloworld"), "0.1",
                     i18n("A KDE version of Hello, world!"),
                     KAboutLicense::LGPL,
                     i18n("Copyright (C) 2014 Arpan P"));

    // Add application author with role and email address.
    about.addAuthor(i18n("Arpan P"), i18n("developer"), i18n("arpan@example.com"), 0);
    
    // Set application data
    KAboutData::setApplicationData(about);

    // We use QApplication instead of KApplication
    QApplication app(argc, argv);  
 
    // Set up display name for our application - This will be displayed in title bar.
    QApplication::setApplicationDisplayName(i18n("Hello World"));
 
    // This button will be used on our message box.
    KGuiItem yesButton( i18n( "Hello" ), QString(),
                        i18n( "This is a tooltip" ),
                        i18n( "This is a WhatsThis help text." ) ); 
 
    // We create a MessageBox and return back it's status.
    return 
        KMessageBox ::questionYesNo 
        (0, i18n( "Hello World" ), i18n( "Hello" ), yesButton ) 
        == KMessageBox ::Yes? EXIT_SUCCESS: EXIT_FAILURE;
}

Save this file as "main.cpp" without quotes in our project folder.

Now, we have our code written, we need to compile it. For compiling and building executable files, KDE uses CMake. We will also use it. Make sure you have installed 'cmake' and 'extra-cmake-modules' packages for your distro.

Now, create another text file with text editor and write below code in it.

# Set required version for cmake
cmake_minimum_required(VERSION 2.8.12)

# Set project name
project(helloworld) 

# Locate extra-cmake-modules version 0.0.11 and make it a required package
find_package(ECM 0.0.11 REQUIRED NO_MODULE)
 
# Set value of CMAKE_MODULE_PATH variable where cmake will search for modules
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
 
# include KF5 directories
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings)
include(FeatureSummary)

# Locate KF5, make it required package and use KF5CoreAddons, KF5I18n and
# KF5WidgetsAddons modules
find_package(KF5 REQUIRED COMPONENTS CoreAddons I18n WidgetsAddons)

# Name of the exe file and source files from which exe will be generated.
add_executable(helloworld main.cpp) 

# Link our exe with KF5 library 
target_link_libraries(helloworld Qt5::Widgets Qt5::Core KF5::CoreAddons KF5::I18n KF5::WidgetsAddons)

Save this file as "CMakeLists.txt" without quotes in our project folder.

At this point you should have a project with below directory structure.


KF5
|
|---- HelloWorld
      |
      |--- main.cpp
      |---CMakeLists.txt


Let's build an exe from our code. For this we will use CMake. We will build exe in a sub-directory called 'build' so our source code file(main.cpp and CMakeLists.txt) are not mixed up with cmake generated files. Run following commands in command prompt while you are in HelloWorld directory(cd KF5/HelloWorld).

mkdir build && cd build
cmake ..
make

CMake will compile the code and build an exe file in 'build' directory. If you get any errors while running cmake command above, most probably it will be related to KF5 and QT5 directory paths not found by cmake. Post your errors in comments below, if any.

To run this executable, type following command in command prompt.
./helloworld


Hello World example running


That's it. We have successfully developed(in fact, ported) our first KDE5 example program.


Next time, we will port example 2 which has Main Window app.

No comments:

Post a Comment