parusri
14th October 2008, 17:47
Hi,
I have a question about QScriptEngine, how to pass an object from QT to Javascript function.
And in Javascript i should be able to call the C++ function.
Ex:
#include <QtGui/QApplication>
#include "QtScript/qscriptengine.h"
#include <QFile>
class Sample: public QObject
{
public:
int data;
Sample(){
data = 100;
}
int display()
{
return 100;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QScriptEngine engine;
QFile scriptFile("C:\\Users\\paruchsr\\Desktop\\Test\\test.js");
scriptFile.open(QIODevice::ReadOnly);
QString str =scriptFile.readAll();
engine.evaluate(str);
scriptFile.close();
//Sample *obj = new Sample();
QObject *obj = new Sample();
QScriptValue ctor = engine.evaluate("Add");
qint16 val = ctor.toInteger();
QString str1 = ctor.toString();
QScriptValue scriptUi = engine.newQObject(obj, QScriptEngine::ScriptOwnership);
str = scriptUi.toString();
QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
str = calc.toString();
// obj->show();
return a.exec();
}
Javascript Function:
==============
function Add(test)
{
var a = test.display();
return a;
}
I could able to return some data from javascript function without using an object.But my requirement is to pass an object to javascript function and able to call function in javascript itself.
Actually there was an example of Calculator, i have followed similarly. Here i don't hany any user interface. I am susupecting that the problem with newQObject funtion. I am feeling that newQObject function is not constructing an object properly like in Calculator.
Thanks in Advance and i really appreciate your help.
Regards
Srinivas
I have a question about QScriptEngine, how to pass an object from QT to Javascript function.
And in Javascript i should be able to call the C++ function.
Ex:
#include <QtGui/QApplication>
#include "QtScript/qscriptengine.h"
#include <QFile>
class Sample: public QObject
{
public:
int data;
Sample(){
data = 100;
}
int display()
{
return 100;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QScriptEngine engine;
QFile scriptFile("C:\\Users\\paruchsr\\Desktop\\Test\\test.js");
scriptFile.open(QIODevice::ReadOnly);
QString str =scriptFile.readAll();
engine.evaluate(str);
scriptFile.close();
//Sample *obj = new Sample();
QObject *obj = new Sample();
QScriptValue ctor = engine.evaluate("Add");
qint16 val = ctor.toInteger();
QString str1 = ctor.toString();
QScriptValue scriptUi = engine.newQObject(obj, QScriptEngine::ScriptOwnership);
str = scriptUi.toString();
QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
str = calc.toString();
// obj->show();
return a.exec();
}
Javascript Function:
==============
function Add(test)
{
var a = test.display();
return a;
}
I could able to return some data from javascript function without using an object.But my requirement is to pass an object to javascript function and able to call function in javascript itself.
Actually there was an example of Calculator, i have followed similarly. Here i don't hany any user interface. I am susupecting that the problem with newQObject funtion. I am feeling that newQObject function is not constructing an object properly like in Calculator.
Thanks in Advance and i really appreciate your help.
Regards
Srinivas