Results 1 to 6 of 6

Thread: How to instantiate a class if I dont know its name?

  1. #1
    Join Date
    Nov 2010
    Location
    Cienfuegos, Cuba
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to instantiate a class if I dont know its name?

    Hi:

    The application I'm trying to develop must be extended using plugins. I have a XML file similar to this (I omitted some non important parts):

    <program>
    <component type="plugin1"/>
    <component type="plugin2"/>
    <component type="plugin1"/>
    <component type="plugin3"/>
    </program>

    So, for each <component> element that I read I create a new page on a QStackedWidget containing a custom widget defined by the classes Plugin1, Plugin2 and Plugin3. Is very easy when I know the names of all the plugins because I can do something like:

    Qt Code:
    1. class AbstractPlugin : public QObject{
    2. //...
    3. virtual QWidget* widget() = 0;
    4. };
    5.  
    6. //all these classes implements "widget()"
    7. class Plugin1 : public AbstractPlugin{/*...*/};
    8. class Plugin2 : public AbstractPlugin{/*...*/};
    9. class Plugin3 : public AbstractPlugin{/*...*/};
    10.  
    11. QList<AbstractPlugin* > components
    12. QString type //this value was readed from the XML in some moment
    13.  
    14. if(type == "Plugin1")
    15. components.append(new Plugin1());
    16. else if(type == "Plugin2")
    17. components.append(new Plugin2());
    18. else if(type == "Plugin3")
    19. components.append(new Plugin3());
    20.  
    21. foreach(AbstractPlugin* c, components){
    22. stackedWidget->addPage(c->widget());
    23. }
    To copy to clipboard, switch view to plain text mode 

    But, if I dont know the names of all the plugins because I don't write each one, how can I specify the correct constructor to call?
    Last edited by anoraxis; 12th March 2012 at 18:03.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to instantiate a class if I dont know its name?

    All you need to know is described in QPluginLoader docs and documents linked from within there. A general rule is that you know where to look for plugins and you load all the files that look like plugins and try to extract the interface you need from each loaded object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Location
    Cienfuegos, Cuba
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to instantiate a class if I dont know its name?

    Hope it works. The main advantage of my aplication depends on this.

  4. #4
    Join Date
    Nov 2010
    Location
    Cienfuegos, Cuba
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to instantiate a class if I dont know its name?

    Ok, I wrote some plugins that works fine but I'm facing another problem. I need to use different instances of the same plugin. It seems that each QPluginLoader that connects with the same library manages the same instance of the plugin.

    I changed the way I do it. My plugin now is using the factory pattern. I have a plugin with a method that returns an instance of another class. So, the plugin project is now:

    plugin.h
    plugin.cpp
    theClass.h
    theClass.cpp
    project.pro

    When I build this plugin and try to load it with QPluginLoader, the plugin dosn't load. If I remove the theClass.h and cpp files from the project and build it, the plugin loads but when using I get an "unknow symbol" error.

    I need some help with this. It's a problem related with the software I have to deliver to graduate from university.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to instantiate a class if I dont know its name?

    you could post some code...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    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: How to instantiate a class if I dont know its name?

    A factory is the approach to take.

    "...the plugin dosn't load.", is not a sufficient description of the problem to allow us to help. Make sure you do a clean rebuild of the plugin and, if the interfaces have changed, the application using it.

Similar Threads

  1. Replies: 18
    Last Post: 11th March 2011, 14:21
  2. Replies: 7
    Last Post: 2nd March 2011, 00:02
  3. Instantiate objects only having their class name
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 17:22
  4. wanted to re-instantiate two classes from a class
    By salmanmanekia in forum General Programming
    Replies: 2
    Last Post: 22nd August 2008, 09:59
  5. Replies: 2
    Last Post: 25th August 2006, 12:35

Tags for this Thread

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.