Results 1 to 8 of 8

Thread: QTScript - getting the name of a QScriptValue

  1. #1
    Join Date
    Jun 2009
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Post QTScript - getting the name of a QScriptValue

    Hello,
    i'm stuck at this for quite some time so i'll explain the situation to you in hope for help:

    i have a script console where one can insert custom scripts based on qtscript,
    there is a simulation which is accessed by a command

    mysimulation = getsimulation("....")

    a simulation is a class.
    now when using specific commands on that simulation it normally works like this:

    simulation_name = mysimulation.getname()

    which calls the corresponding function and so on. the functions like getname() return of course QScriptValues to work with further. i'm currently scripting a function which needs to know the variable it's executed with.
    in my example it would have to know that the name of the variable was "mysimulation".

    my current way of doing that is: mynewfunction("mysimulation") but that is of course no good way for solving the problem. is there a way to get the name of the qscriptvalue/qobject (which it is also?) when its accessed?

    i hope you can understand what i'm talking about thanks

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTScript - getting the name of a QScriptValue

    Hi there!

    What functions are native and which are scripted in your design? And is that choice right?

    Usually you would implement the getname as a public slot (or better a q_property name) of your simulation-class which returns a QString. (The conversion between QString and QScriptValue is done automatically behind the scenes, when you call the function.).

    Your class needs to be derived from QObject, needs the Q_OBJECT macro and you need to register the classtype with qScriptRegisterMetaType implementing the toScriptValue and FromScriptValue Functions.

    Your native getname implementation in the SimulationClass can directly access the objects properties as usual..

    Qt Code:
    1. class Simulation : public QObject
    2. { Q_OBJECT
    3. Q_PROPERTY(QString name READ GetName)
    4. public slots:
    5. QString toString() {return QString("SimObject: %1").arg(GetName());}
    6. public:
    7. Simulation(QString pname) {_name = pname;}
    8. QString GetName() {return _name;}
    9. private:
    10. QString _name;
    11. };
    12. Q_DECLARE_METATYPE(Simulation*)
    13.  
    14. QScriptValue SimulationToScriptValue(QScriptEngine *engine, Simulation* const &in)
    15. {
    16. return engine->newQObject(in,QScriptEngine::QtOwnership);
    17. }
    18. void SimulationFromScriptValue(const QScriptValue &object, Simulation* &out)
    19. {
    20. out = qobject_cast<Simulation*>(object.toQObject());
    21. }
    22.  
    23. qScriptRegisterMetaType<Simulation*>(scripting, SimulationToScriptValue,SimulationFromScriptValue);
    To copy to clipboard, switch view to plain text mode 

    Your native getsimulation function (a function object of your global scripting object?) returns a pointer to an instance of your SimulationClass. Right?

    If however you are trying to implement something which returns a QScriptValue or needs QScriptValueLists as parameters, let me know. But I doubt, that you need it. If you do, the thing you are probably looking for is the "this" object of the script context.

  3. The following user says thank you to JohannesMunk for this useful post:

    android_ (28th October 2009)

  4. #3
    Join Date
    Jun 2009
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTScript - getting the name of a QScriptValue

    thanks for the useful post,

    well i was not interested in a specific getName() function, this was only an example to demonstrate how the code works.
    i'm not interested in specific properties of the qobjects (like the simulation), i have to know the names of the objects/scriptvalues which the parser is creating at runtime. like in the example
    mysimulation = getSimulation("")
    so mysimulation is a qobject consisting of various data

    success = mysimulation.isOpen()
    a qscriptvalue (actually a bool) which can be used with the name "success"

    mysimulation.newFunction()
    the "newFunction()" in this case would have to know that it is called with the prefix "mysimulation", not the object but only the string of its dynamically generated name.

    or did you understand what i mean and i didn't?
    the simulation class is not a child of QObject, the getSimulation function converts the information in the simulation class to a QObject.
    Last edited by android_; 28th October 2009 at 12:31.

  5. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTScript - getting the name of a QScriptValue

    Ouch. No, I didn't get you. But now that I do, I can't think of a way to achieve this. If you were to implement your function like below you can access the very ScriptValue it was called from. But I don't know of a way to get the variable name. Sorry! QScriptValue doesn't feature a name property or something like that.

    What I still don't get is your motivation to get the variable name. What do you want to do with it? You want to generate other scripts/code which access that variable? Well you can do that by accessing its QScriptValue passed as argument or as the this object like so:

    That's the QScriptEngine::FunctionSignature.
    Qt Code:
    1. QScriptValue CDBGetActionScript(QScriptContext *ctx, QScriptEngine *eng)
    2. {
    3. CDB* cdb = eng->fromScriptValue<CDB*>(ctx->thisObject());
    4. return cdb->actionscript;
    5. }
    To copy to clipboard, switch view to plain text mode 



    So here with the context's thisObject you can access the QScriptValue from which the function was called.



    You need to register that function to each ScriptValue of that type.. upon its creation / conversion.

    Qt Code:
    1. QScriptValue CDBToScriptValue(QScriptEngine *engine, CDB* const &in)
    2. {
    3. QScriptValue sv = engine->newQObject(in,QScriptEngine::QtOwnership);
    4. sv.setProperty("Action",engine->newFunction(CDBGetActionScript),QScriptValue::PropertyGetter);
    5. return sv;
    6. }
    7. void CDBFromScriptValue(const QScriptValue &object, CDB* &out)
    8. {
    9. out = qobject_cast<CDB*>(object.toQObject());
    10. }
    To copy to clipboard, switch view to plain text mode 
    As an alternative you could create a prototype for the type. And add the function to it only once.

    Right direction?

    Johannes

  6. The following user says thank you to JohannesMunk for this useful post:

    android_ (28th October 2009)

  7. #5
    Join Date
    Jun 2009
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTScript - getting the name of a QScriptValue

    the purpose for that is a modified include function which already exists. i have a specific script which is used so often that it should be available in an own function.
    when using the script you specify the simulation manually with a command like in my example. the idea is to use the runtime name of the simulation and insert it into the script automatically, so that the code can be parsed with the correct simulation.
    there is of course a way of using the functions which would be called via the script directly in that function, but this method has other disadvantages.

    thanks again for your time!
    Last edited by android_; 28th October 2009 at 13:41.

  8. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTScript - getting the name of a QScriptValue

    Hi there. So as I suspected you have one script which calls another dynamically created one. In the process of creating that other script you insert the name of the (global!) variable which is to be worked upon. Now you want this nameinsertion to work without explicitely passing the name to insert!

    I think what you want to achieve is exactly why arguments to functions have been introduced. That you can call the same function with different values (or pointer to objects to be worked upon).

    Your dynamic creation has one other major flaw: speed. Each time the simulation is started for a different simulationobject you need to reevaluate the generated simulationscript. If you were to use an argument you could just call the function directly.

    Are you perhaps using the name of the object in your simulation script to switch between different cases? In that case I suggest an OOP approach. Just put every specialization into a class-member function and construct a proper inheritance-tree.

    Now that we have nailed your question, maybe somebody else has a solution? Even so I would strongly recommend not using it in terms of code serviceability :->

    Johannes

  9. The following user says thank you to JohannesMunk for this useful post:

    android_ (28th October 2009)

  10. #7
    Join Date
    Jun 2009
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTScript - getting the name of a QScriptValue

    it does already work with arguments, but the nature of the code would result in a function call like mysimulation.newfunction("mysimulation"), where the simulation is used as the argument.

    anyway, i'm choosing the more complicated but resource friendly way of rebuilding the code, which the qtscript engine would have generated with the custom script, myself.

  11. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTScript - getting the name of a QScriptValue

    I was not talking of giving the object's name as argument to the script generator function.

    What I meant was: forget about the script generator and just pass the simulationobject as parameter:

    fixedsimulationfunc(mysimulation);

    or better as it is more oop like:

    mysimulation.run();

    Here you use the this object to access mysimulation..

    ScriptCode:
    Qt Code:
    1. function run()
    2. {
    3. ++this.time;
    4. }
    5.  
    6. function()
    7. {
    8. // Initialize the run script.. maybe differently for different simulation objects?
    9. simulation.run = run;
    10. ...
    11. simulation.run();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Maybe I did still not catch why you want to do this.. Maybe you are forced to use the structure as it is.

    Best of luck to you!

    Johannes

Similar Threads

  1. QtScript: default constructor question
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2009, 19:36
  2. QtScript evaluation question
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2009, 03:46
  3. QScriptValue with simple typedef types
    By JohannesMunk in forum Newbie
    Replies: 9
    Last Post: 14th May 2009, 15:07
  4. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  5. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 16:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.