PySide6.QtWidgets.QDialogButtonBox

class QDialogButtonBox

QDialogButtonBox 类是一个小部件,它以适合当前小部件样式的布局呈现按钮。更多

PySide6.QtWidgets.QDialogButtonBox 的继承图

概要

属性

方法

信号

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

对话框和消息框通常以符合该平台界面指南的布局呈现按钮。不同的平台总是有不同的对话框布局。QDialogButtonBox允许开发者向其添加按钮,并会自动为用户桌面环境使用适当的布局。

对话框中的大多数按钮遵循某些角色。这些角色包括:

  • 接受或拒绝对话框。

  • 请求帮助。

  • 在对话框本身上执行操作(例如重置字段或应用更改)。

也可能存在其他关闭对话框的方式,这可能会导致破坏性结果。

大多数对话框都有几乎可以被认为是标准的按钮(例如确定和取消按钮)。有时以标准方式创建这些按钮是很方便的。

有几种使用QDialogButtonBox的方法。一种方法是自己创建按钮(或按钮文本)并将它们添加到按钮框中,指定它们的角色。

buttonBox = QDialogButtonBox(Qt.Vertical)
buttonBox.addButton(findButton, QDialogButtonBox.ActionRole)
buttonBox.addButton(moreButton, QDialogButtonBox.ActionRole)

或者,QDialogButtonBox 提供了几个标准按钮(例如,确定、取消、保存)供您使用。它们以标志的形式存在,因此您可以在构造函数中将它们进行或运算。

buttonBox = QDialogButtonBox(QDialogButtonBox.Ok
                                 | QDialogButtonBox.Cancel)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)

您可以混合使用普通按钮和标准按钮。

目前,如果按钮框是水平的,按钮的布局如下:

buttonbox-gnomelayout-horizontal1 GnomeLayout 水平

按钮框以水平方式布局 GnomeLayout

buttonbox-kdelayout-horizontal2 KdeLayout 水平

按钮框以水平方式布局 KdeLayout

buttonbox-maclayout-horizontal3 MacLayout 水平

按钮框以水平方式布局 MacLayout

buttonbox-winlayout-horizontal4 WinLayout 水平

按钮框水平布局 WinLayout

如果按钮框是垂直的,按钮的布局如下:

此外,仅包含具有ActionRoleHelpRole按钮的按钮框可以被视为无模式,并且在macOS上具有另一种外观:

无模式水平 MacLayout

buttonbox-mac-modeless-horizontal9 无模式水平 MacLayout 的截图

无模式垂直 MacLayout

buttonbox-mac-modeless-vertical10 无模式垂直 MacLayout 的截图

当按钮框中的按钮被点击时,实际被按下的按钮会发出clicked()信号。为了方便起见,如果按钮具有AcceptRoleRejectRoleHelpRole,则会分别发出accepted()rejected()helpRequested()信号。

如果你想要一个特定的按钮作为默认按钮,你需要自己调用setDefault()。然而,如果没有设置默认按钮,并且为了在使用autoDefault属性时跨平台保留哪个按钮是默认按钮,当QDialogButtonBox显示时,第一个具有接受角色的按钮将被设为默认按钮。

class ButtonRole

此枚举描述了可用于描述按钮框中按钮的角色。这些角色的组合作为标志,用于描述其行为的不同方面。

常量

描述

QDialogButtonBox.InvalidRole

按钮无效。

QDialogButtonBox.AcceptRole

点击按钮会导致对话框被接受(例如确定)。

QDialogButtonBox.RejectRole

点击按钮会导致对话框被拒绝(例如取消)。

QDialogButtonBox.DestructiveRole

点击按钮会导致破坏性更改(例如,放弃更改)并关闭对话框。

QDialogButtonBox.ActionRole

点击按钮会导致对话框内的元素发生变化。

QDialogButtonBox.HelpRole

可以点击该按钮以请求帮助。

QDialogButtonBox.YesRole

该按钮是一个“是”类型的按钮。

QDialogButtonBox.NoRole

该按钮是一个“否”类型的按钮。

QDialogButtonBox.ApplyRole

该按钮应用当前更改。

QDialogButtonBox.ResetRole

该按钮将对话框的字段重置为默认值。

另请参阅

StandardButton

class StandardButton

(继承自 enum.Flag) 这些枚举描述了标准按钮的标志。每个按钮都有一个定义的 ButtonRole

常量

描述

QDialogButtonBox.Ok

一个“确定”按钮,使用AcceptRole定义。

QDialogButtonBox.Open

一个使用AcceptRole定义的“打开”按钮。

QDialogButtonBox.Save

一个“保存”按钮,使用AcceptRole定义。

QDialogButtonBox.Cancel

一个使用RejectRole定义的“取消”按钮。

QDialogButtonBox.Close

一个使用RejectRole定义的“关闭”按钮。

QDialogButtonBox.Discard

一个“丢弃”或“不保存”按钮,具体取决于平台,使用DestructiveRole定义。

QDialogButtonBox.Apply

一个使用ApplyRole定义的“应用”按钮。

QDialogButtonBox.Reset

一个使用ResetRole定义的“重置”按钮。

QDialogButtonBox.RestoreDefaults

一个“恢复默认”按钮,使用ResetRole定义。

QDialogButtonBox.Help

一个使用HelpRole定义的“帮助”按钮。

QDialogButtonBox.SaveAll

一个使用AcceptRole定义的“保存全部”按钮。

QDialogButtonBox.Yes

一个使用 YesRole 定义的“是”按钮。

QDialogButtonBox.YesToAll

一个定义为 YesRole 的“全部是”按钮。

QDialogButtonBox.No

一个使用 NoRole 定义的“否”按钮。

