If you have already set up your environment as described in [[Getting Started/Build/KDE4]], you can already use kross with the javascript language. You can choose optionally to install support for python and ruby from kdebindings. Either checkout and build kdebindings, or just the kdebindings/python and kdebindings/ruby subdirectories ([[Getting_Started/Build/KDE4#Installing_a_subset_of_a_module|Installing_a_subset_of_a_module]]).

+

If you have already set up your environment as described in [[Getting Started/Build]], you can already use kross with the javascript language. You can choose optionally to install support for python and ruby from kdebindings. Either checkout and build kdebindings, or just the kdebindings/python and kdebindings/ruby subdirectories ([[Getting_Started/Build/KDE4#Installing_a_subset_of_a_module|Installing_a_subset_of_a_module]]).

Contents

Additional Bindings

If you have already set up your environment as described in Getting Started/Build, you can already use kross with the javascript language. You can choose optionally to install support for python and ruby from kdebindings. Either checkout and build kdebindings, or just the kdebindings/python and kdebindings/ruby subdirectories (Installing_a_subset_of_a_module).

Hello World

In this tutorial a simple dialog is created which contains a drop-down list and a label. When an interpreter is selected from the list, some scripting code is executed and the label text is updated in the script.

Create a krosshello folder in the kde-devel home directory (or choose another location). Create the following files and run cmakekde:

mainwindow.cpp

This code creates a simple dialog with a combobox showing available interpreters along with a label for displaying a message. The kross/core/manager.h and kross/core/action.h are included to provide kross functionality, which is invoked when a selection is made on the combobox. The code below makes the lblHello label available to scripts as a MyLabel object, and executes different code depending on the interpreter chosen.

// Create the combobox where we display a list of
// available interpreters.
cmbInterpreters = new QComboBox (this);
cmbInterpreters->addItem("Choose Interpreter", "");
// Now let's add the interpreters. Please note, that all
// interpreters that are installed are displayed in the list.
// Per default JavaScript will be always
// available while Python, Ruby or other interpreters
// may need to be installed before like explained at the
// "Additional Bindings" section above.
foreach(QString s, Kross::Manager::self().interpreters())
cmbInterpreters->addItem(s);

// Connect the combobox signal with our slot to be able to
// do something if the active item in the combobox changed.
connect(cmbInterpreters, SIGNAL(activated(const QString &)),
this, SLOT(interpreterActivated(const QString &)));

// The label we want to manipulate from within scripting
// code.
lblHello = new QLabel("Hello", this);

// Put everything into a layout to have it shown in a
// nice way.
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(cmbInterpreters);
layout->addWidget(lblHello);
setLayout(layout);

}

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

// Now let's create a Kross::Action instance which will act
// as container for our script. You are also able to cache
// that action, manipulate it on demand or execute it multiple
// times.
Kross::Action action(this, "MyScript");
// Now let's set the scripting code that should be executed
// depending on the choosen interpreter. You are also able to
// use action.setFile("/path/scriptfile") here to execute
// an external scriptfile, as shown later in this tutorial.
if(strSelectedInterpreter == "python")
action.setCode("import MyLabel\nMyLabel.text = 'Hello from python!'");
else if(strSelectedInterpreter == "ruby")
action.setCode("require 'MyLabel'\nMyLabel.text = 'Hello from ruby!'");
else if(strSelectedInterpreter == "javascript")
action.setCode("MyLabel.setText('Hello from javascript!')");
else
return;

// Set the name of the interpreter that should be used to
// evaluate the scripting code above. It's not needed to set
// it explicitly if we defined an external scripting file via
// action.setFile() since then Kross will determinate the right
// one. But since we set the scripting code above manually by using
// action.setCode() we need to define explicitly what interpreter
// should be used.
action.setInterpreter(strSelectedInterpreter);

Using separate script files

The next step is to extract the scripts into separate files. This has the obvious advantage of being editable without being recompiled. Edit the MainWindow::interpreterActivated in mainwindow.cpp to the following: