Hi,
can I instantiate multiple object of a plugin class?
I show you a prototype of code I'd like to use
QObject *instance
= loader.
instance ();
if (instance) {
IExample *plugin = qobject_cast<IExample*>(instance);
if (plugin) {
qDebug() << instance->metaObject ()->className () << "plugin says:"
<< plugin->messageString ();
newObj = instance->metaObject ()->newInstance (0);
if (0 != newObj)
qDebug () << "Create New Instance OK";
else
qDebug () << "Create New Instance Failed";
}
}
QPluginLoader loader (pluginDir.absoluteFilePath (fileName));
QObject *instance = loader.instance ();
QObject *newObj = 0;
QMetaObject *mo = 0;
if (instance) {
IExample *plugin = qobject_cast<IExample*>(instance);
if (plugin) {
qDebug() << instance->metaObject ()->className () << "plugin says:"
<< plugin->messageString ();
newObj = instance->metaObject ()->newInstance (0);
if (0 != newObj)
qDebug () << "Create New Instance OK";
else
qDebug () << "Create New Instance Failed";
}
}
To copy to clipboard, switch view to plain text mode
where IExample is defined
class IExample
{
public:
virtual ~IExample () {}
virtual QString messageString
() const = 0;
};
Q_DECLARE_INTERFACE (IExample, "local.costa.IExample/1.0")
class IExample
{
public:
virtual ~IExample () {}
virtual QString messageString () const = 0;
};
Q_DECLARE_INTERFACE (IExample, "local.costa.IExample/1.0")
To copy to clipboard, switch view to plain text mode
and this is the loaded plugin
class HelloWorldPlugin
: public QObject, IExample
{
Q_OBJECT
Q_INTERFACES (IExample)
public:
HelloWorldPlugin
(QObject *parent
= 0);
};
class HelloWorldPlugin: public QObject, IExample
{
Q_OBJECT
Q_INTERFACES (IExample)
public:
HelloWorldPlugin(QObject *parent = 0);
QString messageString () const;
};
To copy to clipboard, switch view to plain text mode
HelloWorldPlugin
::HelloWorldPlugin(QObject *parent
){
}
QString HelloWorldPlugin
::messageString() const {
}
Q_EXPORT_PLUGIN2 (HelloWorldPlugin, HelloWorldPlugin)
HelloWorldPlugin::HelloWorldPlugin(QObject *parent)
: QObject (parent)
{
}
QString HelloWorldPlugin::messageString() const
{
return QString("Hello World");
}
Q_EXPORT_PLUGIN2 (HelloWorldPlugin, HelloWorldPlugin)
To copy to clipboard, switch view to plain text mode
the output is
HelloWorldPlugin plugin says: "Hello World"
Create New Instance Failed
Found 1 entries
HelloWorldPlugin plugin says: "Hello World"
Create New Instance Failed
Found 1 entries
To copy to clipboard, switch view to plain text mode
Bookmarks