[英]QPluginLoader instance always returns null
I am in the process of building a Qt application. 我正在构建Qt应用程序。 While I have programmed lots of GUI things before and the framework itself isn't presenting a problem, I have not ever programmed anything (outside of C# .NET applications) that has the capability to load plugins.
虽然我之前已经编写了许多GUI东西,而框架本身并没有出现问题,但我从未编程过任何能够加载插件的东西(C#.NET应用程序之外)。 The project I am trying to build has plugins as an integral part of it and I thought the Qt framework looked nice for this because it was almost universally cross-platform and seems to have a very nice system for plugins.
我正在尝试构建的项目将插件作为其不可或缺的一部分,我认为Qt框架对此看起来不错,因为它几乎普遍都是跨平台的,并且似乎有一个非常好的插件系统。
My problem is this: I can't seem to get the QPluginLoader.instance() method to return anything but a null value . 我的问题是: 我似乎无法让QPluginLoader.instance()方法返回除null值之外的任何值 。
From my readings, this is because it finds no plugin. 根据我的阅读,这是因为它找不到插件。 I think I am probably forgetting to do something, but I can find very little documentation on actually writing plugins (there are examples, but they aren't incredibly detailed).
我想我可能忘了做某事,但是我发现关于实际编写插件的文档很少(有示例,但是没有详尽介绍)。 There are plenty of examples of loading plugins and I think I am doing that right, but I haven't really found an example of actually making a plugin.
有很多加载插件的例子,我想我做对了,但是我还没有找到实际制作插件的例子。
This is what I have done: 这是我所做的:
Using Qt-Creator I have created two projects: A windowed application to pretend that it is my plugin receiving application and a Shared Library project to pretend to be the plugin 使用Qt-Creator,我创建了两个项目:一个假装是我的插件接收应用程序的窗口应用程序,以及一个假装是插件的共享库项目。
In my windowed application I have a header file as follows: 在窗口应用程序中,我有一个头文件,如下所示:
#ifndef PLUGININTERFACE_H
#define PLUGININTERFACE_H
#include <QtPlugin>
class QStringList;
class PluginInterface
{
public:
virtual ~PluginInterface() {};
virtual QStringList messages() const = 0;
};
Q_DECLARE_INTERFACE(PluginInterface,
"com.kevincuzner.LearningPlugins.PluginInterface/1.0")
#endif // PLUGININTERFACE_H
In my shared library application I created a class called ATestPlugin (and this is also the TARGET value of the project): 在我的共享库应用程序中,我创建了一个名为ATestPlugin的类(这也是项目的TARGET值):
#ifndef ATESTPLUGIN_H
#define ATESTPLUGIN_H
#include "ATestPlugin_global.h"
#include "../LearningPlugins/PluginInterface.h"
#include <QStringList>
class ATESTPLUGINSHARED_EXPORT ATestPlugin : public PluginInterface, public QObject
{
Q_OBJECT
Q_INTERFACES(PluginInterface)
public:
ATestPlugin();
virtual QStringList messages() const
{
//this part is actually defined in the .cpp file, but I don't feel like pasting that here
QStringList ret;
ret << "foo" << "bar" << "noms" << "Hello";
return ret;
}
};
#endif // ATESTPLUGIN_H
At the end of the ATestPlugin.cpp file I have placed Q_EXPORT_PLUGIN2(ATestPlugin, ATestPlugin)
在ATestPlugin.cpp文件的末尾,我放置了
Q_EXPORT_PLUGIN2(ATestPlugin, ATestPlugin)
Then in my main.cpp file in the main method I do the following (&w points to the main window): 然后在main方法的main.cpp文件中,执行以下操作(&w指向主窗口):
QString text = QFileDialog::getOpenFileName(&w, "Get a file");
QPluginLoader loader(text);
QObject* plugin = loader.instance();
if (plugin)
{
QMessageBox msgBox;
msgBox.setText(text);
msgBox.exec();
}
When I run the program and select libATestPlugin.so from the build directory of my library project in the file dialog that pops up, I see no message box which means *plugin is null. 当我运行程序并在弹出的文件对话框中从库项目的构建目录中选择libATestPlugin.so时,我看不到任何消息框,这意味着* plugin为null。 Previously I had the messagebox always pop up and show the selected file name, so I know that part is working.
以前,我总是弹出消息框并显示选定的文件名,因此我知道该部分正在工作。
Does anyone have any ideas as to what I need to do to make my plugin visible to QPluginLoader? 是否有人对我的插件对QPluginLoader可见有什么想法?
It didn't work for me. 它对我不起作用。 Change of your previous TARGET to $$qtLibraryTarget(...) ensures right extensions during build on different platforms.
将您以前的TARGET更改为$$ qtLibraryTarget(...),可以确保在不同平台上进行构建时的正确扩展。
I had the same problem with QPluginLoader. 我在QPluginLoader中遇到了同样的问题。 In few words how I solved it: I create plugin library, let's say lib1.dll which use some stuff from lib2.dll.
简而言之,我是如何解决的:我创建了插件库,比如说lib1.dll,它使用来自lib2.dll的一些东西。 In my application I try to load lib1 via QPluginLoader.
在我的应用程序中,我尝试通过QPluginLoader加载lib1。
QPluginLoader loader( adaptersDir.absoluteFilePath(fileName) );
AdapterInterface* adapterIface = qobject_cast<AdapterInterface*>(loader.instance());
In this case loader.instance() returns 0. Solution was to copy lib2.dll into application folder because an application use it for proper load plugin lib1. 在这种情况下,loader.instance()返回0。解决方法是将lib2.dll复制到应用程序文件夹中,因为应用程序将其用于正确的加载插件lib1。
After further experimentation I managed to get the plugin to load. 经过进一步的实验,我设法加载了插件。 I feel sheepish saying this, but it was a classic case of RTFM.
我这么说让我感到有些毛骨悚然,但这是RTFM的经典案例。
The echo plugin tutorial (which I hadn't found until after I posted the question) had some modifications that had to be made to the .pro file in the library to make it declare the plugin correctly: echo插件教程 (直到我发布问题后才找到),必须对库中的.pro文件进行一些修改才能使其正确声明插件:
The line TARGET = ATestPlugin
had to be changed to TARGET = $$qtLibraryTarget(ATestPlugin)
. 行
TARGET = ATestPlugin
必须更改为TARGET = $$qtLibraryTarget(ATestPlugin)
。 After making this change, my messagebox in the above program popped up and later when I ran qobject_cast< PluginInterface* >(plugin)
and then asked ran PluginInterface->messages()
I got the list which was implemented in the separate class. 进行此更改后,在上述程序中弹出我的消息框,稍后当我运行
qobject_cast< PluginInterface* >(plugin)
,然后要求运行PluginInterface->messages()
我得到了在单独的类中实现的列表。
Also, I did change the inheritance order to put QObject first and since it works that way I will probably keep it so (although, I don't know if it made a difference). 另外,我确实更改了继承顺序,将QObject放在首位,并且因为它以这种方式工作,所以我可能会保持它不变(尽管我不知道它是否有所作为)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.