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. Qt invoke method with QVariant
Forum Updated to NodeBB v4.3 + New Features

Qt invoke method with QVariant

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.6k Views 1 Watching
  • 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
    Elii
    wrote on 11 Feb 2015, 16:50 last edited by
    #1

    I have the following function :

    @class TestClass: public QObject
    {
    Q_OBJECT
    public:
    Q_INVOKABLE QString test() { return QString("test"); }
    };@

    And I want to invoke the test method, but get the return type as QVariant, not as QString. So I tried this :

    @TestClass obj;
    QVariant returnedValue;
    bool rslt= QMetaObject::invokeMethod(&obj, "test", Qt::DirectConnection,
    Q_RETURN_ARG(QVariant, returnedValue)
    );
    QString strVar = returnedValue.toString();
    @

    but it doesnot work, invoke returns false;
    If get the return type as QString it works, but unfortunately this will not be usable for me, cause I need to know the return type before calling the function.

    @QString r;
    bool rslt= QMetaObject::invokeMethod(&obj, "test", Qt::DirectConnection,
    Q_RETURN_ARG(QString, r)
    );@

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Feb 2015, 00:07 last edited by
      #2

      Hi,

      AFAIK, you can't call invokeMethod without knowing the signature of the method you are going to invoke.

      Can you describe a bit more your use case ?

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

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Elii
        wrote on 12 Feb 2015, 11:11 last edited by
        #3

        Hi I found solution, but don't understand how it works, and whether it is ok , maybe you can explain :

        @class TestClass: public QObject
        {
        Q_OBJECT
        public:
        Q_INVOKABLE MyStruct test() { return MyStruct(5); }
        };
        Q_DECLARE_METATYPE(MyStruct)
        int ttr=qRegisterMetaType<MyStruct> ();

        TestClass obj;

        int thetype = QMetaType::type("MyStruct");
        void v = NULL;
        QVariant returnedValue (thetype,v);
        void
        data = returnedValue.data();

        bool rslt= QMetaObject::invokeMethod(&obj, "test", Qt::DirectConnection,
        QGenericReturnArgument("MyStruct", data )
        );

        bool can = returnedValue.canConvert<MyStruct> ();
        MyStruct structm = returnedValue.value<MyStruct>();@

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 13 Feb 2015, 00:05 last edited by
          #4

          There's something not clear at all, why doing so ?

          Beware, you are using code that is explicitly discourage.

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

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Elii
            wrote on 13 Feb 2015, 07:24 last edited by
            #5

            I want to iterate through methods of my class, and call them. The methods all are returning different types. These methods are Get methods that are returning the fields of my class. I am aware that maybe marking my fields with Q_PROPERTY may be more appropriate, but I find that more cumbersome.

            Can you elaborate why the code I posted is discouraged?
            Thank you

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 18 Feb 2015, 00:32 last edited by
              #6

              The documentation of "QGenericArgument":http://doc.qt.io/qt-5/qgenericargument.html#details

              You should still consider the property system, if will allow you to really iterate through the declared getters

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

              1 Reply Last reply
              0

              1/6

              11 Feb 2015, 16:50

              • Login

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