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