简体   繁体   中英

Qt plugin: Unable to get enum's metadata from a Qt plugin

I can access a lot of metadata from my Qt plugin, but I cannot access the enumerations as QMetaEnums. I am however able to get the method in my class which returns that enum, and am able to convert it to QMetaType and get its id (1026). I need the info contained in QMetaEnum too. I think I am missing something. Please take a look at my code:

//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

Enum has been moved from outside of the class to inside of it. This fixed the issue. Q_DECLARE_META_TYPE(Fruit) and qRegisterMetaType were redundant:

#define PluginInterface_iid "pluginInterface"
Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)

//Actual plugin implementing PluginInterface
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:
    enum Fruit
    { 
        Apple, 
        Pear, 
        Mango 
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM