c++windowspluginsqt5

How to use the classes of the host application from a Qt plugin?


I have linking problems by creating a Qt C++ plugin to extend the functionality of my application. The code compiles and everithing works, but only as long as I use the Qt library classes, like QString for example. The moment I give to the plugin's class a reference to an object from the classes defined in the host app, the project is not linked anymore. Creating the plugin I follow the procedure from the Qt documentation and take into consideration the given examples - echo and plug&paint. However, such case is not covered there.


update

Here is the error:

myPlugin.obj:-1: error: LNK2019: unresolved external symbol "public: class QString __cdecl myClass::answer(void)const " (?answer@myClass@@QEBA?AVQString@@XZ) referenced in function "public: virtual class QString __cdecl myPlugin::echo(class myClass *)" (?echo@myPlugin@@UEAA?AVQString@@PEAVmyClass@@@Z)

And here is the project that causes it:

plugtest.pro

TEMPLATE = subdirs

SUBDIRS += \
    host \
    plugin

host.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = host
TEMPLATE = app


SOURCES += main.cpp\
        MainWindow.cpp \
    myClass.cpp

HEADERS  += MainWindow.h \
    myClass.h \
    plugInterface.h

FORMS    += MainWindow.ui

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QPluginLoader>
#include <QDir>
#include "myClass.h"
#include "plugInterface.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_button_clicked();

private:
    bool loadPlugin();

    Ui::MainWindow *ui;

    plugInterface *m_interface;

    myClass *m_class;
};

#endif // MAINWINDOW_H

myClass.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>

class myClass : public QObject
{
    Q_OBJECT
public:
    explicit myClass(QObject *parent = 0);

    QString answer() const;
    void setAnswer(const QString &str);

private:
    QString m_answer;
};

#endif // MYCLASS_H

plugInterface.h

#ifndef PLUGINTERFACE_H
#define PLUGINTERFACE_H

#include <QString>
#include "myClass.h"

class plugInterface
{
public:
    virtual ~plugInterface() {}
    virtual QString echo(myClass *value) = 0;
};

#define PlugInterface_iid "example.suite.app.PluginInterface"

Q_DECLARE_INTERFACE(plugInterface, PlugInterface_iid)

#endif // PLUGINTERFACE_H

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    if (!loadPlugin())
    {
        QMessageBox::information(this, "Error", "Could not load the plugin");
    }

    m_class = new myClass(this);
    m_class->setAnswer("Good!");
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::loadPlugin()
  {
      QDir pluginsDir(qApp->applicationDirPath());

      if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
      pluginsDir.cd("plugins");

      foreach (QString fileName, pluginsDir.entryList(QDir::Files))
      {
          QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
          QObject *plugin = pluginLoader.instance();
          if (plugin) {
              m_interface = qobject_cast<plugInterface *>(plugin);
              if (m_interface)
                  return true;
          }
      }

      return false;
  }

void MainWindow::on_button_clicked()
{
    ui->lineResult->setText(m_interface->echo(m_class));
}

myClass.cpp

#include "myClass.h"

myClass::myClass(QObject *parent) : QObject(parent)
{

}

QString myClass::answer() const
{
    return m_answer;
}

void myClass::setAnswer(const QString &str)
{
    m_answer = str;
}

plugin.pro

  TEMPLATE        = lib
  CONFIG         += plugin
  QT             += widgets
  INCLUDEPATH    += ../host
  HEADERS         = myPlugin.h
  SOURCES         = myPlugin.cpp
  TARGET          = $$qtLibraryTarget(myPlugin)
  DESTDIR         = ../plugins

myPlugin.h

#ifndef MYPLUGIN_H
#define MYPLUGIN_H

#include <QObject>
#include "plugInterface.h"

class myPlugin : public QObject, plugInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "example.suite.app.PluginInterface")
    Q_INTERFACES(plugInterface)

public:
    QString echo(myClass *value) Q_DECL_OVERRIDE;
};

#endif // MYPLUGIN_H

myPlugin.cpp

#include "myPlugin.h"

QString myPlugin::echo(myClass *value)
{
    return value->answer();
}

Solution

  • The solution that worked for me is to make the myClass a part of a library and include it dynamically in both the plug-in and the host. However, I am curious if this could be done without creating a library.