QPluginLoader doesn't load plugin
Hello,
i try to load a plugin into my application. I have followed the "Text Area" example from the book "C++ GUI Programming with Qt4" but my application refuses to load the plugin.
That's how i have defined the plugin interface:
Code:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QtPlugin>
class PluginInterface
{
public:
virtual ~PluginInterface() { }
};
Q_DECLARE_INTERFACE(PluginInterface, "cops.demo.PluginInterface/1.0")
#endif
Here is the code of the plugin:
header-file:
Code:
#ifndef MYPLUGIN_H
#define MYPLUGIN_H
#include <QString>
#include "myPlugin.h"
#include "../pluginInterface.h"
class MyPlugin
: public QObject,
public PluginInterface
{
Q_OBJECT
Q_INTERFACES(PluginInterface)
public:
};
#endif
and source file:
Code:
#include <QtGui>
#include <iostream>
#include "myPlugin.h"
{
return "My Plugin Name";
}
Q_EXPORT_PLUGIN2(myplugin, MyPlugin)
I can compile the plugin and get a libmyPlugin.so in my plugin directory.
Here you can see how i try to load the plugin in my application:
Code:
void MainWindow::loadPlugins()
{
if (!pluginDir.cd("plugins"))
return;
foreach
(QString fileName, pluginDir.
entryList(QDir::Files)) { std::cout << qPrintable(fileName) << "\n";
if (PluginInterface *interface = qobject_cast<PluginInterface *>(loader.instance())) {
interfaces.append(interface);
std::cout << qPrintable(interface->pluginName()) << "loaded \n";
} else {
std::cout << "not loaded!\n";
}
}
}
The program finds the plugin ("std::cout << qPrintable(fileName) << "\n";" shows the filename of the plugin) but the if statement is always false so that i end in the else-arm.
Do you have an idea what's wrong here?
Thanks a lot!
Re: QPluginLoader doesn't load plugin
Re: QPluginLoader doesn't load plugin
QPluginLoader::isLoaded() returns false and QPluginLoader::errorString() says:
QLibrary::load_sys: Cannot load /home/foo/src/demo/plugins/libmyPlugin.so (/home/foo/src/demo/plugins/libmyPlugin.so: undefined symbol: _ZTI15PluginInterface)
Re: QPluginLoader doesn't load plugin
The method in the interface should be pure virtual (=0) so that you don't have to have a constructor.
Re: QPluginLoader doesn't load plugin
That symbol has something to do with RTTI. Did you compile the plugin with the same options as Qt? Also add = 0 after pluginName() in PluginInterface.
Re: QPluginLoader doesn't load plugin
Thank you! The missing "= 0" was the problem, now it works.
Maybe a dump question but why do i need "= 0" at the end of the method?
Re: QPluginLoader doesn't load plugin
Quote:
Originally Posted by
BeS
Maybe a dump question but why do i need "= 0" at the end of the method?
"= 0" means that the method is abstract. I.e. it has no implementation in this class and that this implementation will be provided by derived classes.
Without = 0, if you won't provide some implementation of that method, you will get linker error when you try to use it.