本页内容

QDesignerPropertySheetExtension Class

QDesignerPropertySheetExtension 类允许您操作在小工具属性编辑器(Qt Designer )中显示的小部件属性。更多内容...

标题: #include <QDesignerPropertySheetExtension>
CMake: find_package(Qt6 REQUIRED COMPONENTS Designer)
target_link_libraries(mytarget PRIVATE Qt6::Designer)
qmake: QT += designer

公共函数

QDesignerPropertySheetExtension()
QDesignerPropertySheetExtension(const QDesignerPropertySheetExtension &)
QDesignerPropertySheetExtension(QDesignerPropertySheetExtension &&)
virtual ~QDesignerPropertySheetExtension()
virtual int count() const = 0
virtual bool hasReset(int index) const = 0
virtual int indexOf(const QString &name) const = 0
virtual bool isAttribute(int index) const = 0
virtual bool isChanged(int index) const = 0
virtual bool isEnabled(int index) const = 0
virtual bool isVisible(int index) const = 0
virtual QVariant property(int index) const = 0
virtual QString propertyGroup(int index) const = 0
virtual QString propertyName(int index) const = 0
virtual bool reset(int index) = 0
virtual void setAttribute(int index, bool attribute) = 0
virtual void setChanged(int index, bool changed) = 0
virtual void setProperty(int index, const QVariant &value) = 0
virtual void setPropertyGroup(int index, const QString &group) = 0
virtual void setVisible(int index, bool visible) = 0
QDesignerPropertySheetExtension &operator=(QDesignerPropertySheetExtension &&)
QDesignerPropertySheetExtension &operator=(const QDesignerPropertySheetExtension &)

详细说明

QDesignerPropertySheetExtension 提供了一组函数,通常用于查询控件的属性,以及在属性编辑器中操作属性的外观。例如:

QDesignerPropertySheetExtension *propertySheet = nullptr;
QExtensionManager manager = formEditor->extensionManager();

propertySheet = qt_extension<QDesignerPropertySheetExtension*>(manager, widget);
int index = propertySheet->indexOf(u"margin"_s);

propertySheet->setProperty(index, 10);
propertySheet->setChanged(index, true);

delete propertySheet;

请注意,若使用QDesignerPropertySheetExtension::setProperty()函数更改属性值,撤销堆栈将不会更新。若要确保可通过撤销堆栈还原属性值,必须改用QDesignerFormWindowCursorInterface::setProperty()函数,或其配套函数setWidgetProperty()。

在实现自定义控件插件时,QDesignerCustomWidgetInterface::initialize() 函数的参数会提供指向Qt Widgets Designer 当前QDesignerFormEditorInterface 对象(如上例中的formEditor )的指针。

可以通过使用qt_extension() 函数查询Qt Widgets Designer 的扩展管理器来获取属性表或任何其他扩展。当您想要释放该扩展时,只需删除该指针即可。

所有控件都具有一个默认属性表,该属性表会将控件的属性(即使用Q_PROPERTY() 宏定义的属性)填充到Qt Widgets Designer 的属性编辑器中。但 QDesignerPropertySheetExtension 还提供了一个用于创建自定义属性表扩展的接口。

请注意以下限制:

  • Qt Widgets Designer Qt Widgets Designer 使用 QDesignerPropertySheetExtension 来为其属性编辑器提供数据。每当在工作区中选中一个小部件时, 都会查询该小部件的属性表扩展。如果所选小部件已实现属性表扩展,则该扩展将覆盖默认属性表。
  • 属性表中某些属性所使用的数据类型并非普通的 Qt 数据类型,而是包含额外信息的不透明自定义QVariant 类型。例如,枚举、标志、图标、位图和字符串均属于此类情况。
  • Qt Widgets Designer的属性编辑器没有实现来处理通过 `Q_DECLARE_METATYPE()` 声明的自定义类型所使用的 `Q_PROPERTY ` 类型。

要创建属性表扩展,您的扩展类必须同时继承自QObject 和 QDesignerPropertySheetExtension。然后,由于我们正在实现一个接口,必须确保通过Q_INTERFACES() 宏将其告知元对象系统:

class MyPropertySheetExtension : public QObject,
        public QDesignerPropertySheetExtension
{
    Q_OBJECT
    Q_INTERFACES(QDesignerPropertySheetExtension)

public:
    ...
}

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

在Qt Widgets Designer 中,扩展直到被需要时才会被创建。因此,在实现属性表扩展时,您还必须创建一个QExtensionFactory (即能够生成该扩展实例的类),并使用Qt Widgets Designer 的extension manager 方法将其注册。

当需要属性表扩展时,Qt Widgets Designer 的extension manager 会遍历所有已注册的工厂,对每个工厂调用QExtensionFactory::createExtension(),直到找到第一个能够为所选小部件创建属性表扩展的工厂为止。随后,该工厂将创建该扩展的实例。如果找不到这样的工厂,Qt Widgets Designer 将使用默认属性表。

在Qt Widgets Designer 中,共有四种可用的扩展类型:QDesignerContainerExtension 、QDesignerMemberSheetExtension 、QDesignerPropertySheetExtension 和QDesignerTaskMenuExtension 。无论请求的扩展是与多页容器、成员表、属性表还是任务菜单相关联,Qt Designer 的行为均保持一致。

QExtensionFactory 类提供了一个标准的扩展工厂,也可作为自定义扩展工厂的接口。您可以创建一个新的QExtensionFactory ,并重写QExtensionFactory::createExtension()函数。例如:

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

    if (MyCustomWidget *widget = qobject_cast<MyCustomWidget*>
           (object))
        return new MyPropertySheetExtension(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(QDesignerPropertySheetExtension))) {
        return new MyPropertySheetExtension(widget, parent);

    } else {
        return 0;
    }
}

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

