PDA

View Full Version : proving aruments to scripting..



prachi kamble
23rd April 2015, 05:26
hi,

following is my code


QScriptEngine engine;
QScriptValue ctor,scriptUi,calc,scriptUi2;

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_m_TcpSend_clicked");
b1 = engine.hasUncaughtException();
scriptUi = engine.newQObject(this, QScriptEngine::ScriptOwnership);
b1 = engine.hasUncaughtException();
calc = ctor.construct(QScriptValueList() << scriptUi);
b1 = engine.hasUncaughtException();

here in line 11: i m creating new object giving "this" as parameter, and in line 13: i am proving the scriptui as a parameter to the ctor's constructor.
my question is if i can provide more that 1 arguments to scripting. (ex:- if i want to give this and some other object both as a parameter??)
Please answer...

anda_skoa
23rd April 2015, 07:05
my question is if i can provide more that 1 arguments to scripting

Hint 1: the argument of construct() is called "args".
Hint 2: it is a list.

Cheers,
_

prachi kamble
23rd April 2015, 07:53
yes, i know that, that's y i tried to provide the list of arguments, but i don't know exact syntax for it :(. searching for the syntax...
i tried something like this

calc = ctor.construct(QScriptValueList() << scriptUi << scriptui2);

is there any mistake in the code??

anda_skoa
23rd April 2015, 07:59
That looks ok.

Cheers,
_

prachi kamble
23rd April 2015, 09:14
ok... but its not giving me the expected result...
ok i try more on it...

wysota
23rd April 2015, 09:20
What exactly are you expecting to obtain with this code? You realize you are calling a constructor of a function? This will not call the function (provided that on_m_TcpSend_clicked does evaluate to a function).

prachi kamble
23rd April 2015, 09:41
yes, it is constructing the given function.the code above works well, it is evaluating the function and working well, it also calls the function and executes the code in it,(when i provide only one parameter). but there is a requirement to provide 2 parameters.

wysota
23rd April 2015, 09:45
How do you evaluate those two parameters in the function?

prachi kamble
23rd April 2015, 09:49
above written is the code in qt

following is the code in java script

function on_m_TcpSend_clicked(param1,param2)
{
this.ui = param1;
param1.SomeControl.text = "ABC"; //this line gets executed if only param1 is given as parameter
SendData(param2); //this function is written in another js file.
}

wysota
23rd April 2015, 10:13
Have you tried checking the "arguments" object passed to the function?

function on_m_TcpSend_clicked()
{
this.ui = arguments[0];
param1.SomeControl.text = "ABC"; //this line gets executed if only param1 is given as parameter
SendData(arguments[1]); //this function is written in another js file.
}

prachi kamble
23rd April 2015, 11:03
i tried spmeting like below

code in QT:-
ctor = engine.evaluate("on_m_TcpSend_clicked");
b1 = engine.hasUncaughtException();
scriptUi = engine.newQObject(this, QScriptEngine::ScriptOwnership);
b1 = engine.hasUncaughtException();

scriptUi2 = engine.newQObject(objtcpwrapper, QScriptEngine::ScriptOwnership);
b1 = engine.hasUncaughtException();
QScriptValueList objlist;
objlist.append(scriptUi);
objlist.append(scriptUi2);
calc = ctor.construct(objlist);
b1 = engine.hasUncaughtException();

code in Java:-

function on_m_TcpSend_clicked(param)
{
this.ui = param[0];
SendData(param[1]);
}

but it is not working...

I have one more question.. can i declare a global or file lavel variable in the java script file?? if it is possible.. it will solve my issue

wysota
23rd April 2015, 14:12
Did you try the arguments object as I told you?

prachi kamble
24th April 2015, 07:55
Yes sir ,I tried... Not working in java script...

wysota
24th April 2015, 08:19
Yes sir ,I tried... Not working in java script...

Then you tried it wrong because it is part of JavaScript specification.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

prachi kamble
29th April 2015, 09:29
i got to know.. what u r saying is java script with HTMl, and java script with Qt has some different methods. as i have tried many thing which are perfectly working in java with htmal(ex- confirm method for msgbox, require method for TCP connection), but it gives me error while used in qt scripting. so argument method might not be working in qt .

wysota
29th April 2015, 09:40
This works just fine:


#include <QScriptEngine>
#include <QCoreApplication>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QScriptEngine engine;
engine.evaluate("function func() { for(var i = 0; i < arguments.length; ++i) print('Arg ', i+1, ': ', arguments[i]); } func('a1', 'a2');");
return 0;
}

