PDA

View Full Version : QPluginLoader doesn't load plugin



BeS
26th November 2008, 18:18
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:



#ifndef INTERFACE_H
#define INTERFACE_H

#include <QtPlugin>

class PluginInterface
{
public:
virtual ~PluginInterface() { }

virtual QString pluginName();
};

Q_DECLARE_INTERFACE(PluginInterface, "cops.demo.PluginInterface/1.0")

#endif


Here is the code of the plugin:

header-file:


#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:
QString pluginName();

};

#endif


and source file:


#include <QtGui>
#include <iostream>

#include "myPlugin.h"

QString MyPlugin::pluginName()
{
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:



void MainWindow::loadPlugins()
{
QDir pluginDir(QApplication::applicationDirPath());

if (!pluginDir.cd("plugins"))
return;

foreach (QString fileName, pluginDir.entryList(QDir::Files)) {
std::cout << qPrintable(fileName) << "\n";
QPluginLoader loader(pluginDir.absoluteFilePath(fileName));
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!

wysota
26th November 2008, 18:35
What does QPluginLoader::isLoaded() return? What does QPluginLoader::errorString() return?

BeS
26th November 2008, 18:48
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)

wysota
26th November 2008, 18:58
The method in the interface should be pure virtual (=0) so that you don't have to have a constructor.

jacek
26th November 2008, 19:02
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.

BeS
26th November 2008, 19:07
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?

jacek
26th November 2008, 19:12
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.