奥特QT DDS 插件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginLoader.cpp 2.0KB

5 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "PluginLoader.hpp"
  2. #include <QDebug>
  3. #include <QDir>
  4. #include <QFileInfo>
  5. #include <QCoreApplication>
  6. #include "interfaces/plugin_interface.hpp"
  7. PluginLoader::PluginLoader(QObject *parent)
  8. : QObject(parent)
  9. {
  10. }
  11. QObject* PluginLoader::loadPlugin(const QString& path)
  12. {
  13. if (m_loader.isLoaded()) {
  14. m_loader.unload();
  15. }
  16. // 添加当前工作目录的调试输出
  17. qDebug() << "当前工作:" << path;
  18. // 处理相对路径和绝对路径
  19. QString absolutePath = path;
  20. // 确保路径以 "/" 开头
  21. if (!absolutePath.startsWith('/')) {
  22. absolutePath = "/" + absolutePath;
  23. }
  24. qDebug() << "原始路径:" << path;
  25. qDebug() << "处理后的路径:" << absolutePath;
  26. // 检查文件是否存在
  27. if (!QFileInfo::exists(absolutePath)) {
  28. qDebug() << "插件文件不存在:" << absolutePath;
  29. return nullptr;
  30. }
  31. m_loader.setFileName(absolutePath); // 使用处理后的绝对路径
  32. if (!m_loader.load()) {
  33. qDebug() << "插件加载失败:" << m_loader.errorString()<<path;
  34. qDebug() << "库加载错误代码:" << m_loader.loadHints();
  35. qDebug() << "库搜索路径:" << QCoreApplication::libraryPaths();
  36. return nullptr;
  37. }
  38. QObject* plugin = m_loader.instance();
  39. if (!plugin) {
  40. qDebug() << "无法获取插件实例";
  41. return nullptr;
  42. }
  43. AuseftDDSPluginInterface* pluginInterface = qobject_cast<AuseftDDSPluginInterface*>(plugin);
  44. if (!pluginInterface) {
  45. qDebug() << "插件不是有效的 AuseftDDSPluginInterface";
  46. return nullptr;
  47. }
  48. // 连接插件的信号到加载器
  49. connect(plugin, SIGNAL(messagePublished(QString,int)),
  50. this, SIGNAL(messagePublished(QString,int)));
  51. return plugin;
  52. }
  53. bool PluginLoader::unloadPlugin()
  54. {
  55. return m_loader.unload();
  56. }
  57. QString PluginLoader::errorString() const
  58. {
  59. return m_loader.errorString();
  60. }