QDialogButtonBox.NoToAll

一个“全部否”按钮,使用NoRole定义。

QDialogButtonBox.Abort

一个“中止”按钮,使用RejectRole定义。

QDialogButtonBox.Retry

一个“重试”按钮,使用AcceptRole定义。

QDialogButtonBox.Ignore

一个“忽略”按钮,使用AcceptRole定义。

QDialogButtonBox.NoButton

一个无效的按钮。

class ButtonLayout

此枚举描述了在排列按钮框中包含的按钮时使用的布局策略。

常量

描述

QDialogButtonBox.WinLayout

使用适用于Windows应用程序的策略。

QDialogButtonBox.MacLayout

使用适用于macOS应用程序的策略。

QDialogButtonBox.KdeLayout

使用适用于KDE应用程序的策略。

QDialogButtonBox.GnomeLayout

使用适用于GNOME应用程序的策略。

QDialogButtonBox.AndroidLayout

使用适用于Android应用程序的策略。此枚举值在Qt 5.10中添加。

按钮布局由current style指定。然而,在X11平台上,它可能会受到桌面环境的影响。

注意

当使用from __feature__ import true_property时,属性可以直接使用,否则通过访问器函数使用。

property centerButtonsᅟ: bool

此属性表示按钮框中的按钮是否居中。

默认情况下,此属性为false。这种行为适用于大多数类型的对话框。一个显著的例外是大多数平台(例如Windows)上的消息框,其中按钮框是水平居中的。

另请参阅

QMessageBox

Access functions:
property orientationᅟ: Qt.Orientation

此属性保存按钮框的方向。

默认情况下,方向是水平的(即按钮并排排列)。可能的方向有 Qt::Horizontal 和 Qt::Vertical。

Access functions:
property standardButtonsᅟ: Combination of QDialogButtonBox.StandardButton

此属性保存按钮框中标准按钮的集合。

此属性控制按钮框使用哪些标准按钮。

另请参阅

addButton()

Access functions:
__init__([parent=None])
Parameters:

父级QWidget

使用给定的parent构造一个空的水平按钮框。

另请参阅

orientation addButton()

__init__(buttons[, parent=None])
Parameters:

使用给定的parent构建一个水平按钮框,包含由buttons指定的标准按钮。

另请参阅

orientation addButton()

__init__(orientation[, parent=None])
Parameters:

使用给定的orientationparent构造一个空的按钮框。

另请参阅

orientation addButton()

__init__(buttons, orientation[, parent=None])
Parameters:

使用给定的orientationparent构造一个按钮框,包含由buttons指定的标准按钮。

另请参阅

orientation addButton()

accepted()

当按钮框内的按钮被点击时,只要它是用AcceptRoleYesRole定义的,就会发出此信号。

addButton(button)
Parameters:

按钮StandardButton

Return type:

QPushButton

如果有效,则向按钮框添加一个标准的button,并返回一个按钮。如果button无效,则不会将其添加到按钮框中,并返回零。

另请参阅

removeButton() clear()

addButton(button, role)
Parameters:

将给定的button添加到具有指定role的按钮框中。如果角色无效,则不会添加按钮。

如果按钮已经添加,它将被移除并使用新角色再次添加。

注意

按钮框拥有按钮的所有权。

另请参阅

removeButton() clear()

addButton(text, role)
Parameters:
Return type:

QPushButton

创建一个带有给定text的按钮,将其添加到指定role的按钮框中,并返回相应的按钮。如果role无效,则不创建按钮,并返回零。

另请参阅

removeButton() clear()

button(which)
Parameters:

哪个StandardButton

Return type:

QPushButton

返回与标准按钮 which 对应的 QPushButton,如果此按钮框中不存在该标准按钮,则返回 None

buttonRole(button)
Parameters:

按钮QAbstractButton

Return type:

ButtonRole

返回指定button的按钮角色。如果buttonNone或尚未添加到按钮框中,则此函数返回InvalidRole

另请参阅

buttons() addButton()

buttons()
Return type:

QAbstractButton的列表

返回已添加到按钮框中的所有按钮的列表。

centerButtons()
Return type:

布尔

另请参阅

setCenterButtons()

属性 centerButtonsᅟ 的获取器。

clear()

清除按钮框,删除其中的所有按钮。

clicked(button)
Parameters:

按钮QAbstractButton

当按钮框内的按钮被点击时,会发出此信号。被按下的具体按钮由button指定。

helpRequested()

当按钮框内的按钮被点击时,只要它是用HelpRole定义的,就会发出此信号。

orientation()
Return type:

方向

另请参阅

setOrientation()

属性 orientationᅟ 的获取器。

rejected()

当按钮框内的按钮被点击时,如果该按钮是用RejectRoleNoRole定义的,则会发出此信号。

removeButton(button)
Parameters:

按钮QAbstractButton

从按钮框中移除button而不删除它,并将其父级设置为零。

setCenterButtons(center)
Parameters:

center – 布尔值

另请参阅

centerButtons()

属性 centerButtonsᅟ 的设置器。

setOrientation(orientation)
Parameters:

方向Orientation

另请参阅

orientation()

属性 orientationᅟ 的设置器。

setStandardButtons(buttons)
Parameters:

buttonsStandardButton 的组合

另请参阅

standardButtons()

属性 standardButtonsᅟ 的设置器。

standardButton(button)
Parameters:

按钮QAbstractButton

Return type:

StandardButton

返回与给定button对应的标准按钮枚举值,如果给定的button不是标准按钮,则返回NoButton

standardButtons()
Return type:

StandardButton的组合

另请参阅

setStandardButtons()

属性 standardButtonsᅟ 的获取器。