Development/Tutorials/Kross/Call Functions in Kross

From KDE TechBase
Revision as of 21:29, 16 January 2008 by Dipesh (talk | contribs) (New Kross Tutorial)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

The source files

CMakeList.txt

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})

main.cpp

  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..."), "http://kross.dipe.org", "[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(); window->show();

// Finally execute the application. return app.exec(); }

mainwindow.h

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

// The main window to display our combobox and the label. class MainWindow : public QWidget { Q_OBJECT public: // The constructor. MainWindow(QWidget *parent=0); private Q_SLOTS: // This slot is called when the item in the combobox is changed. void interpreterActivated(const QString &); private: QLineEdit* txtInputString; QLabel* lblMessage; QComboBox* cmbInterpreters; // We now have the action as class-member. Kross::Action* action; };

  1. endif

mainwindow.cpp

  1. include "mw.h"
  1. include <QVBoxLayout>
  2. include <QDebug>
  1. include <kross/core/manager.h>
  2. 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()) cmbInterpreters->addItem(s);

connect(cmbInterpreters, SIGNAL(activated(const QString &)), SLOT(interpreterActivated(const QString &)));

QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(cmbInterpreters); vLayout->addWidget(txtInputString); vLayout->addWidget(lblMessage); setLayout(vLayout);

// This time we create the Kross::Action already within the // constructor and add the objects that should be accessible // from within scripting code. 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) { if(strSelectedInterpreter.isEmpty()) { lblMessage->setText("-"); return; }

// this time we are using external script files. QString filename; if(strSelectedInterpreter == "python") filename = "krossSigsSlots.py"; else if(strSelectedInterpreter == "javascript") filename = "krossSigsSlots.js"; else return;

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

// finally execute the scripting code, i.e. preload action->trigger();

// now we can call arbitrary functions QVariantList arguments;

QVariant v; v.setValue( (QWidget*) txtInputString ); arguments << v;

arguments << "Hello World"; QVariant result = action->callFunction("reverseString", arguments); lblMessage->setText(result.toString()); }