Namespaces
Variants
Actions

How to use javascript with QScript in Qt

Jump to: navigation, search

This article demonstrates how to use simple JavaScript with your Qt application. The program in this article will create a button and the text of the button to display is set by the JavaScript file and again button will be shown by Qt application.

MultiMediaTile.png
Article Metadata

Article
Created: james1980 (30 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

Header File

No header file is used in this example. Complete code is written in the single file.

Source file

#include <QApplication>
 
#include <QPushButton>
#include <QtScript>
#include <QLabel>
 
 
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(hello);
QApplication app(argc, argv);
QScriptEngine engine;
QPushButton button;
 
QScriptValue scriptButton = engine.newQObject(&button);
engine.globalObject().setProperty("button", scriptButton);
 
QString fileName(":/hello.js");
QFile scriptFile(fileName);
scriptFile.open(QIODevice::ReadOnly);
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();
QScriptValue result = engine.evaluate(contents, fileName);
 
button.show();
return app.exec();
}


Script file

button.text = 'jajal mehul!';
button.styleSheet = 'color:red;font-style: italic';

Output

Scrpt.jpg

This page was last modified on 11 October 2012, at 04:17.
140 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved