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 |
プロパティ
- centerButtons : bool
- orientation : Qt::Orientation
- standardButtons : 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() |
再実装された保護された関数
virtual void | changeEvent(QEvent *event) override |
virtual bool | event(QEvent *event) override |
詳細説明
ダイアログやメッセージボックスは通常、そのプラットフォームのインターフェースガイドラインに従ったレイアウトでボタンを表示します。必ず、異なるプラットフォームでは、ダイアログのレイアウトが異なります。QDialogButtonBoxでは、開発者がボタンを追加することができ、ユーザーのデスクトップ環境に適したレイアウトを自動的に使用します。
ダイアログのほとんどのボタンは、特定の役割に従います。そのような役割には次のようなものがあります:
- ダイアログを受け入れるか拒否するか。
- 助けを求める。
- ダイアログ自体に対するアクションの実行(フィールドのリセットや変更の適用など)。
破壊的な結果を引き起こす可能性のある、ダイアログを却下する別の方法もありえます。
ほとんどのダイアログには、ほとんど標準的といえるボタンがあります(例えば、OK やCancel ボタン)。これらのボタンを標準的な方法で作成すると便利なことがあります。
QDialogButtonBoxを使用するにはいくつかの方法があります。一つは、ボタン(またはボタンテキスト)を自分で作成し、ボタンボックスに追加する方法です。
QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical); buttonBox->addButton(findButton, QDialogButtonBox::ActionRole); buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
あるいは、QDialogButtonBoxは、いくつかの標準的なボタン(例えば、OK、Cancel、Save)を提供しています。これらはフラグとして存在するので、コンストラクタでそれらを OR OR することができます。
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
通常のボタンと標準のボタンを混ぜて使うこともできます。
現在のところ、ボタンボックスが横長の場合、ボタンは以下のようにレイアウトされます:
![]() | 水平にレイアウトされたボタンボックスGnomeLayout |
![]() | 横並びのボタンボックスKdeLayout |
![]() | 横長のボタンボックスMacLayout |
![]() | 水平にレイアウトされたボタンボックスWinLayout |
ボタンボックスが縦長の場合、ボタンは以下のようにレイアウトされます:
さらに、ActionRole またはHelpRole のボタンだけを含むボタンボックスは、モデルレスとみなされ、macOSでは別の外観になります:
ボタンボックスでボタンがクリックされると、実際に押されたボタンに対してclicked() シグナルが発せられる。便宜上、ボタンがAcceptRole 、RejectRole 、HelpRole の場合、それぞれaccepted ()、rejected ()、helpRequested ()シグナルが発せられます。
特定のボタンをデフォルトにしたい場合は、自分でQPushButton::setDefault() を呼び出す必要がある。しかし、デフォルト・ボタンが設定されておらず、QPushButton::autoDefault プロパティを使用するときに、どのボタンがデフォルト・ボタンであるかをプラットフォーム間で保持するために、QDialogButtonBox が表示されているときに、accept ロールを持つ最初のプッシュ・ボタンがデフォルト・ボタンになります、
QMessageBox 、QPushButton 、QDialogも参照してください 。
メンバ型ドキュメント
enum QDialogButtonBox::ButtonLayout
この列挙型は、ボタンボックスに含まれるボタンを配置するときに使用されるレイアウトポリシーを記述します。
定数 | 値 | 説明 |
---|---|---|
QDialogButtonBox::WinLayout | 0 | Windows 上のアプリケーションに適したポリシーを使用します。 |
QDialogButtonBox::MacLayout | 1 | macOS 上のアプリケーションに適したポリシーを使用する。 |
QDialogButtonBox::KdeLayout | 2 | KDE上のアプリケーションに適切なポリシーを使用する。 |
QDialogButtonBox::GnomeLayout | 3 | GNOME上のアプリケーションに適切なポリシーを使用する。 |
QDialogButtonBox::AndroidLayout | 4 | Android 上のアプリケーションに適したポリシーを使用する。この列挙値は Qt 5.10 で追加されました。 |
ボタンのレイアウトは、current style で指定されます。ただし、X11 プラットフォームでは、デスクトップ環境の影響を受ける可能性があります。
enum QDialogButtonBox::ButtonRole
この列挙型は、ボタンボックスのボタンを記述するために使用できる役割を記述する。これらのロールの組み合わせは、その動作の異なる側面を記述するために使用されるフラグとして使用されます。
定数 | 値 | 説明 |
---|---|---|
QDialogButtonBox::InvalidRole | -1 | ボタンは無効です。 |
QDialogButtonBox::AcceptRole | 0 | ボタンをクリックすると、ダイアログが受理されます(例えば、OK)。 |
QDialogButtonBox::RejectRole | 1 | ボタンをクリックすると、ダイアログは拒否されます(例:キャンセル)。 |
QDialogButtonBox::DestructiveRole | 2 | ボタンをクリックすると、破壊的変更(変更の破棄など)が行われ、ダイアログが閉じます。 |
QDialogButtonBox::ActionRole | 3 | ボタンをクリックすると、ダイアログ内の要素が変更されます。 |
QDialogButtonBox::HelpRole | 4 | ボタンをクリックすると、ヘルプを要求することができます。 |
QDialogButtonBox::YesRole | 5 | ボタンは "Yes "のようなボタンです。 |
QDialogButtonBox::NoRole | 6 | ボタンは「いいえ」のようなボタンです。 |
QDialogButtonBox::ApplyRole | 8 | ボタンは現在の変更を適用します。 |
QDialogButtonBox::ResetRole | 7 | ボタンはダイアログのフィールドをデフォルト値にリセットします。 |
StandardButtonも参照してください 。
enum QDialogButtonBox::StandardButton
flags QDialogButtonBox::StandardButtons
これらの列挙型は、標準ボタンのフラグを記述します。各ボタンはButtonRole で定義されています。
定数 | 値 | 説明 |
---|---|---|
QDialogButtonBox::Ok | 0x00000400 | OK" ボタンは、AcceptRole で定義されています。 |
QDialogButtonBox::Open | 0x00002000 | AcceptRole で定義された「開く」ボタン。 |
QDialogButtonBox::Save | 0x00000800 | AcceptRole で定義された "保存 "ボタン。 |
QDialogButtonBox::Cancel | 0x00400000 | RejectRole で定義された「キャンセル」ボタン。 |
QDialogButtonBox::Close | 0x00200000 | RejectRole で定義された「閉じる」ボタン。 |
QDialogButtonBox::Discard | 0x00800000 | プラットフォームによって異なるが、DestructiveRole で定義された「破棄」または「保存しない」ボタン。 |
QDialogButtonBox::Apply | 0x02000000 | ApplyRole で定義された「適用」ボタン。 |
QDialogButtonBox::Reset | 0x04000000 | ResetRole で定義された「リセット」ボタン。 |
QDialogButtonBox::RestoreDefaults | 0x08000000 | ResetRole で定義されている「デフォルトに戻す」ボタン。 |
QDialogButtonBox::Help | 0x01000000 | HelpRole で定義された「ヘルプ」ボタン。 |
QDialogButtonBox::SaveAll | 0x00001000 | AcceptRole で定義された「すべて保存」ボタン。 |
QDialogButtonBox::Yes | 0x00004000 | YesRole で定義された「はい」ボタン。 |
QDialogButtonBox::YesToAll | 0x00008000 | YesRole で定義された「Yes to All」ボタン。 |
QDialogButtonBox::No | 0x00010000 | NoRole で定義された「いいえ」ボタン。 |
QDialogButtonBox::NoToAll | 0x00020000 | NoRole で定義された「No to All」ボタン。 |
QDialogButtonBox::Abort | 0x00040000 | RejectRole で定義された「中止」ボタン。 |
QDialogButtonBox::Retry | 0x00080000 | AcceptRole で定義される「再試行」ボタン。 |
QDialogButtonBox::Ignore | 0x00100000 | AcceptRole で定義された「無視」ボタン。 |
QDialogButtonBox::NoButton | 0x00000000 | 無効なボタン。 |
StandardButtons型はQFlags<StandardButton>のtypedefである。StandardButtonの値のORの組み合わせが格納されます。
ButtonRole およびstandardButtonsも参照してください 。
プロパティ・ドキュメンテーション
centerButtons : bool
このプロパティは、ボタンボックスのボタンが中央に配置されるかどうかを保持します。
デフォルトでは、このプロパティはfalse
です。この動作はほとんどのダイアログに適しています。特筆すべき例外は、ほとんどのプラットフォーム(例えばWindows)のメッセージボックスで、そこではボタンボックスは水平方向にセンタリングされます。
アクセス関数:
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)
buttons で指定された標準ボタンを含む、与えられたparent の水平ボタンボックスを構築します。
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)
buttons で指定された標準ボタンを含む、与えられたorientation とparent を持つボタンボックスを構築します。
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)
指定されたrole のボタンボックスに、指定されたbutton を追加します。ロールが無効な場合、ボタンは追加されません。
ボタンが既に追加されている場合は、削除され、新しいロールで再度追加されます。
注意: ボタンボックスはボタンの所有権を持ちます。
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 を削除せずにボタン・ボックスから削除し、その親をゼロに設定する。
clear()、buttons()、addButton()も参照 。
QDialogButtonBox::StandardButton QDialogButtonBox::standardButton(QAbstractButton *button) const
与えられたbutton に対応する標準ボタン列挙値を返すか、与えられたbutton が標準ボタンでない場合はNoButton を返す。
button()、buttons()、standardButtons()も参照 。
© 2025 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.