Development/Tutorials/Writing kontact plugins

< Development‎ | Tutorials
Revision as of 12:29, 10 February 2008 by Tstaerk (Talk | contribs) (Writing a KPart)

Jump to: navigation, search


Kontact is a KDE PIM (personal information management) framework that allows embedding several pim applications like kmail and korganizer as plugins into one window. Kontact plugins are KParts that get loaded at runtime. So, this is a good time to learn how to write a kPart... Read on.

Writing a KPart

We will write a "hello world"-KPart here. And we will write a MainWindow to hold this KPart. A KPart also gives you the flexibility to be integrated into kontact or konqueror, but we will do this later. So, the "hello world"-KPart only consists of a pushbutton labeled "hello world". We chose a pushbutton because it is the easiest viewable thing to create. It does not trigger an action on your click. The following is what you do

  • write a subclass mypart of KParts::Part
    • because setWidget is protected
    • use a Widget in it and use setWidget to get your widget into the KPart
  • write a subclass mypartmainwindow of KParts::MainWindow
    • because createGUI is protected (and you may want createGUI later)
  • write main.cpp
    • use new mypart(mw) to get your part into your main window

We will use 4 files to do this:

  • CMakeLists.txt to ease building work
  • main.cpp to show our MainWindow
  • mypart.h to declare our classes
  • mypart.cpp to implement our classes


PROJECT( kdepart )

SET(kdepartSources main.cpp mypart.cpp)

KDE4_ADD_EXECUTABLE(kdepart ${kdepartSources})



  1. include <kapplication.h>
  2. include <kaboutdata.h>
  3. include <kparts/mainwindow.h>
  4. include <kparts/part.h>
  5. include <kcmdlineargs.h>
  6. include "mypart.h"

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

 const QByteArray& ba=QByteArray("test");
 const KLocalizedString name=ki18n("myName");
 KAboutData aboutData( ba, ba, name, ba, name);
 KCmdLineArgs::init( argc, argv, &aboutData );
 KApplication khello;
 mypartmainwindow* mw=new mypartmainwindow();



  1. include <KParts/Part>
  2. include <KParts/MainWindow>

class mypart:public KParts::Part {



 mypart(QObject *parent=0);


class mypartmainwindow:public KParts::MainWindow {



 mypartmainwindow(QWidget* parent=0,
                  Qt::WindowFlags f=KDE_DEFAULT_WINDOWFLAGS);



  1. include <kdebug.h>
  2. include <KPushButton>
  3. include "mypart.h"

mypart::mypart(QObject* parent) {

 kDebug() << "Entering mypart";
 QWidget* mywidget=new QWidget();
 new KPushButton("hello world",mywidget);  


mypartmainwindow::mypartmainwindow(QWidget* parent,

                                  Qt::WindowFlags f)


 kDebug() << "Entering mypartmainwindow";
 mypart* mypart1=new mypart(this);


Compiling your kPart

To compile, link and run the code above, use

cmake . && make && ./kdepart

Using your kPart

You can use konqueror to display your kpart: konqueror -> Settings -> Configure Konqueror -> Web browsing -> Plugins

Kontact integration

To integrate your KPart into kontact, you will need to have a .desktop file like this:

ls /home/kde-devel/kde/share/kde4/services/kontact/
akregatorplugin.desktop     knotesplugin.desktop
akregatorplugin3.2.desktop  korganizerplugin.desktop
journalplugin.desktop       newstickerplugin.desktop
kaddressbookplugin.desktop  plannerplugin.desktop
karmplugin.desktop          specialdatesplugin.desktop
kmailplugin.desktop         summaryplugin.desktop
kmobiletools.desktop        todoplugin.desktop
knodeplugin.desktop         weatherplugin.desktop

See also

Content is available under Creative Commons License SA 4.0 unless otherwise noted.