このページでは

QDialogButtonBox Class

QDialogButtonBox クラスは、現在のウィジェットスタイルに適したレイアウトでボタンを表示するウィジェットです。詳細...

ヘッダー: #include <QDialogButtonBox>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
継承元: QWidget

パブリック型

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

静的パブリックメンバー

(since 6.12) QKeySequence standardButtonShortcut(QDialogButtonBox::StandardButton button)
(since 6.12) QString standardButtonText(QDialogButtonBox::StandardButton button)

再実装された保護関数

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

詳細な説明

ダイアログやメッセージボックスでは通常、そのプラットフォームのインターフェースガイドラインに準拠したレイアウトでボタンが表示されます。当然ながら、プラットフォームごとにダイアログのレイアウトは異なります。QDialogButtonBox を使用すると、開発者はボタンを追加するだけで、ユーザーのデスクトップ環境に適したレイアウトが自動的に適用されます。

ダイアログのボタンのほとんどは、特定の役割を担っています。そのような役割には、次のようなものがあります。

  • ダイアログの承認または拒否。
  • ヘルプを要求する。
  • ダイアログ自体に対してアクションを実行する(フィールドのリセットや変更の適用など)。

また、ダイアログを閉じる別の方法があり、それによって破壊的な結果が生じる可能性もあります。

ほとんどのダイアログには、ほぼ標準的と言えるボタン(例:OK ボタンやCancel ボタン)があります。これらのボタンを標準的な方法で作成すると便利な場合があります。

QDialogButtonBox を使用するにはいくつかの方法があります。1 つの方法は、ボタン(またはボタンのテキスト)を自分で作成し、その役割を指定してボタンボックスに追加することです。

QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);

あるいは、QDialogButtonBox には、使用可能ないくつかの標準ボタン(例:OK、Cancel、Save)が用意されています。これらはフラグとして存在するため、コンストラクタ内で OR 演算を組み合わせて指定することができます。

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);

    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

通常のボタンと標準のボタンを組み合わせて使用することも可能です。

現在、ボタンボックスが水平配置の場合、ボタンは次のように配置されます:

GnomeLayoutの水平レイアウトを使用した複数のボタン GnomeLayout 水平配置水平に配置されたボタンボックスGnomeLayout
KdeLayout の水平レイアウトを使用したいくつかのボタン KdeLayout 横向きボタンボックスが横向きに配置されている場合KdeLayout
MacLayout の水平レイアウトを採用したいくつかのボタン MacLayout 横向きボタンボックスが水平に配置されているMacLayout
WinLayout の水平レイアウトを使用したいくつかのボタン WinLayout 横ボタンボックスが水平に配置されているWinLayout

ボタンボックスが縦配置の場合、ボタンは次のように配置されます:

さらに、ActionRole またはHelpRole が設定されたボタンのみを含むボタンボックスは、モデルレスとみなされ、macOSでは異なる外観になります:

モーデルレス(水平)MacLayoutmacOSで水平型のモーダルなしスタイルを採用した複数のボタン モードレス水平のスクリーンショットMacLayout
モーデルスな縦型MacLayoutmacOS で、水平配置のモーダルなしスタイルを採用した複数のボタン モーデレス垂直のスクリーンショットMacLayout

ボタンボックス内のボタンがクリックされると、実際に押されたボタンに対してclicked()シグナルが発信されます。便宜上、ボタンにAcceptRole 、RejectRole 、またはHelpRole が設定されている場合、それぞれaccepted()、rejected()、またはhelpRequested()シグナルが発信されます。

特定のボタンをデフォルトに設定したい場合は、そのボタンに対してQPushButton::setDefault()を自分で呼び出す必要があります。ただし、デフォルトボタンが設定されておらず、QPushButton::autoDefault プロパティを使用する際にプラットフォーム間でデフォルトボタンが維持されるようにするには、QDialogButtonBoxが表示された際に、acceptロールを持つ最初のプッシュボタンがデフォルトボタンとして設定されます。

QMessageBox 、QPushButton 、およびQDialogも参照してください 。

メンバ型のドキュメント

enum QDialogButtonBox::ButtonLayout

この列挙型は、ボタンボックスに含まれるボタンを配置する際に使用するレイアウトポリシーを表します。

定数値説明
QDialogButtonBox::WinLayout0Windows 上のアプリケーションに適したポリシーを使用します。
QDialogButtonBox::MacLayout1macOS 上のアプリケーションに適したポリシーを使用します。
QDialogButtonBox::KdeLayout2KDE 上のアプリケーションに適したポリシーを使用します。
QDialogButtonBox::GnomeLayout3GNOME 上のアプリケーションに適したポリシーを使用します。
QDialogButtonBox::AndroidLayout4Android上のアプリケーションに適したポリシーを使用します。この列挙型値はQt 5.10で追加されました。

ボタンのレイアウトは、current style によって指定されます。ただし、X11プラットフォームでは、デスクトップ環境の影響を受ける場合があります。

