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
qquickplatformmessagedialog.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
5#include <QtQml/qqmlcomponent.h>
7
8#include <QtQuickTemplates2/private/qquickdialog_p_p.h>
9#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
10#include <QtQuickTemplates2/private/qquickpopupanchors_p.h>
11
13
15
16Q_STATIC_LOGGING_CATEGORY(lcQuickPlatformMessageDialog, "qt.quick.dialogs.quickplatformmessagedialog")
17
18QQuickPlatformMessageDialog::QQuickPlatformMessageDialog(QObject *parent)
19{
20 qCDebug(lcQuickPlatformMessageDialog)
21 << "creating non-native Qt Quick MessageDialog with parent" << parent;
22
23 // Set a parent so that we get deleted if we can't be shown for whatever reason.
24 // Our eventual parent should be the window, though.
25 setParent(parent);
26
27 auto qmlContext = ::qmlContext(parent);
28 if (!qmlContext) {
29 qmlWarning(parent) << "No QQmlContext for QQuickPlatformMessageDialog; can't create "
30 "non-native MessageDialog implementation";
31 return;
32 }
33
34 const auto dialogQmlUrl = QUrl(QStringLiteral(
35 "qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/MessageDialog.qml"));
36
37 QQmlComponent messageDialogComponent(qmlContext->engine(), dialogQmlUrl, parent);
38 if (!messageDialogComponent.isReady()) {
39 qmlWarning(parent) << "Failed to load non-native MessageBox implementation:\n"
40 << messageDialogComponent.errorString();
41 return;
42 }
43
44 m_dialog = qobject_cast<QQuickMessageDialogImpl *>(messageDialogComponent.create());
45
46 if (!m_dialog) {
47 qmlWarning(parent) << "Failed to create an instance of the non-native MessageBox:\n"
48 << messageDialogComponent.errorString();
49 return;
50 }
51
52 m_dialog->setParent(this);
53
54 connect(m_dialog, &QQuickDialog::accepted, this, &QPlatformDialogHelper::accept);
55 connect(m_dialog, &QQuickDialog::rejected, this, &QPlatformDialogHelper::reject);
56 connect(m_dialog, &QQuickMessageDialogImpl::buttonClicked, this,
57 &QQuickPlatformMessageDialog::clicked);
58}
59
60void QQuickPlatformMessageDialog::exec()
61{
62 qCWarning(lcQuickPlatformMessageDialog)
63 << "exec() is not supported for the Qt Quick MessageDialog fallback";
64}
65
66bool QQuickPlatformMessageDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality,
67 QWindow *parent)
68{
69 qCDebug(lcQuickPlatformMessageDialog)
70 << "show called with flags" << flags << "modality" << modality << "parent" << parent;
71 if (!m_dialog)
72 return false;
73
74 if (!parent)
75 return false;
76
77 auto quickWindow = qobject_cast<QQuickWindow *>(parent);
78 if (!quickWindow) {
79 qmlInfo(this->parent()) << "Parent window (" << parent
80 << ") of non-native dialog is not a QQuickWindow";
81 return false;
82 }
83 m_dialog->setParent(parent);
84 m_dialog->resetParentItem();
85
86 auto popupPrivate = QQuickPopupPrivate::get(m_dialog);
87 popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem());
88
89 QSharedPointer<QMessageDialogOptions> options = QPlatformMessageDialogHelper::options();
90 m_dialog->setTitle(options->windowTitle());
91 m_dialog->setOptions(options);
92 m_dialog->setWindowModality(modality);
93 m_dialog->open();
94 return true;
95}
96void QQuickPlatformMessageDialog::hide()
97{
98 if (isValid())
99 m_dialog->close();
100}
101
102bool QQuickPlatformMessageDialog::isValid() const
103{
104 return m_dialog;
105}
106
107QQuickMessageDialogImpl *QQuickPlatformMessageDialog::dialog() const
108{
109 return m_dialog;
110}
111
112QT_END_NAMESPACE
113
114#include "moc_qquickplatformmessagedialog_p.cpp"