Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QtScript call functions with & arguments

    General and Desktop
    qtscript reference qscriptengine
    2
    7
    2067
    Loading More Posts
    • 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
      eddystefes last edited by

      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 Reply Quote 1
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • E
          eddystefes @SGaist last edited by eddystefes

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • E
              eddystefes @SGaist last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • E
                  eddystefes @SGaist last edited by

                  hmm ok :( still thank you for the help

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post