qt4 Q_EXPORT_PLUGIN2 到qt5 Q_PLUGIN_METADATA变化

本文介绍了Qt5中插件系统的改变,Q_EXPORT_PLUGIN2宏不再适用,需要使用新的宏Q_PLUGIN_METADATA来确保Qt5能正确识别和加载插件。对于同时支持Qt4和Qt5的项目,需要添加版本检查宏来适配不同的元数据指定。
Plugin loading

Another significant porting burden, in plugin-heavy systems at least is that the user code required for plugin loading has changed. The moc tool is now responsible for generating plugin metadata, so rather than a preprocessor macro in a C++ file (Q_EXPORT_PLUGIN2), as in Qt 4, in Qt 5 a new macro must be used in the header file, where moc can see it.
The process is described by Lars, and is relatively straightforward. Where it becomes difficult though is in cases where the Q_EXPORT_PLUGIN2 macro is wrapped with another macro, as done in KDE in K_EXPORT_PLUGIN.
Such a wrapping of the new macro would not be possible with Qt 5 because mocwould not parse any wrapper (mocdoes not do full preprocessing).

意思就是:Qt5的插件系统发生了变化,Q_EXPORT_PLUGIN2被淘汰,“a new macro” 诞生了。接下来的工作,就是找出这个 new macro。

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:

Q_EXPORT_PLUGIN2(PluginTest,PluginTest)

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

    ...
};

这样qt5就能正确的识别插件了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值