Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading own metadata within a Qt plugin class

Tags:

c++

json

qt

qt5

qtcore

I am currently writing my own Qt plugin to be used in a Qt program. To identify the plugin version information, I use the metadata as stored in the JSON file like the following:

{
    "type" :            "communication",
    "name" :            "USB-LIN-IB",
    "longname" :        "USB-LIN Communication",
    "version" :         "1.1",
    "dependencies" :    []
}

To access these metadata information from outside the plugin classes (within the Qt program world), I fall back on them as I have defined the JSON file like this:

Q_PLUGIN_METADATA(IID "org.plugins.communications.1" FILE "USBLINCommunication.json")

Is it possible to use a standardized and convenient way to access exactly the same metadata from within the plugin member (e.g. the plugin constructor)? Of course, I could either use the QPluginLoader (for which I have to know the plugin file path) or a file readAll from a JSON Object. However, both methods rely on knowing the exact path of the plugin and the JSON file. This is not very reliable for me.

I thought there is another more standardized way?

like image 961
Maschina Avatar asked Mar 20 '23 21:03

Maschina


1 Answers

You do not need to know the absolute plugin file path for QPluginLoader, so you would be safe.

You can just pass the plugin name, and it will work fine. It will also return the fully-qualified file name of the plugin, including the full path to the plugin if you are reading the file name back with the corresponding method. It will be clear for the class based on the information that can be obtained from QCoreApplication::libraryPaths().

Here is the corresponding part of the documentation:

When loading the plugin, QPluginLoader searches in the current directory and in all plugin locations specified by QCoreApplication::libraryPaths(), unless the file name has an absolute path. After loading the plugin successfully, fileName() returns the fully-qualified file name of the plugin, including the full path to the plugin if one was given in the constructor or passed to setFileName().

Therefore, what you could do basically is this:

myPluginLoader.metaData().value("MetaData").toObject().value("foo").toString()

where foo can be either of your aforementioned keys.

like image 189
lpapp Avatar answered Mar 23 '23 18:03

lpapp