Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PluginLoader returns a QObject instance, but qobject_cast returns null (0x0). [Solved]
Forum Updated to NodeBB v4.3 + New Features

PluginLoader returns a QObject instance, but qobject_cast returns null (0x0). [Solved]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fronald
    wrote on last edited by
    #1

    Hi,

    I have the follow code to load my plugin:

    @foreach(QString fileName, pluginsDir.entryList(QDir::Files)) {
    QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
    QObject *plugin = pluginLoader.instance();
    if (plugin) {
    DLinkModemModule *dlinkModule = qobject_cast<DLinkModemModule *>(plugin);
    if (dlinkModule) {
    moduleInstances[dlinkModule->getModuleName()] = dlinkModule;
    modules << dlinkModule->getModuleName();
    std::cout << "Module " << dlinkModule->getModuleName().toStdString().c_str() << " - " << dlinkModule->getModuleVersion().toStdString().c_str() << " is loaded." << std::endl;
    }
    }
    }@

    But the code qobject_cast<DLinkModemModule *>(plugin) returns null. Anyone know this issue?

    My interface:

    @
    #ifndef DLINKMODULE_H
    #define DLINKMODULE_H

    #include <QtCore/QtPlugin>

    QT_BEGIN_NAMESPACE
    class QBool;
    class QString;
    class QObject;
    QT_END_NAMESPACE

    class DLinkModemModule {
    public:
    /**

    • Destrutor
      */
      virtual ~DLinkModemModule(){};
      //...my methods...
      };

    QT_BEGIN_NAMESPACE
    Q_DECLARE_INTERFACE(DLinkModemModule, "br.com.dlink.ModuleManager.DLinkModemModule/1.0")
    QT_END_NAMESPACE

    #endif /* DLINKMODULE_H */
    @

    My header of implementation plugin:

    @
    #ifndef SUPPORT500B_H
    #define SUPPORT500B_H

    #include "DLinkModemModule.h"
    #include <QtCore/QObject>
    #include <QtCore/QStringList>

    class Support500b : public QObject, public DLinkModemModule {
    Q_INTERFACES(DLinkModemModule)
    public:

    virtual ~Support500b(){};
    // all methods defined in plugin interface
    };

    QT_BEGIN_NAMESPACE
    Q_EXPORT_PLUGIN2(support500b, Support500b)
    QT_END_NAMESPACE

    #endif /* SUPPORT500B_H */
    @

    My CPP with Support500b class, has all interface method implemented. Any idea?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      Did you get any error?

      Be sure that the project build mode "Debug/Release" should be the same while creating the plugin and also while loading the plugin. There is a video tutorial "here":http://www.voidrealms.com/viewtutorial.aspx?id=246. You can also share the .pro file for both the projects (1 creating and 2nd reading/loading).

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fronald
        wrote on last edited by
        #3

        About the error, Qt have any function for read error? Ex:

        @
        DLinkModemModule *dlinkModule = qobject_cast<DLinkModemModule *>(plugin);
        if (dlinkModule) {
        ...
        }else{
        std::cout << qobject_cast_error() << std::endl;
        }
        @

        But now, no error is raised. Just a null pointer is returned.

        I will watch the video and back here to report the results. Thank you Sam!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          Well I am not aware of any errorString() related to qobject_cast . So yes if it is null then it will go to the else part. :)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            Yes the is one more option to check if the plugin fails to load. You can check for pluginLoader.errorString() .

            @DLinkModemModule *dlinkModule = qobject_cast<DLinkModemModule *>(plugin);
            if (dlinkModule) {
            ...
            }else{
            std::cout << pluginLoader.errorString()<< std::endl;
            }@

            Where pluginLoader is defined as
            @QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));@

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fronald
              wrote on last edited by
              #6

              Oh Sam! You are the man!

              Summary:

              1- I remove Q_OBJECT macro from my plugin implementation header, because this issue http://qt-project.org/forums/viewthread/19343/ , but the real cause was the macros QT_BEGIN_NAMESPACE/QT_END_NAMESPACE.
              2- The my reference, was that document http://michael-stengel.com/blog/?p=4 , and many things dont work more.
              3- My plugin interface methods were not with the keyword const in the end

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                Glad to know its working now :) Kindly Edit the title and add [Solved] for both the links.

                Second link not opening there is a "," at the end

                Happy Coding !!!

                Cheers.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved