Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickabstractdialog.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
5
6#include <QtCore/qloggingcategory.h>
7#include <QtGui/private/qguiapplication_p.h>
8#include <QtQuick/qquickitem.h>
9#include <QtQuick/qquickwindow.h>
10#include <QtQuickDialogs2QuickImpl/private/qquickdialogimplfactory_p.h>
11
13
14Q_LOGGING_CATEGORY(lcDialogs, "qt.quick.dialogs")
15
16
73
107
109 : QObject(parent),
110 m_type(type)
111{
112}
113
118
123
131QQmlListProperty<QObject> QQuickAbstractDialog::data()
132{
133 return QQmlListProperty<QObject>(this, &m_data);
134}
135
149
151{
152 qCDebug(lcDialogs) << "set parent window to" << window;
154
155 if (m_parentWindow == window)
156 return;
157
160}
161
163{
165
166 if (!m_parentWindow)
167 return;
168
169 m_parentWindow = nullptr;
171}
172
179{
180 return m_title;
181}
182
184{
185 if (m_title == title)
186 return;
187
188 m_title = title;
190}
191
197Qt::WindowFlags QQuickAbstractDialog::flags() const
198{
199 return m_flags;
200}
201
203{
204 if (m_flags == flags)
205 return;
206
207 m_flags = flags;
209}
210
225
227{
228 if (m_modality == modality)
229 return;
230
233}
234
243{
244 return m_handle && m_visible;
245}
246
248{
249 qCDebug(lcDialogs) << "setVisible called with" << visible;
250
251 if (visible) {
252 // Don't try to open before component completion, as we won't have a window yet,
253 // and open() sets m_visible to false if it fails.
254 if (!m_complete)
255 m_visibleRequested = true;
256 else
257 open();
258 } else {
259 close();
260 }
261}
262
276{
277 return m_result;
278}
279
281{
282 if (m_result == result)
283 return;
284
287}
288
297{
298 qCDebug(lcDialogs) << "open called";
299 if (m_visible || !create())
300 return;
301
302 onShow(m_handle.get());
303
305 if (m_visible) {
306 m_result = Rejected; // in case an accepted dialog gets re-opened, then closed
308 }
309}
310
320{
321 if (!m_handle || !m_visible)
322 return;
323
324 onHide(m_handle.get());
325 m_handle->hide();
326 m_visible = false;
328 m_parentWindow = nullptr;
330
331 if (dialogCode() == Accepted)
332 emit accepted();
333 else if (dialogCode() == Rejected)
334 emit rejected();
335}
336
348
360
369{
371 close();
372}
373
377
379{
380 qCDebug(lcDialogs) << "componentComplete";
381 m_complete = true;
382
384 return;
385
386 m_visibleRequested = false;
387
388 if (windowForOpen()) {
389 open();
390 return;
391 }
392
393 // Since visible were set to true by the user, we want the dialog to be open by default.
394 // There is no guarantee that the dialog will work when it exists in a object tree that lacks a window,
395 // and since qml components are sometimes instantiated before they're given a window
396 // (which is the case when using QQuickView), we want to delay the call to open(), until the window is provided.
397 if (const auto parentItem = findParentItem())
398 connect(parentItem, &QQuickItem::windowChanged, this, &QQuickAbstractDialog::deferredOpen, Qt::SingleShotConnection);
399}
400
401static const char *qmlTypeName(const QObject *object)
402{
403 return object->metaObject()->className() + qstrlen("QQuickPlatform");
404}
405
407{
408 return quickDialogType == QQuickDialogType::FolderDialog
409 ? QPlatformTheme::FileDialog : static_cast<QPlatformTheme::DialogType>(quickDialogType);
410}
411
413{
414 qCDebug(lcDialogs) << qmlTypeName(this) << "attempting to create dialog backend of type"
415 << int(m_type) << "with parent window" << m_parentWindow;
416 if (m_handle)
417 return m_handle.get();
418
419 qCDebug(lcDialogs) << "- attempting to create a native dialog";
420 if (useNativeDialog()) {
421 m_handle.reset(QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(
423 }
424
425 if (!m_handle) {
426 qCDebug(lcDialogs) << "- attempting to create a quick dialog";
428 }
429
430 qCDebug(lcDialogs) << qmlTypeName(this) << "created ->" << m_handle.get();
431 if (m_handle) {
432 onCreate(m_handle.get());
435 }
436 return m_handle.get();
437}
438
440{
441 m_handle.reset();
442}
443
445{
447 qCDebug(lcDialogs) << " - Qt::AA_DontUseNativeDialogs was set; not using native dialog";
448 return false;
449 }
450
451 if (!QGuiApplicationPrivate::platformTheme()->usePlatformNativeDialog(toPlatformDialogType(m_type))) {
452 qCDebug(lcDialogs) << " - the platform theme told us a native dialog isn't available; not using native dialog";
453 return false;
454 }
455
456 return true;
457}
458
468
480
485
487
489{
490 QObject *obj = parent();
491 while (obj) {
493 if (item)
494 return item;
495 obj = obj->parent();
496 }
497 return nullptr;
498}
499
501{
503 return m_parentWindow;
504 else if (auto parentItem = findParentItem())
505 return parentItem->window();
506 return m_parentWindow;
507}
508
514
516
517#include "moc_qquickabstractdialog_p.cpp"
static bool testAttribute(Qt::ApplicationAttribute attribute)
Returns true if attribute attribute is set; otherwise returns false.
static QPlatformTheme * platformTheme()
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QPlatformDialogHelper class allows for platform-specific customization of dialogs.
QWindow * windowForOpen() const
void open()
\qmlmethod void QtQuick.Dialogs::Dialog::open()
void setTitle(const QString &title)
std::unique_ptr< QPlatformDialogHelper > m_handle
Qt::WindowModality modality
QPlatformDialogHelper * handle() const
QQmlListProperty< QObject > data
\qmldefault \qmlproperty list<QtObject> QtQuick.Dialogs::Dialog::data
void setModality(Qt::WindowModality modality)
void deferredOpen(QWindow *window)
void close()
\qmlmethod void QtQuick.Dialogs::Dialog::close()
virtual void done(int result)
\qmlmethod void QtQuick.Dialogs::Dialog::done(int result)
virtual void accept()
\qmlmethod void QtQuick.Dialogs::Dialog::accept()
bool isVisible() const
\qmlproperty bool QtQuick.Dialogs::Dialog::visible
void setFlags(Qt::WindowFlags flags)
Qt::WindowModality m_modality
void classBegin() override
Invoked after class creation, but before any properties have been set.
void setParentWindow(QWindow *window)
virtual void onCreate(QPlatformDialogHelper *dialog)
virtual int dialogCode() const
virtual void onHide(QPlatformDialogHelper *dialog)
virtual bool useNativeDialog() const
void setVisible(bool visible)
QQuickItem * findParentItem() const
virtual void onShow(QPlatformDialogHelper *dialog)
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
virtual void reject()
\qmlmethod void QtQuick.Dialogs::Dialog::reject()
static std::unique_ptr< QPlatformDialogHelper > createPlatformDialogHelper(QQuickDialogType type, QObject *parent)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
WindowModality
@ AA_DontUseNativeDialogs
Definition qnamespace.h:458
@ SingleShotConnection
size_t qstrlen(const char *str)
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLenum type
GLbitfield flags
GLhandleARB obj
[2]
GLuint64EXT * result
[6]
QPlatformTheme::DialogType toPlatformDialogType(QQuickDialogType quickDialogType)
static const char * qmlTypeName(const QObject *object)
QQuickDialogType
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:492
static const char * qmlTypeName(const QObject *object)
#define emit
#define Q_UNUSED(x)
QString title
[35]
QFileDialog dialog(this)
[1]
QGraphicsItem * item
aWidget window() -> setWindowTitle("New Window Title")
[2]