我正在尝试创建一个插件(不适用于Qt创建者(,我创建了一个空项目,并添加了以下文件:但我得到了以下错误:
1。C: \Qt\Qt5.2.0\5.2.0\mingw48_32\include\QtCore \qglobal.h:666:erreur:对不完整类型"QStaticAssertFailure"枚举应用"sizeof"无效{Q_STATIC_ASSERT_PRIVATE_JOIN(Q_STATIC_ASSERT_result,COUNTER(=sizeof(QStaticAssetFailure(}^2.D: \MyFiles\Projects\QtProjects\pluginTest2\plugintest.cpp:9:erreur:"plugintest"之前应为初始值设定项QString plugintest::name((const^
PluginTest2.pro(项目名称(
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";
}
问题是这一行:
Q_EXPORT_PLUGIN2(PluginTest,PluginTest)
这是一个Qt4功能,所以你需要删除它,或者放在一个版本检查宏后面,如下所示:
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
Q_EXPORT_PLUGIN2(PluginTest,PluginTest)
#endif
为了完整起见,您似乎没有为插件指定元数据。如果您希望同时支持Qt4和Qt5,您需要无条件或有条件地添加版本检查宏,如下所示:
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
...
};
另外,请注意,您在项目文件中附加了两次"plugin"CONFIG项。