QStylePlugin Class
QStylePlugin 类为自定义QStyle 插件提供了一个抽象基础。更多
Header: | #include <QStylePlugin> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
继承: | QObject |
- 所有成员(包括继承成员)的列表
- QStylePlugin 属于插件类。
公共函数
QStylePlugin(QObject *parent = nullptr) | |
virtual | ~QStylePlugin() |
virtual QStyle * | create(const QString &key) = 0 |
详细说明
QStylePlugin 是一个简单的插件接口,可轻松创建自定义样式,并通过QStyleFactory 类动态加载到应用程序中。
编写样式插件的方法是子类化该基类,重新实现纯虚拟的create() 函数,并使用Q_PLUGIN_METADATA() 宏导出该类。
class MyStylePlugin : public QStylePlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "mystyleplugin.json") public: MyStylePlugin(QObject *parent = nullptr); QStyle *create(const QString &key) override; };
插件的 json 元数据文件mystyleplugin.json
需要包含插件支持的样式名称信息,如下所示:
{ "Keys": [ "Rocket", "Starbuster" ] }
详情请参阅如何创建 Qt 插件。
另请参阅 QStyleFactory 和QStyle 。
成员函数文档
[explicit]
QStylePlugin::QStylePlugin(QObject *parent = nullptr)
用给定的parent 构建一个样式插件。
请注意,导出插件的 moc 生成代码会自动调用此构造函数,因此无需明确调用。
[virtual noexcept]
QStylePlugin::~QStylePlugin()
销毁样式插件。
请注意,Qt 会在不再使用插件时自动将其销毁,因此无需明确调用析构函数。
[pure virtual]
QStyle *QStylePlugin::create(const QString &key)
为给定的样式key 创建并返回QStyle 对象。如果插件无法创建样式,则应返回 0。
样式键通常是所需样式的类名。请注意,键是不区分大小写的。例如
© 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.