Development/Tutorials/Programming Tutorial KDE 4/Start your CMAKE project
If you disagree with its speedy deletion, remove the template and discuss it on its talk page.
Administrators: Please check the page history, especially the last diff, and What links here before deleting.
To start a KDE 4 project is more than starting a program - you have to deliver the build environment as well. This article shows you how to start a project with KDE 4 and CMake. As a first step, we create a simple but friendly program - it shows you a button and a label. If you click the button, the label greets you with "hello".
Contents |
[edit] Here we go
We will call our project kde4start. Set up your KDE 4 environment as described at Getting_Started/Build/KDE4. Create a directory kde4start and cd into it:
mkdir kde4start cd kde4start
Run designer to start the Qt designer, and chose to create a widget. You'll probably always want to create a widget. The other options in designer are for Qt only apps. Add a label called label and a pushButton called pushButton to the widget. Name the widget Kde4StartUi and save it as Kde4StartUi.ui
Create a Kde4Start.h file as so:
#ifndef KDE4START_H__ #define KDE4START_H__ #include <kmainwindow.h> class Ui_Kde4StartUi; class Kde4Start : public QWidget { Q_OBJECT public: Kde4Start(); public slots: void slotButtonClicked(); private: Ui_Kde4StartUi *mKde4StartUi; }; #endif
Create a Kde4Start.cpp as so:
#include "Kde4Start.h" #include "Kde4Start.moc" #include "ui_Kde4StartUi.h" Kde4Start::Kde4Start() : QWidget(NULL) { mKde4StartUi = new Ui_Kde4StartUi(); mKde4StartUi->setupUi(this); connect(mKde4StartUi->pushButton, SIGNAL(clicked()),SLOT(slotButtonClicked())); } void Kde4Start::slotButtonClicked() { mKde4StartUi->label->setText("Hello"); }
Write your main.cpp:
#include <QString> #include <kapplication.h> #include <kaboutdata.h> #include <kmessagebox.h> #include <kcmdlineargs.h> #include <KMainWindow> #include "Kde4Start.h" int main (int argc, char *argv[]) { KAboutData aboutData( "test", "test", "1.0", "test", KAboutData::License_GPL, "(c) 2006" ); KCmdLineArgs::init( argc, argv, &aboutData ); KApplication khello; Kde4Start *mw = new Kde4Start(); mw->show(); khello.exec(); }
Write your CMakeLists.txt:
PROJECT( kde4start )
FIND_PACKAGE(KDE4 REQUIRED)
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )
SET(kde4startSources main.cpp Kde4Start.cpp )
kde4_add_ui_files( kde4startSources Kde4StartUi.ui)
KDE4_ADD_EXECUTABLE(kde4start ${kde4startSources} )
TARGET_LINK_LIBRARIES(kde4start ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )
install(TARGETS kde4start DESTINATION ${BIN_INSTALL_DIR} )
[edit] Build it
To build and to run it do:
cmake . -DCMAKE_INSTALL_PREFIX=$KDEDIR \
-DCMAKE_BUILD_TYPE=debugfull && \
make && make install
./kde4start
[edit] TroubleShooting
You can find out the actual values cmake found by looking into CMakeCache.txt.
[edit] Suggested Readings
[edit] Thanks to
JohnFlux