Results 1 to 4 of 4

Thread: passing object to plugin

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.