PDA

View Full Version : QtScript Questions



Scorp2us
26th June 2009, 22:36
I am embarking on some QtScript stuff and have some questions:

1) Can I include files? I seem to be able to use eval() however if I provide a function include(filename) {..} in the script I run into the problem that the functions will only be accessible from within the function and not added to the root context.

2) I am trying to use animations, and spefically QPropertyAnimation(item, property, parent) however my custom properties do not seem to work. I am using a JS-defined class, with properties as this.opacity, and passing "opacity" in the constructor. What does it take to have QPropertyAnimation be able to use JS-defined properties?

Thanks.

wysota
27th June 2009, 20:12
I am embarking on some QtScript stuff and have some questions:

1) Can I include files? I seem to be able to use eval() however if I provide a function include(filename) {..} in the script I run into the problem that the functions will only be accessible from within the function and not added to the root context.

Can we see the exact code?

Scorp2us
29th June 2009, 19:02
qs> eval("function f(i){return i*3;}");
qs> b=f(5);
15


qs> function a(i) { eval("function F(i) {return i*3;}"); }
qs> a
function a(i) {
eval("function F(i) {return i*3;}");
}
qs> a();
qs> F(6);
ReferenceError: F is not defined



qs> function b() { eval("function F(i) {return i*3;}"); return F(4); }
qs> b()
12

wysota
29th June 2009, 19:30
Your eval() (in the a() function) creates its own scope, whatever you assign there will not go to the global scope.

Scorp2us
29th June 2009, 19:34
Your eval() (in the a() function) creates its own scope, whatever you assign there will not go to the global scope.

Exactly! (I knew this). But is there a way around that? Like a special context that I can specify (like a Python Module __main__) ?

wysota
29th June 2009, 19:45
I think you have to look on JavaScript forums for that but the question is why are you trying to define a global function from eval(). You can do that from C++ code if you want... Also you can name the global object somehow in the C++ code and then you can access it from JavaScript code and assign the function to it - it should then be globally available. You can also return the function from the eval code and assign it to the global namespace in the parent (also JavaScript) code.

Scorp2us
29th June 2009, 20:47
I was hoping to do all this in JS, but it looks like I can't.

Thanks.

wysota
29th June 2009, 21:14
If you return a function object from the eval() call then you can do everything in JavaScript.

F = eval("return function(){ return 7; }");
x = F();

Scorp2us
29th June 2009, 21:32
Yeah, but that eval is in the global context.

I still can't have a function defining a global function.

wysota
29th June 2009, 22:06
Your a() function can return the result of eval().

function a(){ return eval("return function(){ return 7 }") }
F = a()

But I would try something like this:
globalObj = this // global context
function a() { eval("globalObj.F = function(){ return 7; }") }
a()
F()