QExtensionManager Class

The QExtensionManager class provides extension management facilities for Qt Designer. 更多...

头: #include <QExtensionManager>
CMake: find_package(Qt6 REQUIRED COMPONENTS Designer)
target_link_libraries(mytarget PRIVATE Qt6::Designer)
qmake: QT += designer
继承: QObject and QAbstractExtensionManager

公共函数

QExtensionManager (QObject * parent = nullptr)
virtual ~QExtensionManager ()

重实现公共函数

virtual QObject * extension (QObject * object , const QString & iid ) const override
virtual void registerExtensions (QAbstractExtensionFactory * factory , const QString & iid = QString()) override
virtual void unregisterExtensions (QAbstractExtensionFactory * factory , const QString & iid = QString()) override
T qt_extension (QAbstractExtensionManager * manager , QObject * object )

Q_DECLARE_EXTENSION_INTERFACE ( ExtensionName , Identifier )

详细描述

Qt Designer the extensions are not created until they are required. For that reason, when implementing an 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 Designer 's extension manager.

The registration of an extension factory is typically made in the QDesignerCustomWidgetInterface::initialize () 函数:

        void MyPlugin::initialize(QDesignerFormEditorInterface *formEditor)
        {
            if (initialized)
                return;
            auto *manager = formEditor->extensionManager();
            Q_ASSERT(manager != nullptr);
            manager->registerExtensions(new MyExtensionFactory(manager),
                                        Q_TYPEID(QDesignerTaskMenuExtension));
            initialized = true;
        }
					

The QExtensionManager is not intended to be instantiated directly. You can retrieve an interface to Qt Designer 's extension manager using the QDesignerFormEditorInterface::extensionManager () function. A pointer to Qt Designer 's current QDesignerFormEditorInterface object ( formEditor in the example above) is provided by the QDesignerCustomWidgetInterface::initialize () function's parameter. When implementing a custom widget plugin, you must subclass the QDesignerCustomWidgetInterface to expose your plugin to Qt Designer .

Then, 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 the 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 Designer : QDesignerContainerExtension , QDesignerMemberSheetExtension , QDesignerPropertySheetExtension and QDesignerTaskMenuExtension . Qt Designer 's behavior is the same whether the requested extension is associated with a container, a member sheet, a property sheet or a task menu.

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

另请参阅 QExtensionFactory and QAbstractExtensionManager .

成员函数文档编制

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

Constructs an extension manager with the given parent .

[虚拟] QExtensionManager:: ~QExtensionManager ()

Destroys the extension manager

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

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

Returns the extension specified by iid , for the given object .

[override virtual] void QExtensionManager:: registerExtensions ( QAbstractExtensionFactory * factory , const QString & iid = QString())

重实现: QAbstractExtensionManager::registerExtensions (QAbstractExtensionFactory *factory, const QString &iid).

Register the extension specified by the given factory and extension identifier iid .

[override virtual] void QExtensionManager:: unregisterExtensions ( QAbstractExtensionFactory * factory , const QString & iid = QString())

重实现: QAbstractExtensionManager::unregisterExtensions (QAbstractExtensionFactory *factory, const QString &iid).

Unregister the extension specified by the given factory and extension identifier iid .

相关非成员

template <typename T> T qt_extension ( QAbstractExtensionManager * manager , QObject * object )

Returns the extension of the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. The extension is retrieved using the given extension manager .

       auto *manager = formEditor->extensionManager();
       auto *propertySheet = qt_extension<QDesignerPropertySheetExtension*>(manager, widget);
       if(propertySheet) {...}
					

When implementing a custom widget plugin, a pointer to Qt Designer 's current QDesignerFormEditorInterface object ( formEditor ) is provided by the QDesignerCustomWidgetInterface::initialize () function's parameter.

If the widget in the example above doesn't have a defined QDesignerPropertySheetExtension , propertySheet will be a null pointer.

宏文档编制

Q_DECLARE_EXTENSION_INTERFACE ( ExtensionName , Identifier )

关联给定 Identifier (a string literal) to the extension class called ExtensionNameIdentifier 必须唯一。例如:

   Q_DECLARE_EXTENSION_INTERFACE(MyExtension, "com.mycompany.myproduct.myextension")
					

Using the company and product names is a good way to ensure uniqueness of the identifier.

When implementing a custom extension class, you must use Q_DECLARE_EXTENSION_INTERFACE() to enable usage of the qt_extension () function. The macro is normally located right after the class definition for ExtensionName , in the associated header file.

另请参阅 Q_DECLARE_INTERFACE ().