PDA

View Full Version : Use of a factory in a QTPlugin



PeterPaulAndMary
19th July 2010, 18:05
Hi,

I wanted to implement a factory method in my plugin interface for using more than one instance of the implemented plugin in my program afterwards.

So I have


virtual QObject *createInstance() = 0;
in the interface for my plugins.
And an example plugin implements this part like this:

QObject *TemplatePlugin::createInstance()
{
return new TemplatePlugin();
}
But this seems not to work even if I try to manually cast my class TemplatePlugin to QObject.
The result is allways:

plugininterface.h:19: undefined reference to `_imp___ZTV15PluginInterface'

Perhaps I'm doing it all wrong right now, but that's why I'm asking.

Thanks in advance!

aamer4yu
19th July 2010, 18:26
Shouldnt createInstance be static :rolleyes:

PeterPaulAndMary
19th July 2010, 18:51
Normally it should, but as I use it in a plugin, there are some things that I wasn't really sure about.
1. Every interface should contain pure virtual functions, that's what I often read and tried to consider.
2. Loading the plugin always ends up in creating an instance of the plugin for trying to cast it to the interface afterwards.

That's why I didn't implement it as a static method.

But nevertheless, I tried your proposition and the problem seems to be something different.
Everything runs fine when I implement it like this:

QObject *TemplatePlugin::createInstance()
{
return new QObject();
}

but when I try to return the pointer to an instance of TemplatePlugin, it always crashes or rather doesn't compile.

My class implements the interface and inherits QObject that's why I thought this should actually work.

So, how far am I from beeing completely wrong? :confused:

ChrisW67
19th July 2010, 23:56
The error looks like it is from the linker not the compiler. Have you got the LIBS right?

Failing that, how about posting (attach a file) a minimal compilable example that displays the linker error that you are complaining of.

aamer4yu
20th July 2010, 07:15
Can you share the header for TemplatePlugin ?

PeterPaulAndMary
20th July 2010, 10:18
ChrisW67:

Yeah you're right, it's actually a linker error. And I think that the LIBS should be alright, so I followed your proposal and set up a small example that shows my problem.

Actually, there's something I didn't mention about the interface. It is set up in a shared library and perhaps that's something were some problems arise from.
Whatever, I attached the small example so you can see exactly what I'm messing around with.

PeterPaulAndMary
21st July 2010, 16:04
I'm sorry to annoy someone, but this problem is quite annoying.
I hope anybody can have a look over my attached project.

I realized that I haven't included QtPlugin but even if so, it doesn't solve the problem.

Thanks in advance!

PeterPaulAndMary
21st July 2010, 19:35
Well, well.
I found the problem, quite a little bit stupid a have to admit.
So for anybody who has the same problem my sollution:

I have tried to use the following as interface header:


#if defined(PLUGININTERFACE_LIBRARY)
# define PLUGININTERFACESHARED_EXPORT Q_DECL_EXPORT
#else
# define PLUGININTERFACESHARED_EXPORT Q_DECL_IMPORT
#endif

class PLUGININTERFACESHARED_EXPORT PluginInterface
{
public:

virtual QObject *createInstance() = 0;
};
This has to fail, obviously in the end, because there is nothing to export/compile/link from a header.
That's why I got the linker error.

So removing the following is the solution.
PLUGININTERFACESHARED_EXPORT





Problem solved.