Results 1 to 4 of 4

Thread: passing object to plugin

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default passing object to plugin

    I have troubles with plugins in my application. In general this is only plugin loader.
    I do it like this:

    Qt Code:
    1. //main module
    2. int main(int argc, char *argv[])
    3. {
    4. QApplication a(argc, argv);
    5. QMySettings * conf = new QMySettings("","");
    6. QDir pluginsDir = QDir(qApp->applicationDirPath());
    7. pluginsDir.cdUp();
    8. pluginsDir.cd("plugins");
    9. foreach (QString fileName, pluginsDir.entryList(QDir::Files))
    10. {
    11. QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
    12. QObject *plugin = loader.instance();
    13. if(plugin)
    14. {
    15. MyPlugin * iface = qobject_cast<MyPlugin *>(plugin);
    16. if(iface)
    17. {
    18. iface->init(conf);
    19. }
    20. }
    21. }
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    In main module I initialize class for storing app configuration. This class I want to share with all of my plugins.
    Qt Code:
    1. Conf::Conf(QObject *parent) :
    2. QObject(parent)
    3. {
    4. }
    5. //header
    6. class Conf : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit Conf(QObject *parent = 0);
    11. QString getValue(int i);
    12. };
    13.  
    14. //cpp
    15. QString Conf::getValue(int i)
    16. {
    17. return QString::number(i,16);
    18. }
    To copy to clipboard, switch view to plain text mode 

    plugin interface
    Qt Code:
    1. class MyPlugin
    2. {
    3. public:
    4. ~MyPlugin(){}
    5. virtual void init(QObject * ptr) = 0;
    6.  
    7. };
    8.  
    9. Q_DECLARE_INTERFACE(MyPlugin,"com.trolltech.MyPlugin/1.0")
    To copy to clipboard, switch view to plain text mode 

    and finally my plugin implementation
    Qt Code:
    1. //header
    2. class LIB1SHARED_EXPORT Lib1 : public QObject,public MyPlugin {
    3. Q_OBJECT
    4. Q_INTERFACES(MyPlugin)
    5. public:
    6. Lib1();
    7. void init(QObject * ptr);
    8. };
    9. //cpp
    10. Lib1::Lib1()
    11. {
    12. }
    13.  
    14. void Lib1::init(QObject * ptr)
    15. {
    16. Conf * conf = reinterpret_cast<Conf *>(ptr);
    17. qWarning(conf->getValue(1).toAscii()); // <<<<<<< here error
    18. }
    19.  
    20. Q_EXPORT_PLUGIN2(lib1, Lib1)
    To copy to clipboard, switch view to plain text mode 

    While compiling I get strange error:
    C:\source\PluginTest\lib1\lib1.cpp:11: error: undefined reference to `Conf::getValue(int)'
    and what's interesting, using QSetting in place of My class works fine.

    P.S. Sorry for my pure English
    Last edited by folibis; 17th November 2011 at 05:55. Reason: no reason

  2. #2
    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: passing object to plugin

    Your plugin implementation will have to have access to the header declaring Conf in order to compile this code. On Linux that is probably sufficient. On Windows you might have to split Conf into a library that you can link the plugin against.

    If you want a Conf* in the plugin why not define the interface as Conf* rather than QObject*?

  3. #3
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing object to plugin

    the code compiles without problem, as you can see it is linker error;
    and plugin implementation file include header declaring Conf.h

    I can define
    void Lib1::init(QObject * ptr)
    as
    void Lib1::init(Conf * ptr)

    but it still say "undefined reference to `Conf::getValue(int)'"

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: passing object to plugin

    You have an undefined reference because even though your plugin knows what a Conf class looks like (which means it can compile the code), it doesn't have any idea how to get the Conf instance to do anything, because you haven't linked in the object code that contains the Conf class implementation.

    This is exactly what Chris was saying in his answer. Both the application and the plugin need to be linked against the Conf class object code if they need to use the objects. If all your plugin does is pass around pointers to Conf instances but never tries to call any Conf class methods, then you simply need the header file so it can be compiled. But it looks like you expect your plugins to be able to use the Conf pointers, so you will need all of then to link against the object code.

    Either that, or put the Conf object code in a DLL and link all of your apps and plugins against that so you aren't duplicating code.

  5. The following user says thank you to d_stranz for this useful post:

    folibis (18th November 2011)

Similar Threads

  1. Passing an object from one form to the other.
    By cbarmpar in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2008, 14:12
  2. Replies: 4
    Last Post: 12th August 2008, 01:55
  3. passing an object
    By mickey in forum General Programming
    Replies: 3
    Last Post: 16th January 2008, 10:27
  4. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 09:50
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

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.