gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:37 24 4
gpt4 key购买 nike

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

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

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

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

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

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

最佳答案

您不需要知道QPluginLoader 的绝对插件文件路径,所以您会很安全。

您可以只传递插件名称,它会正常工作。它还将返回插件的完全限定文件名,如果您使用相应的方法读回文件名,则包括插件的完整路径。根据可以从 QCoreApplication::libraryPaths() 中获取的信息,类将一目了然。

这里是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().

因此,您基本上可以做的是:

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

其中 foo 可以是上述任一键。

关于c++ - 在 Qt 插件类中读取自己的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23034047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com