PDA

View Full Version : Qt script combined with java script



prachi kamble
16th April 2015, 08:02
hi,
anyone knows how to pass Qt classes to java script through qt script engine?

I am writing a Qt application where i have used Qt Script engine to evaluate java script.and to run functions from inside java script.
in java script i want to create a tcp client socket. Is it possible to pass Qtcpsocket class to java script through qt script engine in any way ? or is there any other way to accomplish this task?

anda_skoa
16th April 2015, 08:11
E.g. http://doc.qt.io/qt-5/qscriptengine.html#scriptValueFromQMetaObject

Cheers,
_

prachi kamble
16th April 2015, 08:43
will i be able to use all the methods of QTcpsocket class from within the js file(java script file).

Added after 25 minutes:


QString scriptFileName("./AnotherJs.js");
QFile scriptFile(scriptFileName);
scriptFile.open(QIODevice::ReadOnly);
engine.evaluate(scriptFile.readAll(), scriptFileName);
bool b1 = engine.hasUncaughtException();
scriptFile.close();

ctor = engine.evaluate("on_jsfile_clicked");
b1 = engine.hasUncaughtException();
scriptUi = engine.newQObject(&socket, QScriptEngine::ScriptOwnership);
b1 = engine.hasUncaughtException();

//QMetaObject *objmeta = new QMetaObject;
//QTcpSocket objsocket;
//objmeta->cast(&objsocket);
//engine.newQMetaObject(objmeta,ctor);

tcpsocket = engine.scriptValueFromQMetaObject<QTcpSocket>();
engine.globalObject().setProperty("QTcpSocket", tcpsocket);

calc = ctor.construct(QScriptValueList() << scriptUi); // this line gives error,why this is so? (error in the sense engine.hasUncaughtException() returns true value).
b1 = engine.hasUncaughtException(); //and socket is not getting connected to server

anda_skoa
16th April 2015, 09:58
If you have an existing socket, then the newQObject() call is enough.
The scriptValueFromQMetaObject() is only needed if the script wants to create QTcpSocket instances.

Cheers,
_

prachi kamble
16th April 2015, 13:02
i have passed socket object as an orgument, below is the code
scriptUi = engine.newQObject(&socket, QScriptEngine::ScriptOwnership);
calc = ctor.construct(QScriptValueList() << scriptUi);

but still, in "AnotherJs.js" file i m not able to execute following code

// QTcpSocket objsocket;
this = ui;
this.connectToHost("10.7.15.20",50000);

y this is happening? am i doing some wrong coading? please rply

wysota
16th April 2015, 13:43
connectToHost is not a slot, it will not be visible from within a script. You would have to make it a slot first.

prachi kamble
16th April 2015, 14:14
connectToHost() is QTcpSocket's Function, won't it be available to the java script, as i am passing the class to java script while evaluating it.?

wysota
16th April 2015, 14:16
connectToHost() is QTcpSocket's Function, won't it be available to the java script, as i am passing the class to java script while evaluating it.?

Yes, that's what I said, it won't be visible from within a script.

anda_skoa
16th April 2015, 14:30
i have passed socket object as an orgument, below is the code

Why pass a socket as a parent to the constructor of a new socket?


connectToHost() is QTcpSocket's Function, won't it be available to the java script, as i am passing the class to java script while evaluating it.?
Only slots or functions marked as Q_INVOKABLE are accessible to the script.

Cheers,
_