Is there a way to find the QMetaObject instance of a class, given the class name? what I like to do is to load objects from disk, but for this to happen, I need a way to retrieve a QMetaObject instance using the name of a class, in order to use the QMetaObject to create an instance.
相关问题
- QML: Cannot read property 'xxx' of undefin
- C# how to invoke a field initializer using reflect
- QTextEdit.find() doesn't work in Python
- QT Layouts, how to make widgets in horizontal layo
- I want to read exif info in image in android. I ca
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- Are GetCallingAssembly() and GetExecutingAssembly(
- Why doesn't valgrind detect a memory leak in m
- How do I append metadata to an image in Matlab?
- What file sytems support Java UserDefinedFileAttri
For your case the appropriate solution can be using Qt plugin mechanism. It offers functionality to easily load shared/dynamic library and check if it contains implementation of some desired interface, if so - create the instance. Details can be found here: How to Create Qt Plugins
I have been facing the same problem recently. I needed the metaobject without having to create an instance of the class itself. Best I could do is to create a global / static function that retrieves the qmetaobject given the class name. I achieved this by using QObject::staticMetaObject.
See : http://doc.qt.io/qt-5/qobject.html#staticMetaObject-var
If somebody has a better option, please share !
You can also take a look at the function:
QMetaType::metaObjectForType
whichUpdate: That's my code, it create a class by class name. (Note that the class must be registered with qRegisterMetaType (or is QObject base)
Update 2: I have forgot to say. Yout class's constructor must be marked as Q_INVOKABLE
You can store the MetaClass instances you will need in a Hash or Map, and then retrieve them via whatever name you stored them under
You should be able to do this with QMetaType. You might need
Q_DECLARE_META_TYPE()
and/orqRegisterMetaType()
to make your types known. Then it should work roughly like in this example from the link: