PDA

View Full Version : How to get a pointer to a QObject's property ?



HiJack
27th August 2010, 05:46
Assuming here's a class named QXXX inherited from QObject, how can i get the address of the property ["Abc"], any idea?



class QABC : public QObject
{
Q_OBJECT
Q_PROPERTY(int Val READ Val WRITE setVal)
public:
QABC (QObject* parent = 0, int a = 987) : QObject(parent)
{
setVal(a);
}
~QABC ()
{
}
QABC (const QABC & rhs)
{
copyFrom(rhs);
}

QABC & copyFrom(const QABC & rhs)
{
if(this == &rhs)
return *this;

setVal(rhs._val);

return *this;
}

QABC & operator = (const QABC & rhs)
{
return copyFrom(rhs);
}

int Val() const { return _val; }
void setVal( int val ) { _val = val; }
private:
int _val;
};
class QXXX : public QObject
{
Q_OBJECT
Q_PROPERTY(QABC Abc READ Abc WRITE setAbc)
public:
QXXX(QObject* parent = 0, QABC a = QABC()) : QObject(parent)
{
setAbc(a);
}
~QXXX()
{
}
QXXX(const QXXX& rhs)
{
copyFrom(rhs);
}

QXXX& copyFrom(const QXXX& rhs)
{
if(this == &rhs)
return *this;

setAbc(rhs._Abc);

return *this;
}

QXXX& operator = (const QXXX& rhs)
{
return copyFrom(rhs);
}

QAbc Abc() const { return _Abc; }
void setAbc( QAbc val ) { _Abc= val; }
private:
QABC _Abc;
};



QXXX x;
...
QObject * z = (QObject*)x;
QObject* pointer = z ...?? ???? // to get (&QXXX::_Abc)
I just only know the property name, it's 'Abc', can i get &z->_Abc?

HiJack
27th August 2010, 10:43
somebody ...

aamer4yu
27th August 2010, 13:23
QObject::property

HiJack
27th August 2010, 20:57
QObject::property

Man do you mean z->property("Abc") ?? No, I want to get the member's address. For example, I got a QObject* pointer, even I'm not sure whether it has a propery named 'Abc'. If it has,
then give me back its address, please. It should be a QObject* pointer.
something like this.

hobbyist
27th August 2010, 21:46
I got a QObject* pointer, even I'm not sure whether it has a propery named 'Abc'. If it has,
then give me back its address, please.


Did you check the source? See QObject::property() and QMetaProperty::read() for the dirty details.

HiJack
28th August 2010, 03:52
Did you check the source? See QObject::property() and QMetaProperty::read() for the dirty details.
Ya I do have seen back to QMetaProperty, but thers's no way to help me to get the member's address unless you modify the moc file. ( seems like anti-oop, hur? )
I can enumerate the children list of one QObject object, but first I must set each member's parent, and second there's no one child pointer if the member's type is int, or QString, bool, etc. so I gave up this way.
Another suggestion?

HiJack
28th August 2010, 09:35
solved manually. you guys.

wysota
28th August 2010, 10:54
Properties are not QObjects and don't have addresses. I think your whole concept is flawed because of a wrong assumption. If you'd like to get an address to a value of a property, it still wouldn't have worked as QVariant (which stores the value) is implicitly shared and thus the place in memory holding the value may vary in time (and you'd get a stale pointer). If you want to have a property which is QObject then that's another flaw as QVariant values are mutable and QObjects are not. You should only store a pointer to a QObject within a property.


class PropVal : public QObject {
Q_PROPERTY(int val READ ... WRITE ...);
// ...
};

class ABC : public QObject {
Q_PROPERTY(QObject* abc READ ... WRITE ...);
// ...
};
// ...
ABC abc;
PropVal *prop = qobject_cast<PropVal*>(qvariant_cast<QObject*>(abc.property("abc")));
if(prop) {
int val = prop->property("val").toInt();
}