Results 1 to 3 of 3

Thread: Reflection on Qt

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Reflection on Qt

    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?

    My Object:

    Qt Code:
    1. class Obj : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Q_INVOKABLE Obj(QObject *parent = 0);
    7. Q_INVOKABLE virtual ~Obj();
    8.  
    9. Q_INVOKABLE virtual QVariant execute (QList<QVariant> &);
    10. Q_INVOKABLE uint getKey();
    11. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reflection on Qt

    QMetaObject provides this functionality, you can use it to browse properties and methods of Qt Objects.

    http://doc.qt.nokia.com/latest/qmetaobject.html

  3. #3
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Reflection on Qt

    Quote Originally Posted by tchoninho
    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
    Quote Originally Posted by squidge View Post
    QMetaObject provides this functionality, you can use it to browse properties and methods of Qt Objects.

    http://doc.qt.nokia.com/latest/qmetaobject.html
    Does QMetaObject provide the functionality of creating and object?

    If there is a class called MyClass that extends QObject, is it possible to instantiate it from a QString?
    What I mean is, having a QString className("MyClass"); is it possible to instantiate an object of type MyClass?

    If an object does not inherit from QObject, then via:
    Qt Code:
    1. int type = qRegisterMetaType<AClass>();
    2. and
    3. Q_DECLARE_METATYPE(AClass);
    To copy to clipboard, switch view to plain text mode 
    Is possible to instantiate an object knowing its int type returned from qRegisterMetaType, but then is there a way to use introspection to query the object about its methods and parameters?

    For example in Java is possible to do the following:
    Qt Code:
    1. Class myclass = Class.forName("MyClass");
    2. Method[] methods = myclass.getMethods();
    3. for (int i = 0; i < methods.length; i++)
    4. {
    5. // methods can also be invoked
    6. System.out.println(methods[i].getName());
    7. }
    To copy to clipboard, switch view to plain text mode 
    Is it possible to do something similar using Qt?


    Added after 45 minutes:


    Ah found it, for anyone interested:

    Having the following class:

    Qt Code:
    1. class MyClass : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Q_INVOKABLE MyClass(QObject *parent = 0);
    6.  
    7. public:
    8. Q_INVOKABLE void myMethod();
    9. };
    To copy to clipboard, switch view to plain text mode 

    Then the following code will print the above class methods:
    Qt Code:
    1. MyClass *obj = static_cast<MyClass*>(MyClass::staticMetaObject.newInstance());
    2. for (int i = 0; i < obj->metaObject()->methodCount(); i++)
    3. {
    4. QMetaMethod method = obj->metaObject()->method(i);
    5. qDebug() << method.signature();
    6. }
    To copy to clipboard, switch view to plain text mode 
    the above will print something like:
    Qt Code:
    1. destroyed(QObject*)
    2. destroyed()
    3. deleteLater()
    4. _q_reregisterTimers(void*)
    5. myMethod()
    To copy to clipboard, switch view to plain text mode 
    Last edited by scieck; 13th December 2011 at 12:51.

Similar Threads

  1. QxOrm : Persistence (ORM), Serialization, Reflection
    By QxOrm in forum Qt-based Software
    Replies: 19
    Last Post: 27th January 2014, 04:36
  2. How to make the "Reflection" effect by QML?
    By charlse in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 13:04
  3. java reflection
    By mickey in forum General Programming
    Replies: 1
    Last Post: 24th July 2008, 21:17
  4. Making a reflection of a picture
    By desch in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2008, 18:38
  5. reflection and qmetaobject
    By KShots in forum Qt Programming
    Replies: 8
    Last Post: 16th May 2007, 20:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.