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
QtWS25 Last Chance

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 12 Mar 2015, 15:57 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Mar 2015, 21:50 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 13 Mar 2015, 07:29
      0
      • S SGaist
        12 Mar 2015, 21:50

        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

        E Offline
        E Offline
        eddystefes
        wrote on 13 Mar 2015, 07:29 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Mar 2015, 20:19 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 16 Mar 2015, 07:51
          0
          • S SGaist
            14 Mar 2015, 20:19

            I see what you want to do but why ?

            E Offline
            E Offline
            eddystefes
            wrote on 16 Mar 2015, 07:51 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 17 Mar 2015, 21:11 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 18 Mar 2015, 09:42
              0
              • S SGaist
                17 Mar 2015, 21:11

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

                E Offline
                E Offline
                eddystefes
                wrote on 18 Mar 2015, 09:42 last edited by
                #7

                hmm ok :( still thank you for the help

                1 Reply Last reply
                0

                1/7

                12 Mar 2015, 15:57

                • Login

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