1 Attachment(s)
Class name from QmetaProperty
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,
Re: Class name from QmetaProperty
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,
_
Re: Class name from QmetaProperty
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.
Re: Class name from QmetaProperty
There is no widget that is a subclass of both QLineEdit and QPushButton.
Cheers,
_
Re: Class name from QmetaProperty
i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.
Re: Class name from QmetaProperty
Quote:
Originally Posted by
anbu01
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,
_
Re: Class name from QmetaProperty
Quote:
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:
Code:
#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"
Re: Class name from QmetaProperty
@guru thanks this is wat im looking for......
Re: Class name from QmetaProperty
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,
_
Re: Class name from QmetaProperty
@guru im not qt certified developer , i had some logics but it didnt work.....