Development/Tutorials/Writing kontact plugins: Difference between revisions
Line 21: | Line 21: | ||
** use a Widget in it and use setWidget to get your widget into the KPart | ** use a Widget in it and use setWidget to get your widget into the KPart | ||
* write a subclass mypartmainwindow of KParts::MainWindow | * write a subclass mypartmainwindow of KParts::MainWindow | ||
** because [http://api.kde.org/4.x-api/kdelibs-apidocs/kparts/html/classKParts_1_1MainWindow.html#f96742c32da821dd7350deebc0a65382 createGUI] is protected | ** because [http://api.kde.org/4.x-api/kdelibs-apidocs/kparts/html/classKParts_1_1MainWindow.html#f96742c32da821dd7350deebc0a65382 createGUI] is protected (and you may want createGUI later) | ||
* write main.cpp | * write main.cpp | ||
** use new mypart(mw) to get your part into your main window | ** use new mypart(mw) to get your part into your main window | ||
Line 88: | Line 88: | ||
#include <KPushButton> | #include <KPushButton> | ||
#include "mypart.h" | #include "mypart.h" | ||
mypart::mypart(QObject* parent) | mypart::mypart(QObject* parent) | ||
Line 94: | Line 93: | ||
kDebug() << "Entering mypart"; | kDebug() << "Entering mypart"; | ||
QWidget* mywidget=new QWidget(); | QWidget* mywidget=new QWidget(); | ||
KPushButton* button=new KPushButton("ho",mywidget); | KPushButton* button=new KPushButton("ho",mywidget); | ||
setWidget(mywidget); | setWidget(mywidget); | ||
} | } | ||
mypartmainwindow::mypartmainwindow(QWidget* parent, Qt::WindowFlags f) | mypartmainwindow::mypartmainwindow(QWidget* parent, | ||
Qt::WindowFlags f) | |||
{ | { | ||
kDebug() << "Entering mypartmainwindow"; | kDebug() << "Entering mypartmainwindow"; |
Revision as of 12:01, 10 February 2008
Introduction
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. 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
Writing a KPart
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
CMakeLists.txt
PROJECT( kdepart ) FIND_PACKAGE(KDE4 REQUIRED) INCLUDE_DIRECTORIES(${KDE4_INCLUDES} .) SET(kdepartSources main.cpp mypart.cpp) KDE4_ADD_EXECUTABLE(kdepart ${kdepartSources}) TARGET_LINK_LIBRARIES(kdepart ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS})
main.cpp
- include <kaboutdata.h>
- include <kapplication.h>
- include <kcmdlineargs.h>
- include <klistwidget.h>
- include <kmessagebox.h>
- include <kparts/mainwindow.h>
- include <kparts/part.h>
- include <KPushButton>
- include <ktextedit.h>
- include "mypart.h"
- include <QString>
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();
mw->show();
khello.exec();
}
mypart.h
- include <KParts/Part>
- include <KParts/MainWindow>
class mypart:public KParts::Part
{
Q_OBJECT
public:
mypart(QObject *parent=0);
};
class mypartmainwindow:public KParts::MainWindow
{
Q_OBJECT
public:
mypartmainwindow(QWidget* parent = 0, Qt::WindowFlags f = KDE_DEFAULT_WINDOWFLAGS);
};
mypart.cpp
- include <kdebug.h>
- include <KPushButton>
- include "mypart.h"
mypart::mypart(QObject* parent)
{
kDebug() << "Entering mypart";
QWidget* mywidget=new QWidget();
KPushButton* button=new KPushButton("ho",mywidget);
setWidget(mywidget);
}
mypartmainwindow::mypartmainwindow(QWidget* parent,
Qt::WindowFlags f)
{
kDebug() << "Entering mypartmainwindow";
mypart* mypart1=new mypart(this);
mypart1->embed(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
See also
- http://pim.kde.org/development/tutorials/creatingplugins.php - tutorial for KDE 3
- http://api.kde.org/4.x-api/kdelibs-apidocs/kparts/html/classKParts_1_1MainWindow.html - the mainwindow where you display your kpart in