Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtScript call functions with & arguments
QtWS: Super Early Bird Tickets Available!

QtScript call functions with & arguments

Scheduled Pinned Locked Moved General and Desktop
qtscriptreferenceqscriptengine
7 Posts 2 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eddystefes
    wrote on last edited by
    #1

    i have a class that has a method with reference arguments:

    bool myMethod(QString arg1, int & someRefArg);
    

    how do i make this function callable from QtScript?

    if i try it like:

    var a = 123;
    myObj['myMethod(QString, int&)']("test",a);
    
    TypeError: cannot call myMethod(): argument 2 has unknown type `int&' (register the type with qScriptRegisterMetaType())
    

    I tryed to register the type :

    QScriptValue toScriptValue(QScriptEngine *engine, const int &&s)
    {
      QScriptValue obj = engine->newObject();
      obj = s;
      return obj;
    }
    
    void fromScriptValue(const QScriptValue &obj, int &&s)
    {
      s = obj.toInt();
    }
    
    
    qScriptRegisterMetaType(&engine,toScriptValue,fromScriptValue);
    

    but this has no effect.
    what do i need to do to pass a reference to the method?

    1 Reply Last reply
    1
  • SGaistS Offline
    SGaistS Offline
    SGaist Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Is it a typo or are your function signature really:

    QScriptValue toScriptValue(QScriptEngine *engine, const int &&s) << one & too much
    void fromScriptValue(const QScriptValue &obj, int &&s)  << one & too much
    

    Hope it helps

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    E 1 Reply Last reply
    0
  • E Offline
    E Offline
    eddystefes
    replied to SGaist on last edited by eddystefes
    #3

    @SGaist

    no not relay the && was intended.

    the standard devinition of a fromScriptValue uses one & so it can access the value and does not get a copy. My intention by using 2 && was to actually be able to pass a int& and not just an int . Perhaps this makes it clearer what i want to do:

    QScriptValue toScriptValue(QScriptEngine *engine, const (int&) &s)
    {
    ....
    
    void fromScriptValue(const QScriptValue &obj, (int&) &s)
    {
    ....
    

    also now I see in the documentation that I need to register the type with Q_DECLARE_METATYPE. But

    Q_DECLARE_METATYPE(int&) 
    

    does not compile. :(

    1 Reply Last reply
    0
  • SGaistS Offline
    SGaistS Offline
    SGaist Lifetime Qt Champion
    wrote on last edited by
    #4

    I see what you want to do but why ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    E 1 Reply Last reply
    0
  • E Offline
    E Offline
    eddystefes
    replied to SGaist on last edited by
    #5

    because:
    I want to script some parts the code
    and the methods in the library i need to use are defined like this: bool getMyValue(QString, ValueType&)
    and i can not change that library
    and i do not want to write a wrapper

    1 Reply Last reply
    0
  • SGaistS Offline
    SGaistS Offline
    SGaist Lifetime Qt Champion
    wrote on last edited by
    #6

    AFAIK, you can't use reference to types to create metatypes

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    E 1 Reply Last reply
    0
  • E Offline
    E Offline
    eddystefes
    replied to SGaist on last edited by
    #7

    hmm ok :( still thank you for the help

    1 Reply Last reply
    0

  • Login

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved