PDA

View Full Version : QScript design questions



ctgrund
24th March 2011, 08:54
Hi,

Following situation:

We have a C++ data structure which is a tree of nodes. Each node has a name and represents a double value or double array or a child list of nodes. The tree is quite large. There is one C++ funtion "F" which operates on a part of the tree. We would like to have a scripting interface for it using QScript. I have a couple of questions where I only need some pointers to the right design and the right classes/examples since right now I am not even able to ask the right questions.

In JavaScript we would like to something like

Tree.a.b = 3.2
Tree.f.s.a = 4
F() or even better F(Tree.f)
print(Tree.f.s.b)
Tree.f.s.a = 4
F(Tree.f)
print(Tree.f.s.b)
...

1) First we have to copy the tree somehow to script variables? What is the strategy and which of the QT-examples ist most approbiate?
2) To call C++-"F" from javascript-"F" is quite clear.
3) With a call to JavaScript F(Tree.f) can I change Tree.f or is this read only?
4) After Tree.f.s.a = 4, how can I copy data from JavaScript to C++-Tree without having to copy all data? Is there some signal when a JavaScript variables changes or somethin similar?
5) After calling F we have marks in the C++Tree so that we know in principal which nodes to update back to Javascript.

Points 3/4 is most unknown for me.

Thanks for your help!

Thomas

wysota
24th March 2011, 10:18
1) First we have to copy the tree somehow to script variables? What is the strategy and which of the QT-examples ist most approbiate?
You can either "copy" the data into new objects or you can expose your existing objects in form of ecmascript api. It mainly depends if you intend to modify those objects from the script. If not, copying should be easier - just create a bunch of objects (using newObject()) and assign properties to them.


3) With a call to JavaScript F(Tree.f) can I change Tree.f or is this read only?
It depends on how you solve issue #1 and also what the function signature is.


4) After Tree.f.s.a = 4, how can I copy data from JavaScript to C++-Tree without having to copy all data?
You can either resynchronize the two object groups manually or you just wrap your real objects into script api as said in the beginning.


Is there some signal when a JavaScript variables changes or somethin similar?
No.

ctgrund
24th March 2011, 11:58
You can either "copy" the data into new objects or you can expose your existing objects in form of ecmascript api. It mainly depends if you intend to modify those objects from the script. If not, copying should be easier - just create a bunch of objects (using newObject()) and assign properties to them.


I feel that I need the second case because I would like to modify C++ Objects from ecmascript. Can you give me some keywords (in form of QT class or method names or examples or some small skeleton) how I "expose your existing objects in form of ecmascript api". I just need a starting point.

Thank you!

Thomas

wysota
24th March 2011, 12:10
You can start here: Making a C++ object available to scripts written in QtScript. Also have a look at QScriptable and QScriptClass. The "Default Prototypes" example might be something useful for you.

ctgrund
24th March 2011, 12:27
This was exactly the kind of link I was looking for.

Thanks!