Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickmessagedialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtQuickDialogs2QuickImpl/private/qquickplatformmessagedialog_p.h>
8#include <QtQuickDialogs2QuickImpl/private/qquickmessagedialogimpl_p.h>
9#include <QtQuickTemplates2/private/qquicktemplatesutils_p.h>
10
11#include <QtGui/qtextdocument.h>
12
14
15/*!
16 \qmltype MessageDialog
17 \inherits Dialog
18//! \nativetype QQuickMessageDialog
19 \inqmlmodule QtQuick.Dialogs
20 \since 6.3
21 \brief A message dialog.
22
23 The MessageDialog type provides a QML API for message dialogs.
24
25 \image qtquickdialogs-messagedialog-android.png {The message dialog informs the user of some information, and will close when the user acknowledges the dialog}
26
27 A message dialog is used to inform the user, or ask the user a question.
28 A message dialog displays a primary \l text to alert the user to a situation,
29 an \l {informativeText}{informative text} to further explain the alert or to
30 ask the user a question, and an optional \l {detailedText}{detailed text} to
31 provide even more data if the user requests it. A message box can also display
32 a configurable set of \l buttons for accepting a user response.
33
34 To show a message dialog, construct an instance of MessageDialog, set the
35 desired properties, and call \l {Dialog::}{open()}.
36
37 \code
38 MessageDialog {
39 buttons: MessageDialog.Ok
40 text: "The document has been modified."
41 }
42 \endcode
43
44 The user must click the \uicontrol OK button to dismiss the message dialog.
45
46 A more elaborate approach than just alerting the user to an event is to
47 also ask the user what to do about it. Store the question in the
48 \l {informativeText}{informative text} property, and specify the \l buttons
49 property to the set of buttons you want as the set of user responses. The
50 buttons are specified by combining values using the bitwise OR operator.
51
52 \code
53 MessageDialog {
54 text: "The document has been modified."
55 informativeText: "Do you want to save your changes?"
56 buttons: MessageDialog.Ok | MessageDialog.Cancel
57
58 onAccepted: document.save()
59 }
60 \endcode
61
62 \image qtquickdialogs-messagedialog-informative-android.png {The message dialog allows the user to make a choice based on the message, and will perform the associated action based on the buttons property}
63
64
65 \section2 Availability
66
67 A native platform message dialog is currently available on the following platforms:
68
69 \list
70 \li Android
71 \li iOS
72 \li macOS
73 \endlist
74
75 \include includes/fallback.qdocinc
76*/
77
78/*!
79 \qmlsignal QtQuick.Dialogs::MessageDialog::buttonClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role)
80
81 This signal is emitted when a \a button with the specified \a role is
82 clicked.
83
84 By giving this signal a handler, you can respond to any custom button being pressed.
85 The \a button argument tells which button was clicked, while the \a role argument tells the functional role of that button.
86
87 \code
88 MessageDialog {
89 id: dialog
90 text: qsTr("The document has been modified.")
91 informativeText: qsTr("Do you want to save your changes?")
92 buttons: MessageDialog.Ok | MessageDialog.Cancel
93 onButtonClicked: function (button, role) {
94 switch (button) {
95 case MessageDialog.Ok:
96 document.save()
97 break;
98 }
99 }
100 }
101 \endcode
102
103 \sa buttons
104*/
105
106QQuickMessageDialog::QQuickMessageDialog(QObject *parent)
107 : QQuickAbstractDialog(QQuickDialogType::MessageDialog, parent),
108 m_options(QMessageDialogOptions::create())
109{
110}
111
112/*!
113 \qmlproperty string QtQuick.Dialogs::MessageDialog::text
114
115 This property holds the text to be displayed on the message dialog.
116
117 \sa informativeText, detailedText
118*/
119QString QQuickMessageDialog::text() const
120{
121 return m_options->text();
122}
123
124void QQuickMessageDialog::setText(const QString &text)
125{
126 if (m_options->text() == text)
127 return;
128
129 m_options->setText(text);
130
131 emit textChanged();
132}
133
134/*!
135 \qmlproperty string QtQuick.Dialogs::MessageDialog::informativeText
136
137 This property holds the informative text that provides a fuller description for the message.
138
139 Informative text can be used to expand upon the \l text to give more information to the user.
140
141 \sa text, detailedText
142*/
143QString QQuickMessageDialog::informativeText() const
144{
145 return m_options->informativeText();
146}
147
148void QQuickMessageDialog::setInformativeText(const QString &text)
149{
150 if (m_options->informativeText() == text)
151 return;
152
153 m_options->setInformativeText(text);
154
155 emit informativeTextChanged();
156}
157
158/*!
159 \qmlproperty string QtQuick.Dialogs::MessageDialog::detailedText
160
161 This property holds the text to be displayed in the details area.
162
163 \sa text, informativeText
164*/
165QString QQuickMessageDialog::detailedText() const
166{
167 return m_options->detailedText();
168}
169
170void QQuickMessageDialog::setDetailedText(const QString &text)
171{
172 if (m_options->detailedText() == text)
173 return;
174
175 m_options->setDetailedText(text);
176
177 emit detailedTextChanged();
178}
179
180/*!
181 \qmlproperty flags QtQuick.Dialogs::MessageDialog::buttons
182
183 This property holds a combination of buttons that are used by the message dialog.
184 The default value is \c MessageDialog.NoButton.
185
186 Possible flags:
187 \value MessageDialog.Ok An "OK" button defined with the \c AcceptRole.
188 \value MessageDialog.Open An "Open" button defined with the \c AcceptRole.
189 \value MessageDialog.Save A "Save" button defined with the \c AcceptRole.
190 \value MessageDialog.Cancel A "Cancel" button defined with the \c RejectRole.
191 \value MessageDialog.Close A "Close" button defined with the \c RejectRole.
192 \value MessageDialog.Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the \c DestructiveRole.
193 \value MessageDialog.Apply An "Apply" button defined with the \c ApplyRole.
194 \value MessageDialog.Reset A "Reset" button defined with the \c ResetRole.
195 \value MessageDialog.RestoreDefaults A "Restore Defaults" button defined with the \c ResetRole.
196 \value MessageDialog.Help A "Help" button defined with the \c HelpRole.
197 \value MessageDialog.SaveAll A "Save All" button defined with the \c AcceptRole.
198 \value MessageDialog.Yes A "Yes" button defined with the \c YesRole.
199 \value MessageDialog.YesToAll A "Yes to All" button defined with the \c YesRole.
200 \value MessageDialog.No A "No" button defined with the \c NoRole.
201 \value MessageDialog.NoToAll A "No to All" button defined with the \c NoRole.
202 \value MessageDialog.Abort An "Abort" button defined with the \c RejectRole.
203 \value MessageDialog.Retry A "Retry" button defined with the \c AcceptRole.
204 \value MessageDialog.Ignore An "Ignore" button defined with the \c AcceptRole.
205 \value MessageDialog.NoButton The dialog has no buttons.
206
207 \sa buttonClicked()
208*/
209
210QPlatformDialogHelper::StandardButtons QQuickMessageDialog::buttons() const
211{
212 return m_options->standardButtons();
213}
214
215void QQuickMessageDialog::setButtons(QPlatformDialogHelper::StandardButtons buttons)
216{
217 if (m_options->standardButtons() == buttons)
218 return;
219
220 m_options->setStandardButtons(buttons);
221
222 emit buttonsChanged();
223}
224
225/*!
226 \since 6.13
227 \qmlproperty enumeration QtQuick.Dialogs::MessageDialog::textFormat
228
229 This property holds the format of the \l {Dialog::}{title}, \l text and
230 \l informativeText of the message dialog.
231
232 The supported values are the same as for \l {Text::textFormat}{Text.textFormat}.
233 The default value is \c Text.AutoText.
234
235 \note This property only affects the non-native message dialog. Native
236 message dialogs always display the texts as plain text.
237*/
238QQuickText::TextFormat QQuickMessageDialog::textFormat() const
239{
240 return m_textFormat.value_or(QQuickText::AutoText);
241}
242
243void QQuickMessageDialog::setTextFormat(QQuickText::TextFormat textFormat)
244{
245 const bool changed = textFormat != this->textFormat();
246 m_textFormat = textFormat;
247 if (changed)
248 emit textFormatChanged();
249}
250
251void QQuickMessageDialog::resetTextFormat()
252{
253 const bool changed = textFormat() != QQuickText::AutoText;
254 m_textFormat.reset();
255 if (changed)
256 emit textFormatChanged();
257}
258
259void QQuickMessageDialog::handleClick(QPlatformDialogHelper::StandardButton button,
260 QPlatformDialogHelper::ButtonRole role)
261{
262 m_roleOfLastButtonPressed = role;
263 emit buttonClicked(button, role);
264 done(button);
265}
266
267void QQuickMessageDialog::onCreate(QPlatformDialogHelper *dialog)
268{
269 if (QPlatformMessageDialogHelper *messageDialog =
270 qobject_cast<QPlatformMessageDialogHelper *>(dialog)) {
271 connect(messageDialog, &QPlatformMessageDialogHelper::clicked, this,
272 &QQuickMessageDialog::handleClick);
273 messageDialog->setOptions(m_options);
274 }
275}
276
277void QQuickMessageDialog::onShow(QPlatformDialogHelper *dialog)
278{
279 m_options->setWindowTitle(title());
280
281 if (QPlatformMessageDialogHelper *messageDialog =
282 qobject_cast<QPlatformMessageDialogHelper *>(dialog))
283 messageDialog->setOptions(m_options); // setOptions only assigns a member and isn't virtual
284
285 if (QQuickPlatformMessageDialog *messageDialog = qobject_cast<QQuickPlatformMessageDialog *>(dialog)) {
286 messageDialog->dialog()->setPopupType(m_popupType);
287 messageDialog->dialog()->setTitleFormat(textFormat());
288 }
289
290 if (!m_textFormat && !m_textFormatWarningDisplayed
291 && (Qt::mightBeRichText(title()) || Qt::mightBeRichText(text())
292 || Qt::mightBeRichText(informativeText()))) {
293 QQuickTemplatesUtils::warnAboutImplicitRichText(this);
294 m_textFormatWarningDisplayed = true;
295 }
296
297 QQuickAbstractDialog::onShow(dialog);
298}
299
300int QQuickMessageDialog::dialogCode() const
301{
302 switch (m_roleOfLastButtonPressed) {
303 case QPlatformDialogHelper::AcceptRole:
304 case QPlatformDialogHelper::YesRole:
305 return Accepted;
306 case QPlatformDialogHelper::RejectRole:
307 case QPlatformDialogHelper::NoRole:
308 return Rejected;
309 default:
310 return QQuickAbstractDialog::dialogCode();
311 }
312}
313
314QT_END_NAMESPACE
315
316#include "moc_qquickmessagedialog_p.cpp"
Combined button and popup list for selecting options.