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 [[Development/Tutorials/Kross/Hello World|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.

+

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 [[Development/Tutorials/Kross/Hello World|Kross Hello World]] tutorial, we also display a QComboBox that does allow to choose an interpreter.

<code cpp>

<code cpp>

Revision as of 23:19, 17 January 2008

This tutorial shows how to deal with functions provided by a script. 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. A good example here is SuperKaramba which uses the signal+slot rather then callFunction() way. All it defines is the rather huge class that inherits QObject (karambainterface.h). The signals within that class are transparent mapped to scripting functions (e.g. template.py and template.js) while the slots are then callable from within the scripting code.

mainwindow.cpp

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.

// set the script file that should be executed
action->setFile(filename);

// 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.
action->trigger();

// Use the returnvalue of the function call
lblMessage->setText(result.toString());

}

The Script Files

We are then able to use supported scripting backends like Python, Ruby and JavaScript to execute a scripting function named "reverseString" and called in the mainwindow.cpp file. The "reverseString" function has a QLineEdit and a QString as arguments and returns a QString.

Testscriptfile.js

The Testscriptfile.js JavaScript file does provide us the reverseString scripting function.