Hi there! I'm trying to play with plugins for my app...
here is some code:
plugin interface:
#ifndef PLUGINIF_H
#define PLUGINIF_H
class PluginInterface {
public:
virtual ~PluginInterface() {}
};
Q_DECLARE_INTERFACE(PluginInterface, "Qxygen.PluginInterface")
#endif
#ifndef PLUGINIF_H
#define PLUGINIF_H
class PluginInterface {
public:
virtual ~PluginInterface() {}
virtual QString version();
virtual QString author();
virtual QString description();
virtual QString name();
};
Q_DECLARE_INTERFACE(PluginInterface, "Qxygen.PluginInterface")
#endif
To copy to clipboard, switch view to plain text mode
plugin header:
#ifndef PLUGIN_H
#define PLUGIN_H
#include <QObject>
#include <pluginif.h>
class plugin
: public QObject,
public PluginInterface
{Q_OBJECT
Q_INTERFACES(PluginInterface)
public:
};
#endif
#ifndef PLUGIN_H
#define PLUGIN_H
#include <QObject>
#include <pluginif.h>
class plugin: public QObject, public PluginInterface {
Q_OBJECT
Q_INTERFACES(PluginInterface)
public:
QString author();
QString version();
QString description();
QString name();
};
#endif
To copy to clipboard, switch view to plain text mode
plugin implementation:
#include <QtPlugin>
#include "plugin.h"
return "Naresh";
}
return "0.0.1";
}
return "plugin descr";
}
return "plugin";
}
Q_EXPORT_PLUGIN2(my_plugin, plugin)
#include <QtPlugin>
#include "plugin.h"
QString plugin::author() {
return "Naresh";
}
QString plugin::version() {
return "0.0.1";
}
QString plugin::description() {
return "plugin descr";
}
QString plugin::name() {
return "plugin";
}
Q_EXPORT_PLUGIN2(my_plugin, plugin)
To copy to clipboard, switch view to plain text mode
here is plugin loading loop:
foreach
( QString fileName, fileNames
) { qDebug()<<dir.absoluteFilePath(fileName); // returns proper path everytime
if ( plugin ) {
PluginInterface *plif=qobject_cast<PluginInterface*>( plugin );
values<< plif->name() << plif->version();
}
qDebug()<<loader.isLoaded(); // always return false
}
QDir dir( QCoreApplication::applicationDirPath()+"/plugins/" );
QStringList fileNames=dir.entryList( QStringList("*.so"), QDir::Files, QDir::Name);
foreach( QString fileName, fileNames ) {
qDebug()<<dir.absoluteFilePath(fileName); // returns proper path everytime
QPluginLoader loader( dir.absoluteFilePath(fileName) );
QObject *plugin=loader.instance();
if ( plugin ) {
PluginInterface *plif=qobject_cast<PluginInterface*>( plugin );
QStringList values;
values<< plif->name() << plif->version();
new QTreeWidgetItem( ui.pluginList, values );
}
qDebug()<<loader.isLoaded(); // always return false
}
To copy to clipboard, switch view to plain text mode
project file looks like this:
TEMPLATE = lib
CONFIG += plugin release
INCLUDEPATH += ../interfaces
TARGET = ../../plugins/plugin
VERSION=0.0.1
HEADERS = \
plugin.h
SOURCES = \
plugin.cpp
TEMPLATE = lib
CONFIG += plugin release
INCLUDEPATH += ../interfaces
TARGET = ../../plugins/plugin
VERSION=0.0.1
HEADERS = \
plugin.h
SOURCES = \
plugin.cpp
To copy to clipboard, switch view to plain text mode
As you can see in comments... plugin loading loop always return false for isLoaded and never loads plugin... and path it uses always is proper... any ideas?
Bookmarks