QDialogButtonBox Class
QDialogButtonBox 是一个widget, 它以适合当前widget样式, 显示布局中的按钮. 更多...
Header: | #include <QDialogButtonBox> |
qmake: | QT += widgets |
Since: | Qt 4.2 |
Inherits: | QWidget |
Public Types
enum | ButtonLayout { WinLayout, MacLayout, KdeLayout, GnomeLayout } |
enum | ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ..., ResetRole } |
enum | StandardButton { Ok, Open, Save, Cancel, ..., NoButton } |
flags | StandardButtons |
Properties
- centerButtons : bool
- orientation : Qt::Orientation
- standardButtons : StandardButtons
Public Functions
QDialogButtonBox(QWidget *parent = Q_NULLPTR) | |
QDialogButtonBox(Qt::Orientation orientation, QWidget *parent = Q_NULLPTR) | |
QDialogButtonBox(StandardButtons buttons, QWidget *parent = Q_NULLPTR) | |
QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation, QWidget *parent = Q_NULLPTR) | |
~QDialogButtonBox() | |
void | addButton(QAbstractButton *button, ButtonRole role) |
QPushButton * | addButton(const QString &text, ButtonRole role) |
QPushButton * | addButton(StandardButton button) |
QPushButton * | button(StandardButton which) const |
ButtonRole | buttonRole(QAbstractButton *button) const |
QList<QAbstractButton *> | buttons() const |
bool | centerButtons() const |
void | clear() |
Qt::Orientation | orientation() const |
void | removeButton(QAbstractButton *button) |
void | setCenterButtons(bool center) |
void | setOrientation(Qt::Orientation orientation) |
void | setStandardButtons(StandardButtons buttons) |
StandardButton | standardButton(QAbstractButton *button) const |
StandardButtons | standardButtons() const |
- 214 public functions inherited from QWidget
- 32 public functions inherited from QObject
- 14 public functions inherited from QPaintDevice
Signals
void | accepted() |
void | clicked(QAbstractButton *button) |
void | helpRequested() |
void | rejected() |
Reimplemented Protected Functions
virtual void | changeEvent(QEvent *event) |
virtual bool | event(QEvent *event) |
- 35 protected functions inherited from QWidget
- 9 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
Additional Inherited Members
- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject
- 1 public variable inherited from QObject
- 5 static public members inherited from QWidget
- 10 static public members inherited from QObject
- 35 protected functions inherited from QWidget
- 9 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
- 1 protected slot inherited from QWidget
- 2 protected variables inherited from QObject
- 1 protected type inherited from QPaintDevice
Detailed Description
QDialogButtonBox 是一个widget, 它以适合当前widget样式, 显示布局中的按钮.
对话框和消息框通常以平台样式显示布局中的按钮. 总之, 不同的平台有不同的对话框布局. QDialogButtonBox 允许开发人员向其添加按钮, 并自动根据用户的桌面环境采用适当的布局.
对话框的多数按钮有特定的作用. 如:
- 接受或拒绝对话.
- 寻求帮助.
- 对话框自身执行action (如重置或应用修改).
你也可以使用其他方式取消对话框, 这可能导致破坏性结果.
大多数对话框有标准按钮 (如. OK 和 Cancel 按钮). 有时, 使用标准方式创建这些按钮是很方便的.
使用 QDialogButtonBox有几种方式. 一种是创建按钮, 然后将其加入按钮框, 如下:
findButton = new QPushButton(tr("&Find")); findButton->setDefault(true); moreButton = new QPushButton(tr("&More")); moreButton->setCheckable(true); moreButton->setAutoDefault(false);
还有一种方式, QDialogButtonBox 提供几种标准按钮 (如. OK, Cancel, Save). 你可以在构造时采用枚举类型的与运算.
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
你可以混合和匹配普通按钮和标准按钮.
目前, 如果按钮是水平的, 它的样式显示如下:
![]() | Button box laid out in horizontal GnomeLayout |
![]() | Button box laid out in horizontal KdeLayout |
![]() | Button box laid out in horizontal MacLayout |
![]() | Button box laid out in horizontal WinLayout |
如果按钮是垂直的, 它的样式显示如下:
此外, 仅包含 ActionRole 或 HelpRole 的按钮框可以视为无模式. 在macOS上 macOS 它有其他外观:
在按钮框中点击按钮时, 被点击的按钮发送 clicked() 信号. 方便起见, 若按钮具有 AcceptRole, RejectRole 或 HelpRole, 自动发送 accepted(), rejected() 或 helpRequested() 信号.
如果你想设置默认按钮, 你可以调用 QPushButton::setDefault(). 然而, 然而, 若用户没有设置默认按钮, 系统显示默认按钮(QPushButton::autoDefault )时, QDialogButtonBox将AcceptRole的按钮设置为默认按钮.
参见 QMessageBox, QPushButton, QDialog.
Member Type Documentation
enum QDialogButtonBox::ButtonLayout
This enum describes the layout policy to be used when arranging the buttons contained in the button box.
Constant | Value | Description |
---|---|---|
QDialogButtonBox::WinLayout | 0 | Use a policy appropriate for applications on Windows. |
QDialogButtonBox::MacLayout | 1 | Use a policy appropriate for applications on macOS. |
QDialogButtonBox::KdeLayout | 2 | Use a policy appropriate for applications on KDE. |
QDialogButtonBox::GnomeLayout | 3 | Use a policy appropriate for applications on GNOME. |
The button layout is specified by the current style. However, on the X11 platform, it may be influenced by the desktop environment.
enum QDialogButtonBox::ButtonRole
This enum describes the roles that can be used to describe buttons in the button box. Combinations of these roles are as flags used to describe different aspects of their behavior.
Constant | Value | Description |
---|---|---|
QDialogButtonBox::InvalidRole | -1 | The button is invalid. |
QDialogButtonBox::AcceptRole | 0 | Clicking the button causes the dialog to be accepted (e.g. OK). |
QDialogButtonBox::RejectRole | 1 | Clicking the button causes the dialog to be rejected (e.g. Cancel). |
QDialogButtonBox::DestructiveRole | 2 | Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog. |
QDialogButtonBox::ActionRole | 3 | Clicking the button causes changes to the elements within the dialog. |
QDialogButtonBox::HelpRole | 4 | The button can be clicked to request help. |
QDialogButtonBox::YesRole | 5 | The button is a "Yes"-like button. |
QDialogButtonBox::NoRole | 6 | The button is a "No"-like button. |
QDialogButtonBox::ApplyRole | 8 | The button applies current changes. |
QDialogButtonBox::ResetRole | 7 | The button resets the dialog's fields to default values. |
See also StandardButton.
enum QDialogButtonBox::StandardButton
flags QDialogButtonBox::StandardButtons
These enums describe flags for standard buttons. Each button has a defined ButtonRole.
Constant | Value | Description |
---|---|---|
QDialogButtonBox::Ok | 0x00000400 | An "OK" button defined with the AcceptRole. |
QDialogButtonBox::Open | 0x00002000 | An "Open" button defined with the AcceptRole. |
QDialogButtonBox::Save | 0x00000800 | A "Save" button defined with the AcceptRole. |
QDialogButtonBox::Cancel | 0x00400000 | A "Cancel" button defined with the RejectRole. |
QDialogButtonBox::Close | 0x00200000 | A "Close" button defined with the RejectRole. |
QDialogButtonBox::Discard | 0x00800000 | A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole. |
QDialogButtonBox::Apply | 0x02000000 | An "Apply" button defined with the ApplyRole. |
QDialogButtonBox::Reset | 0x04000000 | A "Reset" button defined with the ResetRole. |
QDialogButtonBox::RestoreDefaults | 0x08000000 | A "Restore Defaults" button defined with the ResetRole. |
QDialogButtonBox::Help | 0x01000000 | A "Help" button defined with the HelpRole. |
QDialogButtonBox::SaveAll | 0x00001000 | A "Save All" button defined with the AcceptRole. |
QDialogButtonBox::Yes | 0x00004000 | A "Yes" button defined with the YesRole. |
QDialogButtonBox::YesToAll | 0x00008000 | A "Yes to All" button defined with the YesRole. |
QDialogButtonBox::No | 0x00010000 | A "No" button defined with the NoRole. |
QDialogButtonBox::NoToAll | 0x00020000 | A "No to All" button defined with the NoRole. |
QDialogButtonBox::Abort | 0x00040000 | An "Abort" button defined with the RejectRole. |
QDialogButtonBox::Retry | 0x00080000 | A "Retry" button defined with the AcceptRole. |
QDialogButtonBox::Ignore | 0x00100000 | An "Ignore" button defined with the AcceptRole. |
QDialogButtonBox::NoButton | 0x00000000 | An invalid button. |
The StandardButtons type is a typedef for QFlags<StandardButton>. It stores an OR combination of StandardButton values.
See also ButtonRole and standardButtons.
Property Documentation
centerButtons : bool
This property holds whether the buttons in the button box are centered
By default, this property is false
. This behavior is appopriate for most types of dialogs. A notable exception is message boxes on most platforms (e.g. Windows), where the button box is centered horizontally.
Access functions:
bool | centerButtons() const |
void | setCenterButtons(bool center) |
See also QMessageBox.
orientation : Qt::Orientation
This property holds the orientation of the button box
By default, the orientation is horizontal (i.e. the buttons are laid out side by side). The possible orientations are Qt::Horizontal and Qt::Vertical.
Access functions:
Qt::Orientation | orientation() const |
void | setOrientation(Qt::Orientation orientation) |
standardButtons : StandardButtons
collection of standard buttons in the button box
This property controls which standard buttons are used by the button box.
Access functions:
StandardButtons | standardButtons() const |
void | setStandardButtons(StandardButtons buttons) |
See also addButton().
Member Function Documentation
QDialogButtonBox::QDialogButtonBox(QWidget *parent = Q_NULLPTR)
Constructs an empty, horizontal button box with the given parent.
See also orientation and addButton().
QDialogButtonBox::QDialogButtonBox(Qt::Orientation orientation, QWidget *parent = Q_NULLPTR)
Constructs an empty button box with the given orientation and parent.
See also orientation and addButton().
QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, QWidget *parent = Q_NULLPTR)
Constructs a horizontal button box with the given parent, containing the standard buttons specified by buttons.
This function was introduced in Qt 5.2.
See also orientation and addButton().
QDialogButtonBox::QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation, QWidget *parent = Q_NULLPTR)
Constructs a button box with the given orientation and parent, containing the standard buttons specified by buttons.
See also orientation and addButton().
QDialogButtonBox::~QDialogButtonBox()
Destroys the button box.
[signal]
void QDialogButtonBox::accepted()
This signal is emitted when a button inside the button box is clicked, as long as it was defined with the AcceptRole or YesRole.
See also rejected(), clicked(), and helpRequested().
void QDialogButtonBox::addButton(QAbstractButton *button, ButtonRole role)
Adds the given button to the button box with the specified role. If the role is invalid, the button is not added.
If the button has already been added, it is removed and added again with the new role.
Note: The button box takes ownership of the button.
See also removeButton() and clear().
QPushButton *QDialogButtonBox::addButton(const QString &text, ButtonRole role)
Creates a push button with the given text, adds it to the button box for the specified role, and returns the corresponding push button. If role is invalid, no button is created, and zero is returned.
See also removeButton() and clear().
QPushButton *QDialogButtonBox::addButton(StandardButton button)
Adds a standard button to the button box if it is valid to do so, and returns a push button. If button is invalid, it is not added to the button box, and zero is returned.
See also removeButton() and clear().
QPushButton *QDialogButtonBox::button(StandardButton which) const
Returns the QPushButton corresponding to the standard button which, or 0 if the standard button doesn't exist in this button box.
See also standardButton(), standardButtons(), and buttons().
ButtonRole QDialogButtonBox::buttonRole(QAbstractButton *button) const
Returns the button role for the specified button. This function returns InvalidRole if button is 0 or has not been added to the button box.
See also buttons() and addButton().
QList<QAbstractButton *> QDialogButtonBox::buttons() const
Returns a list of all the buttons that have been added to the button box.
See also buttonRole(), addButton(), and removeButton().
[virtual protected]
void QDialogButtonBox::changeEvent(QEvent *event)
Reimplemented from QWidget::changeEvent().
void QDialogButtonBox::clear()
Clears the button box, deleting all buttons within it.
See also removeButton() and addButton().
[signal]
void QDialogButtonBox::clicked(QAbstractButton *button)
This signal is emitted when a button inside the button box is clicked. The specific button that was pressed is specified by button.
See also accepted(), rejected(), and helpRequested().
[virtual protected]
bool QDialogButtonBox::event(QEvent *event)
Reimplemented from QObject::event().
[signal]
void QDialogButtonBox::helpRequested()
This signal is emitted when a button inside the button box is clicked, as long as it was defined with the HelpRole.
See also accepted(), rejected(), and clicked().
[signal]
void QDialogButtonBox::rejected()
This signal is emitted when a button inside the button box is clicked, as long as it was defined with the RejectRole or NoRole.
See also accepted(), helpRequested(), and clicked().
void QDialogButtonBox::removeButton(QAbstractButton *button)
Removes button from the button box without deleting it and sets its parent to zero.
See also clear(), buttons(), and addButton().
StandardButton QDialogButtonBox::standardButton(QAbstractButton *button) const
Returns the standard button enum value corresponding to the given button, or NoButton if the given button isn't a standard button.
See also button(), buttons(), and standardButtons().