QMessageBox Class
QMessageBox クラスは、ユーザーに情報を伝えたり、ユーザーに質問して回答を受け取ったりするためのモーダルダイアログを提供します。詳細...
ヘッダー | #include <QMessageBox> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
継承: | QDialog |
- 継承されたメンバを含むすべてのメンバのリスト
- 非推奨メンバー
- QMessageBoxはStandard Dialogsの一部です。
パブリックタイプ
enum | ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ActionRole, …, ResetRole } |
enum | Icon { NoIcon, Question, Information, Warning, Critical } |
(since 6.6) enum class | Option { DontUseNativeDialog } |
flags | Options |
enum | StandardButton { Ok, Open, Save, Cancel, Close, …, ButtonMask } |
flags | StandardButtons |
プロパティ
|
|
パブリック機能
QMessageBox(QWidget *parent = nullptr) | |
QMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint) | |
virtual | ~QMessageBox() |
void | addButton(QAbstractButton *button, QMessageBox::ButtonRole role) |
QPushButton * | addButton(QMessageBox::StandardButton button) |
QPushButton * | addButton(const QString &text, QMessageBox::ButtonRole role) |
QAbstractButton * | button(QMessageBox::StandardButton which) const |
QMessageBox::ButtonRole | buttonRole(QAbstractButton *button) const |
QList<QAbstractButton *> | buttons() const |
QCheckBox * | checkBox() const |
QAbstractButton * | clickedButton() const |
QPushButton * | defaultButton() const |
QString | detailedText() const |
QAbstractButton * | escapeButton() const |
QMessageBox::Icon | icon() const |
QPixmap | iconPixmap() const |
QString | informativeText() const |
void | open(QObject *receiver, const char *member) |
QMessageBox::Options | options() const |
void | removeButton(QAbstractButton *button) |
void | setCheckBox(QCheckBox *cb) |
void | setDefaultButton(QMessageBox::StandardButton button) |
void | setDefaultButton(QPushButton *button) |
void | setDetailedText(const QString &text) |
void | setEscapeButton(QAbstractButton *button) |
void | setEscapeButton(QMessageBox::StandardButton button) |
void | setIcon(QMessageBox::Icon) |
void | setIconPixmap(const QPixmap &pixmap) |
void | setInformativeText(const QString &text) |
(since 6.6) void | setOption(QMessageBox::Option option, bool on = true) |
void | setOptions(QMessageBox::Options options) |
void | setStandardButtons(QMessageBox::StandardButtons buttons) |
void | setText(const QString &text) |
void | setTextFormat(Qt::TextFormat format) |
void | setTextInteractionFlags(Qt::TextInteractionFlags flags) |
void | setWindowModality(Qt::WindowModality windowModality) |
void | setWindowTitle(const QString &title) |
QMessageBox::StandardButton | standardButton(QAbstractButton *button) const |
QMessageBox::StandardButtons | standardButtons() const |
(since 6.6) bool | testOption(QMessageBox::Option option) const |
QString | text() const |
Qt::TextFormat | textFormat() const |
Qt::TextInteractionFlags | textInteractionFlags() const |
パブリックスロット
virtual int | exec() override |
シグナル
void | buttonClicked(QAbstractButton *button) |
静的パブリックメンバ
void | about(QWidget *parent, const QString &title, const QString &text) |
void | aboutQt(QWidget *parent, const QString &title = QString()) |
QMessageBox::StandardButton | critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) |
QMessageBox::StandardButton | information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) |
QMessageBox::StandardButton | question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton) |
QMessageBox::StandardButton | warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) |
再実装された保護された関数
virtual void | changeEvent(QEvent *ev) override |
virtual void | closeEvent(QCloseEvent *e) override |
virtual bool | event(QEvent *e) override |
virtual void | keyPressEvent(QKeyEvent *e) override |
virtual void | resizeEvent(QResizeEvent *event) override |
virtual void | showEvent(QShowEvent *e) override |
マクロ
QT_REQUIRE_VERSION(int argc, char **argv, const char *version) |
詳細説明
メッセージボックスは、ユーザーに状況を知らせるための一次text 、状況をさらに説明するためのinformative text 、ユーザーが要求した場合にさらに多くのデータを提供するためのオプションdetailed text 。
メッセージ・ボックスには、icon と、ユーザーからの応答を受け付けるためのstandard buttons も表示できます。
QMessageBox を使用するための API には、プロパティ・ベースの API と静的関数の 2 つがあります。静的関数のいずれかを呼び出すのは、より単純なアプローチですが、プロパティベースのAPIを使用するよりも柔軟性に欠け、結果はあまり有益ではありません。プロパテ ィベースの API を使用する こ と を推奨 し ます。
プ ロ パテ ィ ベース API
プロパティ・ベースの API を使用するには、QMessageBox のインスタンスを構築し、必要なプロパティを設定し、exec() を呼び出してメッセージを表示します。最も単純な構成は、message text プロパティのみを設定することです。
QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec();
ユーザーは、OK ボタンをクリックして、メッセージ・ボックスを閉じる必要があります。メッセージ・ボックスが解除されるまで、GUIの他の部分はブロックされます。
ユーザーにイベントを警告するだけよりも良い方法は、ユーザーにそのイベントに対して何をするかを尋ねることです。
standard buttons プロパティに、ユーザーの応答として必要なボタンのセットを設定します。ボタンは、StandardButtons の値をビット OR 演算子で組み合わせて指定します。ボタンの表示順序はプラットフォームに依存します。例えば、WindowsではSave がCancel の左に表示されますが、macOSでは順序が逆になります。標準ボタンの1つをdefault button にマークしてください。
informative text プロパティは、ユーザーが適切なアクションを選択できるように、追加のコンテキストを追加するために使用できます。
QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.setInformativeText("Do you want to save your changes?"); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Save); int ret = msgBox.exec();
exec() スロットは、クリックされたボタンのStandardButtons 値を返します。
switch (ret) { case QMessageBox::Save: // Save was clicked break; case QMessageBox::Discard: // Don't Save was clicked break; case QMessageBox::Cancel: // Cancel was clicked break; default: // should never be reached break; }
ユーザが適切なアクションを選択できるように、より多くの情報を与えるには、detailed text プロパティを設定します。プラットフォームによっては、detailed text 、Show Details... ボタンをクリックする必要があります。
Show Details... ボタンをクリックすると、詳細テキストが表示されます。
リッチテキストとテキスト形式プロパティ
detailed text プロパティは常にプレーンテキストとして解釈されます。main text とinformative text プロパティは、プレーンテキストかリッチテキストのいずれかにすることができます。こ れ ら の文字列は、text format プ ロ パテ ィ の設定に従っ て解釈 さ れます。デフォルト設定はauto-text です。
XMLメタ・キャラクタを含むプレーン・テキスト文字列の中には、自動テキストrich text detection test が失敗し、プレーン・テキスト文字列がリッチ・テキストとして誤って解釈される場合があることに注意してください。このようなまれなケースでは、Qt::convertFromPlainText() を使用してプレーン・テキスト文字列を視覚的に等価なリッチ・テキスト文字列に変換するか、setTextFormat() を使用してtext format プロパティを明示的に設定してください。
重大度レベルおよびアイコン・ピクスマップ・プロパティ
QMessageBoxは4つの定義済みのメッセージの重大度レベル、またはメッセージタイプをサポートしています。icon プロパティをpredefined icons のいずれかに設定することで、4つの定義済みメッセージタイプのいずれかを指定します。 以下のルールはガイドラインです:
Question | 通常業務中に質問する場合。 | |
Information | 通常の操作に関する情報を報告する場合。 | |
Warning | 重要でないエラーを報告する。 | |
Critical | 重大なエラーを報告する場合。 |
Predefined icons は QMessageBox では定義されていませんが、スタイルによって提供されます。デフォルト値は です。メッセージ・ボックスは、それ以外の場合はすべて同じです。標準アイコンを使用する場合は、表で推奨されているものを使用するか、プラットフォームのスタイル・ガイドラインで推奨されているものを使用してください。標準アイコンのどれもがメッセージ・ボックスに適していない場合は、 プロパティを設定する代わりに、 プロパティを設定することで、カスタム・アイコンを使用することができます。No Icon icon icon pixmap
まとめると、アイコンを設定するには、標準アイコンの場合はsetIcon() を、カスタムアイコンの場合はsetIconPixmap()を使用します。
静的関数API
informative text detailed text 静的関数 API を使用したメッセージ・ボックスの作成は便利ですが、プロパティ・ベースの API を使用するよりも柔軟性に欠けます。この回避策として、title
パラメータをメッセージ・ボックスのメイン・テキストとして使い、text
パラメータをメッセージ・ボックスのインフォメーティブ・テキストとして使う方法があります。これには、メッセージ・ボックスが読みにくくなるという明らかな欠点があるため、プラットフォーム・ガイドラインはこれを推奨していません。Microsoft Windows User Interface Guidelinesでは、application name をwindow's title として使用することを推奨しています。つまり、メイン・テキストの他に情報テキストがある場合は、text
パラメータに連結する必要があります。
静的関数のシグネチャは、ボタンパラメータに関して変更されており、standard buttons とdefault button の設定に使われるようになっています。
静的関数は、information ()、question ()、warning ()、critical ()メッセージ・ボックスの作成に使用できます。
int ret = QMessageBox::warning(this, tr("My Application"), tr("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
標準ダイアログの例では、QMessageBox と他の組み込み Qt ダイアログの使い方を示しています。
高度な使用法
standard buttons ではメッセージボックスの柔軟性が足りない場合、addButton() オーバーロードを使用することができます。このオーバーロードは text とButtonRole を受け取り、カスタムボタンを追加します。ButtonRole は QMessageBox が画面上のボタンの順序を決定するために使用します(プラットフォームによって異なります)。exec() を呼び出した後、clickedButton() の値をテストできます。例えば
QMessageBox msgBox; QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole); QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort); msgBox.exec(); if (msgBox.clickedButton() == connectButton) { // connect } else if (msgBox.clickedButton() == abortButton) { // abort }
デフォルト・キーとエスケープ・キー
デフォルト・ボタン(すなわち、Enter が押されたときにアクティブになるボタン)は、setDefaultButton ()を使用して指定することができる。デフォルト・ボタンが指定されていない場合、QMessageBox はメッセージ・ボックスで使用されているボタンのbutton roles に基づいてボタンを探そうとします。
エスケープ・ボタン(Esc が押されたときにアクティブになるボタン)は、setEscapeButton ()を使用して指定できます。エスケープボタンが指定されていない場合、QMessageBoxはこれらのルールを使ってエスケープボタンを見つけようとします:
- ボタンが1つしかない場合、Esc が押されたときにアクティブになるボタンです。
- Cancel ボタンがある場合、それはEsc が押されたときにアクティブになるボタンです。
- the Reject role かthe No role のどちらかを持つボタンが1つだけある場合、それはEsc が押されたときに起動されるボタンです。
これらのルールでエスケープボタンが決定できない場合、Esc を押しても効果はありません。
QDialogButtonBox と標準ダイアログの例も参照して ください。
メンバー・タイプ・ドキュメンテーション
enum QMessageBox::ButtonRole
この列挙型は、ボタンボックスのボタンを記述するために使用できる役割を記述する。これらのロールの組み合わせは、その動作の異なる側面を記述するために使用されるフラグとして使用されます。
定数 | 値 | 説明 |
---|---|---|
QMessageBox::InvalidRole | -1 | ボタンは無効です。 |
QMessageBox::AcceptRole | 0 | ボタンをクリックすると、ダイアログが受理されます(例えば、OK)。 |
QMessageBox::RejectRole | 1 | ボタンをクリックすると、ダイアログは拒否されます(例:キャンセル)。 |
QMessageBox::DestructiveRole | 2 | ボタンをクリックすると、破壊的変更(変更の破棄など)が行われ、ダイアログが閉じます。 |
QMessageBox::ActionRole | 3 | ボタンをクリックすると、ダイアログ内の要素が変更されます。 |
QMessageBox::HelpRole | 4 | ボタンをクリックすると、ヘルプを要求することができます。 |
QMessageBox::YesRole | 5 | ボタンは "Yes "のようなボタンです。 |
QMessageBox::NoRole | 6 | ボタンは「いいえ」のようなボタンです。 |
QMessageBox::ApplyRole | 8 | ボタンは現在の変更を適用します。 |
QMessageBox::ResetRole | 7 | ボタンはダイアログのフィールドをデフォルト値にリセットします。 |
StandardButtonも参照してください 。
enum QMessageBox::Icon
この列挙型には以下の値がある:
定数 | 値 | 説明 |
---|---|---|
QMessageBox::NoIcon | 0 | メッセージ・ボックスにアイコンはありません。 |
QMessageBox::Question | 4 | メッセージが質問していることを示すアイコン。 |
QMessageBox::Information | 1 | メッセージに異常がないことを示すアイコン。 |
QMessageBox::Warning | 2 | メッセージが警告であるが、対処可能であることを示すアイコン。 |
QMessageBox::Critical | 3 | メッセージが重大な問題であることを示すアイコン。 |
[since 6.6]
enum class QMessageBox::Option
flags QMessageBox::Options
定数 | 値 | 説明 |
---|---|---|
QMessageBox::Option::DontUseNativeDialog | 0x00000001 | ネイティブメッセージダイアログを使用しません。 |
この列挙型は Qt 6.6 で導入されました。
Options 型はQFlags<Option> の typedef です。Option 値の OR の組み合わせを格納します。
enum QMessageBox::StandardButton
flags QMessageBox::StandardButtons
これらの列挙型は、標準ボタンのフラグを記述します。各ボタンにはButtonRole が定義されています。
定数 | 値 | 説明 |
---|---|---|
QMessageBox::Ok | 0x00000400 | OK" ボタンは、AcceptRole で定義されています。 |
QMessageBox::Open | 0x00002000 | AcceptRole で定義された「開く」ボタン。 |
QMessageBox::Save | 0x00000800 | AcceptRole で定義された "保存 "ボタン。 |
QMessageBox::Cancel | 0x00400000 | RejectRole で定義された「キャンセル」ボタン。 |
QMessageBox::Close | 0x00200000 | RejectRole で定義された「閉じる」ボタン。 |
QMessageBox::Discard | 0x00800000 | プラットフォームによって異なるが、DestructiveRole で定義された「破棄」または「保存しない」ボタン。 |
QMessageBox::Apply | 0x02000000 | ApplyRole で定義された「適用」ボタン。 |
QMessageBox::Reset | 0x04000000 | ResetRole で定義された「リセット」ボタン。 |
QMessageBox::RestoreDefaults | 0x08000000 | ResetRole で定義されている「デフォルトに戻す」ボタン。 |
QMessageBox::Help | 0x01000000 | HelpRole で定義された「ヘルプ」ボタン。 |
QMessageBox::SaveAll | 0x00001000 | AcceptRole で定義された「すべて保存」ボタン。 |
QMessageBox::Yes | 0x00004000 | YesRole で定義された「はい」ボタン。 |
QMessageBox::YesToAll | 0x00008000 | YesRole で定義された「Yes to All」ボタン。 |
QMessageBox::No | 0x00010000 | NoRole で定義された「いいえ」ボタン。 |
QMessageBox::NoToAll | 0x00020000 | NoRole で定義された「No to All」ボタン。 |
QMessageBox::Abort | 0x00040000 | RejectRole で定義された「中止」ボタン。 |
QMessageBox::Retry | 0x00080000 | AcceptRole で定義される「再試行」ボタン。 |
QMessageBox::Ignore | 0x00100000 | AcceptRole で定義された「無視」ボタン。 |
QMessageBox::NoButton | 0x00000000 | 無効なボタン。 |
以下の値は廃止されました:
定数 | 値 | 説明 |
---|---|---|
QMessageBox::YesAll | YesToAll | 代わりに YesToAll を使用してください。 |
QMessageBox::NoAll | NoToAll | 代わりに NoToAll を使用してください。 |
QMessageBox::Default | 0x00000100 | 代わりにinformation()、warning() などのdefaultButton 引数を使用するか、setDefaultButton() を呼び出します。 |
QMessageBox::Escape | 0x00000200 | 代わりにsetEscapeButton() を呼び出す。 |
QMessageBox::FlagMask | 0x00000300 | |
QMessageBox::ButtonMask | ~FlagMask |
StandardButtons型はQFlags<StandardButton>のtypedefです。StandardButtonの値のORの組み合わせを格納します。
ButtonRole およびstandardButtonsも参照してください 。
プロパティ・ドキュメント
detailedText : QString
このプロパティは、詳細領域に表示されるテキストを保持します。
テキストはプレーンテキストとして解釈されます。
デフォルトでは、このプロパティには空の文字列が含まれます。
アクセス関数:
QString | detailedText() const |
void | setDetailedText(const QString &text) |
QMessageBox::text およびQMessageBox::informativeTextも参照してください 。
icon : Icon
このプロパティは、メッセージ・ボックスのアイコンを保持します。
メッセージ・ボックスのアイコンは、いずれかの値で指定できます:
- QMessageBox::NoIcon
- QMessageBox::Question
- QMessageBox::Information
- QMessageBox::Warning
- QMessageBox::Critical
デフォルトはQMessageBox::NoIcon です。
実際のアイコン表示に使用される pixmap は、現在のGUI style に依存します。また、icon pixmap プロパティを設定することで、アイコンにカスタム・ピクセルマップを設定することもできます。
アクセス関数:
QMessageBox::Icon | icon() const |
void | setIcon(QMessageBox::Icon) |
iconPixmapも参照してください 。
iconPixmap : QPixmap
このプロパティは、現在のアイコンを保持します。
メッセージ・ボックスが現在使用しているアイコン。すべての GUI スタイルで適切な pixmap を描画するのは難しいことが多いので、プラットフォームごとに異なる pixmap を指定するとよいでしょう。
デフォルトでは、このプロパティは未定義です。
アクセス関数:
QPixmap | iconPixmap() const |
void | setIconPixmap(const QPixmap &pixmap) |
iconも参照してください 。
informativeText : QString
このプロパティは、メッセージの詳細な説明を提供する情報テキストを保持します。
情報テキストは、text()を拡張して、より多くの情報をユーザーに与えるために使用することができます。例えば、状況の結果を説明したり、代替ソリューションを提案したりすることができます。
テキストは、テキスト・フォーマットの設定(QMessageBox::textFormat)によって、プレーン・テキストとしてもリッチ・テキストとしても解釈されます。Qt::AutoTextすなわち、メッセージ・ボックスはテキストの形式を自動検出しようとします。
デフ ォル ト では、 こ のプ ロ パテ ィ には空文字列が格納 さ れてい ます。
アクセス関数:
QString | informativeText() const |
void | setInformativeText(const QString &text) |
textFormat 、QMessageBox::text 、QMessageBox::detailedTextも参照してください 。
[since 6.6]
options : Options
ダイアログのルック&フィールに影響するオプション。
デフォルトでは、これらのオプションは無効になっています。
オプションOption::DontUseNativeDialog は、ダイアログのプロパティを変更したり、ダイアログを表示したりする前に設定する必要があります。
ダイアログが表示されている間にオプションを設定しても、ダイアログに即座に反映される保証はありません。
他のプロパティを変更した後にオプションを設定すると、これらの値が効果を持たない可能性があります。
このプロパティは Qt 6.6 で導入されました。
アクセス関数:
QMessageBox::Options | options() const |
void | setOptions(QMessageBox::Options options) |
setOption() およびtestOption()も参照してください 。
standardButtons : StandardButtons
メッセージ・ボックスの標準ボタンのコレクション
このプロパティは、メッセージ・ボックスで使用される標準ボタンを制御します。
デフォルトでは、このプロパティには標準ボタンは含まれていません。
アクセス関数:
QMessageBox::StandardButtons | standardButtons() const |
void | setStandardButtons(QMessageBox::StandardButtons buttons) |
addButton()も参照してください 。
text : QString
このプロパティは、表示されるメッセージボックスのテキストを保持する。
テキストは、状況を説明する簡潔な文かフレーズであるべきで、理想的には中立的な文か、行動を促す質問として定式化される。
テキストは、テキスト・フォーマット設定(QMessageBox::textFormat)に応じて、プレーン・テキストまたはリッチ・テキストとして解釈されます。Qt::AutoTextすなわち、 メ ッ セージ ・ ボ ッ ク ス はテ キ ス ト の形式を自動検出 し よ う と し ます。
こ のプ ロパテ ィ のデフ ォ ル ト 値は空文字列です。
アクセス関数:
QString | text() const |
void | setText(const QString &text) |
textFormat 、QMessageBox::informativeText 、QMessageBox::detailedTextも参照してください 。
textFormat : Qt::TextFormat
このプロパティは、メッセージ・ボックスが表示するテキストのフォーマットを保持する。
メッセージ・ボックスが現在使用しているテキスト・フォーマット。可能なオプションの説明については、Qt::TextFormat enumを参照してください。
デフォルトの書式はQt::AutoText です。
アクセス関数:
Qt::TextFormat | textFormat() const |
void | setTextFormat(Qt::TextFormat format) |
setText()も参照してください 。
textInteractionFlags : Qt::TextInteractionFlags
メッセージ・ボックスのラベルがユーザ入力とどのように相互作用するかを指定します。
デフォルト値はスタイルによって異なります。
アクセス関数:
Qt::TextInteractionFlags | textInteractionFlags() const |
void | setTextInteractionFlags(Qt::TextInteractionFlags flags) |
QStyle::SH_MessageBox_TextInteractionFlagsも参照してください 。
メンバ関数ドキュメント
[explicit]
QMessageBox::QMessageBox(QWidget *parent = nullptr)
application modal parent は コンストラクタに渡されます。QDialog
ウィンドウのモダリティは、show() を呼び出す前に、setWindowModality() でオーバーライドできます。
macOS では、メッセージ・ボックスをparent のQt::Sheet として表示したい場合は、メッセージ・ボックスのwindow modality をQt::WindowModal に設定するか、open() を使用します。そうしないと、メッセージ・ボックスは標準のダイアログになります。
setWindowTitle()、setText()、setIcon()、setStandardButtons()、setWindowModality()も参照してください 。
QMessageBox::QMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)
与えられたicon 、title 、text 、および標準buttons を用いてapplication modal メッセージ・ボックスを構築します。標準ボタンやカスタムボタンはaddButton() を使っていつでも追加できます。parent とf の引数はQDialog コンストラクタに渡されます。
ウィンドウのモダリティは、show() を呼び出す前に、setWindowModality() でオーバーライドできます。
macOSでは、parent がnullptr
でなく、メッセージ・ボックスをその親のQt::Sheet として表示したい場合は、メッセージ・ボックスのwindow modality をQt::WindowModal に設定します(デフォルト)。そうしないと、メッセージ・ボックスは標準のダイアログになります。
setWindowTitle()、setText()、setIcon()、setStandardButtons()、setWindowModality()も参照してください 。
[virtual noexcept]
QMessageBox::~QMessageBox()
メッセージボックスを破棄する。
[static]
void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
タイトルtitle とテキストtext を持つシンプルな about ボックスを表示する。aboutボックスの親はparent である。
about()は、4つの場所で適切なアイコンを探す:
- 存在する場合はparent->icon() を優先する。
- 存在しない場合、parent を含むトップ・レベル・ウィジェットを試す。
- それがなければ、 を含むトップ・レベル・ウィジェットを試す。active window.
- 最後の手段として、情報アイコンを使う。
aboutボックスには、「OK」と書かれたボタンが1つあります。
macOSでは、aboutボックスはモードレスウィンドウとしてポップアップ表示されます。
QWidget::windowIcon() とQApplication::activeWindow()も参照 。
[static]
void QMessageBox::aboutQt(QWidget *parent, const QString &title = QString())
Qt に関するシンプルなメッセージボックスを、指定されたtitle で、parent の中央に表示します(parent がnullptr
でない場合)。メッセージには、アプリケーションで使用されている Qt のバージョン番号が含まれます。
これは、Menus の例で示したように、アプリケーションのHelp メニューに含めるのに便利です。
QApplication は、この機能をスロットとして提供します。
macOSでは、aboutQtボックスはモードレスウィンドウとしてポップアップします。
QApplication::aboutQt()も参照してください 。
void QMessageBox::addButton(QAbstractButton *button, QMessageBox::ButtonRole role)
指定されたrole で、指定されたbutton をメッセージ・ボックスに追加します。
removeButton(),button(),setStandardButtons()も参照 。
QPushButton *QMessageBox::addButton(QMessageBox::StandardButton button)
これはオーバーロードされた関数である。
メッセージ・ボックスに標準のbutton を追加し、それが有効であれば、プッシュ・ボタンを返します。
setStandardButtons()も参照 。
QPushButton *QMessageBox::addButton(const QString &text, QMessageBox::ButtonRole role)
これはオーバーロードされた関数です。
与えられたtext を持つボタンを作成し、指定されたrole のメッセージボックスに追加し、それを返します。
QAbstractButton *QMessageBox::button(QMessageBox::StandardButton which) const
標準ボタンwhich に対応するポインタを返し、標準ボタンがこのメッセージボックスに存在しない場合はnullptr
を返します。
注意: 返されたボタンのプロパティを変更しても、メッセージダイアログのネイティブ実装には反映されない場合があります。ダイアログボタンをカスタマイズするには、代わりにcustom button またはbutton title を追加するか、Option::DontUseNativeDialog オプションを設定してください。
standardButtons およびstandardButton()も参照してください 。
[signal]
void QMessageBox::buttonClicked(QAbstractButton *button)
このシグナルは、QMessageBox 内でボタンがクリックされるたびに発せられる。 クリックされたボタンはbutton 内で返される。
QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button) const
指定されたbutton のボタンの役割を返す。button がnullptr
であるか、メッセージ・ボックスに追加されていない場合、この関数はInvalidRole を返す。
buttons() およびaddButton()も参照してください 。
QList<QAbstractButton *> QMessageBox::buttons() const
メッセージ・ボックスに追加されたすべてのボタンのリストを返します。
buttonRole()、addButton()、removeButton()も参照 。
[override virtual protected]
void QMessageBox::changeEvent(QEvent *ev)
再実装:QWidget::changeEvent(QEvent *event)。
QCheckBox *QMessageBox::checkBox() const
ダイアログに表示されているチェックボックスを返します。チェックボックスが設定されていない場合は、nullptr
。
setCheckBox()も参照してください 。
QAbstractButton *QMessageBox::clickedButton() const
ユーザーがクリックしたボタン、またはユーザーがEsc キーを押したがescape button が設定されていない場合はnullptr
を返す。
exec()がまだ呼び出されていない場合は、nullptrを返す。
例
QMessageBox messageBox(this); QAbstractButton *disconnectButton = messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole); ... messageBox.exec(); if (messageBox.clickedButton() == disconnectButton) { ... }
standardButton() およびbutton()も参照 。
[override virtual protected]
void QMessageBox::closeEvent(QCloseEvent *e)
再インプリメント:QDialog::closeEvent(QCloseEvent *e).
[static]
QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
指定されたparent ウィジェットの前に、指定されたtitle とtext を持つクリティカルメッセージボックスを開きます。
標準のbuttons がメッセージボックスに追加されます。defaultButton はEnter が押されたときに使われるボタンを指定します。defaultButton はbuttons で指定されたボタンを参照しなければなりません。defaultButton がQMessageBox::NoButton の場合、QMessageBox は自動的に適切なデフォルトを選択します。
クリックされた標準ボタンの ID を返します。代わりにEsc が押された場合は、escape button が返されます。
メッセージボックスはapplication modal ダイアログボックスである。
警告: ダイアログの実行中にparent を削除しないでください。これを行いたい場合は、QMessageBox コンストラクタのいずれかを使用してダイアログを自分で作成する必要があります。
question()、warning()、information()も参照してください 。
QPushButton *QMessageBox::defaultButton() const
メッセージボックスのdefault button となるべきボタンを返します。デフォルトのボタンが設定されていない場合は nullptr を返します。
setDefaultButton()、addButton() およびQPushButton::setDefault()も参照して ください。
QAbstractButton *QMessageBox::escapeButton() const
エスケープが押されたときにアクティブになるボタンを返します。
デフォルトでは、QMessageBox は以下のようにエスケープボタンを自動的に検出しようとします:
- ボタンが一つしかない場合、そのボタンをエスケープボタンとする。
- ボタンが1つしかない場合、それをエスケープ・ボタンとします。Cancel ボタンがある場合、それをエスケープ・ボタンとします。
- macOSのみ、QMessageBox::RejectRole の役割を持つボタンが1つだけあれば、それをエスケープ・ボタンとします。
エスケープボタンが自動的に検出されなかった場合、Esc を押しても効果はありません。
setEscapeButton() およびaddButton()も参照 。
[override virtual protected]
bool QMessageBox::event(QEvent *e)
再実装:QWidget::event(QEvent *event)。
[override virtual slot]
int QMessageBox::exec()
再インプリメント:QDialog::exec().
メッセージボックスをmodal dialog として表示し、ユーザが閉じるまでブロックします。
標準ボタンでQMessageBox を使用する場合、この関数は、クリックされた標準ボタンを示すStandardButton 値を返します。カスタム・ボタンでQMessageBox を使用する場合、この関数は不透明な値を返します。どのボタンがクリックされたかを判別するには、clickedButton() を使用します。
注: result() 関数は、QDialog::DialogCode の代わりにStandardButton の値も返します。
ユーザは、ボタンをクリックするか、ウィンドウ・システムが提供するメカニズムを使用して、ダイアログを閉じるまで、同じアプリケーション内の他のウィンドウと対話することはできません。
[static]
QMessageBox::StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
指定されたparent ウィジェットの前に、指定されたtitle とtext を持つ情報メッセージボックスを開きます。
buttons defaultButton は、 が押されたときに使われるボタンを指定します。 は、 で指定されたボタンを参照しなければなりません。 が の場合、 は自動的に適切なデフォルトを選択します。Enter defaultButton buttons defaultButton QMessageBox::NoButton QMessageBox
クリックされた標準ボタンの ID を返します。代わりにEsc が押された場合は、escape button が返されます。
メッセージボックスはapplication modal ダイアログボックスである。
警告: ダイアログの実行中にparent を削除しないでください。これを行いたい場合は、QMessageBox コンストラクタのいずれかを使用してダイアログを自分で作成する必要があります。
question()、warning()、critical()も参照してください 。
[override virtual protected]
void QMessageBox::keyPressEvent(QKeyEvent *e)
再実装:QDialog::keyPressEvent(QKeyEvent *e).
void QMessageBox::open(QObject *receiver, const char *member)
ダイアログを開き、そのfinished() またはbuttonClicked() シグナルをreceiver とmember で指定されたスロットに接続します。member のスロットの最初のパラメータがポインタの場合、接続先はbuttonClicked() となり、そうでない場合はfinished() となります。
ダイアログが閉じられると、シグナルはスロットから切断されます。
[static]
QMessageBox::StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
指定されたparent ウィジェットの前に、与えられたtitle とtext で質問メッセージボックスを開きます。
標準のbuttons がメッセージボックスに追加されます。defaultButton は、Enter が押されたときに使われるボタンを指定します。defaultButton は、buttons で指定されたボタンを参照しなければなりません。defaultButton がQMessageBox::NoButton の場合、QMessageBox は自動的に適切なデフォルトを選択します。
クリックされた標準ボタンの ID を返します。代わりにEsc が押された場合は、escape button が返されます。
メッセージボックスはapplication modal ダイアログボックスである。
警告: ダイアログの実行中にparent を削除しないでください。これを行いたい場合は、QMessageBox コンストラクタのいずれかを使用して、ダイアログを自分で作成する必要があります。
information()、warning()、critical()も参照してください 。
void QMessageBox::removeButton(QAbstractButton *button)
button を削除せずにボタン・ボックスから取り除く。
addButton() およびsetStandardButtons()も参照してください 。
[override virtual protected]
void QMessageBox::resizeEvent(QResizeEvent *event)
再実装:QDialog::resizeEvent(QResizeEvent *).
void QMessageBox::setCheckBox(QCheckBox *cb)
メッセージダイアログのチェックボックスcb を設定します。チェックボックスの所有権はメッセージ・ボックスにあります。引数cb にnullptr
を指定すると、メッセージ・ボックスから既存のチェックボックスを削除することができます。
checkBox()も参照してください 。
void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)
メッセージ・ボックスのdefault button をbutton に設定する。
defaultButton()、addButton()、QPushButton::setDefault()も参照 。
void QMessageBox::setDefaultButton(QPushButton *button)
メッセージ・ボックスのdefault button をbutton に設定する。
addButton() およびQPushButton::setDefault()も参照 。
void QMessageBox::setEscapeButton(QAbstractButton *button)
Escape キーが押されたときにアクティブになるボタンをbutton に設定する。
escapeButton(),addButton(),clickedButton()も参照のこと 。
void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)
Escape キーが押されたときにアクティブになるボタンをbutton に設定する。
addButton() およびclickedButton()も参照 。
[since 6.6]
void QMessageBox::setOption(QMessageBox::Option option, bool on = true)
on が true ならば、与えられたoption を有効にする。そうでなければ、与えられたoption をクリアする。
オプション(特にOption::DontUseNativeDialog オプション)は、ダイアログを表示する前に設定する必要があります。
ダイアログが表示されている間にオプションを設定しても、ダイアログに即座に影響を与えることは保証されません。
他のプロパティを変更した後にオプションを設定すると、これらの値が効果を持たない可能性があります。
この関数は Qt 6.6 で導入されました。
options およびtestOption()も参照してください 。
void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
この関数はQWidget::setWindowModality() をシャドウする。
メッセージ・ボックスのモダリティをwindowModality に設定します。
macOS では、モダリティがQt::WindowModal に設定され、メッセージボックスに親がある場合、メッセージボックスはQt::Sheet になり、そうでない場合、メッセージボックスは標準ダイアログになります。
void QMessageBox::setWindowTitle(const QString &title)
この関数はQWidget::setWindowTitle() をシャドウする。
メッセージボックスのタイトルをtitle に設定します。macOSでは、ウィンドウ・タイトルは無視されます(macOSガイドラインで要求されています)。
[override virtual protected]
void QMessageBox::showEvent(QShowEvent *e)
再実装:QDialog::showEvent(QShowEvent *event)。
QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button) const
与えられたbutton に対応する標準ボタンの列挙値を返します。与えられたbutton が標準ボタンでない場合はNoButton を返します。
button() およびstandardButtons()も参照 。
[since 6.6]
bool QMessageBox::testOption(QMessageBox::Option option) const
与えられたoption が有効であればtrue
を返し、そうでなければ false を返す。
この関数は Qt 6.6 で導入されました。
options およびsetOption()も参照してください 。
[static]
QMessageBox::StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
指定されたparent ウィジェットの前に、与えられたtitle とtext を持つ警告メッセージボックスを開きます。
標準のbuttons がメッセージボックスに追加されます。defaultButton はEnter が押されたときに使われるボタンを指定します。defaultButton はbuttons で指定されたボタンを参照しなければなりません。defaultButton がQMessageBox::NoButton の場合、QMessageBox は自動的に適切なデフォルトを選択します。
クリックされた標準ボタンの ID を返します。代わりにEsc が押された場合は、escape button が返されます。
メッセージボックスはapplication modal ダイアログボックスである。
警告: ダイアログの実行中にparent を削除しないでください。これを行いたい場合は、QMessageBox コンストラクタのいずれかを使用して、ダイアログを自分で作成する必要があります。
question()、information()、critical()も参照してください 。
マクロ・ドキュメント
QT_REQUIRE_VERSION(int argc, char **argv, const char *version)
このマクロは、アプリケーションが十分新しいバージョンの Qt で実行されていることを確認するために使用できます。これは、アプリケーションがバグフィックスリリース(例えば、6.1.2)で導入された特定のバグフィックスに依存している場合に特に便利です。
argc とargv パラメータは、main()
関数のargc
とargv
パラメータです。version パラメータは、アプリケーションが必要とする Qt のバージョンを指定する文字列リテラルです(例えば、"6.1.2")。
例
#include <QApplication> #include <QMessageBox> int main(int argc, char *argv[]) { QT_REQUIRE_VERSION(argc, argv, "4.0.2") QApplication app(argc, argv); ... return app.exec(); }
© 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.