Development/Tutorials/First program: Difference between revisions

From KDE TechBase
No edit summary
(Fit in better with the tutorial series)
Line 9: Line 9:
int main (int argc, char *argv[])
int main (int argc, char *argv[])
{
{
KAboutData aboutData( "test", "test",
  KAboutData aboutData( "tutorial1", "Tutorial 2",
                      "1.0", "test", KAboutData::License_GPL,
      "1.0", "KMessageBox popup",
                      "(c) 2006" );
      KAboutData::License_GPL, "(c) 2006" );
KCmdLineArgs::init( argc, argv, &aboutData );
  KCmdLineArgs::init( argc, argv, &aboutData );
KApplication khello;
  KApplication app;
KGuiItem kgi( QString( "Hello" ), QString(),
  KGuiItem guiItem( QString( "Hello" ), QString(),
               QString( "this is a tooltip" ),
               QString( "this is a tooltip" ),
               QString( "this is whatsthis" ) );
               QString( "this is whatsthis" ) );
  KMessageBox::questionYesNo( 0, "text", "caption", kgi );
  KMessageBox::questionYesNo( 0, "text", "caption", guiItem );
}
}
</code>
</code>
If you set up your environment as described in [[Build/Unstable Version|Build/Unstable Version]], you can compile this code with
If you set up your environment as described in [[Build/Unstable Version|Build/Unstable Version]], you can compile this code with
   
   
  gcc hello.cpp -o hello -I/home/kde-devel/qt-unstable/include/Qt \
  gcc main.cpp -o tutorial1 -I/home/kde-devel/qt-unstable/include/Qt \
  -I/home/kde-devel/qt-unstable/include/Qt-Core \
  -I/home/kde-devel/qt-unstable/include/Qt-Core \
  -I/home/kde-devel/qt-unstable/include -I/home/kde-devel/kde/include \
  -I/home/kde-devel/qt-unstable/include -I/home/kde-devel/kde/include \
Line 28: Line 28:
  -L/home/kde-devel/qt-unstable/lib -lkdeui -lkdecore -ldl  
  -L/home/kde-devel/qt-unstable/lib -lkdeui -lkdecore -ldl  
and then run it with  
and then run it with  
  ./hello
  ./tutorial1


If that worked, you may want to use cmake, just like the rest of KDE.
If that worked, you may want to use cmake, just like the rest of KDE.
Line 34: Line 34:
<code>
<code>
#name of the project
#name of the project
project (hello)
project (tutorial1)


#find and use the necessary libraries and headers
#find and use the necessary libraries and headers
Line 43: Line 43:
# using a variable is not really necessary:
# using a variable is not really necessary:
# using the single line  
# using the single line  
# kde4_add_executable(hello hello.cpp)
# kde4_add_executable(tutorial1 main.cpp)
# does the same, but when you add more stuff to the project,  
# does the same, but when you add more stuff to the project,  
# using the variable makes things a bit easier.
# using the variable makes things a bit easier.
set(helloSources hello.cpp)
set(tutorial1_SRCS hello.cpp)
kde4_add_executable(hello ${helloSources})
kde4_add_executable(tutorial1 ${helloSources})


# and link properly
# and link properly
target_link_libraries( hello ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS})
target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})
</code>
</code>


{{KDE4}}
{{KDE4}}

Revision as of 18:01, 3 January 2007

Your first program shall greet the world with a friendly "hello world", what else ? For that, we will use a KMessageBox. To get more information about the KMessageBox-Class, type "kde: kmessagebox" in your konqueror and it will redirect you to http://developer.kde.org/documentation/library/cvs-api/kdelibs-apidocs/kdeui/html/classKMessageBox.html

  1. include <QString>
  2. include <kapplication.h>
  3. include <kaboutdata.h>
  4. include <kmessagebox.h>
  5. include <kcmdlineargs.h>

int main (int argc, char *argv[]) {

 KAboutData aboutData( "tutorial1", "Tutorial 2",
     "1.0", "KMessageBox popup",
     KAboutData::License_GPL, "(c) 2006" );
 KCmdLineArgs::init( argc, argv, &aboutData );
 KApplication app;
 KGuiItem guiItem( QString( "Hello" ), QString(),
              QString( "this is a tooltip" ),
              QString( "this is whatsthis" ) );
KMessageBox::questionYesNo( 0, "text", "caption", guiItem );

} If you set up your environment as described in Build/Unstable Version, you can compile this code with

gcc main.cpp -o tutorial1 -I/home/kde-devel/qt-unstable/include/Qt \
-I/home/kde-devel/qt-unstable/include/Qt-Core \
-I/home/kde-devel/qt-unstable/include -I/home/kde-devel/kde/include \
-L/home/kde-devel/kde/lib \
-L/home/kde-devel/qt-unstable/lib -lkdeui -lkdecore -ldl 

and then run it with

./tutorial1

If that worked, you may want to use cmake, just like the rest of KDE. Create a file named CMakeLists.txt with this content:

  1. name of the project

project (tutorial1)

  1. find and use the necessary libraries and headers

find_package(KDE4 REQUIRED) include_directories( ${KDE4_INCLUDES} )

  1. compile
  2. using a variable is not really necessary:
  3. using the single line
  4. kde4_add_executable(tutorial1 main.cpp)
  5. does the same, but when you add more stuff to the project,
  6. using the variable makes things a bit easier.

set(tutorial1_SRCS hello.cpp) kde4_add_executable(tutorial1 ${helloSources})

  1. and link properly

target_link_libraries(tutorial1 ${KDE4_KDEUI_LIBS})

Tip
Note: This page is about KDE 4. It isn't applicable for KDE 3 development.