如何创建插件(Qt 5.2.0) [英] how to create plugins (Qt 5.2.0)

查看:62
本文介绍了如何创建插件(Qt 5.2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个插件(不适用于 Qt 的创建者),我创建了一个空项目并添加了以下文件:但我收到以下错误:

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 : 'sizeof' 无效应用到不完整类型 'QStaticAssertFailure' 枚举 {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static(count)ert_result_)QStaticAssertFailure)} ^2.D:\MyFiles\Projects\QtProjects\pluginTest2\plugintest.cpp:9: erreur : 'PluginTest' QString PluginTest::name() const 之前的预期初始化器 ^

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(项目名称)

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

接口.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

插件测试.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)

这是 Qt 4 的一项功能,因此您需要将其删除,或者如下放置一个版本检查宏:

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

为了完整起见,您似乎没有为插件指定元数据.如果您希望同时支持 Qt 4 和 Qt 5,则需要无条件或有条件地添加版本检查宏,如下所示:

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

    ...
};

另外,请注意您在项目文件中两次附加了插件"CONFIG 项.

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

这篇关于如何创建插件(Qt 5.2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