vendredi 3 février 2017

Qt - get QVariant value using QMetaType

I am trying to figure out how to get an instance of "MyClass" from a QVariant, the problem is that the name of "MyClass" is loaded dynamically, so i cannot do something like this:

QVariant v = ...; //it has the data!
MyClass c = v.value<MyClass>();

So, I have a QVariant with the data (I read it from a Q_PROPERTY)

QVariant variant = this->property(propertyName);

And I need to create an instance of "MyClass" with the QVariant data... just using the className (it is a QString or const char *)

is it possible to do that?

NOTES:

  • I have already added Q_DECLARE_METATYPE(MyClass*) and (MyClass)
  • I could created an instance of "MyClass", but I could NOT read data from the QVariant using properties to fill that instance

code to create an instance using the name:

int id = QMetaType::type("MyClass*");
const QMetaObject *mo = QMetaType::metaObjectForType(id);
QObject* o = mo->newInstance();





Aucun commentaire:

Enregistrer un commentaire