如何从类名称中找到Qt元对象实例? [英] How can I find a Qt metaobject instance from a class name?
本文介绍了如何从类名称中找到Qt元对象实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在给定类名的情况下,是否可以找到类的QMetaObject实例?我想做的是从磁盘加载对象,但是要实现这一点,我需要一种使用类名检索QMetaObject实例的方法,以便使用QMetaObject创建实例.
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.
推荐答案
您应该可以使用 QMetaType .您可能需要Q_DECLARE_META_TYPE()
和/或qRegisterMetaType()
才能知道您的类型.然后,它应该大致类似于本示例中的链接:
You should be able to do this with QMetaType. You might need Q_DECLARE_META_TYPE()
and/or qRegisterMetaType()
to make your types known. Then it should work roughly like in this example from the link:
int id = QMetaType::type("MyClass");
if (id == 0) {
void *myClassPtr = QMetaType::construct(id);
...
QMetaType::destroy(id, myClassPtr);
myClassPtr = 0;
}
这篇关于如何从类名称中找到Qt元对象实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文