PySide6.QtDesigner.QDesignerPropertySheetExtension¶
- class QDesignerPropertySheetExtension¶
QDesignerPropertySheetExtension
类允许您操作在 Qt Designer 的属性编辑器中显示的小部件的属性。更多…概要¶
方法¶
def
__init__()
虚拟方法¶
def
count()
def
hasReset()
def
indexOf()
def
isAttribute()
def
isChanged()
def
isEnabled()
def
isVisible()
def
property()
def
propertyGroup()
def
propertyName()
def
reset()
def
setAttribute()
def
setChanged()
def
setProperty()
def
setVisible()
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
QDesignerPropertySheetExtension
提供了一系列函数,通常用于查询小部件的属性,并操作属性在属性编辑器中的显示。例如:propertySheet = None manager = formEditor.extensionManager() propertySheet = qt_extension<QDesignerPropertySheetExtension*>(manager, widget) index = propertySheet.indexOf("margin") propertySheet.setProperty(index, 10) propertySheet.setChanged(index, True) del propertySheet
请注意,如果您使用
setProperty()
函数更改属性的值,撤销堆栈不会被更新。为了确保可以使用撤销堆栈恢复属性的值,您必须使用setProperty()
函数或其伙伴setWidgetProperty()
。在实现自定义小部件插件时,指向Qt Widgets Designer当前的
QDesignerFormEditorInterface
对象(在上面的示例中为formEditor
)的指针由initialize()
函数的参数提供。属性表或任何其他扩展可以通过使用
qt_extension()
函数查询Qt Widgets Designer的扩展管理器来获取。当你想释放扩展时,只需删除指针。所有小部件都有一个默认的属性表,该属性表将小部件的属性(即使用Q_PROPERTY()宏定义的属性)填充到Qt小部件设计器的属性编辑器中。但
QDesignerPropertySheetExtension
也提供了一个接口,用于创建自定义属性表扩展。请记住以下限制:
Qt Widgets Designer 使用
QDesignerPropertySheetExtension
来填充其属性编辑器。每当在工作区中选择一个部件时,Qt Widgets Designer 将查询该部件的属性表扩展。如果选中的部件实现了属性表扩展,此扩展将覆盖默认的属性表。属性表用于某些属性的数据类型是不透明的自定义QVariant类型,包含附加信息,而不是普通的Qt数据类型。例如,枚举、标志、图标、像素图和字符串就是这种情况。
Qt Widgets Designer 的属性编辑器没有实现对使用 Q_DECLARE_METATYPE() 声明的自定义类型的 Q_PROPERTY 类型的处理。
要创建一个属性表扩展,您的扩展类必须同时继承自 QObject 和
QDesignerPropertySheetExtension
。然后,由于我们正在实现一个接口,我们必须确保使用 Q_INTERFACES() 宏将其告知元对象系统:class MyPropertySheetExtension(QObject, public QDesignerPropertySheetExtension Q_OBJECT Q_INTERFACES(QDesignerPropertySheetExtension) # public ...
这使得 Qt Widgets Designer 能够使用 qobject_cast() 仅通过 QObject 指针来查询支持的接口。
在Qt Widgets Designer中,扩展只有在需要时才会被创建。因此,在实现属性表扩展时,您还必须创建一个
QExtensionFactory
,即一个能够创建扩展实例的类,并使用Qt Widgets Designer的extension manager
进行注册。当需要属性表扩展时,Qt Widgets Designer 的
extension manager
将遍历所有已注册的工厂,为每个工厂调用createExtension()
,直到找到第一个能够为所选小部件创建属性表扩展的工厂。然后,该工厂将创建扩展的实例。如果找不到这样的工厂,Qt Widgets Designer 将使用默认的属性表。Qt Widgets Designer 中有四种可用的扩展类型:
QDesignerContainerExtension
、QDesignerMemberSheetExtension
、QDesignerPropertySheetExtension
和QDesignerTaskMenuExtension
。无论请求的扩展是与多页容器、成员表、属性表还是任务菜单相关联,Qt Designer 的行为都是相同的。QExtensionFactory
类提供了一个标准的扩展工厂,也可以用作自定义扩展工厂的接口。你可以创建一个新的QExtensionFactory
并重新实现createExtension()
函数。例如:QObject ANewExtensionFactory.createExtension(QObject object, QString iid, QObject parent) if iid != Q_TYPEID(QDesignerPropertySheetExtension): return 0 if (MyCustomWidget widget = qobject_cast<MyCustomWidget> (object)) return MyPropertySheetExtension(widget, parent) return 0
或者你可以使用现有的工厂,扩展
createExtension()
函数,使工厂也能够创建属性表扩展。例如:QObject AGeneralExtensionFactory.createExtension(QObject object, QString iid, QObject parent) widget = MyCustomWidget(object) if widget and (iid == Q_TYPEID(QDesignerTaskMenuExtension)): return MyTaskMenuExtension(widget, parent) elif widget and (iid == Q_TYPEID(QDesignerPropertySheetExtension)): return MyPropertySheetExtension(widget, parent) else: return 0
有关使用扩展类的完整示例,请参见任务菜单扩展示例。该示例展示了如何为Qt Designer创建自定义小部件插件,以及如何使用
QDesignerTaskMenuExtension
类向Qt Widgets Designer的任务菜单添加自定义项。另请参阅
QDesignerDynamicPropertySheetExtension
QExtensionFactory
QExtensionManager
创建自定义小部件扩展- __init__()¶
- abstract count()¶
- Return type:
整数
返回所选小部件的属性数量。
- abstract hasReset(index)¶
- Parameters:
索引 – int
- Return type:
布尔
如果给定的
index
处的属性在Qt Widgets Designer的属性编辑器中有一个重置按钮,则返回true,否则返回false。- abstract indexOf(name)¶
- Parameters:
name – str
- Return type:
整数
返回给定属性
name
的索引。另请参阅
- abstract isAttribute(index)¶
- Parameters:
索引 – int
- Return type:
布尔
如果给定
index
处的属性是一个属性,并且将从UI文件中排除,则返回true,否则返回false。另请参阅
- abstract isChanged(index)¶
- Parameters:
索引 – int
- Return type:
布尔
如果给定
index
处的属性值与属性的默认值不同,则返回true,否则返回false。另请参阅
- abstract isEnabled(index)¶
- Parameters:
索引 – int
- Return type:
布尔
如果在Qt Widgets Designer的属性编辑器中启用了给定
index
处的属性,则返回true,否则返回false。另请参阅
- abstract isVisible(index)¶
- Parameters:
索引 – int
- Return type:
布尔
如果给定
index
处的属性在Qt Widgets Designer的属性编辑器中可见,则返回true,否则返回false。另请参阅
- abstract property(index)¶
- Parameters:
索引 – int
- Return type:
对象
返回给定
index
处属性的值。- abstract propertyGroup(index)¶
- Parameters:
索引 – int
- Return type:
字符串
返回给定
index
处属性的属性组。Qt Widgets Designer 的属性编辑器支持属性组,即相关属性的部分。可以使用
setPropertyGroup()
函数将属性与组关联。任何属性的默认组是定义它的类的名称。例如,QObject::objectName 属性出现在 QObject 属性组中。- abstract propertyName(index)¶
- Parameters:
索引 – int
- Return type:
字符串
返回给定
index
处的属性名称。另请参阅
- abstract reset(index)¶
- Parameters:
索引 – int
- Return type:
布尔
将给定
index
处的属性值重置为默认值。如果找到默认值,则返回true,否则返回false。- abstract setAttribute(index, b)¶
- Parameters:
index – 整数
b – 布尔值
如果
attribute
为真,则给定index
处的属性将被设置为一个属性,该属性将从 UI 文件中排除;否则它将被包含在内。另请参阅
- abstract setChanged(index, changed)¶
- Parameters:
index – 整数
changed – bool
设置给定
index
处的属性是否与其默认值不同,取决于changed
参数。另请参阅
- abstract setProperty(index, value)¶
- Parameters:
index – 整数
value – 对象
设置给定
index
处属性的value
。警告
如果你使用这个函数更改属性的值,撤销堆栈不会被更新。为了确保属性的值可以使用撤销堆栈恢复,你必须使用
setProperty()
函数,或者它的伙伴setWidgetProperty()
来代替。- abstract setPropertyGroup(index, group)¶
- Parameters:
index – 整数
group – str
将给定
index
处的属性组设置为group
。将属性关联到一个组会使它出现在属性编辑器中该组的部分。任何属性的默认属性组是定义它的类的名称。例如,QObject::objectName 属性出现在 QObject 属性组中。
- abstract setVisible(index, b)¶
- Parameters:
index – 整数
b – 布尔值
如果
visible
为 true,则在 Qt Widgets Designer 的属性编辑器中,给定index
处的属性是可见的;否则该属性是隐藏的。另请参阅