Ok, that creates a workaround. The problem there is that I lose the type information of the property that I want to set.
I want to write plugins, so I am forced to use QObject and the meta object. I hoped that I could use const char * QMetaProperty::typeName together with void QObject::inherits(const char*) to check if I can use a particular plugin for the property. Like so:
// initialize the plugin loaders and all that jazz
QObject * pluginA
= pluginLoaderForA.
instance();
QObject * pluginB
= pluginLoaderForB.
instance();
QMetaProperty propA
= mobj
->property
(mobj
->indexOfProperty
("B"));
typeName.chop(1); //remove the * at the end
if ( pluginB.inherits(typeName.toAscii().constData()) ) {
// it inherits the object, so we can use it.
pluginA.
setProperty("B",
QVariant::fromValue(pluginB
));
}
// initialize the plugin loaders and all that jazz
QObject * pluginA = pluginLoaderForA.instance();
QObject * pluginB = pluginLoaderForB.instance();
const QMetaObject * mobjA = pluginA->metaObject;
QMetaProperty propA = mobj->property(mobj->indexOfProperty("B"));
QString typeName = QString(propA.typeName());
typeName.chop(1); //remove the * at the end
if ( pluginB.inherits(typeName.toAscii().constData()) ) {
// it inherits the object, so we can use it.
pluginA.setProperty("B", QVariant::fromValue(pluginB));
}
To copy to clipboard, switch view to plain text mode
That is of course lost when I use QObject* as the type. On the other hand I am not sure if the setProperty still works. The object is still a QObject, but I doubt that the meta object stuff knows this.
Now one solution would probably be to inherit the interface from QObject, since then I know about the interface in the meta object system. It gets tricky with multiple inheritance of interfaces though, since the inheritance path to QObject is not unique any longer.
I guess I just need to provide the type of BInterface as a const char* property and force some naming scheme on the whole thing.
p.s.: I figured out that the crash in your code is due to some QVariant stuff. This is how I got that to work
// QObject *a = new A();
// a->setProperty( "B", new B() ); // crashes
a
->setProperty
("B",
QVariant::fromValue(new B
()));
//works
// QObject *a = new A();
// a->setProperty( "B", new B() ); // crashes
QObject *a = new A();
a->setProperty("B", QVariant::fromValue(new B())); //works
To copy to clipboard, switch view to plain text mode
Bookmarks