Unable to load Qt Plugin

2.3k views Asked by At

I use Qt 5.3 and when calling

QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
bool pluginLoaded = pluginLoader.isLoaded();

It gives false :(

// serverplugin.h

    #ifndef SERVERPLUGIN_H
#define SERVERPLUGIN_H

#include <QObject>
#include <QtPlugin>
#include "acepos/aceclient/serverinterface.h"

#include <QThread>
#include "server.h"

class ServerPlugin : public QObject,
                     public ServerInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.AcePos.ServerInterface/1.1" FILE "serverplugin.json")
    Q_INTERFACES(ServerInterface)
    QThread *m_pServerThread;
    Server *m_pServer;

public:
    ServerPlugin():m_pServerThread(NULL), m_pServer(NULL){}

private slots:
    //void handleServiceStartNotify();

public:
    bool startServerService();
    bool stopServerService();
};

#endif

// serverinterface.h

#ifndef SERVERINTERFACE_H
#define SERVERINTERFACE_H

#include <QtPlugin>

class ServerInterface
{
public:
    virtual ~ServerInterface() { }
    virtual bool startServerService() = 0;
    virtual bool stopServerService() = 0;
};

#define ServerInterface_iid "org.qt-project.Qt.AcePos.ServerInterface/1.1"

Q_DECLARE_INTERFACE(ServerInterface, ServerInterface_iid)

#endif // SERVERINTERFACE_H

Please let me know if anything wrong im doing in creating plugin.

When I build the plugin project I am able to see the generated library file. I am doing this for android qt project. When debugging it works fine. I had made changes in the plugin part during development.

2

There are 2 answers

0
dom0 On

QPluginLoader makes the following checks:

  • Same build configuration (release/debug, except on Unix-like systems)
  • Same major version and minor/patch version equal or smaller than application minor/patch version of Qt

When developing one sometimes forgets to rebuild a plugin with the correct Qt configuration, leading to the plugin not loaded. Solution: rebuild plugin with the correct build configuration.

You can also set QT_DEBUG_PLUGINS=1 in the launch environment of the application to get more verbose debugging information about plugin loading.

1
santahopar On

I stumbled upon this problem yesterday and here is what I did to fix it: Please note that both my plugin and the app that was consuming the plugin were in debug mode and built with the same version of Qt, so that was not the problem. The problem was that I had forgotten to put the following line in the main export class of the plugin:

Q_PLUGIN_METADATA(IID "namespace.if.any.classname" FILE "package.json")

After this I had no problem loading the plugin.