在Qt插件类中读取自己的元数据

时间:2021-07-08 23:16:54

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:

我目前正在编写自己的Qt插件,用于Qt程序。为了识别插件版本信息,我使用存储在JSON文件中的元数据,如下所示:

{
    "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:

要从插件类外部(在Qt程序世界中)访问这些元数据信息,我会依赖它们,因为我已经定义了这样的JSON文件:

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.

是否可以使用标准化且方便的方法从插件成员(例如插件构造函数)中访问完全相同的元数据?当然,我可以使用QPluginLoader(我必须知道插件文件路径)或来自JSON对象的文件readAll。但是,这两种方法都依赖于知道插件和JSON文件的确切路径。这对我来说不太可靠。

I thought there is another more standardized way?

我以为还有另一种更标准化的方式?

1 个解决方案

#1


5  

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

您不需要知道QPluginLoader的绝对插件文件路径,因此您将是安全的。

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().

您可以只传递插件名称,它将正常工作。它还将返回插件的完全限定文件名,如果您使用相应的方法读回文件名,则包括插件的完整路径。基于可以从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().

加载插件时,QPluginLoader将在当前目录和QCoreApplication :: libraryPaths()指定的所有插件位置中进行搜索,除非文件名具有绝对路径。成功加载插件后,fileName()返回插件的完全限定文件名,包括插件的完整路径(如果在构造函数中给出了一个或传递给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.

其中foo可以是你上面提到的键之一。

#1


5  

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

您不需要知道QPluginLoader的绝对插件文件路径,因此您将是安全的。

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().

您可以只传递插件名称,它将正常工作。它还将返回插件的完全限定文件名,如果您使用相应的方法读回文件名,则包括插件的完整路径。基于可以从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().

加载插件时,QPluginLoader将在当前目录和QCoreApplication :: libraryPaths()指定的所有插件位置中进行搜索,除非文件名具有绝对路径。成功加载插件后,fileName()返回插件的完全限定文件名,包括插件的完整路径(如果在构造函数中给出了一个或传递给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.

其中foo可以是你上面提到的键之一。