我可以从Qt插件访问大量元数据,但我无法以QMetaEnums的身份访问枚举。然而,我能够在我的类中获取返回该枚举的方法,并且能够将其转换为QMetaType并获取其id(1026)。我也需要QMetaEnum中包含的信息。我想我错过了什么。请看一下我的代码:
//Plugin interface
class PluginInterface
{
public:
virtual void initialize() = 0;
};
#define PluginInterface_iid "pluginInterface"
Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
//Actual plugin implementing PluginInterface
enum Fruit{ Apple, Pear, Mango };
Q_DECLARE_METATYPE(Fruit)
class MYQTCALCPLUGINSHARED_EXPORT MyQtCalcPlugin : public QObject, PluginInterface
{
Q_CLASSINFO("version", "0.1")
Q_OBJECT
Q_PLUGIN_METADATA(IID PluginInterface_iid FILE "myqtcalcplug.json")
Q_INTERFACES(PluginInterface)
Q_ENUMS(Fruit)
public:
explicit MyQtCalcPlugin(QObject *parent = 0);
void MyQtCalcPlugin::initialize()
{
qRegisterMetaType<MyQtCalcPlugin*>("MyQtCalcPluginPtr");
qRegisterMetaType<Fruit>("Fruit");
qRegisterMetaType<Fruit*>("FruitPtr");
}
public slots:
Fruit TasteFruit()
{
return Fruit::Apple;
}
};
#endif // MYQTCALCPLUGIN_H
//application that is reading the metadata
QPluginLoader pluginLoader(pluginPath);
if (pluginLoader.load());
QObject *pluginInstance = pluginLoader.instance();
auto pluginInterface = qobject_cast<PluginInterface*>(pluginInstance);
pluginInterface->initialize();
const QMetaObject *pMetaObject = pluginInstance->metaObject();
int count = pMetaObject->enumeratorCount(); //count becomes 0
答案 0 :(得分:0)
Enum已经从班级外部转移到班级内部。这解决了这个问题。 Q_DECLARE_META_TYPE(Fruit)和qRegisterMetaType是多余的:
c_views.reduceByKey(lambda x, y: x + y)