QExtensionFactory Class

通过 QExtensionFactory 类,您可以在Qt Designer 中创建一个能够制作自定义扩展实例的工厂。更多

头文件: #include <QExtensionFactory>
CMake: find_package(Qt6 REQUIRED COMPONENTS Designer)
target_link_libraries(mytarget PRIVATE Qt6::Designer)
qmake: QT += designer
继承:QObjectQAbstractExtensionFactory

公共函数

QExtensionFactory(QExtensionManager *parent = nullptr)
QExtensionManager *extensionManager() const

重新实现的公共函数

virtual QObject *extension(QObject *object, const QString &iid) const override

受保护函数

virtual QObject *createExtension(QObject *object, const QString &iid, QObject *parent) const

详细说明

Qt Widgets Designer 中,扩展在需要时才会创建。因此,在实现自定义扩展时,您还必须创建一个 QExtensionFactory,即一个能够创建扩展实例的类,并使用Qt Widgets Designerextension manager 进行注册。

QExtensionManager 类为Qt Widgets Designer 提供了扩展管理功能。当需要扩展时,Qt Designerextension manager 会遍历所有已注册的工厂,每个工厂都会调用QExtensionFactory::createExtension() ,直到找到第一个能为选定对象创建所需扩展的工厂为止。然后,该工厂将创建一个扩展实例。

Qt Widgets Designer 中有四种可用的扩展类型:QDesignerContainerExtensionQDesignerMemberSheetExtensionQDesignerPropertySheetExtensionQDesignerTaskMenuExtension 。无论请求的扩展是与多页面容器、成员表、属性表还是任务菜单相关联,Qt Designer 的行为都是一样的。

您可以创建一个新的 QExtensionFactory 并重新实现QExtensionFactory::createExtension() 函数。举个例子:

        QObject *ANewExtensionFactory::createExtension(QObject *object,
                const QString &iid, QObject *parent) const
        {
            if (iid != Q_TYPEID(QDesignerContainerExtension))
                return nullptr;

            if (auto *widget = qobject_cast<MyCustomWidget*>(object))
                return new MyContainerExtension(widget, parent);

            return nullptr;
        }

或者使用现有的工厂,扩展QExtensionFactory::createExtension() 函数,使工厂也能创建扩展。例如

        QObject *AGeneralExtensionFactory::createExtension(QObject *object,
                const QString &iid, QObject *parent) const
        {
            auto *widget = qobject_cast<MyCustomWidget*>(object);
            if (!widget)
                return nullptr;

            if (iid == Q_TYPEID(QDesignerTaskMenuExtension))
                return new MyTaskMenuExtension(widget, parent);

            if (iid == Q_TYPEID(QDesignerContainerExtension))
                return new MyContainerExtension(widget, parent);

            return nullptr;
        }

有关使用 QExtensionFactory 类的完整示例,请参阅任务菜单扩展示例。该示例展示了如何为Qt Designer 创建自定义 widget 插件,以及如何使用QDesignerTaskMenuExtension 类向Qt Widgets Designer 的任务菜单添加自定义项目。

另请参阅 QExtensionManagerQAbstractExtensionFactory

成员函数文档

[explicit] QExtensionFactory::QExtensionFactory(QExtensionManager *parent = nullptr)

用给定的parent 构建扩展工厂。

[virtual protected] QObject *QExtensionFactory::createExtension(QObject *object, const QString &iid, QObject *parent) const

为给定的object 创建iid 指定的扩展。扩展对象将作为指定parent 的子对象创建。

另请参阅 extension().

[override virtual] QObject *QExtensionFactory::extension(QObject *object, const QString &iid) const

重实现:QAbstractExtensionFactory::extension(QObject *object, const QString &iid) const.

返回iid 为给定的object 指定的扩展名。

另请参阅 createExtension() 。

QExtensionManager *QExtensionFactory::extensionManager() const

返回扩展工厂的扩展管理器。

© 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.