This tutorial will show you how to use a KPart in your application, and how to create your own KPart.

This tutorial will show you how to use a KPart in your application, and how to create your own KPart.

+

+

KParts consist of a .desktop file describing the KPart and a .so file containing the function names and code. The .desktop file resides in KDE's services directory, you can find it out like this:

+

# kde4-config --path services

+

/root/.kde/share/kde4/services/:/usr/local/share/kde4/services/

+

+

The first kPart we describe is KatePart, an editor representing a typical [http://api.kde.org/4.0-api/kdelibs-apidocs/kparts/html/classKParts_1_1ReadWritePart.html ReadWritePart]. A typical [http://api.kde.org/4.0-api/kdelibs-apidocs/kparts/html/classKParts_1_1ReadOnlyPart.html ReadOnlyPart] would be [http://api.kde.org/4.5-api/kdebase-apps-apidocs/konsole/html/classKonsole_1_1Part.html konsolePart].

== Using Katepart ==

== Using Katepart ==

Line 27:

Line 33:

=== main.cpp ===

=== main.cpp ===

−

<code cppqt>

+

<syntaxhighlight lang="cpp-qt">

#include <KApplication>

#include <KApplication>

#include <KAboutData>

#include <KAboutData>

Line 37:

Line 43:

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

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

{

{

−

KAboutData aboutData( "kparttutorial1", "kparttutorial1",

+

KAboutData aboutData( "kparttut1", "kparttut1",

ki18n("KPart Tutorial 1"), "0.1",

ki18n("KPart Tutorial 1"), "0.1",

ki18n("A MainWindow for a KatePart."),

ki18n("A MainWindow for a KatePart."),

Line 62:

Line 68:

}

}

−

</code>

+

</syntaxhighlight>

The main.cpp file is the same as that used in [[Development/Tutorials/KCmdLineArgs|Tutorial 5 - Command line arguments]].The only difference is in the details of the KAboutData.

The main.cpp file is the same as that used in [[Development/Tutorials/KCmdLineArgs|Tutorial 5 - Command line arguments]].The only difference is in the details of the KAboutData.

Line 68:

Line 74:

=== mainwindow.h ===

=== mainwindow.h ===

−

<code cppqt>

+

<syntaxhighlight lang="cpp-qt">

#ifndef KPARTTUTORIAL1_H

#ifndef KPARTTUTORIAL1_H

Line 97:

Line 103:

virtual ~MainWindow();

virtual ~MainWindow();

+

public slots:

/**

/**

* Use this method to load whatever file/URL you have

* Use this method to load whatever file/URL you have

Line 117:

Line 124:

#endif // KPARTTUT1_H

#endif // KPARTTUT1_H

−

</code>

+

</syntaxhighlight>

The mainwindow.h file is very simple. The important thing to notice here is that the MainWindow class inherits from KParts::MainWindow.

The mainwindow.h file is very simple. The important thing to notice here is that the MainWindow class inherits from KParts::MainWindow.

Line 123:

Line 130:

=== mainwindow.cpp ===

=== mainwindow.cpp ===

−

<code cppqt>

+

<syntaxhighlight lang="cpp-qt">

#include "mainwindow.h"

#include "mainwindow.h"

Line 151:

Line 158:

//query the .desktop file to load the requested Part

//query the .desktop file to load the requested Part

KService::Ptr service = KService::serviceByDesktopPath

KService::Ptr service = KService::serviceByDesktopPath

−

("katepart.desktop");

+

("katepart");

if (service)

if (service)

Line 170:

Line 177:

else

else

{

{

−

return 1;

+

return;//return 1;

}

}

}

}

Line 177:

Line 184:

// if we couldn't find our Part, we exit since the Shell by

// if we couldn't find our Part, we exit since the Shell by

// itself can't do anything useful

// itself can't do anything useful

−

KMessageBox::error(this, "service kwordpart.desktop not found");

+

KMessageBox::error(this, "service katepart not found");

qApp->quit();

qApp->quit();

// we return here, cause qApp->quit() only means "exit the

// we return here, cause qApp->quit() only means "exit the

Line 196:

Line 203:

void MainWindow::setupActions()

void MainWindow::setupActions()

{

{

−

KStandardAction::open(this, SLOT(fileOpen()),

+

KStandardAction::open(this, SLOT(load()),

actionCollection());

actionCollection());

KStandardAction::quit(qApp, SLOT(closeAllWindows()),

KStandardAction::quit(qApp, SLOT(closeAllWindows()),

Line 207:

Line 214:

}

}

−

</code>

+

</syntaxhighlight>

The mainwindow.cpp file contains the implementation of MainWindow. The constructor of this class contains all the code used to load the KPart.

The mainwindow.cpp file contains the implementation of MainWindow. The constructor of this class contains all the code used to load the KPart.

Line 213:

Line 220:

First it sets up the actions used by the main window (Open and Quit), and then sets up the gui elements to go with these items (toolbar items, menu items, keyboard shortcuts). Next is the standard code used to load the KPart. The createGUI method is responsible for merging the toolbars and menus of the KPart with the rest of the application.

First it sets up the actions used by the main window (Open and Quit), and then sets up the gui elements to go with these items (toolbar items, menu items, keyboard shortcuts). Next is the standard code used to load the KPart. The createGUI method is responsible for merging the toolbars and menus of the KPart with the rest of the application.

−

Note: although the part can also be loaded using KLibLoader, this method is depricated.

+

Note: although the part can also be loaded using KPluginLoader, this method is depricated.

−

KLibFactory *factory = KLibLoader::self()->factory("katepart");

+

KPluginFactory* factory = KPluginLoader("katepart").factory();

=== kparttut1ui.rc ===

=== kparttut1ui.rc ===

−

<code xml>

+

<syntaxhighlight lang="xml">

<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">

<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">

<kpartgui name="kparttut1" version="1">

<kpartgui name="kparttut1" version="1">

Line 238:

Line 245:

</ToolBar>

</ToolBar>

</kpartgui>

</kpartgui>

−

</code>

+

</syntaxhighlight>

The kparttut1ui.rc file is used to define how the actions in the part and the actions in the main window will be merged together. The <tt><Merge /></tt> element in the file menu for example indicates that any part containing actions in a file menu should list its parts after the file_open action and before the file_quit action.

The kparttut1ui.rc file is used to define how the actions in the part and the actions in the main window will be merged together. The <tt><Merge /></tt> element in the file menu for example indicates that any part containing actions in a file menu should list its parts after the file_open action and before the file_quit action.

Contents

Introduction

KPart technology is used in kde to reuse GUI components. The advantage that a KPart presents is that it comes with predefined toolbar actions. By using kparts in applications developers can spend less time implementing text editor or command line features, for example and just use a katepart or a konsolepart instead. KParts are also used with Plugin technology to embed applications inside another, such as integrating PIM applications into Kontact.

This tutorial will show you how to use a KPart in your application, and how to create your own KPart.

KParts consist of a .desktop file describing the KPart and a .so file containing the function names and code. The .desktop file resides in KDE's services directory, you can find it out like this:

Using Katepart

Simple KDE applications use a MainWindow derived from KMainWindow (such as in previous tutorials). To use a KPart in an application, the MainWindow must instead be derived from KParts::MainWindow. This will then take care of integrating the toolbar and menu items of the kpart together.

The following code creates a KParts::MainWindow with a kpart inside it.

The mainwindow.cpp file contains the implementation of MainWindow. The constructor of this class contains all the code used to load the KPart.

First it sets up the actions used by the main window (Open and Quit), and then sets up the gui elements to go with these items (toolbar items, menu items, keyboard shortcuts). Next is the standard code used to load the KPart. The createGUI method is responsible for merging the toolbars and menus of the KPart with the rest of the application.

Note: although the part can also be loaded using KPluginLoader, this method is depricated.

The kparttut1ui.rc file is used to define how the actions in the part and the actions in the main window will be merged together. The <Merge /> element in the file menu for example indicates that any part containing actions in a file menu should list its parts after the file_open action and before the file_quit action.