Originally Posted by
anbu01
i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.
Seems to be fairly straightforward.
You start with an instance of a QObject sub-class, e.g. MyTextEdit. Find its QMetaObject pointer. Repeat until the metaobject pointer is null:
- extract the className(),
- enumerate the properties coming from that class (see QMetaObject::propertyCount()), and
- use QMetaObject::superClass() to get the parent class meta object.
By now you could have built a map from property name to class name, which is most useful if you are building a Designer-like editor.
For a one-off lookup you can just scan backward until metaObject->propertyOffset() is less than or equal to the property index for the desired property.
A full example:
#include <QApplication>
#include <QLabel>
#include <QMetaObject>
#include <QMetaProperty>
#include <QDebug>
{
Q_OBJECT
Q_PROPERTY(int floober READ floober WRITE setFloober)
public:
int floober() const { return m_floober; }
void setFloober(int value) { m_floober = value; }
private:
int m_floober;
};
{
if (object) {
const int index = mo->indexOfProperty(name);
if (index != -1) {
while (mo && mo->propertyOffset() > index)
mo = mo->superClass();
return QString::fromLatin1(mo
->className
());
}
}
}
int main(int argc, char **argv)
{
Label l;
// To develop a complete map
while (mo) {
qDebug() << "From" << mo->className();
for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
qDebug() << " " << mo->property(i).name();
}
mo = mo->superClass();
}
// Use an as-required lookup
qDebug() << classForProperty(&l, "floober");
qDebug() << classForProperty(&l, "modal");
qDebug() << classForProperty(&l, "doesNotExist");
return 0;
}
#include "main.moc"
#include <QApplication>
#include <QLabel>
#include <QMetaObject>
#include <QMetaProperty>
#include <QDebug>
class Label: public QLabel
{
Q_OBJECT
Q_PROPERTY(int floober READ floober WRITE setFloober)
public:
Label(QWidget *p = 0): QLabel(p), m_floober(0) { }
int floober() const { return m_floober; }
void setFloober(int value) { m_floober = value; }
private:
int m_floober;
};
QString classForProperty(QObject *object, const char *name)
{
if (object) {
const QMetaObject *mo = object->metaObject();
const int index = mo->indexOfProperty(name);
if (index != -1) {
while (mo && mo->propertyOffset() > index)
mo = mo->superClass();
return QString::fromLatin1(mo->className());
}
}
return QString();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Label l;
// To develop a complete map
const QMetaObject *mo = l.metaObject();
while (mo) {
qDebug() << "From" << mo->className();
for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
qDebug() << " " << mo->property(i).name();
}
mo = mo->superClass();
}
// Use an as-required lookup
qDebug() << classForProperty(&l, "floober");
qDebug() << classForProperty(&l, "modal");
qDebug() << classForProperty(&l, "doesNotExist");
return 0;
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks