skydave
26th June 2009, 02:20
Hi I have the following and I guess very stupid questions:
I have defined a usertype which I store in a qvariant. In my app I later try to get the data (which is a class pointer) out of the qvariant via
myVariant.value<MyClass *>();
But the returned value is a zero pointer. I have tracked the problem down to this code in qvariant.h(line 564 qt4.5.0):
template<typename T> T qvariant_cast(const QVariant &v)
{
const int vid = qMetaTypeId<T>(static_cast<T *>(0)); // returns 257 which is the typeid of MyClass*
if (vid == v.userType()) //PROBLEM: v.userType returns 256
return *reinterpret_cast<const T *>(v.constData()); // wont get executed...but should be
if (vid < int(QMetaType::User)) {
T t;
if (qvariant_cast_helper(v, QVariant::Type(vid), &t))
return t;
}
return T(); // zero pointer (in debug mode)
}
The function compars the typeid of my userdata which is 257 with the userType value returned from v. Only when they are the same the function actually casts the constData into MyClass*. But when one has more Userdata types (than one) those all get an id bigger than 256 which is the base id of all userdata type ids. So this comparison seems quite weird to me.
I know that the qtcode is working as it should and therefore I guess I have some big missunderstanding of userdata type ids and their usage. Can someone help me and shed some light on how I get my userdata casted correctly?
Many thanks in advance,
David
P.S.: Hi All, great forum!
I have defined a usertype which I store in a qvariant. In my app I later try to get the data (which is a class pointer) out of the qvariant via
myVariant.value<MyClass *>();
But the returned value is a zero pointer. I have tracked the problem down to this code in qvariant.h(line 564 qt4.5.0):
template<typename T> T qvariant_cast(const QVariant &v)
{
const int vid = qMetaTypeId<T>(static_cast<T *>(0)); // returns 257 which is the typeid of MyClass*
if (vid == v.userType()) //PROBLEM: v.userType returns 256
return *reinterpret_cast<const T *>(v.constData()); // wont get executed...but should be
if (vid < int(QMetaType::User)) {
T t;
if (qvariant_cast_helper(v, QVariant::Type(vid), &t))
return t;
}
return T(); // zero pointer (in debug mode)
}
The function compars the typeid of my userdata which is 257 with the userType value returned from v. Only when they are the same the function actually casts the constData into MyClass*. But when one has more Userdata types (than one) those all get an id bigger than 256 which is the base id of all userdata type ids. So this comparison seems quite weird to me.
I know that the qtcode is working as it should and therefore I guess I have some big missunderstanding of userdata type ids and their usage. Can someone help me and shed some light on how I get my userdata casted correctly?
Many thanks in advance,
David
P.S.: Hi All, great forum!