Hello World Using QtScript

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to use QtScript through a simple "Helloworld" example. It demonstrates how to embed a script engine into the application, how to evaluate a script, and how to process the result of the evaluation.

Article Metadata
Tested with
Devices(s): Emulator
Platform(s): S60 3rd Edition, S60 5th Edition, Symbian^3, Maemo
Keywords: QtScript, QApplication, QPushButton, QMessageBox
Created: chintandave_er (05 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)



#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
#include <QtScript>
int main(int argc, char *argv[])
// load the script file to evaluate from a resource
QApplication app(argc, argv);
QScriptEngine engine;
// push button is created and exported to the script environment as a global variable, button.
// Scripts will be able to access properties, signals and slots of the button as properties of the button script object
QPushButton button;
QScriptValue scriptButton = engine.newQObject(&button);
engine.globalObject().setProperty("button", scriptButton);
// read the contents of the script file
QString fileName(":/helloscript.js");
QFile scriptFile(fileName);
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
// script evaluated.
QScriptValue result = engine.evaluate(contents, fileName);
// If any Error, Display line number and error in a message box.
if (result.isError()) {
QMessageBox::critical(0, "Hello Script",
QString::fromLatin1("%0:%1: %2")
return -1;
// App Event loop is entered if everything went well
return app.exec();

Script File (helloscript.js)

// set the text and stylesheet of button in script
button.text = 'Hello World!';
button.styleSheet = 'font-style: italic';

Project file (helloscript.pro)

You need following linkage in your .pro file.

QT += script
RESOURCES += helloscript.qrc
SOURCES += main.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/script/helloscript
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS helloscript.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/script/helloscript
INSTALLS += target sources
symbian: include($$PWD/../../symbianpkgrules.pri)

Resource file (helloscript.qrc)

Add helloscript.js file in Resource file.

<qresource prefix="/" >


This page was last modified on 11 October 2012, at 01:17.
87 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.