Use of a factory in a QTPlugin
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
Code:
virtual QObject *createInstance
() = 0;
in the interface for my plugins.
And an example plugin implements this part like this:
Code:
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!
Re: Use of a factory in a QTPlugin
Shouldnt createInstance be static :rolleyes:
Re: Use of a factory in a QTPlugin
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:
Code:
QObject *TemplatePlugin
::createInstance() {
}
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:
Re: Use of a factory in a QTPlugin
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.
Re: Use of a factory in a QTPlugin
Can you share the header for TemplatePlugin ?
1 Attachment(s)
Re: Use of a factory in a QTPlugin
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.
Re: Use of a factory in a QTPlugin
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!
Re: Use of a factory in a QTPlugin
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:
Code:
#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.
Code:
PLUGININTERFACESHARED_EXPORT
Problem solved.