1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui/qmessagebox.h>
|
---|
43 |
|
---|
44 | #ifndef QT_NO_MESSAGEBOX
|
---|
45 |
|
---|
46 | #include <QtGui/qdialogbuttonbox.h>
|
---|
47 | #include "private/qlabel_p.h"
|
---|
48 | #include <QtCore/qlist.h>
|
---|
49 | #include <QtCore/qdebug.h>
|
---|
50 | #include <QtGui/qstyle.h>
|
---|
51 | #include <QtGui/qstyleoption.h>
|
---|
52 | #include <QtGui/qgridlayout.h>
|
---|
53 | #include <QtGui/qdesktopwidget.h>
|
---|
54 | #include <QtGui/qpushbutton.h>
|
---|
55 | #include <QtGui/qaccessible.h>
|
---|
56 | #include <QtGui/qicon.h>
|
---|
57 | #include <QtGui/qtextdocument.h>
|
---|
58 | #include <QtGui/qapplication.h>
|
---|
59 | #include <QtGui/qtextedit.h>
|
---|
60 | #include <QtGui/qmenu.h>
|
---|
61 | #include "qdialog_p.h"
|
---|
62 | #include <QtGui/qfont.h>
|
---|
63 | #include <QtGui/qfontmetrics.h>
|
---|
64 | #include <QtGui/qclipboard.h>
|
---|
65 |
|
---|
66 | #ifdef Q_OS_WINCE
|
---|
67 | extern bool qt_wince_is_mobile(); //defined in qguifunctions_wince.cpp
|
---|
68 | extern bool qt_wince_is_smartphone();//defined in qguifunctions_wince.cpp
|
---|
69 | extern bool qt_wince_is_pocket_pc(); //defined in qguifunctions_wince.cpp
|
---|
70 |
|
---|
71 | #include "qguifunctions_wince.h"
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | QT_BEGIN_NAMESPACE
|
---|
75 |
|
---|
76 | extern QHash<QByteArray, QFont> *qt_app_fonts_hash();
|
---|
77 |
|
---|
78 | enum Button { Old_Ok = 1, Old_Cancel = 2, Old_Yes = 3, Old_No = 4, Old_Abort = 5, Old_Retry = 6,
|
---|
79 | Old_Ignore = 7, Old_YesAll = 8, Old_NoAll = 9, Old_ButtonMask = 0xFF,
|
---|
80 | NewButtonMask = 0xFFFFFC00 };
|
---|
81 |
|
---|
82 | enum DetailButtonLabel { ShowLabel = 0, HideLabel = 1 };
|
---|
83 | #ifndef QT_NO_TEXTEDIT
|
---|
84 | class QMessageBoxDetailsText : public QWidget
|
---|
85 | {
|
---|
86 | public:
|
---|
87 | class TextEdit : public QTextEdit
|
---|
88 | {
|
---|
89 | public:
|
---|
90 | TextEdit(QWidget *parent=0) : QTextEdit(parent) { }
|
---|
91 | void contextMenuEvent(QContextMenuEvent * e)
|
---|
92 | {
|
---|
93 | #ifndef QT_NO_CONTEXTMENU
|
---|
94 | QMenu *menu = createStandardContextMenu();
|
---|
95 | menu->exec(e->globalPos());
|
---|
96 | delete menu;
|
---|
97 | #else
|
---|
98 | Q_UNUSED(e);
|
---|
99 | #endif
|
---|
100 | }
|
---|
101 | };
|
---|
102 |
|
---|
103 | QMessageBoxDetailsText(QWidget *parent=0)
|
---|
104 | : QWidget(parent)
|
---|
105 | {
|
---|
106 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
107 | layout->setMargin(0);
|
---|
108 | QFrame *line = new QFrame(this);
|
---|
109 | line->setFrameShape(QFrame::HLine);
|
---|
110 | line->setFrameShadow(QFrame::Sunken);
|
---|
111 | layout->addWidget(line);
|
---|
112 | textEdit = new TextEdit();
|
---|
113 | textEdit->setFixedHeight(100);
|
---|
114 | textEdit->setFocusPolicy(Qt::NoFocus);
|
---|
115 | textEdit->setReadOnly(true);
|
---|
116 | layout->addWidget(textEdit);
|
---|
117 | setLayout(layout);
|
---|
118 | }
|
---|
119 | void setText(const QString &text) { textEdit->setPlainText(text); }
|
---|
120 | QString text() const { return textEdit->toPlainText(); }
|
---|
121 | QString label(DetailButtonLabel label)
|
---|
122 | { return label == ShowLabel ? QMessageBox::tr("Show Details...")
|
---|
123 | : QMessageBox::tr("Hide Details..."); }
|
---|
124 | private:
|
---|
125 | TextEdit *textEdit;
|
---|
126 | };
|
---|
127 | #endif // QT_NO_TEXTEDIT
|
---|
128 |
|
---|
129 | class QMessageBoxPrivate : public QDialogPrivate
|
---|
130 | {
|
---|
131 | Q_DECLARE_PUBLIC(QMessageBox)
|
---|
132 |
|
---|
133 | public:
|
---|
134 | QMessageBoxPrivate() : escapeButton(0), defaultButton(0), clickedButton(0), detailsButton(0),
|
---|
135 | #ifndef QT_NO_TEXTEDIT
|
---|
136 | detailsText(0),
|
---|
137 | #endif
|
---|
138 | compatMode(false), autoAddOkButton(true),
|
---|
139 | detectedEscapeButton(0), informativeLabel(0) { }
|
---|
140 |
|
---|
141 | void init(const QString &title = QString(), const QString &text = QString());
|
---|
142 | void _q_buttonClicked(QAbstractButton *);
|
---|
143 |
|
---|
144 | QAbstractButton *findButton(int button0, int button1, int button2, int flags);
|
---|
145 | void addOldButtons(int button0, int button1, int button2);
|
---|
146 |
|
---|
147 | QAbstractButton *abstractButtonForId(int id) const;
|
---|
148 | int execReturnCode(QAbstractButton *button);
|
---|
149 |
|
---|
150 | void detectEscapeButton();
|
---|
151 | void updateSize();
|
---|
152 | int layoutMinimumWidth();
|
---|
153 | void retranslateStrings();
|
---|
154 |
|
---|
155 | #ifdef Q_OS_WINCE
|
---|
156 | void hideSpecial();
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon,
|
---|
160 | const QString &title, const QString &text,
|
---|
161 | int button0, int button1, int button2);
|
---|
162 | static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon,
|
---|
163 | const QString &title, const QString &text,
|
---|
164 | const QString &button0Text,
|
---|
165 | const QString &button1Text,
|
---|
166 | const QString &button2Text,
|
---|
167 | int defaultButtonNumber,
|
---|
168 | int escapeButtonNumber);
|
---|
169 |
|
---|
170 | static QMessageBox::StandardButton showNewMessageBox(QWidget *parent,
|
---|
171 | QMessageBox::Icon icon, const QString& title, const QString& text,
|
---|
172 | QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton);
|
---|
173 |
|
---|
174 | static QPixmap standardIcon(QMessageBox::Icon icon, QMessageBox *mb);
|
---|
175 |
|
---|
176 | QLabel *label;
|
---|
177 | QMessageBox::Icon icon;
|
---|
178 | QLabel *iconLabel;
|
---|
179 | QDialogButtonBox *buttonBox;
|
---|
180 | QList<QAbstractButton *> customButtonList;
|
---|
181 | QAbstractButton *escapeButton;
|
---|
182 | QPushButton *defaultButton;
|
---|
183 | QAbstractButton *clickedButton;
|
---|
184 | QPushButton *detailsButton;
|
---|
185 | #ifndef QT_NO_TEXTEDIT
|
---|
186 | QMessageBoxDetailsText *detailsText;
|
---|
187 | #endif
|
---|
188 | bool compatMode;
|
---|
189 | bool autoAddOkButton;
|
---|
190 | QAbstractButton *detectedEscapeButton;
|
---|
191 | QLabel *informativeLabel;
|
---|
192 | QPointer<QObject> receiverToDisconnectOnClose;
|
---|
193 | QByteArray memberToDisconnectOnClose;
|
---|
194 | QByteArray signalToDisconnectOnClose;
|
---|
195 | };
|
---|
196 |
|
---|
197 | void QMessageBoxPrivate::init(const QString &title, const QString &text)
|
---|
198 | {
|
---|
199 | Q_Q(QMessageBox);
|
---|
200 |
|
---|
201 | label = new QLabel;
|
---|
202 | label->setObjectName(QLatin1String("qt_msgbox_label"));
|
---|
203 | label->setTextInteractionFlags(Qt::TextInteractionFlags(q->style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, q)));
|
---|
204 | label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
|
---|
205 | label->setOpenExternalLinks(true);
|
---|
206 | #if defined(Q_WS_MAC)
|
---|
207 | label->setContentsMargins(16, 0, 0, 0);
|
---|
208 | #elif !defined(Q_WS_QWS)
|
---|
209 | label->setContentsMargins(2, 0, 0, 0);
|
---|
210 | label->setIndent(9);
|
---|
211 | #endif
|
---|
212 | icon = QMessageBox::NoIcon;
|
---|
213 | iconLabel = new QLabel;
|
---|
214 | iconLabel->setObjectName(QLatin1String("qt_msgboxex_icon_label"));
|
---|
215 | iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
---|
216 |
|
---|
217 | buttonBox = new QDialogButtonBox;
|
---|
218 | buttonBox->setObjectName(QLatin1String("qt_msgbox_buttonbox"));
|
---|
219 | buttonBox->setCenterButtons(q->style()->styleHint(QStyle::SH_MessageBox_CenterButtons, 0, q));
|
---|
220 | QObject::connect(buttonBox, SIGNAL(clicked(QAbstractButton*)),
|
---|
221 | q, SLOT(_q_buttonClicked(QAbstractButton*)));
|
---|
222 |
|
---|
223 | QGridLayout *grid = new QGridLayout;
|
---|
224 | #ifndef Q_WS_MAC
|
---|
225 | grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop);
|
---|
226 | grid->addWidget(label, 0, 1, 1, 1);
|
---|
227 | // -- leave space for information label --
|
---|
228 | grid->addWidget(buttonBox, 2, 0, 1, 2);
|
---|
229 | #else
|
---|
230 | grid->setMargin(0);
|
---|
231 | grid->setVerticalSpacing(8);
|
---|
232 | grid->setHorizontalSpacing(0);
|
---|
233 | q->setContentsMargins(24, 15, 24, 20);
|
---|
234 | grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop | Qt::AlignLeft);
|
---|
235 | grid->addWidget(label, 0, 1, 1, 1);
|
---|
236 | // -- leave space for information label --
|
---|
237 | grid->setRowStretch(1, 100);
|
---|
238 | grid->setRowMinimumHeight(2, 6);
|
---|
239 | grid->addWidget(buttonBox, 3, 1, 1, 1);
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | grid->setSizeConstraint(QLayout::SetNoConstraint);
|
---|
243 | q->setLayout(grid);
|
---|
244 |
|
---|
245 | if (!title.isEmpty() || !text.isEmpty()) {
|
---|
246 | q->setWindowTitle(title);
|
---|
247 | q->setText(text);
|
---|
248 | }
|
---|
249 | q->setModal(true);
|
---|
250 |
|
---|
251 | #ifdef Q_WS_MAC
|
---|
252 | QFont f = q->font();
|
---|
253 | f.setBold(true);
|
---|
254 | label->setFont(f);
|
---|
255 | #endif
|
---|
256 | retranslateStrings();
|
---|
257 | }
|
---|
258 |
|
---|
259 | int QMessageBoxPrivate::layoutMinimumWidth()
|
---|
260 | {
|
---|
261 | Q_Q(QMessageBox);
|
---|
262 |
|
---|
263 | q->layout()->activate();
|
---|
264 | return q->layout()->totalMinimumSize().width();
|
---|
265 | }
|
---|
266 |
|
---|
267 | void QMessageBoxPrivate::updateSize()
|
---|
268 | {
|
---|
269 | Q_Q(QMessageBox);
|
---|
270 |
|
---|
271 | if (!q->isVisible())
|
---|
272 | return;
|
---|
273 |
|
---|
274 | QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size();
|
---|
275 | #ifdef Q_WS_QWS
|
---|
276 | // the width of the screen, less the window border.
|
---|
277 | int hardLimit = screenSize.width() - (q->frameGeometry().width() - q->geometry().width());
|
---|
278 | #elif defined(Q_OS_WINCE)
|
---|
279 | // the width of the screen, less the window border.
|
---|
280 | int hardLimit = screenSize.width() - (q->frameGeometry().width() - q->geometry().width());
|
---|
281 | #else
|
---|
282 | int hardLimit = qMin(screenSize.width() - 480, 1000); // can never get bigger than this
|
---|
283 | #endif
|
---|
284 | #ifdef Q_WS_MAC
|
---|
285 | int softLimit = qMin(screenSize.width()/2, 420);
|
---|
286 | #elif defined(Q_WS_QWS)
|
---|
287 | int softLimit = qMin(hardLimit, 500);
|
---|
288 | #else
|
---|
289 | // note: ideally on windows, hard and soft limits but it breaks compat
|
---|
290 | #ifndef Q_OS_WINCE
|
---|
291 | int softLimit = qMin(screenSize.width()/2, 500);
|
---|
292 | #else
|
---|
293 | int softLimit = qMin(screenSize.width() * 3 / 4, 500);
|
---|
294 | #endif //Q_OS_WINCE
|
---|
295 | #endif
|
---|
296 |
|
---|
297 | if (informativeLabel)
|
---|
298 | informativeLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
---|
299 |
|
---|
300 | label->setWordWrap(false); // makes the label return min size
|
---|
301 | int width = layoutMinimumWidth();
|
---|
302 |
|
---|
303 | if (width > softLimit) {
|
---|
304 | label->setWordWrap(true);
|
---|
305 | width = qMax(softLimit, layoutMinimumWidth());
|
---|
306 |
|
---|
307 | if (width > hardLimit) {
|
---|
308 | label->d_func()->ensureTextControl();
|
---|
309 | if (QTextControl *control = label->d_func()->control) {
|
---|
310 | QTextOption opt = control->document()->defaultTextOption();
|
---|
311 | opt.setWrapMode(QTextOption::WrapAnywhere);
|
---|
312 | control->document()->setDefaultTextOption(opt);
|
---|
313 | }
|
---|
314 | width = hardLimit;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (informativeLabel) {
|
---|
319 | label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
---|
320 | QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Preferred);
|
---|
321 | policy.setHeightForWidth(true);
|
---|
322 | informativeLabel->setSizePolicy(policy);
|
---|
323 | width = qMax(width, layoutMinimumWidth());
|
---|
324 | if (width > hardLimit) { // longest word is really big, so wrap anywhere
|
---|
325 | informativeLabel->d_func()->ensureTextControl();
|
---|
326 | if (QTextControl *control = informativeLabel->d_func()->control) {
|
---|
327 | QTextOption opt = control->document()->defaultTextOption();
|
---|
328 | opt.setWrapMode(QTextOption::WrapAnywhere);
|
---|
329 | control->document()->setDefaultTextOption(opt);
|
---|
330 | }
|
---|
331 | width = hardLimit;
|
---|
332 | }
|
---|
333 | policy.setHeightForWidth(label->wordWrap());
|
---|
334 | label->setSizePolicy(policy);
|
---|
335 | }
|
---|
336 |
|
---|
337 | QFontMetrics fm(qApp->font("QWorkspaceTitleBar"));
|
---|
338 | int windowTitleWidth = qMin(fm.width(q->windowTitle()) + 50, hardLimit);
|
---|
339 | if (windowTitleWidth > width)
|
---|
340 | width = windowTitleWidth;
|
---|
341 |
|
---|
342 | q->layout()->activate();
|
---|
343 | int height = (q->layout()->hasHeightForWidth())
|
---|
344 | ? q->layout()->totalHeightForWidth(width)
|
---|
345 | : q->layout()->totalMinimumSize().height();
|
---|
346 | q->setFixedSize(width, height);
|
---|
347 | QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | #ifdef Q_OS_WINCE
|
---|
352 | /*!
|
---|
353 | \internal
|
---|
354 | Hides special buttons which are rather shown in the title bar
|
---|
355 | on WinCE, to conserve screen space.
|
---|
356 | */
|
---|
357 |
|
---|
358 | void QMessageBoxPrivate::hideSpecial()
|
---|
359 | {
|
---|
360 | Q_Q(QMessageBox);
|
---|
361 | QList<QPushButton*> list = qFindChildren<QPushButton*>(q);
|
---|
362 | for (int i=0; i<list.size(); ++i) {
|
---|
363 | QPushButton *pb = list.at(i);
|
---|
364 | QString text = pb->text();
|
---|
365 | text.remove(QChar::fromLatin1('&'));
|
---|
366 | if (text == qApp->translate("QMessageBox", "OK" ))
|
---|
367 | pb->setFixedSize(0,0);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | #endif
|
---|
371 |
|
---|
372 | static int oldButton(int button)
|
---|
373 | {
|
---|
374 | switch (button & QMessageBox::ButtonMask) {
|
---|
375 | case QMessageBox::Ok:
|
---|
376 | return Old_Ok;
|
---|
377 | case QMessageBox::Cancel:
|
---|
378 | return Old_Cancel;
|
---|
379 | case QMessageBox::Yes:
|
---|
380 | return Old_Yes;
|
---|
381 | case QMessageBox::No:
|
---|
382 | return Old_No;
|
---|
383 | case QMessageBox::Abort:
|
---|
384 | return Old_Abort;
|
---|
385 | case QMessageBox::Retry:
|
---|
386 | return Old_Retry;
|
---|
387 | case QMessageBox::Ignore:
|
---|
388 | return Old_Ignore;
|
---|
389 | case QMessageBox::YesToAll:
|
---|
390 | return Old_YesAll;
|
---|
391 | case QMessageBox::NoToAll:
|
---|
392 | return Old_NoAll;
|
---|
393 | default:
|
---|
394 | return 0;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | int QMessageBoxPrivate::execReturnCode(QAbstractButton *button)
|
---|
399 | {
|
---|
400 | int ret = buttonBox->standardButton(button);
|
---|
401 | if (ret == QMessageBox::NoButton) {
|
---|
402 | ret = customButtonList.indexOf(button); // if button == 0, correctly sets ret = -1
|
---|
403 | } else if (compatMode) {
|
---|
404 | ret = oldButton(ret);
|
---|
405 | }
|
---|
406 | return ret;
|
---|
407 | }
|
---|
408 |
|
---|
409 | void QMessageBoxPrivate::_q_buttonClicked(QAbstractButton *button)
|
---|
410 | {
|
---|
411 | Q_Q(QMessageBox);
|
---|
412 | #ifndef QT_NO_TEXTEDIT
|
---|
413 | if (detailsButton && detailsText && button == detailsButton) {
|
---|
414 | detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel));
|
---|
415 | detailsText->setHidden(!detailsText->isHidden());
|
---|
416 | updateSize();
|
---|
417 | } else
|
---|
418 | #endif
|
---|
419 | {
|
---|
420 | clickedButton = button;
|
---|
421 | q->done(execReturnCode(button)); // does not trigger closeEvent
|
---|
422 | emit q->buttonClicked(button);
|
---|
423 |
|
---|
424 | if (receiverToDisconnectOnClose) {
|
---|
425 | QObject::disconnect(q, signalToDisconnectOnClose, receiverToDisconnectOnClose,
|
---|
426 | memberToDisconnectOnClose);
|
---|
427 | receiverToDisconnectOnClose = 0;
|
---|
428 | }
|
---|
429 | signalToDisconnectOnClose.clear();
|
---|
430 | memberToDisconnectOnClose.clear();
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | /*!
|
---|
435 | \class QMessageBox
|
---|
436 |
|
---|
437 | \brief The QMessageBox class provides a modal dialog for informing
|
---|
438 | the user or for asking the user a question and receiving an answer.
|
---|
439 |
|
---|
440 | \ingroup dialogs
|
---|
441 | \mainclass
|
---|
442 |
|
---|
443 | A message box displays a primary \l{QMessageBox::text}{text} to
|
---|
444 | alert the user to a situation, an \l{QMessageBox::informativeText}
|
---|
445 | {informative text} to further explain the alert or to ask the user
|
---|
446 | a question, and an optional \l{QMessageBox::detailedText}
|
---|
447 | {detailed text} to provide even more data if the user requests
|
---|
448 | it. A message box can also display an \l{QMessageBox::icon} {icon}
|
---|
449 | and \l{QMessageBox::standardButtons} {standard buttons} for
|
---|
450 | accepting a user response.
|
---|
451 |
|
---|
452 | Two APIs for using QMessageBox are provided, the property-based
|
---|
453 | API, and the static functions. Calling one of the static functions
|
---|
454 | is the simpler approach, but it is less flexible than using the
|
---|
455 | property-based API, and the result is less informative. Using the
|
---|
456 | property-based API is recommended.
|
---|
457 |
|
---|
458 | \section1 The Property-based API
|
---|
459 |
|
---|
460 | To use the property-based API, construct an instance of
|
---|
461 | QMessageBox, set the desired properties, and call exec() to show
|
---|
462 | the message. The simplest configuration is to set only the
|
---|
463 | \l{QMessageBox::text} {message text} property.
|
---|
464 |
|
---|
465 | \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 5
|
---|
466 |
|
---|
467 | The user must click the \gui{OK} button to dismiss the message
|
---|
468 | box. The rest of the GUI is blocked until the message box is
|
---|
469 | dismissed.
|
---|
470 |
|
---|
471 | \image msgbox1.png
|
---|
472 |
|
---|
473 | A better approach than just alerting the user to an event is to
|
---|
474 | also ask the user what to do about it. Store the question in the
|
---|
475 | \l{QMessageBox::informativeText} {informative text} property, and
|
---|
476 | set the \l{QMessageBox::standardButtons} {standard buttons}
|
---|
477 | property to the set of buttons you want as the set of user
|
---|
478 | responses. The buttons are specified by combining values from
|
---|
479 | StandardButtons using the bitwise OR operator. The display order
|
---|
480 | for the buttons is platform-dependent. For example, on Windows,
|
---|
481 | \gui{Save} is displayed to the left of \gui{Cancel}, whereas on
|
---|
482 | Mac OS, the order is reversed.
|
---|
483 |
|
---|
484 | Mark one of your standard buttons to be your
|
---|
485 | \l{QMessageBox::defaultButton()} {default button}.
|
---|
486 |
|
---|
487 | \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 6
|
---|
488 |
|
---|
489 | This is the approach recommended in the
|
---|
490 | \l{http://developer.apple.com/documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGWindows/chapter_18_section_7.html}
|
---|
491 | {Mac OS X Guidlines}. Similar guidlines apply for the other
|
---|
492 | platforms, but note the different ways the
|
---|
493 | \l{QMessageBox::informativeText} {informative text} is handled for
|
---|
494 | different platforms.
|
---|
495 |
|
---|
496 | \image msgbox2.png
|
---|
497 |
|
---|
498 | The exec() slot returns the StandardButtons value of the button
|
---|
499 | that was clicked.
|
---|
500 |
|
---|
501 | \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 7
|
---|
502 |
|
---|
503 | To give the user more information to help him answer the question,
|
---|
504 | set the \l{QMessageBox::detailedText} {detailed text} property. If
|
---|
505 | the \l{QMessageBox::detailedText} {detailed text} property is set,
|
---|
506 | the \gui{Show Details...} button will be shown.
|
---|
507 |
|
---|
508 | \image msgbox3.png
|
---|
509 |
|
---|
510 | Clicking the \gui{Show Details...} button displays the detailed text.
|
---|
511 |
|
---|
512 | \image msgbox4.png
|
---|
513 |
|
---|
514 | \section2 Rich Text and the Text Format Property
|
---|
515 |
|
---|
516 | The \l{QMessageBox::detailedText} {detailed text} property is
|
---|
517 | always interpreted as plain text. The \l{QMessageBox::text} {main
|
---|
518 | text} and \l{QMessageBox::informativeText} {informative text}
|
---|
519 | properties can be either plain text or rich text. These strings
|
---|
520 | are interpreted according to the setting of the
|
---|
521 | \l{QMessageBox::textFormat} {text format} property. The default
|
---|
522 | setting is \l{Qt::AutoText} {auto-text}.
|
---|
523 |
|
---|
524 | Note that for some plain text strings containing XML
|
---|
525 | meta-characters, the auto-text \l{Qt::mightBeRichText()} {rich
|
---|
526 | text detection test} may fail causing your plain text string to be
|
---|
527 | interpreted incorrectly as rich text. In these rare cases, use
|
---|
528 | Qt::convertFromPlainText() to convert your plain text string to a
|
---|
529 | visually equivalent rich text string, or set the
|
---|
530 | \l{QMessageBox::textFormat} {text format} property explicitly with
|
---|
531 | setTextFormat().
|
---|
532 |
|
---|
533 | \section2 Severity Levels and the Icon and Pixmap Properties
|
---|
534 |
|
---|
535 | QMessageBox supports four predefined message severity levels, or message
|
---|
536 | types, which really only differ in the predefined icon they each show.
|
---|
537 | Specify one of the four predefined message types by setting the
|
---|
538 | \l{QMessageBox::icon}{icon} property to one of the
|
---|
539 | \l{QMessageBox::Icon}{predefined icons}. The following rules are
|
---|
540 | guidelines:
|
---|
541 |
|
---|
542 | \table
|
---|
543 | \row
|
---|
544 | \o \img qmessagebox-quest.png
|
---|
545 | \o \l Question
|
---|
546 | \o For asking a question during normal operations.
|
---|
547 | \row
|
---|
548 | \o \img qmessagebox-info.png
|
---|
549 | \o \l Information
|
---|
550 | \o For reporting information about normal operations.
|
---|
551 | \row
|
---|
552 | \o \img qmessagebox-warn.png
|
---|
553 | \o \l Warning
|
---|
554 | \o For reporting non-critical errors.
|
---|
555 | \row
|
---|
556 | \o \img qmessagebox-crit.png
|
---|
557 | \o \l Critical
|
---|
558 | \o For reporting critical errors.
|
---|
559 | \endtable
|
---|
560 |
|
---|
561 | \l{QMessageBox::Icon}{Predefined icons} are not defined by QMessageBox, but
|
---|
562 | provided by the style. The default value is \l{QMessageBox::NoIcon}
|
---|
563 | {No Icon}. The message boxes are otherwise the same for all cases. When
|
---|
564 | using a standard icon, use the one recommended in the table, or use the
|
---|
565 | one recommended by the style guidelines for your platform. If none of the
|
---|
566 | standard icons is right for your message box, you can use a custom icon by
|
---|
567 | setting the \l{QMessageBox::iconPixmap}{icon pixmap} property instead of
|
---|
568 | setting the \l{QMessageBox::icon}{icon} property.
|
---|
569 |
|
---|
570 | In summary, to set an icon, use \e{either} setIcon() for one of the
|
---|
571 | standard icons, \e{or} setIconPixmap() for a custom icon.
|
---|
572 |
|
---|
573 | \section1 The Static Functions API
|
---|
574 |
|
---|
575 | Building message boxes with the static functions API, although
|
---|
576 | convenient, is less flexible than using the property-based API,
|
---|
577 | because the static function signatures lack parameters for setting
|
---|
578 | the \l{QMessageBox::informativeText} {informative text} and
|
---|
579 | \l{QMessageBox::detailedText} {detailed text} properties. One
|
---|
580 | work-around for this has been to use the \c{title} parameter as
|
---|
581 | the message box main text and the \c{text} parameter as the
|
---|
582 | message box informative text. Because this has the obvious
|
---|
583 | drawback of making a less readable message box, platform
|
---|
584 | guidelines do not recommend it. The \e{Microsoft Windows User
|
---|
585 | Interface Guidelines} recommend using the
|
---|
586 | \l{QCoreApplication::applicationName} {application name} as the
|
---|
587 | \l{QMessageBox::setWindowTitle()} {window's title}, which means
|
---|
588 | that if you have an informative text in addition to your main
|
---|
589 | text, you must concatenate it to the \c{text} parameter.
|
---|
590 |
|
---|
591 | Note that the static function signatures have changed with respect
|
---|
592 | to their button parameters, which are now used to set the
|
---|
593 | \l{QMessageBox::standardButtons} {standard buttons} and the
|
---|
594 | \l{QMessageBox::defaultButton()} {default button}.
|
---|
595 |
|
---|
596 | Static functions are available for creating information(),
|
---|
597 | question(), warning(), and critical() message boxes.
|
---|
598 |
|
---|
599 | \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 0
|
---|
600 |
|
---|
601 | The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
|
---|
602 | how to use QMessageBox and the other built-in Qt dialogs.
|
---|
603 |
|
---|
604 | \section1 Advanced Usage
|
---|
605 |
|
---|
606 | If the \l{QMessageBox::StandardButtons} {standard buttons} are not
|
---|
607 | flexible enough for your message box, you can use the addButton()
|
---|
608 | overload that takes a text and a ButtonRoleto to add custom
|
---|
609 | buttons. The ButtonRole is used by QMessageBox to determine the
|
---|
610 | ordering of the buttons on screen (which varies according to the
|
---|
611 | platform). You can test the value of clickedButton() after calling
|
---|
612 | exec(). For example,
|
---|
613 |
|
---|
614 | \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 2
|
---|
615 |
|
---|
616 | \section1 Default and Escape Keys
|
---|
617 |
|
---|
618 | The default button (i.e., the button activated when \key Enter is
|
---|
619 | pressed) can be specified using setDefaultButton(). If a default
|
---|
620 | button is not specified, QMessageBox tries to find one based on
|
---|
621 | the \l{ButtonRole} {button roles} of the buttons used in the
|
---|
622 | message box.
|
---|
623 |
|
---|
624 | The escape button (the button activated when \key Esc is pressed)
|
---|
625 | can be specified using setEscapeButton(). If an escape button is
|
---|
626 | not specified, QMessageBox tries to find one using these rules:
|
---|
627 |
|
---|
628 | \list 1
|
---|
629 |
|
---|
630 | \o If there is only one button, it is the button activated when
|
---|
631 | \key Esc is pressed.
|
---|
632 |
|
---|
633 | \o If there is a \l Cancel button, it is the button activated when
|
---|
634 | \key Esc is pressed.
|
---|
635 |
|
---|
636 | \o If there is exactly one button having either
|
---|
637 | \l{QMessageBox::RejectRole} {the Reject role} or the
|
---|
638 | \l{QMessageBox::NoRole} {the No role}, it is the button
|
---|
639 | activated when \key Esc is pressed.
|
---|
640 |
|
---|
641 | \endlist
|
---|
642 |
|
---|
643 | When an escape button can't be determined using these rules,
|
---|
644 | pressing \key Esc has no effect.
|
---|
645 |
|
---|
646 | \sa QDialogButtonBox, {fowler}{GUI Design Handbook: Message Box}, {Standard Dialogs Example}, {Application Example}
|
---|
647 | */
|
---|
648 |
|
---|
649 | /*!
|
---|
650 | \enum QMessageBox::StandardButton
|
---|
651 | \since 4.2
|
---|
652 |
|
---|
653 | These enums describe flags for standard buttons. Each button has a
|
---|
654 | defined \l ButtonRole.
|
---|
655 |
|
---|
656 | \value Ok An "OK" button defined with the \l AcceptRole.
|
---|
657 | \value Open A "Open" button defined with the \l AcceptRole.
|
---|
658 | \value Save A "Save" button defined with the \l AcceptRole.
|
---|
659 | \value Cancel A "Cancel" button defined with the \l RejectRole.
|
---|
660 | \value Close A "Close" button defined with the \l RejectRole.
|
---|
661 | \value Discard A "Discard" or "Don't Save" button, depending on the platform,
|
---|
662 | defined with the \l DestructiveRole.
|
---|
663 | \value Apply An "Apply" button defined with the \l ApplyRole.
|
---|
664 | \value Reset A "Reset" button defined with the \l ResetRole.
|
---|
665 | \value RestoreDefaults A "Restore Defaults" button defined with the \l ResetRole.
|
---|
666 | \value Help A "Help" button defined with the \l HelpRole.
|
---|
667 | \value SaveAll A "Save All" button defined with the \l AcceptRole.
|
---|
668 | \value Yes A "Yes" button defined with the \l YesRole.
|
---|
669 | \value YesToAll A "Yes to All" button defined with the \l YesRole.
|
---|
670 | \value No A "No" button defined with the \l NoRole.
|
---|
671 | \value NoToAll A "No to All" button defined with the \l NoRole.
|
---|
672 | \value Abort An "Abort" button defined with the \l RejectRole.
|
---|
673 | \value Retry A "Retry" button defined with the \l AcceptRole.
|
---|
674 | \value Ignore An "Ignore" button defined with the \l AcceptRole.
|
---|
675 |
|
---|
676 | \value NoButton An invalid button.
|
---|
677 |
|
---|
678 | \omitvalue FirstButton
|
---|
679 | \omitvalue LastButton
|
---|
680 |
|
---|
681 | The following values are obsolete:
|
---|
682 |
|
---|
683 | \value YesAll Use YesToAll instead.
|
---|
684 | \value NoAll Use NoToAll instead.
|
---|
685 | \value Default Use the \c defaultButton argument of
|
---|
686 | information(), warning(), etc. instead, or call
|
---|
687 | setDefaultButton().
|
---|
688 | \value Escape Call setEscapeButton() instead.
|
---|
689 | \value FlagMask
|
---|
690 | \value ButtonMask
|
---|
691 |
|
---|
692 | \sa ButtonRole, standardButtons
|
---|
693 | */
|
---|
694 |
|
---|
695 | /*!
|
---|
696 | \fn void QMessageBox::buttonClicked(QAbstractButton *button)
|
---|
697 |
|
---|
698 | This signal is emitted whenever a button is clicked inside the QMessageBox.
|
---|
699 | The button that was clicked in returned in \a button.
|
---|
700 | */
|
---|
701 |
|
---|
702 | /*!
|
---|
703 | Constructs a message box with no text and no buttons. \a parent is
|
---|
704 | passed to the QDialog constructor.
|
---|
705 |
|
---|
706 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
707 | {application modal} dialog box. If \a parent is a widget, the
|
---|
708 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
709 | parent.
|
---|
710 |
|
---|
711 | On Mac OS X, if \a parent is not 0 and you want your message box
|
---|
712 | to appear as a Qt::Sheet of that parent, set the message box's
|
---|
713 | \l{setWindowModality()} {window modality} to Qt::WindowModal
|
---|
714 | (default). Otherwise, the message box will be a standard dialog.
|
---|
715 |
|
---|
716 | */
|
---|
717 | QMessageBox::QMessageBox(QWidget *parent)
|
---|
718 | : QDialog(*new QMessageBoxPrivate, parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
|
---|
719 | {
|
---|
720 | Q_D(QMessageBox);
|
---|
721 | d->init();
|
---|
722 | }
|
---|
723 |
|
---|
724 | /*!
|
---|
725 | Constructs a message box with the given \a icon, \a title, \a
|
---|
726 | text, and standard \a buttons. Standard or custom buttons can be
|
---|
727 | added at any time using addButton(). The \a parent and \a f
|
---|
728 | arguments are passed to the QDialog constructor.
|
---|
729 |
|
---|
730 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
731 | {application modal} dialog box. If \a parent is a widget, the
|
---|
732 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
733 | parent.
|
---|
734 |
|
---|
735 | On Mac OS X, if \a parent is not 0 and you want your message box
|
---|
736 | to appear as a Qt::Sheet of that parent, set the message box's
|
---|
737 | \l{setWindowModality()} {window modality} to Qt::WindowModal
|
---|
738 | (default). Otherwise, the message box will be a standard dialog.
|
---|
739 |
|
---|
740 | \sa setWindowTitle(), setText(), setIcon(), setStandardButtons()
|
---|
741 | */
|
---|
742 | QMessageBox::QMessageBox(Icon icon, const QString &title, const QString &text,
|
---|
743 | StandardButtons buttons, QWidget *parent,
|
---|
744 | Qt::WindowFlags f)
|
---|
745 | : QDialog(*new QMessageBoxPrivate, parent, f | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
|
---|
746 | {
|
---|
747 | Q_D(QMessageBox);
|
---|
748 | d->init(title, text);
|
---|
749 | setIcon(icon);
|
---|
750 | if (buttons != NoButton)
|
---|
751 | setStandardButtons(buttons);
|
---|
752 | }
|
---|
753 |
|
---|
754 | /*!
|
---|
755 | Destroys the message box.
|
---|
756 | */
|
---|
757 | QMessageBox::~QMessageBox()
|
---|
758 | {
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*!
|
---|
762 | \since 4.2
|
---|
763 |
|
---|
764 | Adds the given \a button to the message box with the specified \a
|
---|
765 | role.
|
---|
766 |
|
---|
767 | \sa removeButton(), button(), setStandardButtons()
|
---|
768 | */
|
---|
769 | void QMessageBox::addButton(QAbstractButton *button, ButtonRole role)
|
---|
770 | {
|
---|
771 | Q_D(QMessageBox);
|
---|
772 | if (!button)
|
---|
773 | return;
|
---|
774 | removeButton(button);
|
---|
775 | d->buttonBox->addButton(button, (QDialogButtonBox::ButtonRole)role);
|
---|
776 | d->customButtonList.append(button);
|
---|
777 | d->autoAddOkButton = false;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /*!
|
---|
781 | \since 4.2
|
---|
782 | \overload
|
---|
783 |
|
---|
784 | Creates a button with the given \a text, adds it to the message box for the
|
---|
785 | specified \a role, and returns it.
|
---|
786 | */
|
---|
787 | QPushButton *QMessageBox::addButton(const QString& text, ButtonRole role)
|
---|
788 | {
|
---|
789 | Q_D(QMessageBox);
|
---|
790 | QPushButton *pushButton = new QPushButton(text);
|
---|
791 | addButton(pushButton, role);
|
---|
792 | d->updateSize();
|
---|
793 | return pushButton;
|
---|
794 | }
|
---|
795 |
|
---|
796 | /*!
|
---|
797 | \since 4.2
|
---|
798 | \overload
|
---|
799 |
|
---|
800 | Adds a standard \a button to the message box if it is valid to do so, and
|
---|
801 | returns the push button.
|
---|
802 |
|
---|
803 | \sa setStandardButtons()
|
---|
804 | */
|
---|
805 | QPushButton *QMessageBox::addButton(StandardButton button)
|
---|
806 | {
|
---|
807 | Q_D(QMessageBox);
|
---|
808 | QPushButton *pushButton = d->buttonBox->addButton((QDialogButtonBox::StandardButton)button);
|
---|
809 | if (pushButton)
|
---|
810 | d->autoAddOkButton = false;
|
---|
811 | return pushButton;
|
---|
812 | }
|
---|
813 |
|
---|
814 | /*!
|
---|
815 | \since 4.2
|
---|
816 |
|
---|
817 | Removes \a button from the button box without deleting it.
|
---|
818 |
|
---|
819 | \sa addButton(), setStandardButtons()
|
---|
820 | */
|
---|
821 | void QMessageBox::removeButton(QAbstractButton *button)
|
---|
822 | {
|
---|
823 | Q_D(QMessageBox);
|
---|
824 | d->customButtonList.removeAll(button);
|
---|
825 | if (d->escapeButton == button)
|
---|
826 | d->escapeButton = 0;
|
---|
827 | if (d->defaultButton == button)
|
---|
828 | d->defaultButton = 0;
|
---|
829 | d->buttonBox->removeButton(button);
|
---|
830 | d->updateSize();
|
---|
831 | }
|
---|
832 |
|
---|
833 | /*!
|
---|
834 | \property QMessageBox::standardButtons
|
---|
835 | \brief collection of standard buttons in the message box
|
---|
836 | \since 4.2
|
---|
837 |
|
---|
838 | This property controls which standard buttons are used by the message box.
|
---|
839 |
|
---|
840 | By default, this property contains no standard buttons.
|
---|
841 |
|
---|
842 | \sa addButton()
|
---|
843 | */
|
---|
844 | void QMessageBox::setStandardButtons(StandardButtons buttons)
|
---|
845 | {
|
---|
846 | Q_D(QMessageBox);
|
---|
847 | d->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
|
---|
848 |
|
---|
849 | QList<QAbstractButton *> buttonList = d->buttonBox->buttons();
|
---|
850 | if (!buttonList.contains(d->escapeButton))
|
---|
851 | d->escapeButton = 0;
|
---|
852 | if (!buttonList.contains(d->defaultButton))
|
---|
853 | d->defaultButton = 0;
|
---|
854 | d->autoAddOkButton = false;
|
---|
855 | d->updateSize();
|
---|
856 | }
|
---|
857 |
|
---|
858 | QMessageBox::StandardButtons QMessageBox::standardButtons() const
|
---|
859 | {
|
---|
860 | Q_D(const QMessageBox);
|
---|
861 | return QMessageBox::StandardButtons(int(d->buttonBox->standardButtons()));
|
---|
862 | }
|
---|
863 |
|
---|
864 | /*!
|
---|
865 | \since 4.2
|
---|
866 |
|
---|
867 | Returns the standard button enum value corresponding to the given \a button,
|
---|
868 | or NoButton if the given \a button isn't a standard button.
|
---|
869 |
|
---|
870 | \sa button(), standardButtons()
|
---|
871 | */
|
---|
872 | QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button) const
|
---|
873 | {
|
---|
874 | Q_D(const QMessageBox);
|
---|
875 | return (QMessageBox::StandardButton)d->buttonBox->standardButton(button);
|
---|
876 | }
|
---|
877 |
|
---|
878 | /*!
|
---|
879 | \since 4.2
|
---|
880 |
|
---|
881 | Returns a pointer corresponding to the standard button \a which,
|
---|
882 | or 0 if the standard button doesn't exist in this message box.
|
---|
883 |
|
---|
884 | \sa standardButtons, standardButton()
|
---|
885 | */
|
---|
886 | QAbstractButton *QMessageBox::button(StandardButton which) const
|
---|
887 | {
|
---|
888 | Q_D(const QMessageBox);
|
---|
889 | return d->buttonBox->button(QDialogButtonBox::StandardButton(which));
|
---|
890 | }
|
---|
891 |
|
---|
892 | /*!
|
---|
893 | \since 4.2
|
---|
894 |
|
---|
895 | Returns the button that is activated when escape is pressed.
|
---|
896 |
|
---|
897 | By default, QMessageBox attempts to automatically detect an
|
---|
898 | escape button as follows:
|
---|
899 |
|
---|
900 | \list 1
|
---|
901 | \o If there is only one button, it is made the escape button.
|
---|
902 | \o If there is a \l Cancel button, it is made the escape button.
|
---|
903 | \o On Mac OS X only, if there is exactly one button with the role
|
---|
904 | QMessageBox::RejectRole, it is made the escape button.
|
---|
905 | \endlist
|
---|
906 |
|
---|
907 | When an escape button could not be automatically detected, pressing
|
---|
908 | \key Esc has no effect.
|
---|
909 |
|
---|
910 | \sa addButton()
|
---|
911 | */
|
---|
912 | QAbstractButton *QMessageBox::escapeButton() const
|
---|
913 | {
|
---|
914 | Q_D(const QMessageBox);
|
---|
915 | return d->escapeButton;
|
---|
916 | }
|
---|
917 |
|
---|
918 | /*!
|
---|
919 | \since 4.2
|
---|
920 |
|
---|
921 | Sets the button that gets activated when the \key Escape key is
|
---|
922 | pressed to \a button.
|
---|
923 |
|
---|
924 | \sa addButton(), clickedButton()
|
---|
925 | */
|
---|
926 | void QMessageBox::setEscapeButton(QAbstractButton *button)
|
---|
927 | {
|
---|
928 | Q_D(QMessageBox);
|
---|
929 | if (d->buttonBox->buttons().contains(button))
|
---|
930 | d->escapeButton = button;
|
---|
931 | }
|
---|
932 |
|
---|
933 | /*!
|
---|
934 | \since 4.3
|
---|
935 |
|
---|
936 | Sets the buttons that gets activated when the \key Escape key is
|
---|
937 | pressed to \a button.
|
---|
938 |
|
---|
939 | \sa addButton(), clickedButton()
|
---|
940 | */
|
---|
941 | void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)
|
---|
942 | {
|
---|
943 | Q_D(QMessageBox);
|
---|
944 | setEscapeButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
|
---|
945 | }
|
---|
946 |
|
---|
947 | void QMessageBoxPrivate::detectEscapeButton()
|
---|
948 | {
|
---|
949 | if (escapeButton) { // escape button explicitly set
|
---|
950 | detectedEscapeButton = escapeButton;
|
---|
951 | return;
|
---|
952 | }
|
---|
953 |
|
---|
954 | // Cancel button automatically becomes escape button
|
---|
955 | detectedEscapeButton = buttonBox->button(QDialogButtonBox::Cancel);
|
---|
956 | if (detectedEscapeButton)
|
---|
957 | return;
|
---|
958 |
|
---|
959 | // If there is only one button, make it the escape button
|
---|
960 | const QList<QAbstractButton *> buttons = buttonBox->buttons();
|
---|
961 | if (buttons.count() == 1) {
|
---|
962 | detectedEscapeButton = buttons.first();
|
---|
963 | return;
|
---|
964 | }
|
---|
965 |
|
---|
966 | // if the message box has one RejectRole button, make it the escape button
|
---|
967 | for (int i = 0; i < buttons.count(); i++) {
|
---|
968 | if (buttonBox->buttonRole(buttons.at(i)) == QDialogButtonBox::RejectRole) {
|
---|
969 | if (detectedEscapeButton) { // already detected!
|
---|
970 | detectedEscapeButton = 0;
|
---|
971 | break;
|
---|
972 | }
|
---|
973 | detectedEscapeButton = buttons.at(i);
|
---|
974 | }
|
---|
975 | }
|
---|
976 | if (detectedEscapeButton)
|
---|
977 | return;
|
---|
978 |
|
---|
979 | // if the message box has one NoRole button, make it the escape button
|
---|
980 | for (int i = 0; i < buttons.count(); i++) {
|
---|
981 | if (buttonBox->buttonRole(buttons.at(i)) == QDialogButtonBox::NoRole) {
|
---|
982 | if (detectedEscapeButton) { // already detected!
|
---|
983 | detectedEscapeButton = 0;
|
---|
984 | break;
|
---|
985 | }
|
---|
986 | detectedEscapeButton = buttons.at(i);
|
---|
987 | }
|
---|
988 | }
|
---|
989 | }
|
---|
990 |
|
---|
991 | /*!
|
---|
992 | \since 4.2
|
---|
993 |
|
---|
994 | Returns the button that was clicked by the user,
|
---|
995 | or 0 if the user hit the \key Esc key and
|
---|
996 | no \l{setEscapeButton()}{escape button} was set.
|
---|
997 |
|
---|
998 | If exec() hasn't been called yet, returns 0.
|
---|
999 |
|
---|
1000 | Example:
|
---|
1001 |
|
---|
1002 | \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 3
|
---|
1003 |
|
---|
1004 | \sa standardButton(), button()
|
---|
1005 | */
|
---|
1006 | QAbstractButton *QMessageBox::clickedButton() const
|
---|
1007 | {
|
---|
1008 | Q_D(const QMessageBox);
|
---|
1009 | return d->clickedButton;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | /*!
|
---|
1013 | \since 4.2
|
---|
1014 |
|
---|
1015 | Returns the button that should be the message box's
|
---|
1016 | \l{QPushButton::setDefault()}{default button}. Returns 0
|
---|
1017 | if no default button was set.
|
---|
1018 |
|
---|
1019 | \sa addButton(), QPushButton::setDefault()
|
---|
1020 | */
|
---|
1021 | QPushButton *QMessageBox::defaultButton() const
|
---|
1022 | {
|
---|
1023 | Q_D(const QMessageBox);
|
---|
1024 | return d->defaultButton;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | /*!
|
---|
1028 | \since 4.2
|
---|
1029 |
|
---|
1030 | Sets the message box's \l{QPushButton::setDefault()}{default button}
|
---|
1031 | to \a button.
|
---|
1032 |
|
---|
1033 | \sa addButton(), QPushButton::setDefault()
|
---|
1034 | */
|
---|
1035 | void QMessageBox::setDefaultButton(QPushButton *button)
|
---|
1036 | {
|
---|
1037 | Q_D(QMessageBox);
|
---|
1038 | if (!d->buttonBox->buttons().contains(button))
|
---|
1039 | return;
|
---|
1040 | d->defaultButton = button;
|
---|
1041 | button->setDefault(true);
|
---|
1042 | button->setFocus();
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | /*!
|
---|
1046 | \since 4.3
|
---|
1047 |
|
---|
1048 | Sets the message box's \l{QPushButton::setDefault()}{default button}
|
---|
1049 | to \a button.
|
---|
1050 |
|
---|
1051 | \sa addButton(), QPushButton::setDefault()
|
---|
1052 | */
|
---|
1053 | void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)
|
---|
1054 | {
|
---|
1055 | Q_D(QMessageBox);
|
---|
1056 | setDefaultButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /*!
|
---|
1060 | \property QMessageBox::text
|
---|
1061 | \brief the message box text to be displayed.
|
---|
1062 |
|
---|
1063 | The text will be interpreted either as a plain text or as rich text,
|
---|
1064 | depending on the text format setting (\l QMessageBox::textFormat).
|
---|
1065 | The default setting is Qt::AutoText, i.e., the message box will try
|
---|
1066 | to auto-detect the format of the text.
|
---|
1067 |
|
---|
1068 | The default value of this property is an empty string.
|
---|
1069 |
|
---|
1070 | \sa textFormat, QMessageBox::informativeText, QMessageBox::detailedText
|
---|
1071 | */
|
---|
1072 | QString QMessageBox::text() const
|
---|
1073 | {
|
---|
1074 | Q_D(const QMessageBox);
|
---|
1075 | return d->label->text();
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | void QMessageBox::setText(const QString &text)
|
---|
1079 | {
|
---|
1080 | Q_D(QMessageBox);
|
---|
1081 | d->label->setText(text);
|
---|
1082 | d->label->setWordWrap(d->label->textFormat() == Qt::RichText
|
---|
1083 | || (d->label->textFormat() == Qt::AutoText && Qt::mightBeRichText(text)));
|
---|
1084 | d->updateSize();
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | /*!
|
---|
1088 | \enum QMessageBox::Icon
|
---|
1089 |
|
---|
1090 | This enum has the following values:
|
---|
1091 |
|
---|
1092 | \value NoIcon the message box does not have any icon.
|
---|
1093 |
|
---|
1094 | \value Question an icon indicating that
|
---|
1095 | the message is asking a question.
|
---|
1096 |
|
---|
1097 | \value Information an icon indicating that
|
---|
1098 | the message is nothing out of the ordinary.
|
---|
1099 |
|
---|
1100 | \value Warning an icon indicating that the
|
---|
1101 | message is a warning, but can be dealt with.
|
---|
1102 |
|
---|
1103 | \value Critical an icon indicating that
|
---|
1104 | the message represents a critical problem.
|
---|
1105 |
|
---|
1106 | */
|
---|
1107 |
|
---|
1108 | /*!
|
---|
1109 | \property QMessageBox::icon
|
---|
1110 | \brief the message box's icon
|
---|
1111 |
|
---|
1112 | The icon of the message box can be specified with one of the
|
---|
1113 | values:
|
---|
1114 |
|
---|
1115 | \list
|
---|
1116 | \o QMessageBox::NoIcon
|
---|
1117 | \o QMessageBox::Question
|
---|
1118 | \o QMessageBox::Information
|
---|
1119 | \o QMessageBox::Warning
|
---|
1120 | \o QMessageBox::Critical
|
---|
1121 | \endlist
|
---|
1122 |
|
---|
1123 | The default is QMessageBox::NoIcon.
|
---|
1124 |
|
---|
1125 | The pixmap used to display the actual icon depends on the current
|
---|
1126 | \l{QWidget::style()} {GUI style}. You can also set a custom pixmap
|
---|
1127 | for the icon by setting the \l{QMessageBox::iconPixmap} {icon
|
---|
1128 | pixmap} property.
|
---|
1129 |
|
---|
1130 | \sa iconPixmap
|
---|
1131 | */
|
---|
1132 | QMessageBox::Icon QMessageBox::icon() const
|
---|
1133 | {
|
---|
1134 | Q_D(const QMessageBox);
|
---|
1135 | return d->icon;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | void QMessageBox::setIcon(Icon icon)
|
---|
1139 | {
|
---|
1140 | Q_D(QMessageBox);
|
---|
1141 | setIconPixmap(QMessageBoxPrivate::standardIcon((QMessageBox::Icon)icon,
|
---|
1142 | this));
|
---|
1143 | d->icon = icon;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /*!
|
---|
1147 | \property QMessageBox::iconPixmap
|
---|
1148 | \brief the current icon
|
---|
1149 |
|
---|
1150 | The icon currently used by the message box. Note that it's often
|
---|
1151 | hard to draw one pixmap that looks appropriate in all GUI styles;
|
---|
1152 | you may want to supply a different pixmap for each platform.
|
---|
1153 |
|
---|
1154 | By default, this property is undefined.
|
---|
1155 |
|
---|
1156 | \sa icon
|
---|
1157 | */
|
---|
1158 | QPixmap QMessageBox::iconPixmap() const
|
---|
1159 | {
|
---|
1160 | Q_D(const QMessageBox);
|
---|
1161 | if (d->iconLabel && d->iconLabel->pixmap())
|
---|
1162 | return *d->iconLabel->pixmap();
|
---|
1163 | return QPixmap();
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | void QMessageBox::setIconPixmap(const QPixmap &pixmap)
|
---|
1167 | {
|
---|
1168 | Q_D(QMessageBox);
|
---|
1169 | d->iconLabel->setPixmap(pixmap);
|
---|
1170 | d->updateSize();
|
---|
1171 | d->icon = NoIcon;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | /*!
|
---|
1175 | \property QMessageBox::textFormat
|
---|
1176 | \brief the format of the text displayed by the message box
|
---|
1177 |
|
---|
1178 | The current text format used by the message box. See the \l
|
---|
1179 | Qt::TextFormat enum for an explanation of the possible options.
|
---|
1180 |
|
---|
1181 | The default format is Qt::AutoText.
|
---|
1182 |
|
---|
1183 | \sa setText()
|
---|
1184 | */
|
---|
1185 | Qt::TextFormat QMessageBox::textFormat() const
|
---|
1186 | {
|
---|
1187 | Q_D(const QMessageBox);
|
---|
1188 | return d->label->textFormat();
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | void QMessageBox::setTextFormat(Qt::TextFormat format)
|
---|
1192 | {
|
---|
1193 | Q_D(QMessageBox);
|
---|
1194 | d->label->setTextFormat(format);
|
---|
1195 | d->label->setWordWrap(format == Qt::RichText
|
---|
1196 | || (format == Qt::AutoText && Qt::mightBeRichText(d->label->text())));
|
---|
1197 | d->updateSize();
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | /*!
|
---|
1201 | \reimp
|
---|
1202 | */
|
---|
1203 | bool QMessageBox::event(QEvent *e)
|
---|
1204 | {
|
---|
1205 | bool result =QDialog::event(e);
|
---|
1206 | switch (e->type()) {
|
---|
1207 | case QEvent::LayoutRequest:
|
---|
1208 | d_func()->updateSize();
|
---|
1209 | break;
|
---|
1210 | case QEvent::LanguageChange:
|
---|
1211 | d_func()->retranslateStrings();
|
---|
1212 | break;
|
---|
1213 | #ifdef Q_OS_WINCE
|
---|
1214 | case QEvent::OkRequest:
|
---|
1215 | case QEvent::HelpRequest: {
|
---|
1216 | QString bName =
|
---|
1217 | (e->type() == QEvent::OkRequest)
|
---|
1218 | ? qApp->translate("QMessageBox", "OK")
|
---|
1219 | : qApp->translate("QMessageBox", "Help");
|
---|
1220 | QList<QPushButton*> list = qFindChildren<QPushButton*>(this);
|
---|
1221 | for (int i=0; i<list.size(); ++i) {
|
---|
1222 | QPushButton *pb = list.at(i);
|
---|
1223 | if (pb->text() == bName) {
|
---|
1224 | if (pb->isEnabled())
|
---|
1225 | pb->click();
|
---|
1226 | return pb->isEnabled();
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 | }
|
---|
1230 | #endif
|
---|
1231 | default:
|
---|
1232 | break;
|
---|
1233 | }
|
---|
1234 | return result;
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | /*!
|
---|
1238 | \reimp
|
---|
1239 | */
|
---|
1240 | void QMessageBox::resizeEvent(QResizeEvent *event)
|
---|
1241 | {
|
---|
1242 | QDialog::resizeEvent(event);
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | /*!
|
---|
1246 | \reimp
|
---|
1247 | */
|
---|
1248 | void QMessageBox::closeEvent(QCloseEvent *e)
|
---|
1249 | {
|
---|
1250 | Q_D(QMessageBox);
|
---|
1251 | if (!d->detectedEscapeButton) {
|
---|
1252 | e->ignore();
|
---|
1253 | return;
|
---|
1254 | }
|
---|
1255 | QDialog::closeEvent(e);
|
---|
1256 | d->clickedButton = d->detectedEscapeButton;
|
---|
1257 | setResult(d->execReturnCode(d->detectedEscapeButton));
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /*!
|
---|
1261 | \reimp
|
---|
1262 | */
|
---|
1263 | void QMessageBox::changeEvent(QEvent *ev)
|
---|
1264 | {
|
---|
1265 | Q_D(QMessageBox);
|
---|
1266 | switch (ev->type()) {
|
---|
1267 | case QEvent::StyleChange:
|
---|
1268 | {
|
---|
1269 | if (d->icon != NoIcon)
|
---|
1270 | setIcon(d->icon);
|
---|
1271 | Qt::TextInteractionFlags flags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, this));
|
---|
1272 | d->label->setTextInteractionFlags(flags);
|
---|
1273 | d->buttonBox->setCenterButtons(style()->styleHint(QStyle::SH_MessageBox_CenterButtons, 0, this));
|
---|
1274 | if (d->informativeLabel)
|
---|
1275 | d->informativeLabel->setTextInteractionFlags(flags);
|
---|
1276 | // intentional fall through
|
---|
1277 | }
|
---|
1278 | case QEvent::FontChange:
|
---|
1279 | case QEvent::ApplicationFontChange:
|
---|
1280 | #ifdef Q_WS_MAC
|
---|
1281 | {
|
---|
1282 | QFont f = font();
|
---|
1283 | f.setBold(true);
|
---|
1284 | d->label->setFont(f);
|
---|
1285 | }
|
---|
1286 | #endif
|
---|
1287 | default:
|
---|
1288 | break;
|
---|
1289 | }
|
---|
1290 | QDialog::changeEvent(ev);
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /*!
|
---|
1294 | \reimp
|
---|
1295 | */
|
---|
1296 | void QMessageBox::keyPressEvent(QKeyEvent *e)
|
---|
1297 | {
|
---|
1298 | Q_D(QMessageBox);
|
---|
1299 | if (e->key() == Qt::Key_Escape
|
---|
1300 | #ifdef Q_WS_MAC
|
---|
1301 | || (e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period)
|
---|
1302 | #endif
|
---|
1303 | ) {
|
---|
1304 | if (d->detectedEscapeButton) {
|
---|
1305 | #ifdef Q_WS_MAC
|
---|
1306 | d->detectedEscapeButton->animateClick();
|
---|
1307 | #else
|
---|
1308 | d->detectedEscapeButton->click();
|
---|
1309 | #endif
|
---|
1310 | }
|
---|
1311 | return;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | #if defined (Q_OS_WIN) && !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
|
---|
1315 | if (e == QKeySequence::Copy) {
|
---|
1316 | QString separator = QString::fromLatin1("---------------------------\n");
|
---|
1317 | QString textToCopy = separator;
|
---|
1318 | separator.prepend(QLatin1String("\n"));
|
---|
1319 | textToCopy += windowTitle() + separator; // title
|
---|
1320 | textToCopy += d->label->text() + separator; // text
|
---|
1321 |
|
---|
1322 | if (d->informativeLabel)
|
---|
1323 | textToCopy += d->informativeLabel->text() + separator;
|
---|
1324 |
|
---|
1325 | QString buttonTexts;
|
---|
1326 | QList<QAbstractButton *> buttons = d->buttonBox->buttons();
|
---|
1327 | for (int i = 0; i < buttons.count(); i++) {
|
---|
1328 | buttonTexts += buttons[i]->text() + QLatin1String(" ");
|
---|
1329 | }
|
---|
1330 | textToCopy += buttonTexts + separator;
|
---|
1331 |
|
---|
1332 | qApp->clipboard()->setText(textToCopy);
|
---|
1333 | return;
|
---|
1334 | }
|
---|
1335 | #endif //QT_NO_SHORTCUT QT_NO_CLIPBOARD Q_OS_WIN
|
---|
1336 |
|
---|
1337 | #ifndef QT_NO_SHORTCUT
|
---|
1338 | if (!(e->modifiers() & Qt::AltModifier)) {
|
---|
1339 | int key = e->key() & ~((int)Qt::MODIFIER_MASK|(int)Qt::UNICODE_ACCEL);
|
---|
1340 | if (key) {
|
---|
1341 | const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
|
---|
1342 | for (int i = 0; i < buttons.count(); ++i) {
|
---|
1343 | QAbstractButton *pb = buttons.at(i);
|
---|
1344 | int acc = pb->shortcut() & ~((int)Qt::MODIFIER_MASK|(int)Qt::UNICODE_ACCEL);
|
---|
1345 | if (acc == key) {
|
---|
1346 | pb->animateClick();
|
---|
1347 | return;
|
---|
1348 | }
|
---|
1349 | }
|
---|
1350 | }
|
---|
1351 | }
|
---|
1352 | #endif
|
---|
1353 | QDialog::keyPressEvent(e);
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | #ifdef Q_OS_WINCE
|
---|
1357 | /*!
|
---|
1358 | \reimp
|
---|
1359 | */
|
---|
1360 | void QMessageBox::setVisible(bool visible)
|
---|
1361 | {
|
---|
1362 | Q_D(QMessageBox);
|
---|
1363 | if (visible)
|
---|
1364 | d->hideSpecial();
|
---|
1365 | QDialog::setVisible(visible);
|
---|
1366 | }
|
---|
1367 | #endif
|
---|
1368 |
|
---|
1369 |
|
---|
1370 | /*!
|
---|
1371 | \overload
|
---|
1372 |
|
---|
1373 | Opens the dialog and connects its accepted() signal to the slot specified
|
---|
1374 | by \a receiver and \a member.
|
---|
1375 |
|
---|
1376 | The signal will be disconnected from the slot when the dialog is closed.
|
---|
1377 | */
|
---|
1378 | void QMessageBox::open(QObject *receiver, const char *member)
|
---|
1379 | {
|
---|
1380 | Q_D(QMessageBox);
|
---|
1381 | const char *signal = member && strchr(member, '*') ? SIGNAL(buttonClicked(QAbstractButton*))
|
---|
1382 | : SIGNAL(finished(int));
|
---|
1383 | connect(this, signal, receiver, member);
|
---|
1384 | d->signalToDisconnectOnClose = signal;
|
---|
1385 | d->receiverToDisconnectOnClose = receiver;
|
---|
1386 | d->memberToDisconnectOnClose = member;
|
---|
1387 | QDialog::open();
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | /*!
|
---|
1391 | \since 4.5
|
---|
1392 |
|
---|
1393 | Returns a list of all the buttons that have been added to the message box.
|
---|
1394 |
|
---|
1395 | \sa buttonRole(), addButton(), removeButton()
|
---|
1396 | */
|
---|
1397 | QList<QAbstractButton *> QMessageBox::buttons() const
|
---|
1398 | {
|
---|
1399 | Q_D(const QMessageBox);
|
---|
1400 | return d->buttonBox->buttons();
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | /*!
|
---|
1404 | \since 4.5
|
---|
1405 |
|
---|
1406 | Returns the button role for the specified \a button. This function returns
|
---|
1407 | \l InvalidRole if \a button is 0 or has not been added to the message box.
|
---|
1408 |
|
---|
1409 | \sa buttons(), addButton()
|
---|
1410 | */
|
---|
1411 | QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button) const
|
---|
1412 | {
|
---|
1413 | Q_D(const QMessageBox);
|
---|
1414 | return QMessageBox::ButtonRole(d->buttonBox->buttonRole(button));
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | /*!
|
---|
1418 | \reimp
|
---|
1419 | */
|
---|
1420 | void QMessageBox::showEvent(QShowEvent *e)
|
---|
1421 | {
|
---|
1422 | Q_D(QMessageBox);
|
---|
1423 | if (d->autoAddOkButton) {
|
---|
1424 | addButton(Ok);
|
---|
1425 | #if defined(Q_OS_WINCE)
|
---|
1426 | d->hideSpecial();
|
---|
1427 | #endif
|
---|
1428 | }
|
---|
1429 | if (d->detailsButton)
|
---|
1430 | addButton(d->detailsButton, QMessageBox::ActionRole);
|
---|
1431 | d->detectEscapeButton();
|
---|
1432 | d->updateSize();
|
---|
1433 |
|
---|
1434 | #ifndef QT_NO_ACCESSIBILITY
|
---|
1435 | QAccessible::updateAccessibility(this, 0, QAccessible::Alert);
|
---|
1436 | #endif
|
---|
1437 | #ifdef Q_WS_WIN
|
---|
1438 | HMENU systemMenu = GetSystemMenu((HWND)winId(), FALSE);
|
---|
1439 | if (!d->detectedEscapeButton) {
|
---|
1440 | EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED);
|
---|
1441 | }
|
---|
1442 | else {
|
---|
1443 | EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND|MF_ENABLED);
|
---|
1444 | }
|
---|
1445 | #endif
|
---|
1446 | QDialog::showEvent(e);
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 |
|
---|
1450 | static QMessageBox::StandardButton showNewMessageBox(QWidget *parent,
|
---|
1451 | QMessageBox::Icon icon,
|
---|
1452 | const QString& title, const QString& text,
|
---|
1453 | QMessageBox::StandardButtons buttons,
|
---|
1454 | QMessageBox::StandardButton defaultButton)
|
---|
1455 | {
|
---|
1456 | // necessary for source compatibility with Qt 4.0 and 4.1
|
---|
1457 | // handles (Yes, No) and (Yes|Default, No)
|
---|
1458 | if (defaultButton && !(buttons & defaultButton))
|
---|
1459 | return (QMessageBox::StandardButton)
|
---|
1460 | QMessageBoxPrivate::showOldMessageBox(parent, icon, title,
|
---|
1461 | text, int(buttons),
|
---|
1462 | int(defaultButton), 0);
|
---|
1463 |
|
---|
1464 | QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
|
---|
1465 | QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&msgBox);
|
---|
1466 | Q_ASSERT(buttonBox != 0);
|
---|
1467 |
|
---|
1468 | uint mask = QMessageBox::FirstButton;
|
---|
1469 | while (mask <= QMessageBox::LastButton) {
|
---|
1470 | uint sb = buttons & mask;
|
---|
1471 | mask <<= 1;
|
---|
1472 | if (!sb)
|
---|
1473 | continue;
|
---|
1474 | QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
|
---|
1475 | // Choose the first accept role as the default
|
---|
1476 | if (msgBox.defaultButton())
|
---|
1477 | continue;
|
---|
1478 | if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
|
---|
1479 | || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
|
---|
1480 | msgBox.setDefaultButton(button);
|
---|
1481 | }
|
---|
1482 | if (msgBox.exec() == -1)
|
---|
1483 | return QMessageBox::Cancel;
|
---|
1484 | return msgBox.standardButton(msgBox.clickedButton());
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /*!
|
---|
1488 | \since 4.2
|
---|
1489 |
|
---|
1490 | Opens an information message box with the specified \a title and
|
---|
1491 | \a text. The standard \a buttons are added to the message box. \a
|
---|
1492 | defaultButton specifies the button used when \key Enter is
|
---|
1493 | pressed. \a defaultButton must refer to a button that was given in \a buttons.
|
---|
1494 | If \a defaultButton is QMessageBox::NoButton, QMessageBox
|
---|
1495 | chooses a suitable default automatically.
|
---|
1496 |
|
---|
1497 | Returns the identity of the standard button that was clicked. If
|
---|
1498 | \key Esc was pressed instead, the \l{Default and Escape Keys}
|
---|
1499 | {escape button} is returned.
|
---|
1500 |
|
---|
1501 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
1502 | {application modal} dialog box. If \a parent is a widget, the
|
---|
1503 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
1504 | parent.
|
---|
1505 |
|
---|
1506 | \sa question(), warning(), critical()
|
---|
1507 | */
|
---|
1508 | QMessageBox::StandardButton QMessageBox::information(QWidget *parent, const QString &title,
|
---|
1509 | const QString& text, StandardButtons buttons,
|
---|
1510 | StandardButton defaultButton)
|
---|
1511 | {
|
---|
1512 | return showNewMessageBox(parent, Information, title, text, buttons,
|
---|
1513 | defaultButton);
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 |
|
---|
1517 | /*!
|
---|
1518 | \since 4.2
|
---|
1519 |
|
---|
1520 | Opens a question message box with the specified \a title and \a
|
---|
1521 | text. The standard \a buttons are added to the message box. \a
|
---|
1522 | defaultButton specifies the button used when \key Enter is
|
---|
1523 | pressed. \a defaultButton must refer to a button that was given in \a buttons.
|
---|
1524 | If \a defaultButton is QMessageBox::NoButton, QMessageBox
|
---|
1525 | chooses a suitable default automatically.
|
---|
1526 |
|
---|
1527 | Returns the identity of the standard button that was clicked. If
|
---|
1528 | \key Esc was pressed instead, the \l{Default and Escape Keys}
|
---|
1529 | {escape button} is returned.
|
---|
1530 |
|
---|
1531 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
1532 | {application modal} dialog box. If \a parent is a widget, the
|
---|
1533 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
1534 | parent.
|
---|
1535 |
|
---|
1536 | \sa information(), warning(), critical()
|
---|
1537 | */
|
---|
1538 | QMessageBox::StandardButton QMessageBox::question(QWidget *parent, const QString &title,
|
---|
1539 | const QString& text, StandardButtons buttons,
|
---|
1540 | StandardButton defaultButton)
|
---|
1541 | {
|
---|
1542 | return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | /*!
|
---|
1546 | \since 4.2
|
---|
1547 |
|
---|
1548 | Opens a warning message box with the specified \a title and \a
|
---|
1549 | text. The standard \a buttons are added to the message box. \a
|
---|
1550 | defaultButton specifies the button used when \key Enter is
|
---|
1551 | pressed. \a defaultButton must refer to a button that was given in \a buttons.
|
---|
1552 | If \a defaultButton is QMessageBox::NoButton, QMessageBox
|
---|
1553 | chooses a suitable default automatically.
|
---|
1554 |
|
---|
1555 | Returns the identity of the standard button that was clicked. If
|
---|
1556 | \key Esc was pressed instead, the \l{Default and Escape Keys}
|
---|
1557 | {escape button} is returned.
|
---|
1558 |
|
---|
1559 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
1560 | {application modal} dialog box. If \a parent is a widget, the
|
---|
1561 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
1562 | parent.
|
---|
1563 |
|
---|
1564 | \sa question(), information(), critical()
|
---|
1565 | */
|
---|
1566 | QMessageBox::StandardButton QMessageBox::warning(QWidget *parent, const QString &title,
|
---|
1567 | const QString& text, StandardButtons buttons,
|
---|
1568 | StandardButton defaultButton)
|
---|
1569 | {
|
---|
1570 | return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | /*!
|
---|
1574 | \since 4.2
|
---|
1575 |
|
---|
1576 | Opens a critical message box with the specified \a title and \a
|
---|
1577 | text. The standard \a buttons are added to the message box. \a
|
---|
1578 | defaultButton specifies the button used when \key Enter is
|
---|
1579 | pressed. \a defaultButton must refer to a button that was given in \a buttons.
|
---|
1580 | If \a defaultButton is QMessageBox::NoButton, QMessageBox
|
---|
1581 | chooses a suitable default automatically.
|
---|
1582 |
|
---|
1583 | Returns the identity of the standard button that was clicked. If
|
---|
1584 | \key Esc was pressed instead, the \l{Default and Escape Keys}
|
---|
1585 | {escape button} is returned.
|
---|
1586 |
|
---|
1587 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
1588 | {application modal} dialog box. If \a parent is a widget, the
|
---|
1589 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
1590 | parent.
|
---|
1591 |
|
---|
1592 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
1593 | If you want to do this, you should create the dialog
|
---|
1594 | yourself using one of the QMessageBox constructors.
|
---|
1595 |
|
---|
1596 | \sa question(), warning(), information()
|
---|
1597 | */
|
---|
1598 | QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title,
|
---|
1599 | const QString& text, StandardButtons buttons,
|
---|
1600 | StandardButton defaultButton)
|
---|
1601 | {
|
---|
1602 | return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 | /*!
|
---|
1606 | Displays a simple about box with title \a title and text \a
|
---|
1607 | text. The about box's parent is \a parent.
|
---|
1608 |
|
---|
1609 | about() looks for a suitable icon in four locations:
|
---|
1610 |
|
---|
1611 | \list 1
|
---|
1612 | \o It prefers \link QWidget::windowIcon() parent->icon() \endlink
|
---|
1613 | if that exists.
|
---|
1614 | \o If not, it tries the top-level widget containing \a parent.
|
---|
1615 | \o If that fails, it tries the \link
|
---|
1616 | QApplication::activeWindow() active window. \endlink
|
---|
1617 | \o As a last resort it uses the Information icon.
|
---|
1618 | \endlist
|
---|
1619 |
|
---|
1620 | The about box has a single button labelled "OK". On Mac OS X, the
|
---|
1621 | about box is popped up as a modeless window; on other platforms,
|
---|
1622 | it is currently a window modal.
|
---|
1623 |
|
---|
1624 | \sa QWidget::windowIcon(), QApplication::activeWindow()
|
---|
1625 | */
|
---|
1626 | void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
|
---|
1627 | {
|
---|
1628 | #ifdef Q_WS_MAC
|
---|
1629 | static QPointer<QMessageBox> oldMsgBox;
|
---|
1630 |
|
---|
1631 | if (oldMsgBox && oldMsgBox->text() == text) {
|
---|
1632 | oldMsgBox->show();
|
---|
1633 | oldMsgBox->raise();
|
---|
1634 | oldMsgBox->activateWindow();
|
---|
1635 | return;
|
---|
1636 | }
|
---|
1637 | #endif
|
---|
1638 |
|
---|
1639 | QMessageBox *msgBox = new QMessageBox(title, text, Information, 0, 0, 0, parent
|
---|
1640 | #ifdef Q_WS_MAC
|
---|
1641 | , Qt::WindowTitleHint | Qt::WindowSystemMenuHint
|
---|
1642 | #endif
|
---|
1643 | );
|
---|
1644 | msgBox->setAttribute(Qt::WA_DeleteOnClose);
|
---|
1645 | QIcon icon = msgBox->windowIcon();
|
---|
1646 | QSize size = icon.actualSize(QSize(64, 64));
|
---|
1647 | msgBox->setIconPixmap(icon.pixmap(size));
|
---|
1648 |
|
---|
1649 | // should perhaps be a style hint
|
---|
1650 | #ifdef Q_WS_MAC
|
---|
1651 | oldMsgBox = msgBox;
|
---|
1652 | #if 0
|
---|
1653 | // ### doesn't work until close button is enabled in title bar
|
---|
1654 | msgBox->d_func()->autoAddOkButton = false;
|
---|
1655 | #else
|
---|
1656 | msgBox->d_func()->buttonBox->setCenterButtons(true);
|
---|
1657 | #endif
|
---|
1658 | msgBox->show();
|
---|
1659 | #else
|
---|
1660 | msgBox->exec();
|
---|
1661 | #endif
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 | /*!
|
---|
1665 | Displays a simple message box about Qt, with the given \a title
|
---|
1666 | and centered over \a parent (if \a parent is not 0). The message
|
---|
1667 | includes the version number of Qt being used by the application.
|
---|
1668 |
|
---|
1669 | This is useful for inclusion in the \gui Help menu of an application,
|
---|
1670 | as shown in the \l{mainwindows/menus}{Menus} example.
|
---|
1671 |
|
---|
1672 | QApplication provides this functionality as a slot.
|
---|
1673 |
|
---|
1674 | On Mac OS X, the about box is popped up as a modeless window; on
|
---|
1675 | other platforms, it is currently window modal.
|
---|
1676 |
|
---|
1677 | \sa QApplication::aboutQt()
|
---|
1678 | */
|
---|
1679 | void QMessageBox::aboutQt(QWidget *parent, const QString &title)
|
---|
1680 | {
|
---|
1681 | #ifdef Q_WS_MAC
|
---|
1682 | static QPointer<QMessageBox> oldMsgBox;
|
---|
1683 |
|
---|
1684 | if (oldMsgBox) {
|
---|
1685 | oldMsgBox->show();
|
---|
1686 | oldMsgBox->raise();
|
---|
1687 | oldMsgBox->activateWindow();
|
---|
1688 | return;
|
---|
1689 | }
|
---|
1690 | #endif
|
---|
1691 |
|
---|
1692 | QString translatedTextAboutQt;
|
---|
1693 | translatedTextAboutQt = QMessageBox::tr(
|
---|
1694 | "<h3>About Qt</h3>"
|
---|
1695 | "<p>This program uses Qt version %1.</p>"
|
---|
1696 | "<p>Qt is a C++ toolkit for cross-platform application "
|
---|
1697 | "development.</p>"
|
---|
1698 | "<p>Qt provides single-source portability across MS Windows, "
|
---|
1699 | "Mac OS X, Linux, and all major commercial Unix variants. "
|
---|
1700 | "Qt is also available for embedded devices as Qt for Embedded Linux "
|
---|
1701 | "and Qt for Windows CE.</p>"
|
---|
1702 | "<p>Qt is available under three different licensing options designed "
|
---|
1703 | "to accommodate the needs of our various users.</p>"
|
---|
1704 | "Qt licensed under our commercial license agreement is appropriate "
|
---|
1705 | "for development of proprietary/commercial software where you do not "
|
---|
1706 | "want to share any source code with third parties or otherwise cannot "
|
---|
1707 | "comply with the terms of the GNU LGPL version 2.1 or GNU GPL version "
|
---|
1708 | "3.0.</p>"
|
---|
1709 | "<p>Qt licensed under the GNU LGPL version 2.1 is appropriate for the "
|
---|
1710 | "development of Qt applications (proprietary or open source) provided "
|
---|
1711 | "you can comply with the terms and conditions of the GNU LGPL version "
|
---|
1712 | "2.1.</p>"
|
---|
1713 | "<p>Qt licensed under the GNU General Public License version 3.0 is "
|
---|
1714 | "appropriate for the development of Qt applications where you wish to "
|
---|
1715 | "use such applications in combination with software subject to the "
|
---|
1716 | "terms of the GNU GPL version 3.0 or where you are otherwise willing "
|
---|
1717 | "to comply with the terms of the GNU GPL version 3.0.</p>"
|
---|
1718 | "<p>Please see <a href=\"http://www.qtsoftware.com/products/licensing\">www.qtsoftware.com/products/licensing</a> "
|
---|
1719 | "for an overview of Qt licensing.</p>"
|
---|
1720 | "<p>Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).</p>"
|
---|
1721 | "<p>Qt is a Nokia product. See <a href=\"http://www.qtsoftware.com/qt/\">www.qtsoftware.com/qt</a> "
|
---|
1722 | "for more information.</p>"
|
---|
1723 | ).arg(QLatin1String(QT_VERSION_STR));
|
---|
1724 |
|
---|
1725 | QMessageBox *msgBox = new QMessageBox(parent);
|
---|
1726 | msgBox->setAttribute(Qt::WA_DeleteOnClose);
|
---|
1727 | msgBox->setWindowTitle(title.isEmpty() ? tr("About Qt") : title);
|
---|
1728 | msgBox->setText(translatedTextAboutQt);
|
---|
1729 |
|
---|
1730 | QPixmap pm(QLatin1String(":/trolltech/qmessagebox/images/qtlogo-64.png"));
|
---|
1731 | if (!pm.isNull())
|
---|
1732 | msgBox->setIconPixmap(pm);
|
---|
1733 | #if defined(Q_OS_WINCE)
|
---|
1734 | msgBox->setDefaultButton(msgBox->addButton(QMessageBox::Ok));
|
---|
1735 | #endif
|
---|
1736 |
|
---|
1737 | // should perhaps be a style hint
|
---|
1738 | #ifdef Q_WS_MAC
|
---|
1739 | oldMsgBox = msgBox;
|
---|
1740 | #if 0
|
---|
1741 | // ### doesn't work until close button is enabled in title bar
|
---|
1742 | msgBox->d_func()->autoAddOkButton = false;
|
---|
1743 | #else
|
---|
1744 | msgBox->d_func()->buttonBox->setCenterButtons(true);
|
---|
1745 | #endif
|
---|
1746 | msgBox->show();
|
---|
1747 | #else
|
---|
1748 | msgBox->exec();
|
---|
1749 | #endif
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | /*!
|
---|
1753 | \internal
|
---|
1754 | */
|
---|
1755 | QSize QMessageBox::sizeHint() const
|
---|
1756 | {
|
---|
1757 | // ### Qt 5: remove
|
---|
1758 | return QDialog::sizeHint();
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
1762 | // Source and binary compatibility routines for 4.0 and 4.1
|
---|
1763 |
|
---|
1764 | static QMessageBox::StandardButton newButton(int button)
|
---|
1765 | {
|
---|
1766 | // this is needed for source compatibility with Qt 4.0 and 4.1
|
---|
1767 | if (button == QMessageBox::NoButton || (button & NewButtonMask))
|
---|
1768 | return QMessageBox::StandardButton(button & QMessageBox::ButtonMask);
|
---|
1769 |
|
---|
1770 | #if QT_VERSION < 0x050000
|
---|
1771 | // this is needed for binary compatibility with Qt 4.0 and 4.1
|
---|
1772 | switch (button & Old_ButtonMask) {
|
---|
1773 | case Old_Ok:
|
---|
1774 | return QMessageBox::Ok;
|
---|
1775 | case Old_Cancel:
|
---|
1776 | return QMessageBox::Cancel;
|
---|
1777 | case Old_Yes:
|
---|
1778 | return QMessageBox::Yes;
|
---|
1779 | case Old_No:
|
---|
1780 | return QMessageBox::No;
|
---|
1781 | case Old_Abort:
|
---|
1782 | return QMessageBox::Abort;
|
---|
1783 | case Old_Retry:
|
---|
1784 | return QMessageBox::Retry;
|
---|
1785 | case Old_Ignore:
|
---|
1786 | return QMessageBox::Ignore;
|
---|
1787 | case Old_YesAll:
|
---|
1788 | return QMessageBox::YesToAll;
|
---|
1789 | case Old_NoAll:
|
---|
1790 | return QMessageBox::NoToAll;
|
---|
1791 | default:
|
---|
1792 | return QMessageBox::NoButton;
|
---|
1793 | }
|
---|
1794 | #endif
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | static bool detectedCompat(int button0, int button1, int button2)
|
---|
1798 | {
|
---|
1799 | if (button0 != 0 && !(button0 & NewButtonMask))
|
---|
1800 | return true;
|
---|
1801 | if (button1 != 0 && !(button1 & NewButtonMask))
|
---|
1802 | return true;
|
---|
1803 | if (button2 != 0 && !(button2 & NewButtonMask))
|
---|
1804 | return true;
|
---|
1805 | return false;
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | QAbstractButton *QMessageBoxPrivate::findButton(int button0, int button1, int button2, int flags)
|
---|
1809 | {
|
---|
1810 | Q_Q(QMessageBox);
|
---|
1811 | int button = 0;
|
---|
1812 |
|
---|
1813 | if (button0 & flags) {
|
---|
1814 | button = button0;
|
---|
1815 | } else if (button1 & flags) {
|
---|
1816 | button = button1;
|
---|
1817 | } else if (button2 & flags) {
|
---|
1818 | button = button2;
|
---|
1819 | }
|
---|
1820 | return q->button(newButton(button));
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | void QMessageBoxPrivate::addOldButtons(int button0, int button1, int button2)
|
---|
1824 | {
|
---|
1825 | Q_Q(QMessageBox);
|
---|
1826 | q->addButton(newButton(button0));
|
---|
1827 | q->addButton(newButton(button1));
|
---|
1828 | q->addButton(newButton(button2));
|
---|
1829 | q->setDefaultButton(
|
---|
1830 | static_cast<QPushButton *>(findButton(button0, button1, button2, QMessageBox::Default)));
|
---|
1831 | q->setEscapeButton(findButton(button0, button1, button2, QMessageBox::Escape));
|
---|
1832 | compatMode = detectedCompat(button0, button1, button2);
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 | QAbstractButton *QMessageBoxPrivate::abstractButtonForId(int id) const
|
---|
1836 | {
|
---|
1837 | Q_Q(const QMessageBox);
|
---|
1838 | QAbstractButton *result = customButtonList.value(id);
|
---|
1839 | if (result)
|
---|
1840 | return result;
|
---|
1841 | if (id & QMessageBox::FlagMask) // for compatibility with Qt 4.0/4.1 (even if it is silly)
|
---|
1842 | return 0;
|
---|
1843 | return q->button(newButton(id));
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | int QMessageBoxPrivate::showOldMessageBox(QWidget *parent, QMessageBox::Icon icon,
|
---|
1847 | const QString &title, const QString &text,
|
---|
1848 | int button0, int button1, int button2)
|
---|
1849 | {
|
---|
1850 | QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
|
---|
1851 | messageBox.d_func()->addOldButtons(button0, button1, button2);
|
---|
1852 | return messageBox.exec();
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | int QMessageBoxPrivate::showOldMessageBox(QWidget *parent, QMessageBox::Icon icon,
|
---|
1856 | const QString &title, const QString &text,
|
---|
1857 | const QString &button0Text,
|
---|
1858 | const QString &button1Text,
|
---|
1859 | const QString &button2Text,
|
---|
1860 | int defaultButtonNumber,
|
---|
1861 | int escapeButtonNumber)
|
---|
1862 | {
|
---|
1863 | QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
|
---|
1864 | QString myButton0Text = button0Text;
|
---|
1865 | if (myButton0Text.isEmpty())
|
---|
1866 | myButton0Text = QDialogButtonBox::tr("OK");
|
---|
1867 | messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
|
---|
1868 | if (!button1Text.isEmpty())
|
---|
1869 | messageBox.addButton(button1Text, QMessageBox::ActionRole);
|
---|
1870 | if (!button2Text.isEmpty())
|
---|
1871 | messageBox.addButton(button2Text, QMessageBox::ActionRole);
|
---|
1872 |
|
---|
1873 | const QList<QAbstractButton *> &buttonList = messageBox.d_func()->customButtonList;
|
---|
1874 | messageBox.setDefaultButton(static_cast<QPushButton *>(buttonList.value(defaultButtonNumber)));
|
---|
1875 | messageBox.setEscapeButton(buttonList.value(escapeButtonNumber));
|
---|
1876 |
|
---|
1877 | return messageBox.exec();
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 | void QMessageBoxPrivate::retranslateStrings()
|
---|
1881 | {
|
---|
1882 | #ifndef QT_NO_TEXTEDIT
|
---|
1883 | if (detailsButton)
|
---|
1884 | detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel));
|
---|
1885 | #endif
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 | /*!
|
---|
1889 | \obsolete
|
---|
1890 |
|
---|
1891 | Constructs a message box with a \a title, a \a text, an \a icon,
|
---|
1892 | and up to three buttons.
|
---|
1893 |
|
---|
1894 | The \a icon must be one of the following:
|
---|
1895 | \list
|
---|
1896 | \o QMessageBox::NoIcon
|
---|
1897 | \o QMessageBox::Question
|
---|
1898 | \o QMessageBox::Information
|
---|
1899 | \o QMessageBox::Warning
|
---|
1900 | \o QMessageBox::Critical
|
---|
1901 | \endlist
|
---|
1902 |
|
---|
1903 | Each button, \a button0, \a button1 and \a button2, can have one
|
---|
1904 | of the following values:
|
---|
1905 | \list
|
---|
1906 | \o QMessageBox::NoButton
|
---|
1907 | \o QMessageBox::Ok
|
---|
1908 | \o QMessageBox::Cancel
|
---|
1909 | \o QMessageBox::Yes
|
---|
1910 | \o QMessageBox::No
|
---|
1911 | \o QMessageBox::Abort
|
---|
1912 | \o QMessageBox::Retry
|
---|
1913 | \o QMessageBox::Ignore
|
---|
1914 | \o QMessageBox::YesAll
|
---|
1915 | \o QMessageBox::NoAll
|
---|
1916 | \endlist
|
---|
1917 |
|
---|
1918 | Use QMessageBox::NoButton for the later parameters to have fewer
|
---|
1919 | than three buttons in your message box. If you don't specify any
|
---|
1920 | buttons at all, QMessageBox will provide an Ok button.
|
---|
1921 |
|
---|
1922 | One of the buttons can be OR-ed with the QMessageBox::Default
|
---|
1923 | flag to make it the default button (clicked when Enter is
|
---|
1924 | pressed).
|
---|
1925 |
|
---|
1926 | One of the buttons can be OR-ed with the QMessageBox::Escape flag
|
---|
1927 | to make it the cancel or close button (clicked when \key Esc is
|
---|
1928 | pressed).
|
---|
1929 |
|
---|
1930 | \snippet doc/src/snippets/dialogs/dialogs.cpp 2
|
---|
1931 |
|
---|
1932 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
1933 | {application modal} dialog box. If \a parent is a widget, the
|
---|
1934 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
1935 | parent.
|
---|
1936 |
|
---|
1937 | The \a parent and \a f arguments are passed to
|
---|
1938 | the QDialog constructor.
|
---|
1939 |
|
---|
1940 | \sa setWindowTitle(), setText(), setIcon()
|
---|
1941 | */
|
---|
1942 | QMessageBox::QMessageBox(const QString &title, const QString &text, Icon icon,
|
---|
1943 | int button0, int button1, int button2, QWidget *parent,
|
---|
1944 | Qt::WindowFlags f)
|
---|
1945 | : QDialog(*new QMessageBoxPrivate, parent,
|
---|
1946 | f /*| Qt::MSWindowsFixedSizeDialogHint #### */| Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
|
---|
1947 | {
|
---|
1948 | Q_D(QMessageBox);
|
---|
1949 | d->init(title, text);
|
---|
1950 | setIcon(icon);
|
---|
1951 | d->addOldButtons(button0, button1, button2);
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | /*!
|
---|
1955 | \obsolete
|
---|
1956 |
|
---|
1957 | Opens an information message box with the given \a title and the
|
---|
1958 | \a text. The dialog may have up to three buttons. Each of the
|
---|
1959 | buttons, \a button0, \a button1 and \a button2 may be set to one
|
---|
1960 | of the following values:
|
---|
1961 |
|
---|
1962 | \list
|
---|
1963 | \o QMessageBox::NoButton
|
---|
1964 | \o QMessageBox::Ok
|
---|
1965 | \o QMessageBox::Cancel
|
---|
1966 | \o QMessageBox::Yes
|
---|
1967 | \o QMessageBox::No
|
---|
1968 | \o QMessageBox::Abort
|
---|
1969 | \o QMessageBox::Retry
|
---|
1970 | \o QMessageBox::Ignore
|
---|
1971 | \o QMessageBox::YesAll
|
---|
1972 | \o QMessageBox::NoAll
|
---|
1973 | \endlist
|
---|
1974 |
|
---|
1975 | If you don't want all three buttons, set the last button, or last
|
---|
1976 | two buttons to QMessageBox::NoButton.
|
---|
1977 |
|
---|
1978 | One button can be OR-ed with QMessageBox::Default, and one
|
---|
1979 | button can be OR-ed with QMessageBox::Escape.
|
---|
1980 |
|
---|
1981 | Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.)
|
---|
1982 | of the button that was clicked.
|
---|
1983 |
|
---|
1984 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
1985 | {application modal} dialog box. If \a parent is a widget, the
|
---|
1986 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
1987 | parent.
|
---|
1988 |
|
---|
1989 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
1990 | If you want to do this, you should create the dialog
|
---|
1991 | yourself using one of the QMessageBox constructors.
|
---|
1992 |
|
---|
1993 | \sa question(), warning(), critical()
|
---|
1994 | */
|
---|
1995 | int QMessageBox::information(QWidget *parent, const QString &title, const QString& text,
|
---|
1996 | int button0, int button1, int button2)
|
---|
1997 | {
|
---|
1998 | return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
|
---|
1999 | button0, button1, button2);
|
---|
2000 | }
|
---|
2001 |
|
---|
2002 | /*!
|
---|
2003 | \obsolete
|
---|
2004 | \overload
|
---|
2005 |
|
---|
2006 | Displays an information message box with the given \a title and
|
---|
2007 | \a text, as well as one, two or three buttons. Returns the index
|
---|
2008 | of the button that was clicked (0, 1 or 2).
|
---|
2009 |
|
---|
2010 | \a button0Text is the text of the first button, and is optional.
|
---|
2011 | If \a button0Text is not supplied, "OK" (translated) will be
|
---|
2012 | used. \a button1Text is the text of the second button, and is
|
---|
2013 | optional. \a button2Text is the text of the third button, and is
|
---|
2014 | optional. \a defaultButtonNumber (0, 1 or 2) is the index of the
|
---|
2015 | default button; pressing Return or Enter is the same as clicking
|
---|
2016 | the default button. It defaults to 0 (the first button). \a
|
---|
2017 | escapeButtonNumber is the index of the escape button; pressing
|
---|
2018 | \key Esc is the same as clicking this button. It defaults to -1;
|
---|
2019 | supply 0, 1 or 2 to make pressing \key Esc equivalent to clicking
|
---|
2020 | the relevant button.
|
---|
2021 |
|
---|
2022 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2023 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2024 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2025 | parent.
|
---|
2026 |
|
---|
2027 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2028 | If you want to do this, you should create the dialog
|
---|
2029 | yourself using one of the QMessageBox constructors.
|
---|
2030 |
|
---|
2031 | \sa question(), warning(), critical()
|
---|
2032 | */
|
---|
2033 |
|
---|
2034 | int QMessageBox::information(QWidget *parent, const QString &title, const QString& text,
|
---|
2035 | const QString& button0Text, const QString& button1Text,
|
---|
2036 | const QString& button2Text, int defaultButtonNumber,
|
---|
2037 | int escapeButtonNumber)
|
---|
2038 | {
|
---|
2039 | return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
|
---|
2040 | button0Text, button1Text, button2Text,
|
---|
2041 | defaultButtonNumber, escapeButtonNumber);
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | /*!
|
---|
2045 | \obsolete
|
---|
2046 |
|
---|
2047 | Opens a question message box with the given \a title and \a text.
|
---|
2048 | The dialog may have up to three buttons. Each of the buttons, \a
|
---|
2049 | button0, \a button1 and \a button2 may be set to one of the
|
---|
2050 | following values:
|
---|
2051 |
|
---|
2052 | \list
|
---|
2053 | \o QMessageBox::NoButton
|
---|
2054 | \o QMessageBox::Ok
|
---|
2055 | \o QMessageBox::Cancel
|
---|
2056 | \o QMessageBox::Yes
|
---|
2057 | \o QMessageBox::No
|
---|
2058 | \o QMessageBox::Abort
|
---|
2059 | \o QMessageBox::Retry
|
---|
2060 | \o QMessageBox::Ignore
|
---|
2061 | \o QMessageBox::YesAll
|
---|
2062 | \o QMessageBox::NoAll
|
---|
2063 | \endlist
|
---|
2064 |
|
---|
2065 | If you don't want all three buttons, set the last button, or last
|
---|
2066 | two buttons to QMessageBox::NoButton.
|
---|
2067 |
|
---|
2068 | One button can be OR-ed with QMessageBox::Default, and one
|
---|
2069 | button can be OR-ed with QMessageBox::Escape.
|
---|
2070 |
|
---|
2071 | Returns the identity (QMessageBox::Yes, or QMessageBox::No, etc.)
|
---|
2072 | of the button that was clicked.
|
---|
2073 |
|
---|
2074 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2075 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2076 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2077 | parent.
|
---|
2078 |
|
---|
2079 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2080 | If you want to do this, you should create the dialog
|
---|
2081 | yourself using one of the QMessageBox constructors.
|
---|
2082 |
|
---|
2083 | \sa information(), warning(), critical()
|
---|
2084 | */
|
---|
2085 | int QMessageBox::question(QWidget *parent, const QString &title, const QString& text,
|
---|
2086 | int button0, int button1, int button2)
|
---|
2087 | {
|
---|
2088 | return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
|
---|
2089 | button0, button1, button2);
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | /*!
|
---|
2093 | \obsolete
|
---|
2094 | \overload
|
---|
2095 |
|
---|
2096 | Displays a question message box with the given \a title and \a
|
---|
2097 | text, as well as one, two or three buttons. Returns the index of
|
---|
2098 | the button that was clicked (0, 1 or 2).
|
---|
2099 |
|
---|
2100 | \a button0Text is the text of the first button, and is optional.
|
---|
2101 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
2102 | \a button1Text is the text of the second button, and is optional.
|
---|
2103 | \a button2Text is the text of the third button, and is optional.
|
---|
2104 | \a defaultButtonNumber (0, 1 or 2) is the index of the default
|
---|
2105 | button; pressing Return or Enter is the same as clicking the
|
---|
2106 | default button. It defaults to 0 (the first button). \a
|
---|
2107 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
2108 | Escape is the same as clicking this button. It defaults to -1;
|
---|
2109 | supply 0, 1 or 2 to make pressing Escape equivalent to clicking
|
---|
2110 | the relevant button.
|
---|
2111 |
|
---|
2112 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2113 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2114 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2115 | parent.
|
---|
2116 |
|
---|
2117 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2118 | If you want to do this, you should create the dialog
|
---|
2119 | yourself using one of the QMessageBox constructors.
|
---|
2120 |
|
---|
2121 | \sa information(), warning(), critical()
|
---|
2122 | */
|
---|
2123 | int QMessageBox::question(QWidget *parent, const QString &title, const QString& text,
|
---|
2124 | const QString& button0Text, const QString& button1Text,
|
---|
2125 | const QString& button2Text, int defaultButtonNumber,
|
---|
2126 | int escapeButtonNumber)
|
---|
2127 | {
|
---|
2128 | return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
|
---|
2129 | button0Text, button1Text, button2Text,
|
---|
2130 | defaultButtonNumber, escapeButtonNumber);
|
---|
2131 | }
|
---|
2132 |
|
---|
2133 |
|
---|
2134 | /*!
|
---|
2135 | \obsolete
|
---|
2136 |
|
---|
2137 | Opens a warning message box with the given \a title and \a text.
|
---|
2138 | The dialog may have up to three buttons. Each of the button
|
---|
2139 | parameters, \a button0, \a button1 and \a button2 may be set to
|
---|
2140 | one of the following values:
|
---|
2141 |
|
---|
2142 | \list
|
---|
2143 | \o QMessageBox::NoButton
|
---|
2144 | \o QMessageBox::Ok
|
---|
2145 | \o QMessageBox::Cancel
|
---|
2146 | \o QMessageBox::Yes
|
---|
2147 | \o QMessageBox::No
|
---|
2148 | \o QMessageBox::Abort
|
---|
2149 | \o QMessageBox::Retry
|
---|
2150 | \o QMessageBox::Ignore
|
---|
2151 | \o QMessageBox::YesAll
|
---|
2152 | \o QMessageBox::NoAll
|
---|
2153 | \endlist
|
---|
2154 |
|
---|
2155 | If you don't want all three buttons, set the last button, or last
|
---|
2156 | two buttons to QMessageBox::NoButton.
|
---|
2157 |
|
---|
2158 | One button can be OR-ed with QMessageBox::Default, and one
|
---|
2159 | button can be OR-ed with QMessageBox::Escape.
|
---|
2160 |
|
---|
2161 | Returns the identity (QMessageBox::Ok or QMessageBox::No or ...)
|
---|
2162 | of the button that was clicked.
|
---|
2163 |
|
---|
2164 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2165 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2166 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2167 | parent.
|
---|
2168 |
|
---|
2169 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2170 | If you want to do this, you should create the dialog
|
---|
2171 | yourself using one of the QMessageBox constructors.
|
---|
2172 |
|
---|
2173 | \sa information(), question(), critical()
|
---|
2174 | */
|
---|
2175 | int QMessageBox::warning(QWidget *parent, const QString &title, const QString& text,
|
---|
2176 | int button0, int button1, int button2)
|
---|
2177 | {
|
---|
2178 | return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
|
---|
2179 | button0, button1, button2);
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | /*!
|
---|
2183 | \obsolete
|
---|
2184 | \overload
|
---|
2185 |
|
---|
2186 | Displays a warning message box with the given \a title and \a
|
---|
2187 | text, as well as one, two, or three buttons. Returns the number
|
---|
2188 | of the button that was clicked (0, 1, or 2).
|
---|
2189 |
|
---|
2190 | \a button0Text is the text of the first button, and is optional.
|
---|
2191 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
2192 | \a button1Text is the text of the second button, and is optional,
|
---|
2193 | and \a button2Text is the text of the third button, and is
|
---|
2194 | optional. \a defaultButtonNumber (0, 1 or 2) is the index of the
|
---|
2195 | default button; pressing Return or Enter is the same as clicking
|
---|
2196 | the default button. It defaults to 0 (the first button). \a
|
---|
2197 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
2198 | Escape is the same as clicking this button. It defaults to -1;
|
---|
2199 | supply 0, 1, or 2 to make pressing Escape equivalent to clicking
|
---|
2200 | the relevant button.
|
---|
2201 |
|
---|
2202 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2203 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2204 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2205 | parent.
|
---|
2206 |
|
---|
2207 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2208 | If you want to do this, you should create the dialog
|
---|
2209 | yourself using one of the QMessageBox constructors.
|
---|
2210 |
|
---|
2211 | \sa information(), question(), critical()
|
---|
2212 | */
|
---|
2213 | int QMessageBox::warning(QWidget *parent, const QString &title, const QString& text,
|
---|
2214 | const QString& button0Text, const QString& button1Text,
|
---|
2215 | const QString& button2Text, int defaultButtonNumber,
|
---|
2216 | int escapeButtonNumber)
|
---|
2217 | {
|
---|
2218 | return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
|
---|
2219 | button0Text, button1Text, button2Text,
|
---|
2220 | defaultButtonNumber, escapeButtonNumber);
|
---|
2221 | }
|
---|
2222 |
|
---|
2223 | /*!
|
---|
2224 | \obsolete
|
---|
2225 |
|
---|
2226 | Opens a critical message box with the given \a title and \a text.
|
---|
2227 | The dialog may have up to three buttons. Each of the button
|
---|
2228 | parameters, \a button0, \a button1 and \a button2 may be set to
|
---|
2229 | one of the following values:
|
---|
2230 |
|
---|
2231 | \list
|
---|
2232 | \o QMessageBox::NoButton
|
---|
2233 | \o QMessageBox::Ok
|
---|
2234 | \o QMessageBox::Cancel
|
---|
2235 | \o QMessageBox::Yes
|
---|
2236 | \o QMessageBox::No
|
---|
2237 | \o QMessageBox::Abort
|
---|
2238 | \o QMessageBox::Retry
|
---|
2239 | \o QMessageBox::Ignore
|
---|
2240 | \o QMessageBox::YesAll
|
---|
2241 | \o QMessageBox::NoAll
|
---|
2242 | \endlist
|
---|
2243 |
|
---|
2244 | If you don't want all three buttons, set the last button, or last
|
---|
2245 | two buttons to QMessageBox::NoButton.
|
---|
2246 |
|
---|
2247 | One button can be OR-ed with QMessageBox::Default, and one
|
---|
2248 | button can be OR-ed with QMessageBox::Escape.
|
---|
2249 |
|
---|
2250 | Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.)
|
---|
2251 | of the button that was clicked.
|
---|
2252 |
|
---|
2253 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2254 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2255 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2256 | parent.
|
---|
2257 |
|
---|
2258 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2259 | If you want to do this, you should create the dialog
|
---|
2260 | yourself using one of the QMessageBox constructors.
|
---|
2261 |
|
---|
2262 | \sa information(), question(), warning()
|
---|
2263 | */
|
---|
2264 |
|
---|
2265 | int QMessageBox::critical(QWidget *parent, const QString &title, const QString& text,
|
---|
2266 | int button0, int button1, int button2)
|
---|
2267 | {
|
---|
2268 | return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
|
---|
2269 | button0, button1, button2);
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | /*!
|
---|
2273 | \obsolete
|
---|
2274 | \overload
|
---|
2275 |
|
---|
2276 | Displays a critical error message box with the given \a title and
|
---|
2277 | \a text, as well as one, two, or three buttons. Returns the
|
---|
2278 | number of the button that was clicked (0, 1 or 2).
|
---|
2279 |
|
---|
2280 | \a button0Text is the text of the first button, and is optional.
|
---|
2281 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
2282 | \a button1Text is the text of the second button, and is optional,
|
---|
2283 | and \a button2Text is the text of the third button, and is
|
---|
2284 | optional. \a defaultButtonNumber (0, 1 or 2) is the index of the
|
---|
2285 | default button; pressing Return or Enter is the same as clicking
|
---|
2286 | the default button. It defaults to 0 (the first button). \a
|
---|
2287 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
2288 | Escape is the same as clicking this button. It defaults to -1;
|
---|
2289 | supply 0, 1, or 2 to make pressing Escape equivalent to clicking
|
---|
2290 | the relevant button.
|
---|
2291 |
|
---|
2292 | If \a parent is 0, the message box is an \l{Qt::ApplicationModal}
|
---|
2293 | {application modal} dialog box. If \a parent is a widget, the
|
---|
2294 | message box is \l{Qt::WindowModal} {window modal} relative to \a
|
---|
2295 | parent.
|
---|
2296 |
|
---|
2297 | \warning Do not delete \a parent during the execution of the dialog.
|
---|
2298 | If you want to do this, you should create the dialog
|
---|
2299 | yourself using one of the QMessageBox constructors.
|
---|
2300 |
|
---|
2301 | \sa information(), question(), warning()
|
---|
2302 | */
|
---|
2303 | int QMessageBox::critical(QWidget *parent, const QString &title, const QString& text,
|
---|
2304 | const QString& button0Text, const QString& button1Text,
|
---|
2305 | const QString& button2Text, int defaultButtonNumber,
|
---|
2306 | int escapeButtonNumber)
|
---|
2307 | {
|
---|
2308 | return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
|
---|
2309 | button0Text, button1Text, button2Text,
|
---|
2310 | defaultButtonNumber, escapeButtonNumber);
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 |
|
---|
2314 | /*!
|
---|
2315 | \obsolete
|
---|
2316 |
|
---|
2317 | Returns the text of the message box button \a button, or
|
---|
2318 | an empty string if the message box does not contain the button.
|
---|
2319 |
|
---|
2320 | Use button() and QPushButton::text() instead.
|
---|
2321 | */
|
---|
2322 | QString QMessageBox::buttonText(int button) const
|
---|
2323 | {
|
---|
2324 | Q_D(const QMessageBox);
|
---|
2325 |
|
---|
2326 | if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
|
---|
2327 | return abstractButton->text();
|
---|
2328 | } else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
|
---|
2329 | // for compatibility with Qt 4.0/4.1
|
---|
2330 | return QDialogButtonBox::tr("OK");
|
---|
2331 | }
|
---|
2332 | return QString();
|
---|
2333 | }
|
---|
2334 |
|
---|
2335 | /*!
|
---|
2336 | \obsolete
|
---|
2337 |
|
---|
2338 | Sets the text of the message box button \a button to \a text.
|
---|
2339 | Setting the text of a button that is not in the message box is
|
---|
2340 | silently ignored.
|
---|
2341 |
|
---|
2342 | Use addButton() instead.
|
---|
2343 | */
|
---|
2344 | void QMessageBox::setButtonText(int button, const QString &text)
|
---|
2345 | {
|
---|
2346 | Q_D(QMessageBox);
|
---|
2347 | if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
|
---|
2348 | abstractButton->setText(text);
|
---|
2349 | } else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
|
---|
2350 | // for compatibility with Qt 4.0/4.1
|
---|
2351 | addButton(QMessageBox::Ok)->setText(text);
|
---|
2352 | }
|
---|
2353 | }
|
---|
2354 |
|
---|
2355 | #ifndef QT_NO_TEXTEDIT
|
---|
2356 | /*!
|
---|
2357 | \property QMessageBox::detailedText
|
---|
2358 | \brief the text to be displayed in the details area.
|
---|
2359 | \since 4.2
|
---|
2360 |
|
---|
2361 | The text will be interpreted as a plain text.
|
---|
2362 |
|
---|
2363 | By default, this property contains an empty string.
|
---|
2364 |
|
---|
2365 | \sa QMessageBox::text, QMessageBox::informativeText
|
---|
2366 | */
|
---|
2367 | QString QMessageBox::detailedText() const
|
---|
2368 | {
|
---|
2369 | Q_D(const QMessageBox);
|
---|
2370 | return d->detailsText ? d->detailsText->text() : QString();
|
---|
2371 | }
|
---|
2372 |
|
---|
2373 | void QMessageBox::setDetailedText(const QString &text)
|
---|
2374 | {
|
---|
2375 | Q_D(QMessageBox);
|
---|
2376 | if (text.isEmpty()) {
|
---|
2377 | delete d->detailsText;
|
---|
2378 | d->detailsText = 0;
|
---|
2379 | removeButton(d->detailsButton);
|
---|
2380 | delete d->detailsButton;
|
---|
2381 | d->detailsButton = 0;
|
---|
2382 | return;
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | if (!d->detailsText) {
|
---|
2386 | d->detailsText = new QMessageBoxDetailsText(this);
|
---|
2387 | QGridLayout* grid = qobject_cast<QGridLayout*>(layout());
|
---|
2388 | if (grid)
|
---|
2389 | grid->addWidget(d->detailsText, grid->rowCount(), 0, 1, grid->columnCount());
|
---|
2390 | d->detailsText->hide();
|
---|
2391 | }
|
---|
2392 | if (!d->detailsButton) {
|
---|
2393 | d->detailsButton = new QPushButton(d->detailsText->label(ShowLabel), this);
|
---|
2394 | QPushButton hideDetails(d->detailsText->label(HideLabel));
|
---|
2395 | d->detailsButton->setFixedSize(d->detailsButton->sizeHint().expandedTo(hideDetails.sizeHint()));
|
---|
2396 | }
|
---|
2397 | d->detailsText->setText(text);
|
---|
2398 | }
|
---|
2399 | #endif // QT_NO_TEXTEDIT
|
---|
2400 |
|
---|
2401 | /*!
|
---|
2402 | \property QMessageBox::informativeText
|
---|
2403 |
|
---|
2404 | \brief the informative text that provides a fuller description for
|
---|
2405 | the message
|
---|
2406 |
|
---|
2407 | \since 4.2
|
---|
2408 |
|
---|
2409 | Infromative text can be used to expand upon the text() to give more
|
---|
2410 | information to the user. On the Mac, this text appears in small
|
---|
2411 | system font below the text(). On other platforms, it is simply
|
---|
2412 | appended to the existing text.
|
---|
2413 |
|
---|
2414 | By default, this property contains an empty string.
|
---|
2415 |
|
---|
2416 | \sa QMessageBox::text, QMessageBox::detailedText
|
---|
2417 | */
|
---|
2418 | QString QMessageBox::informativeText() const
|
---|
2419 | {
|
---|
2420 | Q_D(const QMessageBox);
|
---|
2421 | return d->informativeLabel ? d->informativeLabel->text() : QString();
|
---|
2422 | }
|
---|
2423 |
|
---|
2424 | void QMessageBox::setInformativeText(const QString &text)
|
---|
2425 | {
|
---|
2426 | Q_D(QMessageBox);
|
---|
2427 | if (text.isEmpty()) {
|
---|
2428 | layout()->removeWidget(d->informativeLabel);
|
---|
2429 | delete d->informativeLabel;
|
---|
2430 | d->informativeLabel = 0;
|
---|
2431 | #ifndef Q_WS_MAC
|
---|
2432 | d->label->setContentsMargins(2, 0, 0, 0);
|
---|
2433 | #endif
|
---|
2434 | d->updateSize();
|
---|
2435 | return;
|
---|
2436 | }
|
---|
2437 |
|
---|
2438 | if (!d->informativeLabel) {
|
---|
2439 | QLabel *label = new QLabel;
|
---|
2440 | label->setObjectName(QLatin1String("qt_msgbox_informativelabel"));
|
---|
2441 | label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, this)));
|
---|
2442 | label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
---|
2443 | label->setOpenExternalLinks(true);
|
---|
2444 | label->setWordWrap(true);
|
---|
2445 | #ifndef Q_WS_MAC
|
---|
2446 | d->label->setContentsMargins(2, 0, 0, 0);
|
---|
2447 | label->setContentsMargins(2, 0, 0, 6);
|
---|
2448 | label->setIndent(9);
|
---|
2449 | #else
|
---|
2450 | label->setContentsMargins(16, 0, 0, 0);
|
---|
2451 | // apply a smaller font the information label on the mac
|
---|
2452 | label->setFont(qt_app_fonts_hash()->value("QTipLabel"));
|
---|
2453 | #endif
|
---|
2454 | label->setWordWrap(true);
|
---|
2455 | QGridLayout *grid = static_cast<QGridLayout *>(layout());
|
---|
2456 | grid->addWidget(label, 1, 1, 1, 1);
|
---|
2457 | d->informativeLabel = label;
|
---|
2458 | }
|
---|
2459 | d->informativeLabel->setText(text);
|
---|
2460 | d->updateSize();
|
---|
2461 | }
|
---|
2462 |
|
---|
2463 | /*!
|
---|
2464 | \since 4.2
|
---|
2465 |
|
---|
2466 | This function shadows QWidget::setWindowTitle().
|
---|
2467 |
|
---|
2468 | Sets the title of the message box to \a title. On Mac OS X,
|
---|
2469 | the window title is ignored (as required by the Mac OS X
|
---|
2470 | Guidelines).
|
---|
2471 | */
|
---|
2472 | void QMessageBox::setWindowTitle(const QString &title)
|
---|
2473 | {
|
---|
2474 | // Message boxes on the mac do not have a title
|
---|
2475 | #ifndef Q_WS_MAC
|
---|
2476 | QDialog::setWindowTitle(title);
|
---|
2477 | #else
|
---|
2478 | Q_UNUSED(title);
|
---|
2479 | #endif
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 |
|
---|
2483 | /*!
|
---|
2484 | \since 4.2
|
---|
2485 |
|
---|
2486 | This function shadows QWidget::setWindowModality().
|
---|
2487 |
|
---|
2488 | Sets the modality of the message box to \a windowModality.
|
---|
2489 |
|
---|
2490 | On Mac OS X, if the modality is set to Qt::WindowModal and the message box
|
---|
2491 | has a parent, then the message box will be a Qt::Sheet, otherwise the
|
---|
2492 | message box will be a standard dialog.
|
---|
2493 | */
|
---|
2494 | void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
|
---|
2495 | {
|
---|
2496 | QDialog::setWindowModality(windowModality);
|
---|
2497 |
|
---|
2498 | if (parentWidget() && windowModality == Qt::WindowModal)
|
---|
2499 | setParent(parentWidget(), Qt::Sheet);
|
---|
2500 | else
|
---|
2501 | setParent(parentWidget(), Qt::Dialog);
|
---|
2502 | setDefaultButton(d_func()->defaultButton);
|
---|
2503 | }
|
---|
2504 |
|
---|
2505 | #ifdef QT3_SUPPORT
|
---|
2506 | /*!
|
---|
2507 | \compat
|
---|
2508 |
|
---|
2509 | Constructs a message box with the given \a parent, \a name, and
|
---|
2510 | window flags, \a f.
|
---|
2511 | The window title is specified by \a title, and the message box
|
---|
2512 | displays message text and an icon specified by \a text and \a icon.
|
---|
2513 |
|
---|
2514 | The buttons that the user can access to respond to the message are
|
---|
2515 | defined by \a button0, \a button1, and \a button2.
|
---|
2516 | */
|
---|
2517 | QMessageBox::QMessageBox(const QString& title,
|
---|
2518 | const QString &text, Icon icon,
|
---|
2519 | int button0, int button1, int button2,
|
---|
2520 | QWidget *parent, const char *name,
|
---|
2521 | bool modal, Qt::WindowFlags f)
|
---|
2522 | : QDialog(*new QMessageBoxPrivate, parent,
|
---|
2523 | f | Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WindowCloseButtonHint)
|
---|
2524 | {
|
---|
2525 | Q_D(QMessageBox);
|
---|
2526 | setObjectName(QString::fromAscii(name));
|
---|
2527 | d->init(title, text);
|
---|
2528 | d->addOldButtons(button0, button1, button2);
|
---|
2529 | setModal(modal);
|
---|
2530 | setIcon(icon);
|
---|
2531 | }
|
---|
2532 |
|
---|
2533 | /*!
|
---|
2534 | \compat
|
---|
2535 | Constructs a message box with the given \a parent and \a name.
|
---|
2536 | */
|
---|
2537 | QMessageBox::QMessageBox(QWidget *parent, const char *name)
|
---|
2538 | : QDialog(*new QMessageBoxPrivate, parent,
|
---|
2539 | Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WindowCloseButtonHint)
|
---|
2540 | {
|
---|
2541 | Q_D(QMessageBox);
|
---|
2542 | setObjectName(QString::fromAscii(name));
|
---|
2543 | d->init();
|
---|
2544 | }
|
---|
2545 |
|
---|
2546 | /*!
|
---|
2547 | Returns the pixmap used for a standard icon. This
|
---|
2548 | allows the pixmaps to be used in more complex message boxes.
|
---|
2549 | \a icon specifies the required icon, e.g. QMessageBox::Information,
|
---|
2550 | QMessageBox::Warning or QMessageBox::Critical.
|
---|
2551 |
|
---|
2552 | \a style is unused.
|
---|
2553 | */
|
---|
2554 |
|
---|
2555 | QPixmap QMessageBox::standardIcon(Icon icon, Qt::GUIStyle style)
|
---|
2556 | {
|
---|
2557 | Q_UNUSED(style);
|
---|
2558 | return QMessageBox::standardIcon(icon);
|
---|
2559 | }
|
---|
2560 |
|
---|
2561 | /*!
|
---|
2562 | \fn int QMessageBox::message(const QString &title, const QString &text,
|
---|
2563 | const QString &buttonText, QWidget *parent = 0,
|
---|
2564 | const char *name = 0)
|
---|
2565 |
|
---|
2566 | Opens a modal message box with the given \a title and showing the
|
---|
2567 | given \a text. The message box has a single button which has the
|
---|
2568 | given \a buttonText (or tr("OK")). The message box is centred over
|
---|
2569 | its \a parent and is called \a name.
|
---|
2570 |
|
---|
2571 | Use information(), warning(), question(), or critical() instead.
|
---|
2572 |
|
---|
2573 | \oldcode
|
---|
2574 | QMessageBox::message(tr("My App"), tr("All occurrences replaced."),
|
---|
2575 | tr("Close"), this);
|
---|
2576 | \newcode
|
---|
2577 | QMessageBox::information(this, tr("My App"),
|
---|
2578 | tr("All occurrences replaced."),
|
---|
2579 | QMessageBox::Close);
|
---|
2580 | \endcode
|
---|
2581 | */
|
---|
2582 |
|
---|
2583 | /*!
|
---|
2584 | \fn bool QMessageBox::query(const QString &caption,
|
---|
2585 | const QString& text,
|
---|
2586 | const QString& yesButtonText,
|
---|
2587 | const QString& noButtonText,
|
---|
2588 | QWidget *parent, const char *name)
|
---|
2589 |
|
---|
2590 | \obsolete
|
---|
2591 |
|
---|
2592 | Queries the user using a modal message box with up to two buttons.
|
---|
2593 | The message box has the given \a caption (although some window
|
---|
2594 | managers don't show it), and shows the given \a text. The left
|
---|
2595 | button has the \a yesButtonText (or tr("OK")), and the right button
|
---|
2596 | has the \a noButtonText (or isn't shown). The message box is centred
|
---|
2597 | over its \a parent and is called \a name.
|
---|
2598 |
|
---|
2599 | Use information(), question(), warning(), or critical() instead.
|
---|
2600 | */
|
---|
2601 |
|
---|
2602 | #endif
|
---|
2603 |
|
---|
2604 | QPixmap QMessageBoxPrivate::standardIcon(QMessageBox::Icon icon, QMessageBox *mb)
|
---|
2605 | {
|
---|
2606 | QStyle *style = mb ? mb->style() : QApplication::style();
|
---|
2607 | int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, mb);
|
---|
2608 | QIcon tmpIcon;
|
---|
2609 | switch (icon) {
|
---|
2610 | case QMessageBox::Information:
|
---|
2611 | tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation, 0, mb);
|
---|
2612 | break;
|
---|
2613 | case QMessageBox::Warning:
|
---|
2614 | tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning, 0, mb);
|
---|
2615 | break;
|
---|
2616 | case QMessageBox::Critical:
|
---|
2617 | tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical, 0, mb);
|
---|
2618 | break;
|
---|
2619 | case QMessageBox::Question:
|
---|
2620 | tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mb);
|
---|
2621 | default:
|
---|
2622 | break;
|
---|
2623 | }
|
---|
2624 | if (!tmpIcon.isNull())
|
---|
2625 | return tmpIcon.pixmap(iconSize, iconSize);
|
---|
2626 | return QPixmap();
|
---|
2627 | }
|
---|
2628 |
|
---|
2629 | /*!
|
---|
2630 | \obsolete
|
---|
2631 |
|
---|
2632 | Returns the pixmap used for a standard icon. This allows the
|
---|
2633 | pixmaps to be used in more complex message boxes. \a icon
|
---|
2634 | specifies the required icon, e.g. QMessageBox::Question,
|
---|
2635 | QMessageBox::Information, QMessageBox::Warning or
|
---|
2636 | QMessageBox::Critical.
|
---|
2637 |
|
---|
2638 | Call QStyle::standardIcon() with QStyle::SP_MessageBoxInformation etc.
|
---|
2639 | instead.
|
---|
2640 | */
|
---|
2641 |
|
---|
2642 | QPixmap QMessageBox::standardIcon(Icon icon)
|
---|
2643 | {
|
---|
2644 | return QMessageBoxPrivate::standardIcon(icon, 0);
|
---|
2645 | }
|
---|
2646 |
|
---|
2647 | /*!
|
---|
2648 | \typedef QMessageBox::Button
|
---|
2649 | \obsolete
|
---|
2650 |
|
---|
2651 | Use QMessageBox::StandardButton instead.
|
---|
2652 | */
|
---|
2653 |
|
---|
2654 | /*!
|
---|
2655 | \fn int QMessageBox::information(QWidget *parent, const QString &title,
|
---|
2656 | const QString& text, StandardButton button0,
|
---|
2657 | StandardButton button1)
|
---|
2658 | \fn int QMessageBox::warning(QWidget *parent, const QString &title,
|
---|
2659 | const QString& text, StandardButton button0,
|
---|
2660 | StandardButton button1)
|
---|
2661 | \fn int QMessageBox::critical(QWidget *parent, const QString &title,
|
---|
2662 | const QString& text, StandardButton button0,
|
---|
2663 | StandardButton button1)
|
---|
2664 | \fn int QMessageBox::question(QWidget *parent, const QString &title,
|
---|
2665 | const QString& text, StandardButton button0,
|
---|
2666 | StandardButton button1)
|
---|
2667 | \internal
|
---|
2668 |
|
---|
2669 | ### Needed for Qt 4 source compatibility
|
---|
2670 | */
|
---|
2671 |
|
---|
2672 | /*!
|
---|
2673 | \fn int QMessageBox::exec()
|
---|
2674 |
|
---|
2675 | Shows the message box as a \l{QDialog#Modal Dialogs}{modal dialog},
|
---|
2676 | blocking until the user closes it.
|
---|
2677 |
|
---|
2678 | When using a QMessageBox with standard buttons, this functions returns a
|
---|
2679 | \l StandardButton value indicating the standard button that was clicked.
|
---|
2680 | When using QMessageBox with custom buttons, this function returns an
|
---|
2681 | opaque value; use clickedButton() to determine which button was clicked.
|
---|
2682 |
|
---|
2683 | Users cannot interact with any other window in the same
|
---|
2684 | application until they close the dialog, either by clicking a
|
---|
2685 | button or by using a mechanism provided by the window system.
|
---|
2686 |
|
---|
2687 | \sa show(), result()
|
---|
2688 | */
|
---|
2689 |
|
---|
2690 | QT_END_NAMESPACE
|
---|
2691 |
|
---|
2692 | #include "moc_qmessagebox.cpp"
|
---|
2693 |
|
---|
2694 | #endif // QT_NO_MESSAGEBOX
|
---|