I have troubles with plugins in my application. In general this is only plugin loader.
I do it like this:
//main module
int main(int argc, char *argv[])
{
QMySettings * conf = new QMySettings("","");
QDir pluginsDir
= QDir(qApp
->applicationDirPath
());
pluginsDir.cdUp();
pluginsDir.cd("plugins");
foreach
(QString fileName, pluginsDir.
entryList(QDir::Files)) {
QObject *plugin
= loader.
instance();
if(plugin)
{
MyPlugin * iface = qobject_cast<MyPlugin *>(plugin);
if(iface)
{
iface->init(conf);
}
}
}
return a.exec();
}
//main module
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMySettings * conf = new QMySettings("","");
QDir pluginsDir = QDir(qApp->applicationDirPath());
pluginsDir.cdUp();
pluginsDir.cd("plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if(plugin)
{
MyPlugin * iface = qobject_cast<MyPlugin *>(plugin);
if(iface)
{
iface->init(conf);
}
}
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
In main module I initialize class for storing app configuration. This class I want to share with all of my plugins.
{
}
//header
{
Q_OBJECT
public:
explicit Conf
(QObject *parent
= 0);
};
//cpp
{
}
Conf::Conf(QObject *parent) :
QObject(parent)
{
}
//header
class Conf : public QObject
{
Q_OBJECT
public:
explicit Conf(QObject *parent = 0);
QString getValue(int i);
};
//cpp
QString Conf::getValue(int i)
{
return QString::number(i,16);
}
To copy to clipboard, switch view to plain text mode
plugin interface
class MyPlugin
{
public:
~MyPlugin(){}
virtual void init
(QObject * ptr
) = 0;
};
Q_DECLARE_INTERFACE(MyPlugin,"com.trolltech.MyPlugin/1.0")
class MyPlugin
{
public:
~MyPlugin(){}
virtual void init(QObject * ptr) = 0;
};
Q_DECLARE_INTERFACE(MyPlugin,"com.trolltech.MyPlugin/1.0")
To copy to clipboard, switch view to plain text mode
and finally my plugin implementation
//header
class LIB1SHARED_EXPORT Lib1
: public QObject,
public MyPlugin
{ Q_OBJECT
Q_INTERFACES(MyPlugin)
public:
Lib1();
};
//cpp
Lib1::Lib1()
{
}
{
Conf * conf = reinterpret_cast<Conf *>(ptr);
qWarning(conf->getValue(1).toAscii()); // <<<<<<< here error
}
Q_EXPORT_PLUGIN2(lib1, Lib1)
//header
class LIB1SHARED_EXPORT Lib1 : public QObject,public MyPlugin {
Q_OBJECT
Q_INTERFACES(MyPlugin)
public:
Lib1();
void init(QObject * ptr);
};
//cpp
Lib1::Lib1()
{
}
void Lib1::init(QObject * ptr)
{
Conf * conf = reinterpret_cast<Conf *>(ptr);
qWarning(conf->getValue(1).toAscii()); // <<<<<<< here error
}
Q_EXPORT_PLUGIN2(lib1, Lib1)
To copy to clipboard, switch view to plain text mode
While compiling I get strange error:
C:\source\PluginTest\lib1\lib1.cpp:11: error: undefined reference to `Conf::getValue(int)'
and what's interesting, using QSetting in place of My class works fine.
P.S. Sorry for my pure English
Bookmarks