QDialogButtonBox 類

QDialogButtonBox 類是在適閤當前小部件風格的布局中呈現按鈕的 Widget。 更多...

頭: #include <QDialogButtonBox>
qmake: QT += widgets
Since: Qt 4.2
繼承: QWidget

該類在 Qt 4.2 引入。

公共類型

enum ButtonLayout { WinLayout, MacLayout, KdeLayout, GnomeLayout, AndroidLayout }
enum ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ActionRole, …, ResetRole }
enum StandardButton { Ok, Open, Save, Cancel, Close, …, NoButton }
flags StandardButtons

特性

公共函數

QDialogButtonBox (QDialogButtonBox::StandardButtons buttons , Qt::Orientation orientation , QWidget * parent = nullptr)
QDialogButtonBox (QDialogButtonBox::StandardButtons buttons , QWidget * parent = nullptr)
QDialogButtonBox (Qt::Orientation orientation , QWidget * parent = nullptr)
QDialogButtonBox (QWidget * parent = nullptr)
virtual ~QDialogButtonBox ()
void addButton (QAbstractButton * button , QDialogButtonBox::ButtonRole role )
QPushButton * addButton (const QString & text , QDialogButtonBox::ButtonRole role )
QPushButton * addButton (QDialogButtonBox::StandardButton button )
QPushButton * button (QDialogButtonBox::StandardButton which ) const
QDialogButtonBox::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 (QDialogButtonBox::StandardButtons buttons )
QDialogButtonBox::StandardButton standardButton (QAbstractButton * button ) const
QDialogButtonBox::StandardButtons standardButtons () const

信號

void accepted ()
void clicked (QAbstractButton * button )
void helpRequested ()
void rejected ()

重實現保護函數

virtual void changeEvent (QEvent * event ) override
virtual bool event (QEvent * event ) override

詳細描述

對話框和消息框通常在符閤該平颱界麵指導方針的布局中,呈現按鈕。始終不變,不同平颱的對話框擁有不同布局。QDialogButtonBox 允許開發者嚮其添加按鈕,並將自動為用戶桌麵環境使用適當布局。

大多數對話框按鈕遵循某些角色。這些角色包括:

  • 接受 (或拒絕) 對話框。
  • 尋求幫助。
  • 在對話框自身上履行動作 (譬如:重置字段或應用更改)。

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 and Cancel buttons). It is sometimes convenient to create these buttons in a standard way.

There are a couple ways of using QDialogButtonBox. One ways is to create the buttons (or button texts) yourself and add them to the button box, specifying their role.

    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    moreButton = new QPushButton(tr("&More"));
    moreButton->setCheckable(true);
    moreButton->setAutoDefault(false);
					

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 = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::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:

GnomeLayout Horizontal Button box laid out in horizontal GnomeLayout
KdeLayout Horizontal Button box laid out in horizontal KdeLayout
MacLayout Horizontal Button box laid out in horizontal MacLayout
WinLayout Horizontal Button box laid out in horizontal WinLayout

The buttons are laid out the following way if the button box is vertical:

GnomeLayout KdeLayout MacLayout WinLayout
GnomeLayout Vertical KdeLayout Vertical MacLayout Vertical WinLayout Vertical

Additionally, button boxes that contain only buttons with ActionRole or HelpRole can be considered modeless and have an alternate look on macOS:

非模態水平 MacLayout Screenshot of modeless horizontal MacLayout
非模態垂直 MacLayout Screenshot of modeless vertical MacLayout

When a button is clicked in the button box, the clicked () signal is emitted for the actual button is that is pressed. For convenience, if the button has an AcceptRole , RejectRole ,或 HelpRole accepted (), rejected (),或 helpRequested () signals are emitted respectively.

If you want a specific button to be default you need to call QPushButton::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 the QPushButton::autoDefault property, the first push button with the accept role is made the default button when the QDialogButtonBox is shown,

另請參閱 QMessageBox , QPushButton ,和 QDialog .

成員類型文檔編製

enum QDialogButtonBox:: ButtonLayout

This enum describes the layout policy to be used when arranging the buttons contained in the button box.

常量 描述
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.
QDialogButtonBox::AndroidLayout GnomeLayout + 2 Use a policy appropriate for applications on Android. This enum value was added in Qt 5.10.

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.

常量 描述
QDialogButtonBox::InvalidRole -1 按鈕無效。
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 點擊按鈕導緻對話框中元素改變。
QDialogButtonBox::HelpRole 4 可以點擊按鈕以請求幫助。
QDialogButtonBox::YesRole 5 像 Yes 按鈕的按鈕。
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.

另請參閱 StandardButton .

enum QDialogButtonBox:: StandardButton
flags QDialogButtonBox:: StandardButtons

These enums describe flags for standard buttons. Each button has a defined ButtonRole .

常量 描述
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 無效按鈕。