enum QDialogButtonBox::ButtonRole

この列挙型は、ボタンボックス内のボタンを記述するために使用できる役割を表します。これらの役割の組み合わせは、ボタンの動作のさまざまな側面を記述するためのフラグとして機能します。

定数値説明
QDialogButtonBox::InvalidRole-1ボタンが無効です。
QDialogButtonBox::AcceptRole0このボタンをクリックすると、ダイアログが承認されます(例:OK)。
QDialogButtonBox::RejectRole1このボタンをクリックすると、ダイアログが拒否されます(例:[キャンセル])。
QDialogButtonBox::DestructiveRole2このボタンをクリックすると、破壊的な変更(例:変更の破棄)が行われ、ダイアログが閉じられます。
QDialogButtonBox::ActionRole3このボタンをクリックすると、ダイアログ内の要素に変更が加わります。
QDialogButtonBox::HelpRole4このボタンをクリックすると、ヘルプを要求できます。
QDialogButtonBox::YesRole5このボタンは「はい」のようなボタンです。
QDialogButtonBox::NoRole6このボタンは「いいえ」のようなボタンです。
QDialogButtonBox::ApplyRole8このボタンをクリックすると、現在の変更が適用されます。
QDialogButtonBox::ResetRole7このボタンをクリックすると、ダイアログのフィールドがデフォルト値にリセットされます。

StandardButtonも参照してください 。

enum QDialogButtonBox::StandardButton
flags QDialogButtonBox::StandardButtons

これらの列挙型は、標準ボタンのフラグを表しています。各ボタンには、ButtonRole が定義されています。

定数値説明
QDialogButtonBox::Ok0x00000400AcceptRole で定義された「OK」ボタン。
QDialogButtonBox::Open0x00002000AcceptRole で定義された「開く」ボタン。
QDialogButtonBox::Save0x00000800AcceptRole で定義された「保存」ボタン。
QDialogButtonBox::Cancel0x00400000RejectRole で定義された「Cancel」ボタン。
QDialogButtonBox::Close0x00200000RejectRole で定義された「閉じる」ボタン。
QDialogButtonBox::Discard0x00800000プラットフォームに応じて、「破棄」または「保存しない」ボタン。DestructiveRole で定義されます。
QDialogButtonBox::Apply0x02000000ApplyRole で定義された「適用」ボタン。
QDialogButtonBox::Reset0x04000000ResetRole で定義された「リセット」ボタン。
QDialogButtonBox::RestoreDefaults0x08000000ResetRole で定義された「デフォルトに戻す」ボタン。
QDialogButtonBox::Help0x01000000HelpRole で定義された「ヘルプ」ボタン。
QDialogButtonBox::SaveAll0x00001000AcceptRole で定義された「すべて保存」ボタン。
QDialogButtonBox::Yes0x00004000YesRole で定義された「はい」ボタン。
QDialogButtonBox::YesToAll0x00008000YesRole で定義された「すべてに『はい』」ボタン。
QDialogButtonBox::No0x00010000NoRole で定義された「No」ボタン。
QDialogButtonBox::NoToAll0x00020000NoRole で定義された「すべてに『いいえ』」ボタン。
QDialogButtonBox::Abort0x00040000RejectRole で定義された「中止」ボタン。
QDialogButtonBox::Retry0x00080000AcceptRole で定義された「再試行」ボタン。
QDialogButtonBox::Ignore0x00100000AcceptRole で定義された「無視」ボタン。
QDialogButtonBox::NoButton0x00000000無効なボタン。

StandardButtons 型は、QFlags<StandardButton> の typedef です。これは、StandardButton 値の OR 組み合わせを格納します。

ButtonRole およびstandardButtonsも参照してください 。

プロパティのドキュメント

centerButtons : bool

このプロパティは、ボタンボックス内のボタンが中央に配置されるかどうかを指定します

デフォルトでは、このプロパティはfalse に設定されています。この動作は、ほとんどの種類のダイアログに適しています。注目すべき例外として、ほとんどのプラットフォーム(例:Windows)のメッセージボックスが挙げられます。そこでは、ボタンボックスが水平方向に中央揃えになります。

Accessの関数:

bool centerButtons() const
void setCenterButtons(bool center)

「QMessageBox」も参照してください 。

orientation : Qt::Orientation

このプロパティは、ボタンボックスの向きを指定します

デフォルトでは、配置方向は水平(つまり、ボタンが横一列に並んでいる状態)です。指定可能な配置方向は、Qt::Horizontal およびQt::Vertical です。

アクセス関数:

Qt::Orientation orientation() const
void setOrientation(Qt::Orientation orientation)

standardButtons : StandardButtons

ボタンボックス内の標準ボタンのセット

このプロパティは、ボタンボックスで使用される標準ボタンを制御します。

アクセス関数:

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

関連項目: addButton()。

メンバ関数のドキュメント