prachi kamble
29th April 2015, 09:49
ok... that's good...
but what if i just want to give just function name and don't want to write function body while evaluating it.then??
i mean to say.. i have a js file.. in that function body is already written with 2 parameters . and want to call it from qt script and want to give parameters list there.. pls help if u know

wysota
29th April 2015, 10:06
but what if i just want to give just function name and don't want to write function body while evaluating it.then??
i mean to say.. i have a js file.. in that function body is already written with 2 parameters . and want to call it from qt script and want to give parameters list there.. pls help if u know

The code doesn't care where the function is defined.

prachi kamble
29th April 2015, 11:56
engine.evaluate("function func() { for(var i = 0; i < arguments.length; ++i) print('Arg ', i+1, ': ', arguments[i]); } func('a1', 'a2');");

ok..wherever the function is defines.. do i need to provide function body while evaluating then...or i can simply write

engine.evaluate("function func('a1', 'a2');");

wysota
29th April 2015, 12:27
or i can simply write

engine.evaluate("function func('a1', 'a2');");

You definitely can't write that because this code has a syntax error.

As I said the code works regardless where the function is defined.


#include <QScriptEngine>
#include <QCoreApplication>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QScriptEngine engine;
engine.evaluate("function func() { for(var i = 0; i < arguments.length; ++i) print('Arg ',i+1,': ', arguments[i]); }"); // define here
engine.evaluate("func('a1', 'a2');"); // call here
return 0;
}

This also works (notice the parenthesis in function definition):


#include <QScriptEngine>
#include <QCoreApplication>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QScriptEngine engine;
QScriptValue fun = engine.evaluate("(function func() { for(var i = 0; i < arguments.length; ++i) print('Arg ',i+1,': ', arguments[i]); })");
fun.call(QScriptValue(), QScriptValueList() << "a1" << "a2");
return 0;
}

prachi kamble
29th April 2015, 12:39
ok.. i got that... my only problem is i dont want to write function body inside that evaluate function
engine.evaluate("function func() { for(var i = 0; i < arguments.length; ++i) print('Arg ',i+1,': ', arguments[i]); }"); // define here..
because my function consist on more than 8 to 10 lines... i can not write that while evaluating it... also there is requirement that... when we run qt program, function from js file can be changed..... but at run time it should run when we execute script... function body may be unknown when we write evaluate code...

ex...
on some button click qt have button click even in that event i have evaluated a function from a js file.. i run that qt app.. when i click the button, the qt button click event from QT gets called and then the script function will be evaluated.. user has authority to change the function code in js file.. and again on clicking on that futton function from js will be evaluated with the changes user has done..it should happen without quitting and rebuilding qt application.. it should be running whole the time.. only function code can be changed run-time and executed whatever is written inside function.....

wysota
29th April 2015, 12:45
ok.. i got that... my only problem is i dont want to write function body inside that evaluate function
engine.evaluate("function func() { for(var i = 0; i < arguments.length; ++i) print('Arg ',i+1,': ', arguments[i]); }"); // define here..
because my function consist on more than 8 to 10 lines... i can not write that while evaluating it... also there is requirement that... when we run qt program, function from js file can be changed..... but at run time it should run when we execute script... function body may be unknown when we write evaluate code...

