main.cpp Example File
script/helloscript/main.cpp
#include <QApplication>
#include <QFile>
#include <QMessageBox>
#include <QPushButton>
#include <QTextStream>
#include <QTranslator>
#include <QtScript>
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(helloscript);
QApplication app(argc, argv);
QScriptEngine engine;
QTranslator translator;
translator.load("helloscript_la");
app.installTranslator(&translator);
engine.installTranslatorFunctions();
QPushButton button;
QScriptValue scriptButton = engine.newQObject(&button);
engine.globalObject().setProperty("button", scriptButton);
QString fileName(":/helloscript.js");
QFile scriptFile(fileName);
scriptFile.open(QIODevice::ReadOnly);
QTextStream stream(&scriptFile);
QString contents = stream.readAll();
scriptFile.close();
QScriptValue result = engine.evaluate(contents, fileName);
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;
}
return app.exec();
}