QDialogButtongBox类是一个包含很多按钮的控件,在对话框中有多个按钮需要分组排列的按钮时,可以使用QDialogButtongBox类。
对话框或者消息框中的按钮布局,不同平台风格不同。开发人员可以向QDialogButtonBox添加按钮,在添加后QDialogButtonBox会为用户自动使用合适的布局。
Header:
#include <QDialogButtonBox>
qmake:
QT += widgets
Since:
Qt 4.2
Inherits:
QWidget.
enum
ButtonLayout { WinLayout, MacLayout, KdeLayout, GnomeLayout }
enum
ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ..., ResetRole }
enum
StandardButton { Ok, Open, Save, Cancel, ..., NoButton }
flags
StandardButtons
centerButtons : bool
orientation : Qt::Orientation
standardButtons : StandardButtons
59 properties inherited from QWidget
1 property inherited from QObject
QDialogButtonBox(QWidget * parent = 0)
QDialogButtonBox(Qt::Orientation orientation, QWidget * parent = 0)
QDialogButtonBox(StandardButtons buttons, QWidget * parent = 0)
QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation, QWidget * parent = 0)
~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
217 public functions inherited from QWidget
31 public functions inherited from QObject
13 public functions inherited from QPaintDevice
void
accepted()
void
clicked(QAbstractButton * button)
void
helpRequested()
void
rejected()
4 signals inherited from QWidget
2 signals inherited from QObject
virtual void
changeEvent(QEvent * event)
virtual bool
event(QEvent * event)
37 protected functions inherited from QWidget
9 protected functions inherited from QObject
1 protected function inherited from QPaintDevice
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
37 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
对话框的按钮大多有一些角色(roles),包括:
Ø 接受或拒绝;
Ø 帮助;
Ø 完成特定功能;
按钮的角色可以通过ButtonRole()返回的枚举值得到,一个按钮可以同时使用多个角色,通过多个角色的flag的组合来实现。具体如下:
enum QDialogButtonBox::ButtonRole
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.
对于对话框中的标准按钮(如,OK,Cancel,Save)可以在QDialogButtonBox创建时,使用flag标志的或运算指定。
enum QDialogButtonBox::StandardButton flags QDialogButtonBox::StandardButtons
下面的这些枚举值用来描述标准按钮。每一个按钮有一个已经定义好的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.
例如:
对于对话框中的普通按钮,按照如下方式创建:自己创建按钮(button),让后将按钮添加到button box, 并制定按钮的作用(role)。
例如:
findButton = new QPushButton(tr("&Find")); findButton->setDefault(true); moreButton = new QPushButton(tr("&More")); moreButton->setCheckable(true); moreButton->setAutoDefault(false);可以混合搭配使用普通按钮和标准按钮。
另外,QDialogButtongBox中的按钮,可以水平排列也可以竖直排列。在创建 QDialogButtongBox时,通过指定参数Qt::Orientation orientation 来指定按钮的布局方式。
例如:
buttonBox = new QDialogButtonBox(Qt::Horizontal); QPushButton *mkdirButton = buttonBox->addButton( tr("&Create Directory..."), QDialogButtonBox::ActionRole); QPushButton *removeButton = buttonBox->addButton(tr("&Remove"), QDialogButtonBox::ActionRole); buttonBox->addButton(tr("&Quit"), QDialogButtonBox::AcceptRole); connect(mkdirButton, SIGNAL(clicked()), this, SLOT(createDirectory())); connect(removeButton, SIGNAL(clicked()), this, SLOT(remove())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
对话框中,三个按钮的显示效果如下:
