⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Qt Plugin function of metadata

  • Hi,

    I saw in the documentation that you can set metadata for plugins you create.
    The metadata itself is then in a json file and can be accessed via functions in C++.

    Now I was wondering what kind of metadata should go in there or what is the advantage over storing it in C++.

    For example I need my plugins to have names, so I can identify them in my c++ code. So I could write a name property in the json file or (what i currently do) is just add a function name() to my plugin, which returns me the name.

    And currently I see no advantage in using the json approach, in contrast ... if I would use json, I would have to parse the information from the file, whereas with the c++ method, I can get the informations I need directly ...

    So when should I use the json approach? Is it useful at all?

  • @leon_2001 Hi,

    There is some explanation in the doc of QPluginLoader::metaData()

    Returns the meta data for this plugin. The meta data is data specified in a json format using the Q_PLUGIN_METADATA() macro when compiling the plugin.
    The meta data can be queried in a fast and inexpensive way without actually loading the plugin. This makes it possible to e.g. store capabilities of the plugin in there, and make the decision whether to load the plugin dependent on this meta data.

    Hope it helps

    Edit: I think the only advantage is if you want access these values before to load the plugin but otherwise it's more usual to add (pure) virtual functions into your plugin interface for properties like name, version, etc.. if you want to access them from code it would be simpler, and also it's a good practice IMHO because you make them mandatory.

    For the version, I'm using the value declared in the .pro file, so it's always consistent with the dll file version:

    win32:VERSION = # major.minor.patch.build
    else:VERSION = 1.2.3    # major.minor.patch


       virtual QString name() = 0; // or {return this->metaObject()->className();} or default value
       virtual QString version() = 0; // or {return "default_value";}


    QString MyPlugin::name()
        return "My Plugin";
    QString MyPlugin::version()
        return VERSION;

  • Hi,

    thanks for the reply!

    it's a good practice IMHO because you make them mandatory.

    I thought about that, because that's a very strong argument! So I searched a little bit, if it's really not possible and found json schema

    JSON Schema is a vocabulary that allows you to annotate and validate JSON documents.

    Because actually I like the idea of having metadata in a json document, which you can access without loading the plugin.
    A third idea would be to have it both ... so either habe a json scheme + pure virtual functions or only have a json scheme and in the constructor of the interface parse the information from the json and implement getter/setters for it, so it's easy accessible from code, if the plugin is loaded.

    Edit: But there is currently no direct support for json scheme in Qt as far as I know, so that's one downside of this approach for now

  • @leon_2001 I never used JSON scheme so I don't know about this point. But depending of the number of the fields in your file, it should not be so difficult to check the format "manually" using Qt JSON api.

    You could maybe generate the JSON file at compilation.
    The choice of the method (json + pure virtual, json only, pure virtual only...) is up to you and what you need for your final application.

Log in to reply