Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Hello World Using QtScript

From 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
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition, Symbian^3, Maemo
Symbian
Article
Keywords: QtScript, QApplication, QPushButton, QMessageBox
Created: chintandave_er (05 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

Source

#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
#include <QtScript>
 
 
int main(int argc, char *argv[])
{
// load the script file to evaluate from a resource
Q_INIT_RESOURCE(helloscript);
 
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);
scriptFile.open(QIODevice::ReadOnly);
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();
 
 
// 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")
.arg(fileName)
.arg(result.property("lineNumber").toInt32())
.arg(result.toString()));
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';
button.show();


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.

 <RCC>
<qresource prefix="/" >
<file>helloscript.js</file>
</qresource>
</RCC>

References

This page was last modified on 11 October 2012, at 01:17.
113 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.

×