Results 1 to 8 of 8

Thread: Specifying method metadata

  1. #1
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Specifying method metadata

    Hello,

    QMetaProperty data for a class can be specified using the Q_PROPERTY macro. What is the equivalent for QMetaMethod?

    I know how to query it ... just not how to specify in the class declaration.
    Qt Code:
    1. Q_PROPERTY(type name
    2. READ getFunction
    3. [WRITE setFunction]
    4. [RESET resetFunction]
    5. [DESIGNABLE bool]
    6. [SCRIPTABLE bool]
    7. [STORED bool])
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Ben

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Specifying method metadata

    Quote Originally Posted by brcain View Post
    What is the equivalent for QMetaMethod?
    Q_OBJECT, which means that QMetaMethod and QMetaObject work only with classes that inherit QObject.

  3. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Specifying method metadata

    My class does indeed inherit from QObject. My question is ... how do I specify the methods for runtime introspection using a macro similar to Q_PROPERTY?

    Qt Code:
    1. class cMyClass : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(int mData
    5. READ getData()
    6. WRITE setData()
    7. RESET resetData())
    8. Q_METHOD(??? // <--- How do I do this?
    9. public:
    10. cMyClass() : mData(7) {}
    11. type0 someMethod(type1 foo1, type2 foo2, type3 foo3, ...);
    12. int const getData() { return mData; }
    13. void setData(int data) { mData = data; }
    14. void resetData() { mData = 0; }
    15. private:
    16. int mData;
    17. };
    To copy to clipboard, switch view to plain text mode 

    So that I can query like this ...

    Qt Code:
    1. cMyClass * instance(new cMyClass);
    2. cout << "Inspecting methods ..." << endl;
    3. int count(instance->metaObject()->methodCount());
    4. for(int i=0; i<count; i++)
    5. {
    6. cout << "\t" << instance->metaObject()->method(i).signature() << endl;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by brcain; 20th September 2006 at 23:41.

  4. #4
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Specifying method metadata

    Hmm, you can use the signals and slots keywords in your class declaration to make Qt create the method metadata for those functions. QMetaMethod seems to suggest support for a 3rd method type

    enum MethodType { Method, Signal, Slot }

    QMetaMethod::Method 0 The function is a plain member function.
    I don't know how you make Qt recognize these plain methods/functions.
    Software Engineer



  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Specifying method metadata

    Quote Originally Posted by brcain View Post
    how do I specify the methods for runtime introspection using a macro similar to Q_PROPERTY?
    You don't have to do anything to make signals or slots available, but the docs don't say anything about ordinary methods. Yet we have the Source

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QObject>
    3. #include <QMetaObject>
    4.  
    5. #include <QtDebug>
    6.  
    7. class Test : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. Test() : QObject( 0 ), _x( 0 ) {}
    12.  
    13. Q_INVOKABLE void bar( int i ){ _x = i; }
    14. int foo() { return _x; }
    15.  
    16. private:
    17. int _x;
    18. };
    19.  
    20. int main( int argc, char **argv )
    21. {
    22. QCoreApplication app( argc, argv );
    23.  
    24. Test t;
    25.  
    26. qDebug() << "foo = " << t.foo();
    27. QMetaObject::invokeMethod( &t, "bar", Q_ARG( int, 42 ) );
    28. qDebug() << "foo = " << t.foo();
    29.  
    30. return 0;
    31. }
    32.  
    33. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    I couldn't make it work for methods that return something, but you actually don't need it as you can always declare all interesting methods as slots.

    I've found also this: http://lists.trolltech.com/qt4-previ...ad00458-0.html.

    Phonon Backend Development:
    "The first step is to understand how the Phonon frontend calls the backend: In the frontend objects all backend objects are "only" QObjects. But QObject has powerful introspection capabilities that Phonon uses to call methods in the backend. If you're interested look at QMetaObject::invokeMethod. In order to make sure that a backend is fully operational (there are no abstract classes that tell the backend developer what method signatures are wrong or what methods are missing) there are two test programs compiled with kdelibs (if KDE4_BUILD_TESTS is set in cmake) that inspects the backend.

    In short that requires the backend classes to inherit from QObject and to make all methods that are to be called from the frontend slots or prefixed with Q_INVOKABLE (the latter doesn't work reliable with Qt 4.1.3 at least, so you should simply make those methods slots."

  6. The following 3 users say thank you to jacek for this useful post:

    brcain (21st September 2006), gfunk (21st September 2006), sunil.thaha (21st September 2006)

  7. #6
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Specifying method metadata

    Would there be anything particularly bad about just declaring all your plain methods as slots? aside from the metaobject overhead, won't they just behave the same as regular methods?
    Software Engineer



  8. The following user says thank you to gfunk for this useful post:

    brcain (21st September 2006)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Specifying method metadata

    Quote Originally Posted by gfunk View Post
    Would there be anything particularly bad about just declaring all your plain methods as slots?
    No, it will only make qt_metacall() and its data structures a bit bigger.

  10. #8
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Specifying method metadata

    Quote Originally Posted by gfunk
    Would there be anything particularly bad about just declaring all your plain methods as slots?
    I think I'll just do that. It seems to be giving me the results I need.

    Quote Originally Posted by jacek
    You don't have to do anything to make signals or slots available, but the docs don't say anything about ordinary methods. Yet we have the Source
    Much thanks for the detailed post!
    Last edited by brcain; 21st September 2006 at 01:28.

Similar Threads

  1. variable in method not initialized?!
    By frosch in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2006, 14:09
  2. QImage : trouble with save() method
    By krivenok in forum Qt Programming
    Replies: 12
    Last Post: 3rd May 2006, 20:55
  3. Static field and public method
    By probine in forum General Programming
    Replies: 1
    Last Post: 5th March 2006, 11:02
  4. connect returns false
    By krivenok in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 20:01
  5. Qt 4.1 and KDE 3.5.1 on OSX 10.2.8
    By Ptero-4 in forum Installation and Deployment
    Replies: 6
    Last Post: 6th February 2006, 02:44

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.