Contents

Introduction

This tutorial provides a step-by-step introduction on how to integrate scripts as plugins into a C++/Qt/KDE application. This way, an application can be extended with plugins written in scripting languages such as Python, Ruby and KDE JavaScript.

The C++ code

The following C++ code demonstrates how to execute scripting code in C++ and how to let scripting code deal with QObject instances.

For this we define the MyObject class that implements some signals and slots we will access from within scripting code. Scripts are able to access such QObject's as there are classinstances, call slots as they are memberfunctions, get and set properties as there are membervariables and connect scripting functions with signals.

// The Constructor does set up some stuff.
MyObject(QObject* parent) : QObject(parent) {
// Create the QTimer instance we will us to emit
// the update() signal with the defined interval.
m_timer = new QTimer(this);

// Create the script container. myobject is the parent QObject,
// so that our action instance will be destroyed once the myobject
// is destroyed.
Kross::Action* action = new Kross::Action(myobject, file);

Then the script file that should be executed is set. The used interpreter will be determinated by the file-extension like e.g. *.py for Python or or *.rb for Ruby. You are also able to set the interpreter explicit with e.g. action->setInterpreter("python") or action->setInterpreter("ruby"). You are also able to use action->setCode("print 'hello world'") to set the scripting code direct.

Finally the script is executed. This is done by triggering the action. Once executed you are also able to use Kross::ErrorInterface to check if the action was executed successfully like demonstrate below.

if( action->hadError() )
kDebug() << action->errorMessage() << endl;

The Kross::Manager provides also the option to connect with the started and finished signals that got emitted if a script got executed.
connect(&Kross::Manager::self(), SIGNAL( started(Kross::Action*) ),

The Python scripting code

The following Python script demonstrates how a Python plugin looks like. The init() and the update() functions will be called if the matching signals at the myobject instance are emitted.

First we import myobject module. This module provides us access to the MyObject QObject instance and it's slots, signals and properties. Within the init() Python function we set the interval to 2000 milliseconds = 2 seconds. Then we start the QTimer of our MyObject instance by calling it's start() slot. Then each 2 seconds the update() signal will be emitted what in turn calls our update() Python function.

import the published MyObject instance.

import myobject

this function got called if the init(QTimer*,int)

signal got emitted.

def init(timer, interval):

# the following line uses the passed QTimer instance
# and set's the interval Q_PROPERTY to the as argument
# passed interval integer.
timer.interval = interval

# the following lines does basicly the same as above,
# that is to set the interval the QTimer should use
# to call our update() signal. But compared to the
# line above we are doing it by using the setInterval
# slot the published MyObject instance provides us.
myobject.setInterval(2000)

The Ruby scripting code

The following Ruby script does the same as the Python scripting code above except by using the Ruby scripting language. This shows, that the same rich API functionality is accessible independend of the used scripting language.

import the published MyObject instance.

require 'myobject'

this function got called if the init(QTimer*,int)

signal got emitted.

def init(timer, interval)

# the following line uses the passed QTimer instance
# and set's the interval Q_PROPERTY to the as argument
# passed interval integer.
timer.interval = interval

# the following lines does basicly the same as above,
# that is to set the interval the QTimer should use
# to call our update() signal. But compared to the
# line above we are doing it by using the setInterval
# slot the published MyObject instance provides us.
Myobject.setInterval(2000)

The JavaScript scripting code

The following JavaScript script does the same as the Python and the Ruby scripting code above but uses Kjs+KjsEmbed (both included in kdelibs). So, the same rich API functionality is also accessible from within the JavaScript language.

// the following line uses the passed QTimer instance
// and set's the interval Q_PROPERTY to the as argument
// passed interval integer.
timer.interval = interval;

// the following lines does basicly the same as above,
// that is to set the interval the QTimer should use
// to call our update() signal. But compared to the
// line above we are doing it by using the setInterval
// slot the published MyObject instance provides us.
myobject.setInterval(2000);