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() { }
};
Q_DECLARE_INTERFACE(PluginInterface, "cops.demo.PluginInterface/1.0")
#endif
#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
To copy to clipboard, switch view to plain text mode
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:
};
#endif
#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
To copy to clipboard, switch view to plain text mode
and source file:
#include <QtGui>
#include <iostream>
#include "myPlugin.h"
{
return "My Plugin Name";
}
Q_EXPORT_PLUGIN2(myplugin, MyPlugin)
#include <QtGui>
#include <iostream>
#include "myPlugin.h"
QString MyPlugin::pluginName()
{
return "My Plugin Name";
}
Q_EXPORT_PLUGIN2(myplugin, MyPlugin)
To copy to clipboard, switch view to plain text mode
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()
{
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";
}
}
}
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";
}
}
}
To copy to clipboard, switch view to plain text mode
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!
Bookmarks