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
11
12/*!
13 \qmltype MessageDialog
14 \inherits Dialog
15//! \nativetype QQuickMessageDialog
16 \inqmlmodule QtQuick.Dialogs
17 \since 6.3
18 \brief A message dialog.
19
20 The MessageDialog type provides a QML API for message dialogs.
21
22 \image qtquickdialogs-messagedialog-android.png {The message dialog informs the user of some information, and will close when the user acknowledges the dialog}
23
24 A message dialog is used to inform the user, or ask the user a question.
25 A message dialog displays a primary \l text to alert the user to a situation,
26 an \l {informativeText}{informative text} to further explain the alert or to
27 ask the user a question, and an optional \l {detailedText}{detailed text} to
28 provide even more data if the user requests it. A message box can also display
29 a configurable set of \l buttons for accepting a user response.
30
31 To show a message dialog, construct an instance of MessageDialog, set the
32 desired properties, and call \l {Dialog::}{open()}.
33
34 \code
35 MessageDialog {
36 buttons: MessageDialog.Ok
37 text: "The document has been modified."
38 }
39 \endcode
40
41 The user must click the \uicontrol OK button to dismiss the message dialog.
42
43 A more elaborate approach than just alerting the user to an event is to
44 also ask the user what to do about it. Store the question in the
45 \l {informativeText}{informative text} property, and specify the \l buttons
46 property to the set of buttons you want as the set of user responses. The
47 buttons are specified by combining values using the bitwise OR operator.
48
49 \code
50 MessageDialog {
51 text: "The document has been modified."
52 informativeText: "Do you want to save your changes?"
53 buttons: MessageDialog.Ok | MessageDialog.Cancel
54
55 onAccepted: document.save()
56 }
57 \endcode
58
59 \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}
60
61
62 \section2 Availability
63
64 A native platform message dialog is currently available on the following platforms:
65
66 \list
67 \li Android
68 \li iOS
69 \li macOS
70 \endlist
71
72 \include includes/fallback.qdocinc
73*/
74
75/*!
76 \qmlsignal QtQuick.Dialogs::MessageDialog::buttonClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role)
77
78 This signal is emitted when a \a button with the specified \a role is
79 clicked.
80
81 By giving this signal a handler, you can respond to any custom button being pressed.
82 The \a button argument tells which button was clicked, while the \a role argument tells the functional role of that button.
83
84 \code
85 MessageDialog {
86 id: dialog
87 text: qsTr("The document has been modified.")
88 informativeText: qsTr("Do you want to save your changes?")
89 buttons: MessageDialog.Ok | MessageDialog.Cancel
90 onButtonClicked: function (button, role) {
91 switch (button) {
92 case MessageDialog.Ok:
93 document.save()
94 break;
95 }
96 }
97 }
98 \endcode
99
100 \sa buttons
101*/
102
103QQuickMessageDialog::QQuickMessageDialog(QObject *parent)
104 : QQuickAbstractDialog(QQuickDialogType::MessageDialog, parent),
105 m_options(QMessageDialogOptions::create())
106{
107}
108
109/*!
110 \qmlproperty string QtQuick.Dialogs::MessageDialog::text
111
112 This property holds the text to be displayed on the message dialog.
113
114 \sa informativeText, detailedText
115*/
116QString QQuickMessageDialog::text() const
117{
118 return m_options->text();
119}
120
121void QQuickMessageDialog::setText(const QString &text)
122{
123 if (m_options->text() == text)
124 return;
125
126 m_options->setText(text);
127
128 emit textChanged();
129}
130
131/*!
132 \qmlproperty string QtQuick.Dialogs::MessageDialog::informativeText
133
134 This property holds the informative text that provides a fuller description for the message.
135
136 Informative text can be used to expand upon the \l text to give more information to the user.
137
138 \sa text, detailedText
139*/
140QString QQuickMessageDialog::informativeText() const
141{
142 return m_options->informativeText();
143}
144
145void QQuickMessageDialog::setInformativeText(const QString &text)
146{
147 if (m_options->informativeText() == text)
148 return;
149
150 m_options->setInformativeText(text);
151
152 emit informativeTextChanged();
153}
154
155/*!
156 \qmlproperty string QtQuick.Dialogs::MessageDialog::detailedText
157
158 This property holds the text to be displayed in the details area.
159
160 \sa text, informativeText
161*/
162QString QQuickMessageDialog::detailedText() const
163{
164 return m_options->detailedText();
165}
166
167void QQuickMessageDialog::setDetailedText(const QString &text)
168{
169 if (m_options->detailedText() == text)
170 return;
171
172 m_options->setDetailedText(text);
173
174 emit detailedTextChanged();
175}
176
177/*!
178 \qmlproperty flags QtQuick.Dialogs::MessageDialog::buttons
179
180 This property holds a combination of buttons that are used by the message dialog.
181 The default value is \c MessageDialog.NoButton.
182
183 Possible flags:
184 \value MessageDialog.Ok An "OK" button defined with the \c AcceptRole.
185 \value MessageDialog.Open An "Open" button defined with the \c AcceptRole.
186 \value MessageDialog.Save A "Save" button defined with the \c AcceptRole.
187 \value MessageDialog.Cancel A "Cancel" button defined with the \c RejectRole.
188 \value MessageDialog.Close A "Close" button defined with the \c RejectRole.
189 \value MessageDialog.Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the \c DestructiveRole.
190 \value MessageDialog.Apply An "Apply" button defined with the \c ApplyRole.
191 \value MessageDialog.Reset A "Reset" button defined with the \c ResetRole.
192 \value MessageDialog.RestoreDefaults A "Restore Defaults" button defined with the \c ResetRole.
193 \value MessageDialog.Help A "Help" button defined with the \c HelpRole.
194 \value MessageDialog.SaveAll A "Save All" button defined with the \c AcceptRole.
195 \value MessageDialog.Yes A "Yes" button defined with the \c YesRole.
196 \value MessageDialog.YesToAll A "Yes to All" button defined with the \c YesRole.
197 \value MessageDialog.No A "No" button defined with the \c NoRole.
198 \value MessageDialog.NoToAll A "No to All" button defined with the \c NoRole.
199 \value MessageDialog.Abort An "Abort" button defined with the \c RejectRole.
200 \value MessageDialog.Retry A "Retry" button defined with the \c AcceptRole.
201 \value MessageDialog.Ignore An "Ignore" button defined with the \c AcceptRole.
202 \value MessageDialog.NoButton The dialog has no buttons.
203
204 \sa buttonClicked()
205*/
206
207QPlatformDialogHelper::StandardButtons QQuickMessageDialog::buttons() const
208{
209 return m_options->standardButtons();
210}
211
212void QQuickMessageDialog::setButtons(QPlatformDialogHelper::StandardButtons buttons)
213{
214 if (m_options->standardButtons() == buttons)
215 return;
216
217 m_options->setStandardButtons(buttons);
218
219 emit buttonsChanged();
220}
221
222void QQuickMessageDialog::handleClick(QPlatformDialogHelper::StandardButton button,
223 QPlatformDialogHelper::ButtonRole role)
224{
225 m_roleOfLastButtonPressed = role;
226 emit buttonClicked(button, role);
227 done(button);
228}
229
230void QQuickMessageDialog::onCreate(QPlatformDialogHelper *dialog)
231{
232 if (QPlatformMessageDialogHelper *messageDialog =
233 qobject_cast<QPlatformMessageDialogHelper *>(dialog)) {
234 connect(messageDialog, &QPlatformMessageDialogHelper::clicked, this,
235 &QQuickMessageDialog::handleClick);
236 messageDialog->setOptions(m_options);
237 }
238}
239
240void QQuickMessageDialog::onShow(QPlatformDialogHelper *dialog)
241{
242 m_options->setWindowTitle(title());
243
244 if (QPlatformMessageDialogHelper *messageDialog =
245 qobject_cast<QPlatformMessageDialogHelper *>(dialog))
246 messageDialog->setOptions(m_options); // setOptions only assigns a member and isn't virtual
247
248 if (QQuickPlatformMessageDialog *messageDialog = qobject_cast<QQuickPlatformMessageDialog *>(dialog))
249 messageDialog->dialog()->setPopupType(m_popupType);
250
251 QQuickAbstractDialog::onShow(dialog);
252}
253
254int QQuickMessageDialog::dialogCode() const
255{
256 switch (m_roleOfLastButtonPressed) {
257 case QPlatformDialogHelper::AcceptRole:
258 case QPlatformDialogHelper::YesRole:
259 return Accepted;
260 case QPlatformDialogHelper::RejectRole:
261 case QPlatformDialogHelper::NoRole:
262 return Rejected;
263 default:
264 return QQuickAbstractDialog::dialogCode();
265 }
266}
267
268QT_END_NAMESPACE
269
270#include "moc_qquickmessagedialog_p.cpp"