QDialogButtonBox::QDialogButtonBox(QWidget *parent = nullptr)

指定されたparent を使用して、空の水平ボタンボックスを作成します。

orientation およびaddButton()も参照してください 。

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

指定されたparent を使用して、buttons で指定された標準ボタンを含む水平方向のボタンボックスを作成します。

orientation およびaddButton()も参照してください 。

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

指定されたorientation およびparent を使用して、空のボタンボックスを作成します。

orientation およびaddButton()も参照してください 。

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

指定されたorientation およびparent を用いて、buttons で指定された標準ボタンを含むボタンボックスを作成します。

orientation およびaddButton()も参照してください 。

[virtual noexcept] QDialogButtonBox::~QDialogButtonBox()

ボタンボックスを破壊します。

[signal] void QDialogButtonBox::accepted()

この信号は、ボタンボックス内のボタンがクリックされた際に発生します。ただし、そのボタンが `AcceptRole ` または `YesRole` で定義されている場合に限ります。

rejected()、clicked()、およびhelpRequested()も参照してください 。

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

有効な場合、標準のbutton をボタンボックスに追加し、プッシュボタンを返します。button が無効な場合は、ボタンボックスには追加されず、0が返されます。

removeButton() およびclear()も参照してください 。

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

指定されたbutton を、指定されたrole を持つボタンボックスに追加します。ロールが無効な場合、ボタンは追加されません。

ボタンがすでに追加されている場合は、そのボタンが削除され、新しいロールで再度追加されます。

注: ボタンボックスが ボタンの所有権を取得します。

removeButton() およびclear()も参照してください 。

QPushButton *QDialogButtonBox::addButton(const QString &text, QDialogButtonBox::ButtonRole role)

指定されたtext を持つプッシュボタンを作成し、指定されたrole のボタンボックスに追加して、対応するプッシュボタンを返します。role が無効な場合、ボタンは作成されず、0が返されます。

removeButton() およびclear()も参照してください 。

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

標準ボタンwhich に対応する `QPushButton ` を返します。このボタンボックスに標準ボタンが存在しない場合は、`nullptr ` を返します。

standardButton()、standardButtons()、およびbuttons()も参照してください 。

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

指定されたbutton のボタンロールを返します。button がnullptr である場合、またはボタンボックスに追加されていない場合、この関数はInvalidRole を返します。

buttons() およびaddButton()も参照してください 。

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

ボタンボックスに追加されたすべてのボタンのリストを返します。

buttonRole()、addButton()、およびremoveButton()も参照してください 。

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

QWidget::changeEvent(QEvent *event) を再実装しています。

void QDialogButtonBox::clear()

ボタンボックスをクリアし、その中のすべてのボタンを削除します。

removeButton() およびaddButton()も参照してください 。

[signal] void QDialogButtonBox::clicked(QAbstractButton *button)

このシグナルは、ボタンボックス内のボタンがクリックされたときに発火します。押された具体的なボタンは、button で指定されます。

accepted()、rejected()、およびhelpRequested()も参照してください 。

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

QWidget::event(QEvent *event) を再実装します。

[signal] void QDialogButtonBox::helpRequested()

このシグナルは、ボタンボックス内のボタンがクリックされた際に、そのボタンがHelpRole で定義されている場合に限り発火します。

accepted()、rejected()、およびclicked()も参照してください 。

[signal] void QDialogButtonBox::rejected()

この信号は、ボタンボックス内のボタンがクリックされた際に、そのボタンが `RejectRole ` または `NoRole` で定義されている場合に発火します。

accepted()、helpRequested()、およびclicked()も参照してください 。

void QDialogButtonBox::removeButton(QAbstractButton *button)

button を削除せずにボタンボックスから取り除き、その親を null に設定します。

clear()、buttons()、およびaddButton()も参照してください 。

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

指定されたbutton に対応する標準ボタンの列挙値を返します。指定されたbutton が標準ボタンでない場合は、NoButton を返します。

button()、buttons()、およびstandardButtons()も参照してください 。

[static, since 6.12] QKeySequence QDialogButtonBox::standardButtonShortcut(QDialogButtonBox::StandardButton button)

標準のbutton のキーボードショートカットを返します。ショートカットはプラットフォームに依存します。button に関連付けられたショートカットがない場合、または認識された標準ボタンでない場合は、空のQKeySequence を返します。

この関数は Qt 6.12 で導入されました。

standardButtonText() およびStandardButtonも参照してください 。

[static, since 6.12] QString QDialogButtonBox::standardButtonText(QDialogButtonBox::StandardButton button)

標準のbutton の表示テキスト(ボタンのラベルに表示されるもの)を返します。このテキストは、プラットフォームおよびロケールによって異なります。

button が認識可能な標準ボタンでない場合、空の文字列を返します。

この関数はQt 6.12で導入されました。

standardButtonShortcut() およびStandardButtonも参照してください 。

© 2026 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 the GNU 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.