PDA

View Full Version : qt script property



wookoon
28th September 2010, 08:41
I want set a property like xxx.xxx.xxx in qt scirpt ,so I write code like these:



QScriptEngine engine;
QScriptValue o = engine.globalObject();
QScriptValue oo;
QScriptValue o1;

o1 = o.property("org");
if (!o1.isValid()) {
o1 = engine.newObject();
o.setProperty("org", o1);
}

o1= o.property("org.math");
if (!o1.isValid()) {
o1 = engine.newObject();
o.setProperty("math", o1);
}

engine.evaluate("org.sum = function Math_sum(a){ return 123456; }");
engine.evaluate("org.math.sum = function Math_sum(a){ return 654321; }");
engine.evaluate("var r = org.sum(12); print(r); ");
engine.evaluate("var r1 = org.math.sum(12); print(r1); ");


output is: 123456
so the org.math.sum is invalid.

I have try this way too:


QScriptValue o = engine.globalObject();
QScriptValue oo;
QScriptValue o1;

o1 = o.property("org");
if (!o1.isValid()) {
o1 = engine.newObject();
o.setProperty("org", o1);
}

QScriptValue o2;
o2= o1.property("math");
if (!o2.isValid()) {
o2 = engine.newObject();
o.setProperty("math", o2);
}


but the output still is 12345.

If i want excute function org.math.sum(), how should I set the property.

wysota
28th September 2010, 11:13
I think line #12 of your first snippet is wrong. You can transit through properties like that, you have to write it like so:

o1= o.property("org").property("math");

Furthermore line #15 sets a property "math" of the global object and not of the "org" object so org.math will be undefined.

wookoon
28th September 2010, 11:41
Thanks..

follow your suggestion ,I change my codes ,and it does work.



QScriptEngine engine;
QScriptValue o = engine.globalObject();
QScriptValue o1;
QScriptValue o2;

o1 = o.property("org");
if (!o1.isValid()) {
o1 = engine.newObject();
o.setProperty("org", o1);
}


o2= o.property("org").property("math");

if (!o2.isValid()) {
o2 = engine.newObject();

o1.setProperty("math", o2);
}

engine.evaluate("org.sum = function Math_sum(){ return 123456; }");
engine.evaluate("org.math.sum = function Math_sum(){ return 654321; }");
engine.evaluate("var r = org.sum(); print(r); ");
engine.evaluate("var r1 = org.math.sum(); print(r1); ");