Results 1 to 8 of 8

Thread: Use of a factory in a QTPlugin

  1. #1
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Qt Code:
    1. virtual QObject *createInstance() = 0;
    To copy to clipboard, switch view to plain text mode 
    in the interface for my plugins.
    And an example plugin implements this part like this:
    Qt Code:
    1. QObject *TemplatePlugin::createInstance()
    2. {
    3. return new TemplatePlugin();
    4. }
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of a factory in a QTPlugin

    Shouldnt createInstance be static

  3. #3
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. QObject *TemplatePlugin::createInstance()
    2. {
    3. return new QObject();
    4. }
    To copy to clipboard, switch view to plain text mode 

    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?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of a factory in a QTPlugin

    Can you share the header for TemplatePlugin ?

  6. #6
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Attached Files Attached Files

  7. #7
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!

  8. #8
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. #if defined(PLUGININTERFACE_LIBRARY)
    2. # define PLUGININTERFACESHARED_EXPORT Q_DECL_EXPORT
    3. #else
    4. # define PLUGININTERFACESHARED_EXPORT Q_DECL_IMPORT
    5. #endif
    6.  
    7. class PLUGININTERFACESHARED_EXPORT PluginInterface
    8. {
    9. public:
    10.  
    11. virtual QObject *createInstance() = 0;
    12. };
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. PLUGININTERFACESHARED_EXPORT
    To copy to clipboard, switch view to plain text mode 





    Problem solved.

Similar Threads

  1. QObject factory (singleton pattern)
    By area51 in forum Qt Programming
    Replies: 8
    Last Post: 24th June 2010, 14:55
  2. Designer and view factory
    By Rockem in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 11:22
  3. Simple yet powerful object factory
    By mcostalba in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2007, 16:46
  4. MySql and QtPlugin
    By darksaga in forum Installation and Deployment
    Replies: 6
    Last Post: 15th May 2007, 10:09
  5. How to use QMIME Sourse Factory
    By shamik in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2006, 14:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.