Qt5 plug-in mechanism (6) -- Several important macros during Qt plug-in development

Source: Internet
Author: User
To learn how to develop the Qt plugin, search for QtPlugins or HowtoCreateQtPlugins in QtAssistant to see the manual introduction. Several macro Q_DECLARE_INTERFACE (ClassName, Identifier) Thismacroassociatesthegiv are involved.
To learn How to develop the Qt plugin, search for "Qt Plugins" or "How to Create Qt Plugins" in Qt Assistant to see the manual introduction.
Several macros are involved.


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 shoshould be exactly one occurrence of this macro in the source code for a Qt plugin.

Among them, the Q_PLUGIN_METADATA (...) macro has already been introduced in the article "meta information of Qt plug-ins" mentioned earlier. it is basically the most important of these macros, because
MOC will generate many things related to the plug-in based on this macro, including metadata and functions for obtaining the plug-in instance. It can be used to export plug-ins. the function is similar to that of the old version.
Q_EXPORT_PLUGIN2 macro in Qt

The Q_DECLARE_INTERFACE macro is related to qobject_cast. it defines the qobject_interface_iid and qobject_cast templates for the interface class.
The Qt source code defines the macro Q_DECLARE_INTERFACE.
# Define Q_DECLARE_INTERFACE (IFace, IId) \ template <> inline const char * qobject_interface_iid
 
  
() \ {Return IId;} \ template <> inline IFace * qobject_cast
  
   
(QObject * object) \ {return reinterpret_cast
   
    
(Object? Object-> qt_metacast (IId): 0);} \ // qt_metacast ing the interface class pointer through the IID of the plug-in, bind an IID to an interface template <> inline IFace * qobject_cast
    
     
(Const QObject * object) \ {return reinterpret_cast
     
      
(Object? Const_cast
      
        (Object)-> qt_metacast (IId): 0 ));}
      
     
    
   
  
 



The Q_INTERFACES macro is also related to qobject_cast. without the Q_DECLARE_INTERFACE and Q_INTERFACES macros, you cannot map the instance pointers obtained from the plug-in to qobject_cast.
However, the Q_INTERFACES macro is not defined in the Qt source code. it is a MOC dish, and MOC will use this macro to generate some code. Note that if the Q_INTERFACES macro is used in a header file or source file,
Before calling this macro, a Q_DECLARE_INTERFACE macro must declare the corresponding interface (or include a header file that uses the Q_DECLARE_INTERFACE macro to declare this interface). MOC will check this, because it uses the IID parameter of the Q_DECLARE_INTERFACE macro when generating code for the Q_INTERFACES macro.

For example,
The virtual interface class in MyPluginInterface. h is defined as follows:

# Include
 
  
# Define QtPluginDemo_iid "org. qt-project.Qt.PluginDemo" // define the IIDclass MyPluginInterface of the interface {public: virtual ~ MyPluginInterface () {} virtual void showPluginName () ;}; Q_DECLARE_INTERFACE (MyPluginInterface, QtPluginDemo_iid );
 


The class definition in the header file MyPlugin. h is as follows:

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();};

After the header file MyPlugin. h is processed using MOC, the generated code contains the following parts:
(Only the code generated by MOC for Q_INTERFACES macro is listed. the code generated by MOC for Q_PLUGIN_METADATA macro is described in the previous article titled "Qt plug-in metadata ):

...... 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) // The Q_DECLARE_INTERFACE macro is the ING of the qobject_cast type implemented by this function {if (! _ Clname) return 0; if (! Strcmp (_ clname, qt_meta_stringdata_MyPlugin.stringdata) // if _ clname matches the class name MyPlugin, a valid pointer return static_cast
 
  
(Const_cast <MyPlugin *> (this); if (! Strcmp (_ clname, "MyPluginInterface") // if _ clname matches the name of the interface class MyPluginInterface, return the valid pointer return static_cast <MyPluginInterface *> (const_cast <MyPlugin *> (this); if (! Strcmp (_ clname, "org. qt-project.Qt.PluginDemo") // if _ clname matches the IID of the interface class, a valid pointer is returned. // Here the IID parameter used to call the Q_DECLARE_INTERFACE macro is used // and, in the Q_DECLARE_INTERFACE macro code, qobject_cast return static_cast <MyPluginInterface *> (const_cast <MyPlugin *> (this); return QObject :: qt_metacast (_ clname );}......
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.