Results 1 to 3 of 3

Thread: Plugins and Inheritance

  1. #1
    Join Date
    May 2006
    Posts
    70
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 4 Times in 2 Posts

    Default Plugins and Inheritance

    Ok I've been slamming my head against my desk all day now but I think I have some solution. I'm looking for some clarification or agreement or explanation or something.

    I have a shared library that I'm linking to from my application that contains a simple QObject class:
    Qt Code:
    1. class MyBaseClass : public
    2. {
    3. Q_OBJECT
    4. public:
    5. MyBaseClass(QObject *parent=0) { }
    6. virtual ~MyBaseClass() { } //side-note: is this 'virtual' needed and/or problematic?
    7. virtual void doSomething() {
    8. qDebug() << "i'm doing something from my base class";
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    I also have a dynamic library that I'm loading using QPluginLoader. In this plugin I am wanting to inherit MyBaseClass like this:
    Qt Code:
    1. class MyInheritedClass : public MyBaseClass {
    2. Q_OBJECT
    3. public:
    4. MyInheritedClass(QObject *parent=0) { }
    5. ~MyInheritedClass() { }
    6. void doSomething() {
    7. qDebug() << "i'm doing something from my inherited class";
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    I'm starting to realize that this probably the wrong approach. Please enlighten me. I'm realizing that you probably can't have this kind of inheritance across the plugin boundary. I guess the right way to do it is to have an interface declared in the shared library and have a default concrete class in the shared library and my specific plugin class in my plugin.
    Qt Code:
    1. //These two inside my shared library
    2. class MyInterface { ...
    3. class MyDefaultClass : public QObject, public MyInterface { ...
    4.  
    5. //And this one inside my plugin
    6. class MySpecificClass : public QObject, public MyInterface { ...
    To copy to clipboard, switch view to plain text mode 

    Of course this doesn't work exactly the same as inheriting from a base class, but i think i can make it work.

    Now.... here's the REAL kicker to all this. My first example works perfectly under Linux. It does NOT work under Windows with MinGW.
    That's what got me so confused. I had perfectly working code when I developed under Linux. Then I moved my source over to windows and it compiles fine. When I try to load my plugin QPluginLoader::instance() returns a null pointer.

    So anyway, if any one can shed some light on this I would be grateful.

    -Mike

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

    Default Re: Plugins and Inheritance

    Under Windows you have to use __decl(dllexport) and __decl(dllimport) to export/import appropriate symbols. There is a macro in Qt that handles it transparently. Search the docs for dllexport, I'm sure you'll find it. I don't know if this is the problem you are facing but it's a likely candidate.

  3. #3
    Join Date
    May 2006
    Posts
    70
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 4 Times in 2 Posts

    Default Re: Plugins and Inheritance

    Yup you're right, although there is very little documentation in the Qt docs.

    Here is what I did after gleaning information from all over the web. (BTW, this only affects Windows compiles).

    If you have a library and you want your class (or functions) to be used by other apps linking to it you have to export them.

    I made a new header file (i called my libdef.h) with this:
    Qt Code:
    1. #ifndef LIBDEF_H
    2. #define LIBDEF_H
    3.  
    4. #include <Qt/qglobal.h>
    5.  
    6. #if defined(Q_OS_WIN32)
    7. # ifdef MYLIB
    8. # define MYEXPORT Q_DECL_EXPORT
    9. # else
    10. # define MYEXPORT Q_DECL_IMPORT
    11. # endif
    12. #else
    13. # define MYEXPORT
    14. #endif
    15.  
    16. #endif
    To copy to clipboard, switch view to plain text mode 

    Then the class in my shared library needs to look like this:
    Qt Code:
    1. #include "libdef.h"
    2.  
    3. class MYEXPORT MyClassName
    4. {
    5. public:
    6. //methods & etc here
    7. };
    8.  
    9. class MYEXPORT AnotherClassName
    10. {
    11. //etc...
    12. };
    To copy to clipboard, switch view to plain text mode 

    In my .pro file for my shared library I added this line:
    Qt Code:
    1. DEFINES += MYLIB
    To copy to clipboard, switch view to plain text mode 

    Remember, this is needed for Windows systems only. But the above code works on all platforms.

    My main beef is that they do not use this necessary method in any of the Qt docs or in any of the Qt examples. Hopefull my post helps others.

Similar Threads

  1. Multiple inherittance in plugins
    By KShots in forum Qt Programming
    Replies: 13
    Last Post: 24th April 2007, 20:27
  2. Getting Qt to recognize plugins...
    By KShots in forum Qt Programming
    Replies: 4
    Last Post: 21st April 2007, 10:05

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.