[SOLVED] Qt Meta-Obect System: QObject::setProperty() with QVariant user defined types - fails
-
What's the (apparent) type of objVals?
[quote]I have declared all the types with Q_DECLARE_METATYPE[/quote]
Which types are you talking about exactly?
[quote author="steno" date="1311784107"]Do do you register the meta type with the call qRegisterMetaType("your custom meta type")?[/quote]
Not needed.
-
For the sake of trying to pinpoint what happens: if you try to set a property by directly entering its name instead of retreiving it from your array, and then calling setProperty on the QObject* pointer, does that work?
I mean: what happens if you use:
@
myQObjectPtr->setProperty('ThePropertyOfTheSubclass', QVariant("a value"));
@ -
bq. peppe wrote:
What’s the (apparent) type of objVals?The type of objVals is QObject** (array of QObject pointers).
bq. peppe wrote:
Which types are you talking about exactly?I have created my own classes, that inherit QObject. I'm registering the pointers to those classes.
For example
@class Class1 : public QObject {
Q_OBJECTpublic:
Class1() {
}virtual ~Class1() { }
};
Q_DECLARE_METATYPE(Class1*)@
Then I have another class Class2 containing a reference to Class1
@class MyClass : public QObject {
Q_OBJECT
Q_PROPERTY (Class1* cl READ getCl WRITE setCl)
Class1* cl;public:
MyClass() {
cl = NULL;
}Class1* getCl() const { return cl; } void setCl(Class1 *obj) { cl = obj; }
};@
bq. Andre wrote:
I mean: what happens if you use:
@myQObjectPtr->setProperty('ThePropertyOfTheSubclass', QVariant("a value"));@It doesn't work either
Here is a sample code that you can compile and run to troubleshoot the problem (Qt console application)
main.cpp:
@#include <QtCore/QCoreApplication>#include <iostream>
#include "MyClasses.h"using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);MyClass mc; QObject* c = new Class1; QVariant value; value.setValue(c); if (mc.setProperty("cl",value) == false) cout<<"setProperty failed"<<endl; return a.exec();
}
@header file (MyClasses.h)
@#ifndef MYCLASSES_H
#define MYCLASSES_H#include <QObject>
#include <QVariant>
#include <QMetaObject>class Class1: public QObject {
Q_OBJECT
public:
virtual ~Class1() {}
};Q_DECLARE_METATYPE(Class1*)
class MyClass: public QObject {
Q_OBJECT
Q_PROPERTY (Class1* cl READ getCl WRITE setCl)
Class1* cl;public:
MyClass() {
cl = NULL;
}Class1* getCl() const { return cl; } void setCl(Class1 *obj) { cl = obj; } virtual ~MyClass() {}
};
#endif // MYCLASSES_H
@ -
No, that code is wrong -- the type of the QVariant is QObject*, which does not match the type of the property (Class1*). Therefore the setProperty will fail. Create your QVariant from a Class1* value and it will work.
Yes, it's a kind of limitation of the whole QMetaType stuff...
-
[quote author="genC" date="1311851619"]Oh, one last thing: why can't the QVariant of type QObject* be converted to a Class1* (using QVariant::convert(Type t))?[/quote]Because QVariant::type() returns 2 different type ids for Class1* and QObject*, and you can see in the source code of QVariant::canConvert(Type) that if the two type ids are different the functions returns false except for a few hard-coded conversions between Qt builtin types or between numeric types.
But if it's really a composition and not an aggregation, you could just add the QObjects as children, and give them names (QObject::setObjectName) to find them later with yourContainerObject->findChild<Class1*>("yourObjectName");.