1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMESSAGEBOX_H
5#define QMESSAGEBOX_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtWidgets/qdialog.h>
9
10QT_REQUIRE_CONFIG(messagebox);
11
12QT_BEGIN_NAMESPACE
13
14class QAnyStringView;
15class QLabel;
16class QMessageBoxPrivate;
17class QAbstractButton;
18class QCheckBox;
19
20class Q_WIDGETS_EXPORT QMessageBox : public QDialog
21{
22 Q_OBJECT
23 Q_PROPERTY(QString text READ text WRITE setText)
24 Q_PROPERTY(Icon icon READ icon WRITE setIcon)
25 Q_PROPERTY(QPixmap iconPixmap READ iconPixmap WRITE setIconPixmap)
26 Q_PROPERTY(Qt::TextFormat textFormat READ textFormat WRITE setTextFormat)
27 Q_PROPERTY(StandardButtons standardButtons READ standardButtons WRITE setStandardButtons)
28#if QT_CONFIG(textedit)
29 Q_PROPERTY(QString detailedText READ detailedText WRITE setDetailedText)
30#endif
31 Q_PROPERTY(QString informativeText READ informativeText WRITE setInformativeText)
32 Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags
33 WRITE setTextInteractionFlags)
34 Q_PROPERTY(Options options READ options WRITE setOptions)
35public:
36 // Keep in sync with MessageBoxOption in qplatformdialoghelper.h
37 enum class Option {
38 DontUseNativeDialog = 0x00000001
39 };
40 Q_FLAG(Option)
41
42 enum Icon {
43 // keep this in sync with QMessageDialogOptions::StandardIcon
44 NoIcon = 0,
45 Information = 1,
46 Warning = 2,
47 Critical = 3,
48 Question = 4
49 };
50 Q_ENUM(Icon)
51
52 enum ButtonRole {
53 // keep this in sync with QDialogButtonBox::ButtonRole and QPlatformDialogHelper::ButtonRole
54 InvalidRole = -1,
55 AcceptRole,
56 RejectRole,
57 DestructiveRole,
58 ActionRole,
59 HelpRole,
60 YesRole,
61 NoRole,
62 ResetRole,
63 ApplyRole,
64
65 NRoles
66 };
67
68 enum StandardButton {
69 // keep this in sync with QDialogButtonBox::StandardButton and QPlatformDialogHelper::StandardButton
70 NoButton = 0x00000000,
71 Ok = 0x00000400,
72 Save = 0x00000800,
73 SaveAll = 0x00001000,
74 Open = 0x00002000,
75 Yes = 0x00004000,
76 YesToAll = 0x00008000,
77 No = 0x00010000,
78 NoToAll = 0x00020000,
79 Abort = 0x00040000,
80 Retry = 0x00080000,
81 Ignore = 0x00100000,
82 Close = 0x00200000,
83 Cancel = 0x00400000,
84 Discard = 0x00800000,
85 Help = 0x01000000,
86 Apply = 0x02000000,
87 Reset = 0x04000000,
88 RestoreDefaults = 0x08000000,
89
90 FirstButton = Ok, // internal
91 LastButton = RestoreDefaults, // internal
92
93 YesAll = YesToAll, // obsolete
94 NoAll = NoToAll, // obsolete
95
96 Default = 0x00000100, // obsolete
97 Escape = 0x00000200, // obsolete
98 FlagMask = 0x00000300, // obsolete
99 ButtonMask = ~FlagMask // obsolete
100 };
101#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
102 typedef StandardButton Button;
103#endif
104 Q_DECLARE_FLAGS(Options, Option)
105 Q_DECLARE_FLAGS(StandardButtons, StandardButton)
106
107 Q_FLAG(StandardButtons)
108
109 explicit QMessageBox(QWidget *parent = nullptr);
110 QMessageBox(Icon icon, const QString &title, const QString &text,
111 StandardButtons buttons = NoButton, QWidget *parent = nullptr,
112 Qt::WindowFlags flags = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
113 ~QMessageBox();
114
115 void addButton(QAbstractButton *button, ButtonRole role);
116 QPushButton *addButton(const QString &text, ButtonRole role);
117 QPushButton *addButton(StandardButton button);
118 void removeButton(QAbstractButton *button);
119
120 using QDialog::open;
121 void open(QObject *receiver, const char *member);
122
123 QList<QAbstractButton *> buttons() const;
124 ButtonRole buttonRole(QAbstractButton *button) const;
125
126 void setStandardButtons(StandardButtons buttons);
127 StandardButtons standardButtons() const;
128 StandardButton standardButton(QAbstractButton *button) const;
129 QAbstractButton *button(StandardButton which) const;
130
131 QPushButton *defaultButton() const;
132 void setDefaultButton(QPushButton *button);
133 void setDefaultButton(StandardButton button);
134
135 QAbstractButton *escapeButton() const;
136 void setEscapeButton(QAbstractButton *button);
137 void setEscapeButton(StandardButton button);
138
139 QAbstractButton *clickedButton() const;
140
141 QString text() const;
142 void setText(const QString &text);
143
144 Icon icon() const;
145 void setIcon(Icon);
146
147 QPixmap iconPixmap() const;
148 void setIconPixmap(const QPixmap &pixmap);
149
150 Qt::TextFormat textFormat() const;
151 void setTextFormat(Qt::TextFormat format);
152
153 void setTextInteractionFlags(Qt::TextInteractionFlags flags);
154 Qt::TextInteractionFlags textInteractionFlags() const;
155
156 void setCheckBox(QCheckBox *cb);
157 QCheckBox* checkBox() const;
158
159 void setOption(Option option, bool on = true);
160 bool testOption(Option option) const;
161 void setOptions(Options options);
162 Options options() const;
163
164 static StandardButton information(QWidget *parent, const QString &title,
165 const QString &text, StandardButtons buttons = Ok,
166 StandardButton defaultButton = NoButton);
167#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) // needed as long as we have int overloads
168 inline static StandardButton information(QWidget *parent, const QString &title,
169 const QString& text,
170 StandardButton button0, StandardButton button1 = NoButton)
171 { return information(parent, title, text, buttons: StandardButtons(button0), defaultButton: button1); }
172#endif
173
174 static StandardButton question(QWidget *parent, const QString &title,
175 const QString &text, StandardButtons buttons = StandardButtons(Yes | No),
176 StandardButton defaultButton = NoButton);
177#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
178 inline static int question(QWidget *parent, const QString &title,
179 const QString& text,
180 StandardButton button0, StandardButton button1)
181 { return question(parent, title, text, buttons: StandardButtons(button0), defaultButton: button1); }
182#endif
183
184 static StandardButton warning(QWidget *parent, const QString &title,
185 const QString &text, StandardButtons buttons = Ok,
186 StandardButton defaultButton = NoButton);
187#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
188 inline static int warning(QWidget *parent, const QString &title,
189 const QString& text,
190 StandardButton button0, StandardButton button1)
191 { return warning(parent, title, text, buttons: StandardButtons(button0), defaultButton: button1); }
192#endif
193
194 static StandardButton critical(QWidget *parent, const QString &title,
195 const QString &text, StandardButtons buttons = Ok,
196 StandardButton defaultButton = NoButton);
197#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
198 inline static int critical(QWidget *parent, const QString &title,
199 const QString& text,
200 StandardButton button0, StandardButton button1)
201 { return critical(parent, title, text, buttons: StandardButtons(button0), defaultButton: button1); }
202#endif
203
204 static void about(QWidget *parent, const QString &title, const QString &text);
205 static void aboutQt(QWidget *parent, const QString &title = QString());
206
207#if QT_DEPRECATED_SINCE(6,2)
208 // the following functions are obsolete:
209 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
210 QMessageBox(const QString &title, const QString &text, Icon icon,
211 int button0, int button1, int button2,
212 QWidget *parent = nullptr,
213 Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
214
215 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
216 static int information(QWidget *parent, const QString &title,
217 const QString& text,
218 int button0, int button1 = 0, int button2 = 0);
219 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
220 static int information(QWidget *parent, const QString &title,
221 const QString& text,
222 const QString& button0Text,
223 const QString& button1Text = QString(),
224 const QString& button2Text = QString(),
225 int defaultButtonNumber = 0,
226 int escapeButtonNumber = -1);
227
228 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
229 static int question(QWidget *parent, const QString &title,
230 const QString& text,
231 int button0, int button1 = 0, int button2 = 0);
232 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
233 static int question(QWidget *parent, const QString &title,
234 const QString& text,
235 const QString& button0Text,
236 const QString& button1Text = QString(),
237 const QString& button2Text = QString(),
238 int defaultButtonNumber = 0,
239 int escapeButtonNumber = -1);
240
241 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
242 static int warning(QWidget *parent, const QString &title,
243 const QString& text,
244 int button0, int button1, int button2 = 0);
245 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
246 static int warning(QWidget *parent, const QString &title,
247 const QString& text,
248 const QString& button0Text,
249 const QString& button1Text = QString(),
250 const QString& button2Text = QString(),
251 int defaultButtonNumber = 0,
252 int escapeButtonNumber = -1);
253
254 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
255 static int critical(QWidget *parent, const QString &title,
256 const QString& text,
257 int button0, int button1, int button2 = 0);
258 QT_DEPRECATED_VERSION_X_6_2("Use the overload taking StandardButtons instead.")
259 static int critical(QWidget *parent, const QString &title,
260 const QString& text,
261 const QString& button0Text,
262 const QString& button1Text = QString(),
263 const QString& button2Text = QString(),
264 int defaultButtonNumber = 0,
265 int escapeButtonNumber = -1);
266
267 QT_DEPRECATED_VERSION_X_6_2("Use button() and QPushButton::text() instead.")
268 QString buttonText(int button) const;
269 QT_DEPRECATED_VERSION_X_6_2("Use addButton() instead.")
270 void setButtonText(int button, const QString &text);
271#endif
272
273 QString informativeText() const;
274 void setInformativeText(const QString &text);
275
276#if QT_CONFIG(textedit)
277 QString detailedText() const;
278 void setDetailedText(const QString &text);
279#endif
280
281 void setWindowTitle(const QString &title);
282 void setWindowModality(Qt::WindowModality windowModality);
283
284#if QT_DEPRECATED_SINCE(6,2)
285 QT_DEPRECATED_VERSION_X_6_2("Use QStyle::standardIcon() instead.")
286 static QPixmap standardIcon(Icon icon);
287#endif
288
289Q_SIGNALS:
290 void buttonClicked(QAbstractButton *button);
291
292#ifdef Q_QDOC
293public Q_SLOTS:
294 int exec() override;
295#endif
296
297protected:
298 bool event(QEvent *e) override;
299 void resizeEvent(QResizeEvent *event) override;
300 void showEvent(QShowEvent *event) override;
301 void closeEvent(QCloseEvent *event) override;
302 void keyPressEvent(QKeyEvent *event) override;
303 void changeEvent(QEvent *event) override;
304
305private:
306 Q_PRIVATE_SLOT(d_func(), void _q_buttonClicked(QAbstractButton *))
307 Q_PRIVATE_SLOT(d_func(), void _q_helperClicked(QPlatformDialogHelper::StandardButton, QPlatformDialogHelper::ButtonRole))
308
309 Q_DISABLE_COPY(QMessageBox)
310 Q_DECLARE_PRIVATE(QMessageBox)
311};
312
313Q_DECLARE_OPERATORS_FOR_FLAGS(QMessageBox::StandardButtons)
314
315Q_WIDGETS_EXPORT void qRequireVersion(int argc, char *argv[], QAnyStringView req);
316
317#define QT_REQUIRE_VERSION(argc, argv, str) qRequireVersion(argc, argv, str);
318
319QT_END_NAMESPACE
320
321#endif // QMESSAGEBOX_H
322

source code of qtbase/src/widgets/dialogs/qmessagebox.h