QExtensionFactory Class | Qt Widgets Designer Manual
Description: The QExtensionFactory class allows you to create a factory that is able to make instances of custom extensions in Qt Designer.

- Qt 6.10
- Qt Widgets Designer Manual
- C++ Classes
- QExtensionFactory
- Qt 6.10.3 Reference Documentation

# QExtensionFactory Class

The QExtensionFactory class allows you to create a factory that is able to make instances of custom extensions in Qt Designer. More...

Header: | `#include <QExtensionFactory>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Designer)` `target_link_libraries(mytarget PRIVATE Qt6::Designer)`
qmake: | `QT += designer`
Inherits: | QObject and QAbstractExtensionFactory

- List of all members, including inherited members

## Public Functions

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

## Reimplemented Public Functions

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

## Protected Functions

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

## Detailed Description

In Qt Widgets Designer the extensions are not created until they are required. For that reason, when implementing a custom extension, you must also create a QExtensionFactory, i.e. a class that is able to make an instance of your extension, and register it using Qt Widgets Designer's extension manager.

The QExtensionManager class provides extension management facilities for Qt Widgets Designer. When an extension is required, Qt Designer's extension manager will run through all its registered factories calling QExtensionFactory::createExtension() for each until the first one that is able to create a requested extension for the selected object, is found. This factory will then make an instance of the extension.

There are four available types of extensions in Qt Widgets Designer: QDesignerContainerExtension , QDesignerMemberSheetExtension, QDesignerPropertySheetExtension and QDesignerTaskMenuExtension. Qt Designer's behavior is the same whether the requested extension is associated with a multi page container, a member sheet, a property sheet or a task menu.

You can either create a new QExtensionFactory and reimplement the QExtensionFactory::createExtension() function. For example:

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

Or you can use an existing factory, expanding the QExtensionFactory::createExtension() function to make the factory able to create your extension as well. For example:

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

For a complete example using the QExtensionFactory class, see the Task Menu Extension example. The example shows how to create a custom widget plugin for Qt Designer, and how to use the QDesignerTaskMenuExtension class to add custom items to Qt Widgets Designer's task menu.

See also QExtensionManager and QAbstractExtensionFactory.

## Member Function Documentation

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

Constructs an extension factory with the given parent.

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

Creates an extension specified by iid for the given object. The extension object is created as a child of the specified parent.

See also extension().

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

Reimplements: QAbstractExtensionFactory::extension(QObject *object, const QString &iid) const.

Returns the extension specified by iid for the given object.

See also createExtension().

### QExtensionManager *QExtensionFactory::extensionManager() const

Returns the extension manager for the extension factory.