Qt5的插件机制(6)--开发Qt插件时几个重要的宏

版权声明:本文为博主原创文章,欢迎转载(文中有强调禁止转载的例外),但请说明出处。如用于培训等商业用途请先与博主联系并支付部分授权费 https://blog.csdn.net/NewThinker_wei/article/details/41292115

如何开发Qt插件,可以在Qt Assistant 中搜索"Qt Plugins"或"How to Create Qt Plugins",看看那篇manual中的介绍。
其中涉及到了几个宏


Q_DECLARE_INTERFACE( ClassName, Identifier)
This macro associates the given Identifier (a string literal) to the interface class called ClassName. The Identifier must be unique.
This macro is normally used right after the class definition for ClassName, in a header file.

Q_INTERFACES(...)
This macro tells Qt which interfaces the class implements. This is used when implementing plugins.

Q_PLUGIN_METADATA(...)
This macro is being used to declare meta data that is part of a plugin that instantiates this object.
The macro needs to declare the IID of the interface implemented through the object, and reference a file containing the meta data for the plugin.
There should be exactly one occurrence of this macro in the source code for a Qt plugin.

其中,Q_PLUGIN_METADATA(...)宏在前面讲“Qt插件的元信息”的那篇文章中已经介绍过了,它基本是这些宏里最重要的一个,因为
MOC会根据这个宏生成很多跟该插件相关的东西,包括元信息、获取插件实例的函数等。可用它可以将插件导出,其作用类似于老版本
Qt中的 Q_EXPORT_PLUGIN2 宏

Q_DECLARE_INTERFACE 宏是与qobject_cast相关的,它为接口类定义了qobject_interface_iid和qobject_cast这两个模板
Qt的源码中给出了宏Q_DECLARE_INTERFACE的定义
#  define Q_DECLARE_INTERFACE(IFace, IId) \
    template <> inline const char *qobject_interface_iid<IFace *>() \
    { return IId; } \
    template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
    { return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \    
                    // qt_metacast通过插件的IID来映射接口类的指针,一个IID绑定一个接口类
    template <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
    { return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }



Q_INTERFACES宏也是与qobject_cast相关,没有Q_DECLARE_INTERFACE和Q_INTERFACES这两个宏,就无法对从插件中获取的实例指针进行qobject_cast映射。
不过,Q_INTERFACES宏并没有在Qt的源码中定义,他是MOC的菜,MOC会利用这个宏生成一些代码。要注意一点,如果一个头文件或源文件中用到了Q_INTERFACES宏,
那么在调用这个宏之前,必须存在一个 Q_DECLARE_INTERFACE宏声明相应的接口(或者包含一个用Q_DECLARE_INTERFACE宏声明了该接口的头文件),MOC会检查这一点,因为它在为Q_INTERFACES宏生成代码时要用到Q_DECLARE_INTERFACE宏的IID参数。

举例,
头文件 MyPluginInterface.h 中虚拟接口类的定义如下

#include <QtPlugin>
#define QtPluginDemo_iid "org.qt-project.Qt.PluginDemo"    // 定义接口的IID
class MyPluginInterface
{
public:
        virtual ~MyPluginInterface(){}
        virtual void showPluginName();
};
Q_DECLARE_INTERFACE ( MyPluginInterface, QtPluginDemo_iid ) ;


头文件MyPlugin.h中类的定义如下

class MyPlugin : public QObject, public MyPluginInterface
{
    Q_OBJECT
    //  Q_PLUGIN_METADATA ( IID QtPluginDemo_iid FILE "MyPlugin.json")
    Q_PLUGIN_METADATA ( IID QtPluginDemo_iid)
    Q_INTERFACES(MyPluginInterface)

public:
        void showPluginName();
};

将头文件MyPlugin.h用MOC处理之后,生成的代码中有如下部分
(只列出了MOC为Q_INTERFACES宏生成的代码,MOC为Q_PLUGIN_METADATA宏生成的代码在前面讲“Qt插件的元信息”的那篇文章中介绍过了):

    ...
    ...

static const qt_meta_stringdata_MyPlugin_t qt_meta_stringdata_MyPlugin = {
    {
QT_MOC_LITERAL(0, 0, 8)
    },
    "MyPlugin"
};
    ...
    ...

void *MyPlugin::qt_metacast(const char *_clname)    // Q_DECLARE_INTERFACE宏就是利用这个函数实现的qobject_cast类型映射
{
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_MyPlugin.stringdata))    // 如果_clname与类的名称MyPlugin匹配,返回有效指针
        return static_cast<void*>(const_cast< MyPlugin*>(this));
    if (!strcmp(_clname, "MyPluginInterface"))        // 如果_clname与接口类的名称MyPluginInterface匹配,返回有效指针
        return static_cast< MyPluginInterface*>(const_cast< MyPlugin*>(this));
    if (!strcmp(_clname, "org.qt-project.Qt.PluginDemo"))    // 如果_clname与接口类的IID匹配,返回有效指针。
                                // 这里就用到了调用Q_DECLARE_INTERFACE宏时使用的IID参数
                                // 而且,Q_DECLARE_INTERFACE宏的代码中也是利用IID映射实现的qobject_cast
        return static_cast< MyPluginInterface*>(const_cast< MyPlugin*>(this));
    return QObject::qt_metacast(_clname);
}
    ...
    ...




猜你喜欢

转载自blog.csdn.net/NewThinker_wei/article/details/41292115