Qt-interest Archive, May 2008
Qt Plugins and Constuctor Parameters
Message 1 in thread
Hi there,
Is there a way to load dynamic qt plugins which need to be initialised
while they are instantiated?
Here's an example code for the class definition:
#include <QObject>
class Plugin : public QObject
{
Q_OBJECT
public:
Plugin(int mustHaveParameter, QObject * optionalParent = NULL)
: Attribute(mustHaveParameter), QObject(optionalParent) {}
~Plugin();
private:
int Attribute;
}
Q_DECLARE_INTERFACE(Plugin, "Program.Plugin/1.0")
Cheers,
Wolfgang
--
[ signature omitted ]
Message 2 in thread
Well, I guess, I didn't make myself clean enough. This class may seem
absolutely alright, and it is. But my problem is, that I cannot load it
using QPluginLoad or in a similar easy way, since QPluginLoader doesn't
take any arguments, to pass to the constructor (as far as I know), and
there is no similar easy way, I am aware of.
So, what can I do, to load a plugin like this?
Cheers,
Wolfgang
--
[ signature omitted ]
Message 3 in thread
Wolfgang Lorenz wrote:
> Well, I guess, I didn't make myself clean enough. This class may seem
> absolutely alright, and it is. But my problem is, that I cannot load it
> using QPluginLoad or in a similar easy way, since QPluginLoader doesn't
> take any arguments, to pass to the constructor (as far as I know), and
> there is no similar easy way, I am aware of.
Create a PluginFactory that creates your plugins...
i.e. use a layer of indirection:
class Plugin : public QObject
{
Q_OBJECT
public:
Plugin(int mustHaveParameter, QObject * optionalParent = NULL)
: Attribute(mustHaveParameter), QObject(optionalParent) {}
~Plugin();
private:
int Attribute;
};
class PluginFactory : public QObject
{
Q_OBJECT
public:
PluginFactory() {}
Plugin* createPlugin(int mustHaveParameter, ...)
{
return new Plugin(mustHaveParameter, ...);
}
};
HTH
Christoph
--
[ signature omitted ]
Message 4 in thread
On Mon, 26 May 2008 15:08:10 +0200
Christoph Duelli wrote:
>
>Create a PluginFactory that creates your plugins...
>i.e. use a layer of indirection:
>
Hmm... I'd hoped, that there'd be a "direct" way of giving plugins
initial values.
Now, I think I will build a plugin manager, that does the initialising.
I would have to write something like this anyway, sooner or later.
Thanks anyway,
Wolfgang
--
[ signature omitted ]
Message 5 in thread
Wolfgang Lorenz wrote:
> On Mon, 26 May 2008 15:08:10 +0200
> Christoph Duelli wrote:
>
>> Create a PluginFactory that creates your plugins...
>> i.e. use a layer of indirection:
>>
>>
>
> Hmm... I'd hoped, that there'd be a "direct" way of giving plugins
> initial values.
>
> Now, I think I will build a plugin manager, that does the initialising.
> I would have to write something like this anyway, sooner or later.
>
>
>
I find it very useful to make all my "plugins" just factories for the
object that will actually extend the functionality of my program. It
only requires a small factory class compiled into each plugin and it
solves a whole host of issues like the following:
1)The class used with Q_DECALRE_INTERFACE can't inherit from QObject
(thus not sig/slots at the interface level). Factories solve this
because if the factory implements your Qt Plugin interface it won't need
sig/slots. The objects the factory creates can inherit from QObject.
2)Creating multiple instances of a single plugin. Load the factory from
disk once and then create() as many as you want.
3) The above initialization issue. Simply have your create() function on
the factory take the parameters.
Most of the qt plugins systems shipped by Trolltech use this factory
pattern. Check out the interface of QDesignerCustomWidgetInterface,
QImageIOPlugin and QStylePugin.
Good Luck,
--Justin
begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:ICS;Engineering
adr:;;54B Middlesex Trpk.;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Sr. Consulting Engineer / Certified Qt Instructor
tel;work:617-621-0060
x-mozilla-html:FALSE
url:www.ics.com
version:2.1
end:vcard