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. Reflection on Qt
Forum Updated to NodeBB v4.3 + New Features

Reflection on Qt

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 7.7k 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.
  • T Offline
    T Offline
    tchoninho
    wrote on last edited by
    #1

    Hi everybody, I would use Reflection on Qt, but I do not know how I can do that. In Java, any object can be instantiated and be called even if unknown. In Qt, I can only do this when I know that this object. Has anyone used Reflection in Qt?

    In my Object:

    @
    class Obj : public QObject
    {
    Q_OBJECT

    public:
    Q_INVOKABLE Obj(QObject *parent = 0);
    Q_INVOKABLE virtual ~Obj();

    Q_INVOKABLE virtual QVariant execute (QList<QVariant> &);
    Q_INVOKABLE uint getKey();
    

    };
    @

    Computer Scientist
    Belo Horizonte - Brasil
    C++ Development
    Qt Development

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I don't know much about Java or its reflection features. Does QMetaObject provide what you need?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Reflection itself does not exist in C++ (see wikipedia: http://en.wikipedia.org/wiki/Reflection_(computer_programming) ).
        To use the meta data system, you can achive something like that, but not for constructors (which you seem to think in the above code snippet).

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Actually, Q_INVOKABLE is possible on constructors, and if you have such a constructor, you can use QMetaObject::newInstance to create an object of the requested type. But I am not sure that is what tchoninho is after?

          1 Reply Last reply
          0
          • H Offline
            H Offline
            HuXiKa
            wrote on last edited by
            #5

            Well, you could do some nasty thing like this:

            @typedef QVariant (Obj::*pt2Member)(QList<QVariant> &);
            QMap<QString, pt2Member> functions;
            ..
            functions.insert("execute",&Obj::execute());
            ..
            QVariant result = functions["execute"](QList<QVariant> &);
            @

            Use this if there is no other option :)
            (You will still need a this reference I think, haven't tried this out!)

            If you can find faults of spelling in the text above, you can keep them.

            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