另请参阅 QDesignerDynamicPropertySheetExtension 、QExtensionFactory 、QExtensionManager 以及《创建自定义小部件扩展》。

成员函数文档

[constexpr noexcept default] QDesignerPropertySheetExtension::QDesignerPropertySheetExtension()

默认——构造一个QDesignerPropertySheetExtension 的实例。

[delete] QDesignerPropertySheetExtension::QDesignerPropertySheetExtension(const QDesignerPropertySheetExtension &)

复制并构造一个QDesignerPropertySheetExtension 实例。该函数已被删除。

[delete] QDesignerPropertySheetExtension::QDesignerPropertySheetExtension(QDesignerPropertySheetExtension &&)

通过移动构造一个QDesignerPropertySheetExtension 实例。该函数已被删除。

[virtual constexpr noexcept default] QDesignerPropertySheetExtension::~QDesignerPropertySheetExtension()

删除属性表扩展。

[pure virtual] int QDesignerPropertySheetExtension::count() const

返回所选控件的属性数量。

[pure virtual] bool QDesignerPropertySheetExtension::hasReset(int index) const

如果位于给定index 的属性在Qt Widgets Designer 的属性编辑器中带有“重置”按钮,则返回 true;否则返回 false。

另请参阅 indexOf() 和reset()。

[pure virtual] int QDesignerPropertySheetExtension::indexOf(const QString &name) const

返回给定属性name 的索引。

另请参阅 propertyName()。

[pure virtual] bool QDesignerPropertySheetExtension::isAttribute(int index) const

如果指定位置index 处的属性是属性(attribute),则返回 true,该属性将从 UI 文件中排除;否则返回 false。

另请参阅 indexOf() 和setAttribute()。

[pure virtual] bool QDesignerPropertySheetExtension::isChanged(int index) const

如果给定index 处的属性值与该属性的默认值不同,则返回 true;否则返回 false。

另请参阅 indexOf()、setChanged() 和reset()。

[pure virtual] bool QDesignerPropertySheetExtension::isEnabled(int index) const

如果位于index 的属性在Qt Widgets Designer 的属性编辑器中已启用,则返回 true;否则返回 false。

另请参阅 indexOf()。

[pure virtual] bool QDesignerPropertySheetExtension::isVisible(int index) const

如果位于给定index 处的属性在Qt Widgets Designer 的属性编辑器中可见,则返回true;否则返回false。

另请参阅 indexOf() 和setVisible()。

[pure virtual] QVariant QDesignerPropertySheetExtension::property(int index) const

返回指定index 处的属性值。

另请参阅 indexOf()、setProperty() 和propertyGroup()。

[pure virtual] QString QDesignerPropertySheetExtension::propertyGroup(int index) const

返回位于给定index 处的属性的属性组。

Qt Widgets Designer的属性编辑器支持属性组,即相关属性的分组。可以通过setPropertyGroup()函数将属性与一个组相关联。任何属性的默认组是定义该属性的类的名称。例如,QObject::objectName 属性出现在QObject 属性组中。

另请参阅 indexOf() 和setPropertyGroup()。

[pure virtual] QString QDesignerPropertySheetExtension::propertyName(int index) const

返回位于给定index 处的属性名称。

另请参阅 indexOf()。

[pure virtual] bool QDesignerPropertySheetExtension::reset(int index)

将指定位置index 处的属性值重置为默认值。如果找到了默认值,则返回true;否则返回false。

另请参阅 indexOf()、hasReset() 和isChanged()。

[pure virtual] void QDesignerPropertySheetExtension::setAttribute(int index, bool attribute)

如果attribute 为真,则位于指定index 处的属性将被设为属性,从而从 UI 文件中排除;否则将被包含在内。

另请参阅 indexOf() 和isAttribute()。

[pure virtual] void QDesignerPropertySheetExtension::setChanged(int index, bool changed)

根据changed 参数,设置指定index 处的属性是否与默认值不同。

另请参阅 indexOf() 和isChanged()。

[pure virtual] void QDesignerPropertySheetExtension::setProperty(int index, const QVariant &value)

将该属性的value 属性设置为给定的index 。

警告:若 使用此函数更改属性的值,撤销堆栈将不会更新。为确保可通过撤销堆栈还原属性的值,必须改用QDesignerFormWindowCursorInterface::setProperty()函数,或其关联函数setWidgetProperty()。

另请参阅 indexOf()、property() 和propertyGroup()。

[pure virtual] void QDesignerPropertySheetExtension::setPropertyGroup(int index, const QString &group)

将位于指定位置index 的属性的属性组设置为group 。

将属性与组相关联后,该属性将显示在属性编辑器中该组的相应部分内。任何属性的默认属性组即为定义该属性的类名。例如,QObject::objectName 属性会显示在QObject 属性组中。

另请参阅 indexOf()、property() 和propertyGroup()。

[pure virtual] void QDesignerPropertySheetExtension::setVisible(int index, bool visible)

如果visible 为真,则位于指定index 的属性在Qt Widgets Designer 的属性编辑器中可见;否则该属性被隐藏。

另请参阅 indexOf() 和isVisible()。

[delete] QDesignerPropertySheetExtension &QDesignerPropertySheetExtension::operator=(QDesignerPropertySheetExtension &&)

将other 按移动赋值方式赋值给此QDesignerPropertySheetExtension 实例。此函数已被删除。

[delete] QDesignerPropertySheetExtension &QDesignerPropertySheetExtension::operator=(const QDesignerPropertySheetExtension &)

将other 复制并赋值给此QDesignerPropertySheetExtension 实例。该函数已被删除。

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