本页内容

创建自定义小部件扩展

一旦您拥有了适用于Qt Widgets Designer 的自定义控件插件,便可通过自定义控件扩展,在Qt Widgets Designer 的工作区中为其提供预期的行为和功能。

扩展类型

Qt Widgets Designer 中提供了多种扩展类型。您可以按照相同的模式使用所有这些扩展,只需替换相应的扩展基类即可。

QDesignerContainerExtension 在实现自定义多页容器时,此扩展是必需的。

设计师编辑器画布的截图,显示了一个简单的自定义控件,其上下文菜单已打开,并高亮显示了“编辑状态”选项QDesignerTaskMenuExtension

QDesignerTaskMenuExtension 对自定义小部件非常有用。它提供了一个扩展,允许您向Qt Widgets Designer 的任务菜单中添加自定义菜单项。

“任务菜单扩展”示例演示了如何使用该类。

设计师编辑器的屏幕截图,显示了一个容器和一个打开的上下文菜单QDesignerContainerExtension

QDesignerContainerExtension 在实现自定义多页容器时必不可少。它提供了一个扩展,允许您在Qt Widgets Designer 中为多页容器插件添加和删除页面。

“容器扩展”示例进一步说明了如何使用该类。

注意: 由于某些小部件(例如QTabWidget )的实现方式,无法 为其添加自定义的单页属性。

“配置连接”界面的截图,显示了已连接的小部件应相互发送哪些功能QDesignerMemberSheetExtension

QDesignerMemberSheetExtension 类允许您操作在连接信号和插槽时显示的小部件成员函数。

“属性编辑器”的截图,显示了对象的属性及所用的设置QDesignerPropertySheetExtension,QDesignerDynamicPropertySheetExtension

这些扩展类允许您控制控件的属性在Qt Widgets Designer 的属性编辑器中如何显示。

Qt Widgets Designer 使用QDesignerPropertySheetExtension 和QDesignerMemberSheetExtension 类为其属性编辑器以及信号和槽编辑器提供数据。每当在工作区中选中一个控件时,Qt Widgets Designer 会查询该控件的属性表扩展;同样,每当请求建立两个控件之间的连接时,Qt Widgets Designer 会查询这两个控件的成员表扩展。

警告:所有 控件都带有默认的属性表和成员表。如果您实现了自定义属性表或成员表扩展,您的自定义扩展将覆盖默认表。

创建扩展

要创建扩展,必须同时继承 `QObject ` 和相应的基类,并重写其函数。由于我们正在实现一个接口,因此必须通过在扩展类的定义中使用 `Q_INTERFACES()` 宏,确保该接口被元对象系统识别。例如:

class MyExtension: public QObject,
                   public QDesignerContainerExtension
{
    Q_OBJECT
    Q_INTERFACE(QDesignerContainerExtension)

    ...
}

这使得Qt Widgets Designer 能够仅通过QObject 指针,利用qobject_cast()函数查询支持的接口。

向Qt Widgets Designer

在Qt Widgets Designer 中,扩展只有在被需要时才会被创建。因此,在实现扩展时,您必须继承QExtensionFactory 类,以创建一个能够生成扩展实例的类。此外,您必须将工厂注册到Qt Widgets Designer 的扩展管理器中;扩展管理器负责处理扩展的构建。

当请求某个扩展时,Qt Widgets Designer 的扩展管理器会遍历其已注册的工厂,对每个工厂调用QExtensionFactory::createExtension(),直到找到一个能够为所选小部件创建所需扩展的工厂。随后,该工厂将创建该扩展的实例。

显示 Qt Widgets Designer 组件之间关系的图示

创建扩展工厂

QExtensionFactory 类提供了一个标准的扩展工厂,但它也可以作为自定义扩展工厂的接口。

其目的是重写QExtensionFactory::createExtension() 函数,使其能够创建您的扩展,例如MultiPageWidget容器扩展。

您可以创建一个新的QExtensionFactory 并重写QExtensionFactory::createExtension()函数:

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

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

    return 0;
}

或者,您可以使用现有的工厂,通过扩展 `QExtensionFactory::createExtension()` 函数,使该工厂也能创建您的自定义扩展:

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

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

    } else if (widget && (iid == Q_TYPEID(QDesignerContainerExtension))) {
        return new MyContainerExtension(widget, parent);

    } else {
        return 0;
    }
}

访问 `Qt Widgets Designer` 的扩展管理器

在实现自定义小部件插件时,必须继承QDesignerCustomWidgetInterface 类,以便将您的插件暴露给Qt Widgets Designer 。关于这一点,在“为Qt Widgets Designer 创建自定义小部件”一节中有更详细的说明。扩展工厂的注册通常在QDesignerCustomWidgetInterface::initialize() 函数中进行:

void MyPlugin::initialize(QDesignerFormEditorInterface *formEditor)
{
    if (initialized)
        return;

    QExtensionManager *manager = formEditor->extensionManager();
    Q_ASSERT(manager != 0);

    manager->registerExtensions(new MyExtensionFactory(manager),
                                Q_TYPEID(QDesignerTaskMenuExtension));

    initialized = true;
}

QDesignerCustomWidgetInterface::initialize() 函数中的formEditor 参数是Qt Widgets Designer 当前QDesignerFormEditorInterface 对象的指针。您必须使用QDesignerFormEditorInterface::extensionManager() 函数来获取Qt Widgets Designer 扩展管理器的接口。然后,您使用QExtensionManager::registerExtensions() 函数来注册您的自定义扩展工厂。

有关在Qt Widgets Designer 中创建自定义控件扩展的更多信息,请参阅“任务菜单扩展”和“容器扩展”示例。

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