Next Previous Table of Contents
p2 is the p1 analogue using the KDE libraries, all the code is also in main.cpp :
#include <kapp.h> #include <klocale.h> #include <qpushbutton.h> int main( int argc, char **argv ) { KApplication a( argc, argv , "p2"); QPushButton *hello=new QPushButton( i18n("Hello World !"), 0 ); hello->setAutoResize( TRUE ); QObject::connect( hello, SIGNAL(clicked()), &a, SLOT(quit()) ); a.setMainWidget( hello ); hello->show(); return a.exec(); }
Let's analyze it.
KApplication a( argc, argv , "p2");
This code creates a KApplication object. KApplication is a class that inherits QApplication to add more features specific to the KDE libraries, such as support for locales (internationalization), application configuration, etc.
QPushButton *hello=new QPushButton( i18n("Hello World !"), 0 ); hello->setAutoResize( TRUE );
The difference between this code and the one we used in p1 is that now we don't use the "Hello World !" string directly, but the return value of i18n("Hello World !"), why is that ?. We do that because i18n is a function that returns the translated string to the language the user selected on the KControl language module. With this simple change, we have now support for more than 30 languages in which KDE is translated.
Because of that, we cannot use a fixed size for the button anymore, as we don't know the length of the translated string (and it wouldn't be nice if we don't show the complete string because the button is too small). By setting the autoResize flag, the button will change its size automatically whenever the contents change to be sure that all the text is correctly shown.
The rest of the application is the same as p1
Next Previous Table of Contents