Are you even reading what I'm writing? I said, the mechanism doesn't care how you provide the definition of the function to the engine. If you want to read the function body from a file then do that, I don't care and neither does QScriptEngine.

Your question in this thread was how to provide arguments to scripts, I told you to use the 'arguments' object in your function. I have shown you a couple of approaches of calling the function. I really don't care how the function is defined. You told me 'arguments' didn't work in Qt, I proved you wrong. So focus on that -- modify your function code to use 'arguments' to work with different number of arguments passed to the function. If you expect me to write a complete function evaluating framework for you then I will not do that, this is your job not mine.

prachi kamble
29th April 2015, 13:05
sorry to say sir, but i didn't asked u to write my code for me. my only question was "can i omit that sentence which is written in red".. that's it... i am wring my code on my own. of-course this site is to remove all doubts. and i have lots of doubts bcz i am new to QT and QT script engine. still i m doing it on my own, asking only questions.

wysota
29th April 2015, 13:12
my only question was "can i omit that sentence which is written in red".
Yes, you can.

prachi kamble
30th April 2015, 12:37
finally task accomplished.. by following code

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

ctor = engine.evaluate("function on_m_TcpSend_clicked(ui,ui2)");
b1 = engine.hasUncaughtException();
scriptUi = engine.newQObject(objtcpwrapper, QScriptEngine::ScriptOwnership);
b1 = engine.hasUncaughtException();

scriptUi2 = engine.newQObject(this, QScriptEngine::ScriptOwnership);
b1 = engine.hasUncaughtException();

QScriptValue global = engine.globalObject();
QScriptValue func= global.property("on_m_TcpSend_clicked");
QScriptValueList args;
args << scriptUi << scriptUi2;
func.call(QScriptValue(), args);

one stucked with the same problem can refer this code.

wysota
2nd May 2015, 01:48
What does the following line do?


ctor = engine.evaluate("function on_m_TcpSend_clicked(ui,ui2)");

prachi kamble
18th May 2015, 07:34
as per my knowledge...the code evaluates the function with the given form. means it is indicating that the function will have 2 params. and also does check for matching while calling.

wysota
18th May 2015, 09:51
the code evaluates the function with the given form.
The "function" keyword seems to indicate that it is a declaration or definition of a function rather than a call to it.

prachi kamble
18th May 2015, 10:56
yes.. it is a function written in java script. function definition.

in the documentation following is written.
QScriptValue QScriptEngine::​evaluate(const QString & program, const QString & fileName = QString(), int lineNumber = 1)
this Evaluates program, using lineNumber as the base line number, and returns the result of the evaluation.

The script code will be evaluated in the current context.

wysota
18th May 2015, 12:47
I don't think you understand.

What do you think is the value kept in 'ctor'?


ctor = engine.evaluate("function on_m_TcpSend_clicked(ui,ui2)");

Is it:
a) result of a function call to on_m_TcpSend_clicked(ui, ui2)
b) a callable object
c) an undefined value
d) an error
e) something else

prachi kamble
26th May 2015, 05:20
ohh yes sir i got ur question,
as per my knowledge its not the result of on_m_TcpSend_clicked function
it is the result of evaluate function.
the evaluation of program can cause an exception in the engine; in this case the return value will be the exception that was thrown (typically an Error object).

of course i, myself is doing R&D on this, so i may get wrong, anyone who is excellent in this pls correct me if m wrong.

wysota
26th May 2015, 06:23
ohh yes sir i got ur question,
as per my knowledge its not the result of on_m_TcpSend_clicked function
it is the result of evaluate function.
the evaluation of program can cause an exception in the engine; in this case the return value will be the exception that was thrown (typically an Error object).

of course i, myself is doing R&D on this, so i may get wrong, anyone who is excellent in this pls correct me if m wrong.

You didn't answer my question.

prachi kamble
26th May 2015, 06:36
yes i said (ctor)it is the result of evaluate(...) function.isn't it ur answer?

wysota
26th May 2015, 07:12
"ctor" is not one of the options I provided.