Development/Tutorials/Kross/Call Functions in Kross

This tutorial shows how to deal with functions provided by a script. It builds upon the Kross Hello World tutorial.

This tutorial is based on the Hello World tutorial and extends the codebase we wrote there with new functionality to outline how calling scripting functions could be done.

Please note, that it is recommed to use Autoconnected Signals and Slots if you just like to have a bunch of scripting functions within a script that should be called on demand. That way you have a clear interface with signals that are mapped transparent to scripting functions. The "trick" here is, that Kross will automaticly connect each signal a QObject has to a matching scripting function and at the C++ side all what is needed is to emit a signal and behind the scene the arguments will be translated and a possible defined scripting function got called.


We are using cmake to build our small sample project. The CMakeLists.txt file looks like;

project (krosshello) find_package(KDE4 REQUIRED) include_directories( ${KDE4_INCLUDES} ) set(krosshello_SRCS main.cpp mainwindow.cpp) kde4_add_executable(krosshello ${krosshello_SRCS}) target_link_libraries(krosshello ${KDE4_KDEUI_LIBS} ${KDE4_KROSSUI_LIBS})


The main.cpp does create the sample application and shows the mainwindow instance.

  1. include <QString>
  2. include <KApplication>
  3. include <KAboutData>
  4. include <KMessageBox>
  5. include <KCmdLineArgs>
  6. include <KLocalizedString>
  7. include "mainwindow.h"

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

   // Used to store information about a program.
   KAboutData aboutData("krosshello",
       0,ki18n("Kross Hello World"),"1.0",

ki18n("Hello World application for Kross"), KAboutData::License_GPL, ki18n("(c) 2007"),ki18n("Some text..."), "","[email protected]");

   // Access to the command-line arguments.
   KCmdLineArgs::init( argc, argv, &aboutData );
   // Initialize the application.
   KApplication app;
   // Create and show the main window.
   MainWindow* window = new MainWindow();
   // Finally execute the application.
   return app.exec();



This is the main windows class which defines some displayed widgets for our small sample application.

  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <QComboBox>
  2. include <QLabel>
  3. include <QLineEdit>
  4. include <kross/core/action.h>

// The main window to display our combobox and the label. class MainWindow : public QWidget {

       MainWindow(QWidget *parent=0);
   private Q_SLOTS:
       // This slot is called when the item in the combobox is changed.
       void interpreterActivated(const QString &);
       QLineEdit* txtInputString;

QLabel* lblMessage; QComboBox* cmbInterpreters; Kross::Action* action; };

  1. endif


The implementation of the main window functionality. We are displaying a QLineEdit that will be passed as first argument to a scripting function named "reverseString". Those function returns a string that will then displayed in a QLabel. Just like at the Kross Hello World tutorial, we also display a QComboBox that does allow to choose an interpreter. Though this sample does provide only one sample script written in JavaScript to demonstrate the usage.

  1. include "mainwindow.h"
  2. include <QVBoxLayout>
  3. include <QDebug>
  4. include <kross/core/manager.h>
  5. include <kross/core/action.h>

// the constructor. MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {

   txtInputString = new QLineEdit();
   lblMessage = new QLabel("Hello");
   cmbInterpreters = new QComboBox ();
   cmbInterpreters->addItem("Choose Interpreter", "");
   foreach(QString s, Kross::Manager::self().interpreters())
   connect(cmbInterpreters, SIGNAL(activated(const QString &)),
           SLOT(interpreterActivated(const QString &)));
   QVBoxLayout *vLayout = new QVBoxLayout;
   // Create the Kross::Action instance and publish some
   // QObject instances.
   action = new Kross::Action(this, "MyScript");
   action->addObject(txtInputString, "MyInputString");
   action->addObject(cmbInterpreters, "MyInterpreter");
   action->addObject(lblMessage, "MyLabel");


// this slot is called when the active item of the combobox changes. void MainWindow::interpreterActivated(const QString &strSelectedInterpreter) {

   // this time we are using external script files.
   // at this sample we are only using JavaScript but
   // other supported backends will work well too.
   if(strSelectedInterpreter != "javascript") {
   // set the script file that should be executed
   // execute the scripting code, i.e. preload
   // please note, that it's needed to trigger aka
   // execute the script before calling a function
   // using callFunction() or before asking what
   // functions are available using functionNames().
   // also it's only needed to trigger the script
   // once and then call whatever functions should
   // be called or signals emitted or whatever.
   // The list of arguments passed to the function
   QVariantList args;
   // here we pass in the QLineEdit instance
   // as first argument
   QVariant v;
   v.setValue( (QWidget*) txtInputString );
   args << v;
   // and the second argument is a string
   args << "Hello World";
   // Call the arbitrary function
   QVariant result = action->callFunction("reverseString",args);
   // Use the returnvalue of the function call



The following JavaScript file does provide us the reverseString function. This function will be executed at the mainwindow.cpp file.

function reverseString(lineedit, s) {

   println("reverseString lineedit=" + lineedit + " s=" + s);
   lineedit.setText("Text to reverse: " +s);
   //MyInputString.text = "Text to reverse: " +s;
   return s.split("").reverse().join("");


This page was last edited on 31 May 2019, at 08:11. Content is available under Creative Commons License SA 4.0 unless otherwise noted.