Search code examples
c++qtpluginsqtplugin

how to create plugins (Qt 5.2.0)


i ‘m trying to create a plugin(not for Qt creator), i created an empty project and added the following files : but i’m getting the following errors:

1. C:\Qt\Qt5.2.0\5.2.0\mingw48_32\include\QtCore\qglobal.h:666: erreur : invalid application of ‘sizeof’ to incomplete type ‘QStaticAssertFailure’ enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, COUNTER) = sizeof(QStaticAssertFailure)} ^ 2. D:\MyFiles\Projects\QtProjects\pluginTest2\plugintest.cpp:9: erreur : expected initializer before ‘PluginTest’ QString PluginTest::name() const ^

PluginTest2.pro (project name)

CONFIG         += plugin
TARGET = PluginTest
CONFIG   += plugin release
VERSION =1.0.0

TEMPLATE = lib

SOURCES += \
    plugintest.cpp

HEADERS += \
    Interface.h \
    plugintest.h

interface.h

#ifndef INTERFACE_H
#define INTERFACE_H
#include <QString>


class Interface
{
public:
    virtual  QString name() const =0;
};
Q_DECLARE_INTERFACE(Interface,"interface /1.0.0")
#endif // INTERFACE_H

plugintest.h

#ifndef PLUGINTEST_H
#define PLUGINTEST_H
#include <QObject>
#include <QString>
#include<QtPlugin>
#include "Interface.h"


class PluginTest:public QObject,public Interface
{
    Q_OBJECT
    Q_INTERFACES(Interface)
public:
    PluginTest();
    QString name() const;

};

#endif // PLUGINTEST_H


plugintest.cpp


#include "plugintest.h"


PluginTest::PluginTest()
{
}
Q_EXPORT_PLUGIN2(PluginTest,PluginTest)

QString PluginTest::name() const
{
    return "pluginTest";
}

Solution

  • The problem is this line:

    Q_EXPORT_PLUGIN2(PluginTest,PluginTest)
    

    This is a Qt 4 feature, so you would need to either remove it, or put behind a version checking macro as follows:

    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
        Q_EXPORT_PLUGIN2(PluginTest,PluginTest)
    #endif
    

    For completeness, it seems you do not specify the meta data for the plugin. You would need to add that either unconditionally or conditionally with a version checking macro if you wish to support both Qt 4 and Qt 5 as follows:

    class PluginTest:public QObject,public Interface
    {
        Q_OBJECT
        Q_INTERFACES(Interface)
    
        #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
            Q_PLUGIN_METADATA(IID "your-string-here" FILE "file-here-but-can-be-empty") 
        #endif
    
        ...
    };
    

    Also, note that you append the "plugin" CONFIG item twice in your project file.