3

我尝试使用以下代码加载我的插件:

QString path = QFileDialog::getOpenFileName(0);
QPluginLoader loader(path);
AnprPluginInterface *i = qobject_cast< AnprPluginInterface* >(loader.instance());
if (i == NULL )
    QMessageBox::information(0, "this", "error loading plugin." + loader.errorString());
else
    QMessageBox::information(0, "this", "plugin loaded.");

我将插件的绝对路径发送到,QPluginLoader但它说找不到插件!
错误是:

error loading plugin. "Cannot load library The specified module could not be found." 
4

1 回答 1

15

您确定您的插件使用 Q_INTERFACES() 宏导出了正确的接口吗?如果您收到错误,您有两个可能的事情要检查:

1) loader.instance() 返回 0:在这种情况下,您必须调查报告的错误。

2) qobject_cast 返回 0:在这种情况下,我认为问题出在缺少导出的接口上。

此外,您应该考虑到您的两个执行路径都在执行相同的代码,所以也许您根本没有收到错误......我指的是这个:

if (i == NULL )
    // Reports the error
    QMessageBox::information(0, "this", "error loading plugin." + loader.errorString());
else
    // Hey!? WTF!? Repots the error anyway!?
    QMessageBox::information(0, "this", "error loading plugin." + loader.errorString());

3)我忘记了:还要检查插件和应用程序是否以相同的方式构建(调试/发布)。

4)顺便说一句,应该检查的另一件事是插件是否带有一些依赖项(例如其他动态库)。它发生在我身上一次,我花了很长时间才发现我缺少一个 DLL,这导致我的插件无法正确加载!

于 2013-07-29T09:50:00.910 回答