(→Using your kPart) |
m (→Compiling your kPart: correct typo) |
||
| Line 104: | Line 104: | ||
</code> | </code> | ||
| − | = Compiling your | + | = Compiling your KPart = |
To compile, link and run the code above, use | To compile, link and run the code above, use | ||
Contents |
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.
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
We will use 4 files to do this:
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})
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();
}
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::mypart(QObject* parent) {
kDebug() << "Entering mypart";
QWidget* mywidget=new QWidget();
new KPushButton("hello world",mywidget);
setWidget(mywidget);
}
mypartmainwindow::mypartmainwindow(QWidget* parent,
Qt::WindowFlags f)
{
kDebug() << "Entering mypartmainwindow"; mypart* mypart1=new mypart(this); mypart1->embed(this);
}
To compile, link and run the code above, use
cmake . && make && ./kdepart
To use your KPart, we need an object file, ending in .so. So, let's modify CMakeLists.txt a bit:
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})
set(kdepartplugin_PART_SRCS ${kdepartSources})
kde4_add_plugin(kdepartplugin ${kdepartSources})
target_link_libraries(kdepartplugin ${KDE4_KDEUI_LIBS}
${KDE4_KPARTS_LIBS} )
You can use konqueror to display your kpart: konqueror -> Settings -> Configure Konqueror -> Web browsing -> Plugins
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