<QtPlugin> - Defining Plugins
<QtPlugin> 头文件定义了用于定义插件的宏。更多
Header: | #include <QtPlugin> |
是插件类的一部分。
宏
Q_DECLARE_INTERFACE(ClassName, Identifier) | |
Q_IMPORT_PLUGIN(PluginName) | |
Q_PLUGIN_METADATA(...) |
另请参阅 如何创建 Qt 插件。
宏文档
Q_DECLARE_INTERFACE(ClassName, Identifier)
该宏将给定的Identifier (字符串)与名为ClassName 的接口类关联。Identifier 必须是唯一的。
这个宏通常在头文件中ClassName 的类定义之后使用。
如果要将 Q_DECLARE_INTERFACE 与命名空间中声明的接口类一起使用,则必须确保 Q_DECLARE_INTERFACE 不在命名空间内。例如
namespace Foo { struct MyInterface { ... }; } Q_DECLARE_INTERFACE(Foo::MyInterface, "org.examples.MyInterface")
另请参阅 Q_INTERFACES() 和如何创建 Qt 插件。
Q_IMPORT_PLUGIN(PluginName)
此宏导入名为PluginName 的插件,该名称与使用Q_PLUGIN_METADATA() 为插件声明元数据的类名称相对应。
在应用程序的源代码中插入此宏,即可使用静态插件。
举例说明:
Q_IMPORT_PLUGIN(qjpeg)
在构建应用程序时,链接器也必须包含静态插件。有关详细信息,请参阅静态插件。
另请参阅 静态插件和如何创建 Qt 插件。
Q_PLUGIN_METADATA(...)
该宏用于声明元数据,这些数据是实例化该对象的插件的一部分。
该宏需要声明通过该对象实现的接口的 IID,并引用包含插件元数据的文件。
在 Qt 插件的源代码中,该宏应出现一次。
举例说明:
class MyInstance : public QObject { Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDummyPlugin" FILE "mymetadata.json") };
请注意,该宏出现的类必须是默认可构造的。
FILE 是可选项,指向一个 json 文件。
该 json 文件必须位于联编系统指定的 include 目录中。如果 moc 无法找到指定文件,则会错误退出。
另请参阅 Q_DECLARE_INTERFACE() 和如何创建 Qt XML 插件。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.