Op 01/03/2016 om 13:40 schreef Sina Dogru:
Hello everyone,

Basically, I have a file which I do save some QObject-derived instances states. I do save them to be able to use those to recover the states of those QObject-derived instances.

I do also save the QMetaObject::className() in the file. And at the run-time, I would like to dynamically create QObject-derived instances from the file.

After a short research on the qt docs, I have ended to use QMetaType::create() but after tried it, it failed because of this was not for QObject.. So finally I got the point, QMetaObject is for QObjects and QMetaType for the others.. But could not find a propert QMetaObject API to create an instance from string..

Also there is stackoverflow question <http://stackoverflow.com/questions/7872578/how-to-properly-use-qregistermetatype-on-a-class-derived-from-qobject> but the answers are not seems like so elegant.

So I have decided to ask your advices how to create QObject-derived class instance dynamically from a string or class name, even with my that terrible english, I hope you don't mind it.
I think you did not get the point about QMetaType and QMetaObject. They _can_ be used together:

1) mark the constructors in the classes you want to be able to construct from your file as Q_INVOKABLE
2) register the types in the meta object system
3) get the meta type from the QMetaType using the class name
4) use the meta type to get the QMetaObject
5) use QMetaObject::construct to construct an instance of the type, making sure you pass the correct arguments

However, I think you are probably better off just using a factory pattern.

I hope this helps.

André

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to