如何使用QPluginLoader从QByteArray加载插件
How to load a plugin with QPluginLoader from a QByteArray
QPluginLoader类不提供从QByteArray加载Qt插件的方法。如何从QByteArray加载插件?
在我的例子中,插件是通过stdin发送到程序的。这就是为什么它们不能作为文件使用。
您可以先将QByteArray保存到QTemporaryFile,然后用QPluginLoader 加载它
void load_plugin_from_bytearray(const QByteArray &array) {
QTemporaryFile file;
file.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner);
if (file.open()) {
qint64 bytes_written = file.write(array);
if (bytes_written != array.size()) {
throw std::runtime_error("Writing to temporary file failed");
}
} else {
throw std::runtime_error("Could not open temporary file");
}
QPluginLoader loader(file.fileName());
QObject *plugin = loader.instance();
if (plugin) {
do_something_with_plugin(plugin);
} else {
throw std::runtime_error(loader.errorString().toStdString());
}
}
不幸的是,如果您有多个插件,并且需要多次运行我们的函数load_plugin_from_bytearray,这可能不起作用,因为QTemporaryFile可能会为临时文件重用相同的文件路径,而QPluginLoader正在缓存其加载的插件。我需要对此进行更多的调查。无论如何,您可以通过为每个QTemporaryFile 提供不同的templateName
来使临时文件路径唯一,从而避免这个问题
QTemporaryFile::QTemporaryFile(const QString & templateName)
相关文章:
- 无法加载 QML 插件:无法保护模块,因为它从未注册过
- 如何在QApplication中应用QLibraryInfo中加载的库和插件配置
- Qt无法加载可可插件
- 如何在fixup_bundle宏之后修复"即使找到Qt平台插件也无法加载" " "xcb" Qt平台插件"?
- 在" "中加载Qt平台插件"windlows"
- OpenSceneGraph不加载OpenFlight插件
- C++插件不能延迟加载dll时
- 此应用程序无法启动,因为它找不到或加载Qt平台插件"windows"
- 可以加载C/C++和Python插件的应用程序
- 在 silverlight 即插件加载之前修改 .xap 文件
- Qt自定义插件无法加载 - "Shared Library Not Found"
- 使用 C 应用程序加载的C++插件中的C++库
- QML 插件从C++加载
- 此应用程序无法启动,因为它找不到或加载Qt平台插件"xcb"
- IE插件中的加载库不起作用
- InDesign 无法加载示例插件
- Qt无法加载插件:未定义的符号iPlugin::staticMetaObject
- 如何在程序中获取加载插件的目录
- Qt Creator在编译后不能加载插件
- 如何使用QPluginLoader从QByteArray加载插件