PDA

View Full Version : Examples for runtime introspection?



brcain
20th September 2006, 19:54
Hello,

Are there any examples showing how to use Qt's runtime introspection capability. I was really glad to see this capability. My opinion of Qt keeps getting better. It's much more than just a GUI library ... there are many framework development aspects as well.

I've begun looking at these classes:

QMetaObject
QMetaProperty
QMetaMethod
QVariant
...
Any pointers greatly appreciated.

Cheers,
Ben

wysota
20th September 2006, 21:22
What do you want to achieve exactly? AFAIR there has been a thread about introspection recently here. Maybe trying to find it could return valuable results for you.

brcain
20th September 2006, 21:32
I asked a question recently if there *was* runtime introspection capability with Qt. You replied (I think) referring me to QMetaObject.

What I need ... I need the capability to discover the interface of a class (attributes and methods ... their numbers, types, values) during runtime. This sort of thing as you know is supported in Java ... just trying to leverage Qt to provide similar capability in C++.

I've been looking at the Qt documentation and header files ... but need some guidance in the form of examples ... since the documentation is a bit terse. I haven't found any ... in installation examples, book examples, FAQs, Qt Center query, and Google. I've made some progress ... but am having trouble declaring the properties for the introspection mechanism.

brcain
20th September 2006, 21:46
I think I figured out what I was doing wrong ... was putting quotes around the fields for the Q_PROPERTY entries. I'd still like to see a more rich example. It might be something I could add in a Wiki area ... if there is one ... when I get my hands wrapped around it.


#ifndef _MY_CLASS_H_
#define _MY_CLASS_H_

#include <QMetaClassInfo>

class cMyClass : public QObject
{
Q_OBJECT
Q_CLASSINFO("author", "John Doe")
Q_CLASSINFO("url", "http://doc.trolltech.com/")
Q_PROPERTY(int mData
READ getData()
WRITE setData())
public:

cMyClass() :
mData(0) {}

int const getData() { return mData; }

void setData(int data) { mData = data; }
int mData;
}; // cMyClass

#endif // _MY_CLASS_H_

jacek
20th September 2006, 21:55
It might be something I could add in a Wiki area ...
You can always write an article in our wiki. ;)

brcain
20th September 2006, 21:59
I still consider my self a bit of a novice ... little hesitant to write an article ... but maybe I shouldn't be. I do want to get to a point that I can be more of a contributor ... don't want to only be asking questions. I am ramping up though. I have quite a few years of dev experience ... just not Qt.

fullmetalcoder
21st September 2006, 09:18
#ifndef _MY_CLASS_H_
#define _MY_CLASS_H_

#include <QMetaClassInfo>

class cMyClass : public QObject
{
Q_OBJECT
Q_CLASSINFO("author", "John Doe")
Q_CLASSINFO("url", "http://doc.trolltech.com/")
Q_PROPERTY(int mData
READ getData()
WRITE setData())
public:

cMyClass() :
mData(0) {}

int const getData() { return mData; }

void setData(int data) { mData = data; }
int mData;
}; // cMyClass

#endif // _MY_CLASS_H_
I'm quite sure you don't need parentheses in property declaration... And it might even confuse qmake...

You may also consider using the "dynamic" properties introduced in Qt 4.2.

brcain
21st September 2006, 16:57
Thanks ... didn't notice the parens. I will look for the 4.2 features. I'm currently using 4.1.4 ... hope to switch to 4.2 soon.