StandardButtons 類型是 typedef 對於 QFlags <StandardButton>. It stores an OR combination of StandardButton values.

另請參閱 ButtonRole and standardButtons .

特性文檔編製

centerButtons : bool

This property holds whether the buttons in the button box are centered

默認情況下,此特性為 false . This behavior is appropriate for most types of dialogs. A notable exception is message boxes on most platforms (e.g. Windows), where the button box is centered horizontally.

訪問函數:

bool centerButtons () const
void setCenterButtons (bool center )

另請參閱 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 .

訪問函數:

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.

訪問函數:

QDialogButtonBox::StandardButtons standardButtons () const
void setStandardButtons (QDialogButtonBox::StandardButtons buttons )

另請參閱 addButton ().

成員函數文檔編製

QDialogButtonBox:: QDialogButtonBox ( QDialogButtonBox::StandardButtons buttons , Qt::Orientation orientation , QWidget * parent = nullptr)

構造按鈕框采用給定 orientation and parent ,包含的標準按鈕指定通過 buttons .

另請參閱 orientation and addButton ().

QDialogButtonBox:: QDialogButtonBox ( QDialogButtonBox::StandardButtons buttons , QWidget * parent = nullptr)

構造水平按鈕框采用給定 parent ,包含的標準按鈕指定通過 buttons .

該函數在 Qt 5.2 引入。

另請參閱 orientation and addButton ().

QDialogButtonBox:: QDialogButtonBox ( Qt::Orientation orientation , QWidget * parent = nullptr)

構造空按鈕框采用給定 orientation and parent .

另請參閱 orientation and addButton ().

QDialogButtonBox:: QDialogButtonBox ( QWidget * parent = nullptr)

構造空的水平按鈕框采用給定 parent .

另請參閱 orientation and addButton ().

[signal] void QDialogButtonBox:: accepted ()

此信號被發射當點擊按鈕框內按鈕時,隻要它的定義是采用 AcceptRole or YesRole .

另請參閱 rejected (), clicked (),和 helpRequested ().

[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 .

另請參閱 accepted (), rejected (),和 helpRequested ().

[signal] void QDialogButtonBox:: helpRequested ()

此信號被發射當點擊按鈕框內按鈕時,隻要它的定義是采用 HelpRole .

另請參閱 accepted (), rejected (),和 clicked ().

[signal] void QDialogButtonBox:: rejected ()

此信號被發射當點擊按鈕框內按鈕時,隻要它的定義是采用 RejectRole or NoRole .

另請參閱 accepted (), helpRequested (),和 clicked ().

[虛擬] QDialogButtonBox:: ~QDialogButtonBox ()

銷毀按鈕框。

void QDialogButtonBox:: addButton ( QAbstractButton * button , QDialogButtonBox::ButtonRole role )

添加給定 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.

注意: The button box takes ownership of the button.

另請參閱 removeButton () 和 clear ().

QPushButton *QDialogButtonBox:: addButton (const QString & text , QDialogButtonBox::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.

另請參閱 removeButton () 和 clear ().

QPushButton *QDialogButtonBox:: addButton ( QDialogButtonBox::StandardButton button )

添加標準 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.

另請參閱 removeButton () 和 clear ().

QPushButton *QDialogButtonBox:: button ( QDialogButtonBox::StandardButton which ) const

返迴 QPushButton 對應標準按鈕 which ,或 nullptr 若此按鈕框中不存在標準按鈕。

另請參閱 standardButton (), standardButtons (),和 buttons ().

QDialogButtonBox::ButtonRole QDialogButtonBox:: buttonRole ( QAbstractButton * button ) const

Returns the button role for the specified button 。此函數返迴 InvalidRole if button is nullptr or has not been added to the button box.

另請參閱 buttons () 和 addButton ().

QList < QAbstractButton *> QDialogButtonBox:: buttons () const

Returns a list of all the buttons that have been added to the button box.

另請參閱 buttonRole (), addButton (),和 removeButton ().

[override virtual protected] void QDialogButtonBox:: changeEvent ( QEvent * event )

重實現: QWidget::changeEvent (QEvent *event).

void QDialogButtonBox:: clear ()

Clears the button box, deleting all buttons within it.

另請參閱 removeButton () 和 addButton ().

[override virtual protected] bool QDialogButtonBox:: event ( QEvent * event )

重實現: QWidget::event (QEvent *event).

void QDialogButtonBox:: removeButton ( QAbstractButton * button )

移除 button from the button box without deleting it and sets its parent to zero.

另請參閱 clear (), buttons (),和 addButton ().

QDialogButtonBox::StandardButton QDialogButtonBox:: standardButton ( QAbstractButton * button ) const

返迴的標準按鈕枚舉值對應給定 button ,或 NoButton 若給定 button 不是標準按鈕。

另請參閱 button (), buttons (),和 standardButtons ().