
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the current widget style.More...
| Header: | #include <QDialogButtonBox> |
| 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 |
| QDialogButtonBox(QWidget * parent = 0) | |
| QDialogButtonBox(Qt::Orientation orientation, QWidget * parent = 0) | |
| QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation = Qt::Horizontal, 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 |
| void | accepted() |
| void | clicked(QAbstractButton * button) |
| void | helpRequested() |
| void | rejected() |
| virtual void | changeEvent(QEvent * event) |
| virtual bool | event(QEvent * event) |
TheQDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the current widget style.
Dialogs and message boxes typically present buttons in a layout that conforms to the interface guidelines for that platform. Invariably, different platforms have different layouts for their dialogs.QDialogButtonBox allows a developer to add buttons to it and will automatically use the appropriate layout for the user's desktop environment.
Most buttons for a dialog follow certain roles. Such roles include:
There can also be alternate ways of dismissing the dialog which may cause destructive results.
Most dialogs have buttons that can almost be considered standard (e.g.OK andCancel buttons). It is sometimes convenient to create these buttons in a standard way.
There are a couple ways of usingQDialogButtonBox. One ways is to create the buttons (or button texts) yourself and add them to the button box, specifying their role.
findButton=newQPushButton(tr("&Find")); findButton->setDefault(true); moreButton=newQPushButton(tr("&More")); moreButton->setCheckable(true); moreButton->setAutoDefault(false); buttonBox=newQDialogButtonBox(Qt::Vertical); buttonBox->addButton(findButton,QDialogButtonBox::ActionRole); buttonBox->addButton(moreButton,QDialogButtonBox::ActionRole);
Alternatively,QDialogButtonBox provides several standard buttons (e.g. OK, Cancel, Save) that you can use. They exist as flags so you can OR them together in the constructor.
buttonBox=newQDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); connect(buttonBox, SIGNAL(accepted()),this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()),this, SLOT(reject()));
You can mix and match normal buttons and standard buttons.
Currently the buttons are laid out in the following way if the button box is horizontal:
![]() | Button box laid out in horizontalGnomeLayout |
![]() | Button box laid out in horizontalKdeLayout |
![]() | Button box laid out in horizontalMacLayout |
![]() | Button box laid out in horizontalWinLayout |
The buttons are laid out the following way if the button box is vertical:
Additionally, button boxes that contain only buttons withActionRole orHelpRole can be considered modeless and have an alternate look on Mac OS X:
| modeless horizontalMacLayout | ![]() |
When a button is clicked in the button box, theclicked() signal is emitted for the actual button is that is pressed. For convenience, if the button has anAcceptRole,RejectRole, orHelpRole, theaccepted(),rejected(), orhelpRequested() signals are emitted respectively.
If you want a specific button to be default you need to callQPushButton::setDefault() on it yourself. However, if there is no default button set and to preserve which button is the default button across platforms when using theQPushButton::autoDefault property, the first push button with the accept role is made the default button when theQDialogButtonBox is shown,
See alsoQMessageBox,QPushButton, andQDialog.
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 Mac OS X. |
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 thecurrent style. However, on the X11 platform, it may be influenced by the desktop environment.
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 alsoStandardButton.
These enums describe flags for standard buttons. Each button has a definedButtonRole.
| Constant | Value | Description |
|---|---|---|
QDialogButtonBox::Ok | 0x00000400 | An "OK" button defined with theAcceptRole. |
QDialogButtonBox::Open | 0x00002000 | A "Open" button defined with theAcceptRole. |
QDialogButtonBox::Save | 0x00000800 | A "Save" button defined with theAcceptRole. |
QDialogButtonBox::Cancel | 0x00400000 | A "Cancel" button defined with theRejectRole. |
QDialogButtonBox::Close | 0x00200000 | A "Close" button defined with theRejectRole. |
QDialogButtonBox::Discard | 0x00800000 | A "Discard" or "Don't Save" button, depending on the platform, defined with theDestructiveRole. |
QDialogButtonBox::Apply | 0x02000000 | An "Apply" button defined with theApplyRole. |
QDialogButtonBox::Reset | 0x04000000 | A "Reset" button defined with theResetRole. |
QDialogButtonBox::RestoreDefaults | 0x08000000 | A "Restore Defaults" button defined with theResetRole. |
QDialogButtonBox::Help | 0x01000000 | A "Help" button defined with theHelpRole. |
QDialogButtonBox::SaveAll | 0x00001000 | A "Save All" button defined with theAcceptRole. |
QDialogButtonBox::Yes | 0x00004000 | A "Yes" button defined with theYesRole. |
QDialogButtonBox::YesToAll | 0x00008000 | A "Yes to All" button defined with theYesRole. |
QDialogButtonBox::No | 0x00010000 | A "No" button defined with theNoRole. |
QDialogButtonBox::NoToAll | 0x00020000 | A "No to All" button defined with theNoRole. |
QDialogButtonBox::Abort | 0x00040000 | An "Abort" button defined with theRejectRole. |
QDialogButtonBox::Retry | 0x00080000 | A "Retry" button defined with theAcceptRole. |
QDialogButtonBox::Ignore | 0x00100000 | An "Ignore" button defined with theAcceptRole. |
QDialogButtonBox::NoButton | 0x00000000 | An invalid button. |
The StandardButtons type is a typedef forQFlags<StandardButton>. It stores an OR combination of StandardButton values.
See alsoButtonRole andstandardButtons.
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 alsoQMessageBox.
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 areQt::Horizontal andQt::Vertical.
Access functions:
| Qt::Orientation | orientation() const |
| void | setOrientation(Qt::Orientation orientation) |
This property holds 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 alsoaddButton().
Constructs an empty, horizontal button box with the givenparent.
See alsoorientation andaddButton().
Constructs an empty button box with the givenorientation andparent.
See alsoorientation andaddButton().
Constructs a button box with the givenorientation andparent, containing the standard buttons specified bybuttons.
See alsoorientation andaddButton().
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 theAcceptRole orYesRole.
See alsorejected(),clicked(), andhelpRequested().
Adds the givenbutton to the button box with the specifiedrole. 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 alsoremoveButton() andclear().
Creates a push button with the giventext, adds it to the button box for the specifiedrole, and returns the corresponding push button. Ifrole is invalid, no button is created, and zero is returned.
See alsoremoveButton() andclear().
Adds a standardbutton to the button box if it is valid to do so, and returns a push button. Ifbutton is invalid, it is not added to the button box, and zero is returned.
See alsoremoveButton() andclear().
Returns theQPushButton corresponding to the standard buttonwhich, or 0 if the standard button doesn't exist in this button box.
See alsostandardButton(),standardButtons(), andbuttons().
Returns the button role for the specifiedbutton. This function returnsInvalidRole ifbutton is 0 or has not been added to the button box.
See alsobuttons() andaddButton().
Returns a list of all the buttons that have been added to the button box.
See alsobuttonRole(),addButton(), andremoveButton().
[virtual protected]void QDialogButtonBox::changeEvent(QEvent * event)Reimplemented fromQWidget::changeEvent().
Clears the button box, deleting all buttons within it.
See alsoremoveButton() andaddButton().
[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 bybutton.
See alsoaccepted(),rejected(), andhelpRequested().
[virtual protected]bool QDialogButtonBox::event(QEvent * event)Reimplemented fromQObject::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 theHelpRole.
See alsoaccepted(),rejected(), andclicked().
[signal]void QDialogButtonBox::rejected()This signal is emitted when a button inside the button box is clicked, as long as it was defined with theRejectRole orNoRole.
See alsoaccepted(),helpRequested(), andclicked().
Removesbutton from the button box without deleting it and sets its parent to zero.
See alsoclear(),buttons(), andaddButton().
Returns the standard button enum value corresponding to the givenbutton, orNoButton if the givenbutton isn't a standard button.
See alsobutton(),buttons(), andstandardButtons().
© 2016 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 theGNU 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.