Results 1 to 8 of 8

Thread: QTScript - getting the name of a QScriptValue

Threaded View

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

    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

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

    android_ (28th October 2009)

Similar Threads

  1. QtScript: default constructor question
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2009, 20:36
  2. QtScript evaluation question
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2009, 04:46
  3. QScriptValue with simple typedef types
    By JohannesMunk in forum Newbie
    Replies: 9
    Last Post: 14th May 2009, 16:07
  4. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 10:38
  5. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 17: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
  •  
Qt is a trademark of The Qt Company.