Difference between revisions of "Development/Tutorials/Kross/Connecting Signals and slots in Kross"

Jump to: navigation, search
(Extended the TODO. I'll look at it within next days.)
(readded the "import MyInterpreter" line in the python-script. Seems the additional addObject() was just missing in the mainwindow.cpp sample.)
Line 76: Line 76:
   action->addObject(txtInputString, "MyInputString");
   action->addObject(txtInputString, "MyInputString");
  action->addObject(cmbInterpreter, "MyInterpreter");
   action->addObject(lblMessage, "MyLabel");
   action->addObject(lblMessage, "MyLabel");
Line 111: Line 112:
import MyLabel
import MyLabel
import MyInterpreter
import MyInputString
import MyInputString

Revision as of 23:03, 10 October 2007

Hello world in kross
Tutorial Series   Kross tutorials
Previous   Kross Hello World
What's Next   Scripts as plugins
Further Reading   n/a

Under Construction
This page is under construction. This page is actively being developed and updated with new information, and may be incomplete. You can help by editing this page

This tutorial shows how to use a system of signals and slots to provide a scripting interface for a KDE application. It builds upon the Kross Hello World tutorial and again follows a 'Hello World' type format.

Update source files


Edit mainwindow.h to handle the changes to mainwindow.cpp. Note that the change is the addition of a private Kross::Action .

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

class MainWindow : public QWidget {

   MainWindow(QWidget *parent=0);
 private Q_SLOTS:
   void interpreterActivated(const QString &);
   QLineEdit* txtInputString;
   QLabel* lblMessage;
   QComboBox* cmbInterpreters;
   Kross::Action* action;


  1. endif


Using the same CmakeLists.txt and main.cpp from the previous tutorial, edit the mainwindow.cpp as follows:

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

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;
 action = new Kross::Action(this, "MyScript");
 action->addObject(txtInputString, "MyInputString");
 action->addObject(cmbInterpreter, "MyInterpreter");
 action->addObject(lblMessage, "MyLabel");


void MainWindow::interpreterActivated(const QString &strSelectedInterpreter) {

 if(strSelectedInterpreter.isEmpty()) {
 QString filename;
 if(strSelectedInterpreter == "python")
   filename = "krossSigsSlots.py";
 else if(strSelectedInterpreter == "javascript")
   filename = "krossSigsSlots.js";


The changes are addition of a text entry field and the reorganisation of logic handling the Kross::Action. Note also that there is no code in mainwindow.cpp to set the label, as there was in the previous tutorial. Instead, Objects are simply made available to the scripting interface through the action->addObject calls, without any knowledge of what the script will use them for. This removes the need to know at the time of writing the application what function the scripts will perform, and is therefore suited to a plugin interface.


This script catches the textChanged SIGNAL of the QLineEdit, and connects it to a simple python function to reverse the string in the QLineEdit and display it in the QLabel.

  1. !/usr/bin/env kross

import MyLabel import MyInterpreter import MyInputString

def reverseString(s):

 s = s[::-1]
 MyLabel.text = s

MyInputString.connect("textChanged(const QString &)", reverseString)


This script does the same like the script above but using the JavaScript scripting language.

  1. !/usr/bin/env kross

function reverseString(s){

   return s.split("").reverse().join("");


connect(MyInputString, "textChanged(const QString &)", this, "reverseString(const QString &)");

Autoconnecting Signals and Slots

It is possible to connect signals from your Object to slots in your script file automatically. Edit the mainwindow.cpp file to add another argument to the addObject function:

 action->addObject(txtInputString, "MyInputString",
 action->addObject(cmbInterpreter, "MyInterpreter");
 action->addObject(lblMessage, "MyLabel");

The Kross::ChildrenInterface::AutoConnectSignals argument causes signals of the object to be automatically connected with scripting functions of the same name. Therefore, the scripts can again be simplified:

Simplified scripts

The python function reverses the string:

  1. !/usr/bin/env kross

import MyLabel

def textChanged(s):

 s = s[::-1]
 MyLabel.text = s

The javascript function converts the string to pig latin:

function textChanged(text) {

 text = text.replace(/\b([aeiou][a-z]*)\b/gi, "$1way"); // Rule 2
 pigLatin = text.replace(/\b([bcdfghjklmnpqrstvwxy]+)([a-z]*)\b/gi, "$2$1ay"); // Rule 1
 MyLabel.text = pigLatin;


Stop this crashing when the user makes a second selection from the combo box. Note that this seems to be yet another bug in KjsEmbed and the reason for the crash is, that the signal does not got disconnected correct. That's the case cause the KJSEmbed::SlotProxy never got destroyed.

Content is available under Creative Commons License SA 4.0 unless otherwise noted.