PDA

View Full Version : How to send an object to Javascript function using QScriptEngine



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

marcel
14th October 2008, 18:40
I'm not sure if I understand correctly what you're trying to do, but take a look at these paragraphs: http://doc.trolltech.com/4.3/qtscript.html#making-a-c-object-available-to-scripts-written-in-qt-script .

If you want a method of your object to be available in the script then you need to declare it with Q_INVOKABLE, just as in the example.

parusri
14th October 2008, 20:56
Thanks for the information.

But the class member functions should be available in the javascript functions like

ex:


function Add(myObj)
{
myObj.display();
}

In the above myObj is the C++ object passed from QScriptEngine.
ex:

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();


C++ class:
=======

class Sample: public QObject
{
public:
int data;
Sample(){
data = 100;
}
int display()
{
return data;
}
};

marcel
14th October 2008, 21:04
Yes, and in order to make a member function available in QtScript, you must either declare it as a slot or with Q_INVOKABLE in your QObject subclass.
Something like this:



class Sample: public QObject
{

Q_OBJECT

public:
int data;
Sample(){
data = 100;
}
Q_INVOKABLE int display()
{
return data;
}

public slots:
int display2()
{
return data;
}
};


Now you should be able to call both display and display2 in a script.