PDA

View Full Version : Class name from QmetaProperty



anbu01
29th April 2014, 08:17
hi ,
i am working on making my own designer widget that looks and functions like qt.But now i needed to know how the property is created.I knew we can get the properties of an widget using QmetaObject and QMetaProperty but my question is will i be able to get the class name of each property.Like object name property comes from QObject and geomentry property comes from QWidget.Is it so that i should hard code myself to model or is there a way to get the classInfo from property.I have attached the image which im trying to achieve any response or related post is appreciated.

thanks,

anda_skoa
29th April 2014, 08:35
I am sure the property editor does this by going through the inheritance hierarchy.

I.e. it goes down to the QMetaObject fof QObject, lists its properties, then to the QMetaObject of the first derived class, and so on.

QMetaObject::propertyOffset() tells you where the properties of the specific level start.

Cheers,
_

anbu01
29th April 2014, 08:45
thanks for the reply @anda_skoa but according to doc's the offset is sum of all the properties so i cannot differentiate between QpushButton and QlineEdit so now what can be done to do so.Is there any similiar function to do like we pass object we get property as well as their superclass.

anda_skoa
29th April 2014, 09:18
There is no widget that is a subclass of both QLineEdit and QPushButton.

Cheers,
_

anbu01
29th April 2014, 09:20
i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.

anda_skoa
29th April 2014, 13:04
i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.

I already answered that. You came up with some kind of situation that is not possible to exist.

Cheers,
_

ChrisW67
29th April 2014, 22:22
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>

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"

anbu01
30th April 2014, 13:58
@guru thanks this is wat im looking for......

anda_skoa
30th April 2014, 16:50
Ah, too lazy to do it yourself?
You could have said so instead of creating mythical obstacles like classes inheriting from two widget base classes

Cheers,
_

anbu01
1st May 2014, 18:28
@guru im not qt certified developer , i had some logics but it didnt work.....