本页内容

QExtensionFactory Class

QExtensionFactory 类允许您创建一个工厂,该工厂能够生成Qt Designer 中自定义扩展的实例。更多内容...

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

公共函数

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 Designer 的extension manager 将其注册。

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

在Qt Widgets Designer 中,共有四种可用的扩展类型:QDesignerContainerExtension 、QDesignerMemberSheetExtension 、QDesignerPropertySheetExtension 和QDesignerTaskMenuExtension 。无论请求的扩展是与多页容器、成员表、属性表还是任务菜单相关联,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 创建自定义控件插件,以及如何使用QDesignerTaskMenuExtension 类将自定义项目添加到Qt Widgets Designer 的任务菜单中。

另请参阅 QExtensionManager 和QAbstractExtensionFactory 。

成员函数文档

[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

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

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