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
qquickapplicationwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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
6#include "qquickpopup_p_p.h"
7#include "qquickcontrol_p_p.h"
8#include "qquicktextarea_p.h"
9#include "qquicktextfield_p.h"
10#include "qquicktoolbar_p.h"
11#include <private/qtquicktemplates2-config_p.h>
12#if QT_CONFIG(quicktemplates2_container)
13#include "qquicktabbar_p.h"
15#endif
18
19#include <QtCore/private/qobject_p.h>
20#include <QtCore/qscopedvaluerollback.h>
21#include <QtQuick/private/qquickitem_p.h>
22#include <QtQuick/private/qquickitemchangelistener_p.h>
23#include <QtQuick/private/qquickwindowmodule_p_p.h>
24
26
31
90static const QQuickItemPrivate::ChangeTypes ItemChanges = QQuickItemPrivate::Visibility
92
93class Q_QUICKTEMPLATES2_EXPORT QQuickApplicationWindowPrivate
96{
97 Q_DECLARE_PUBLIC(QQuickApplicationWindow)
98
99public:
101 {
102 return window->d_func();
103 }
104
105 QQmlListProperty<QObject> contentData();
106
107 void relayout();
108
109 void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override;
110 void itemVisibilityChanged(QQuickItem *item) override;
111 void itemImplicitWidthChanged(QQuickItem *item) override;
112 void itemImplicitHeightChanged(QQuickItem *item) override;
113 QPalette windowPalette() const override { return defaultPalette(); }
114
115 void updateFont(const QFont &f);
116 inline void setFont_helper(const QFont &f) {
117 if (font.resolveMask() == f.resolveMask() && font == f)
118 return;
119 updateFont(f);
120 }
121 void resolveFont();
122
123 void _q_updateActiveFocus();
124 void setActiveFocusControl(QQuickItem *item);
125
126 static void contentData_append(QQmlListProperty<QObject> *prop, QObject *obj);
127
128 void cancelBackground();
129 void executeBackground(bool complete = false);
130
132 void updateChildrenPalettes(const QPalette &parentPalette) override
133 {
134 // Update regular children
136
137 // And cover one special case
138 for (auto &&popup : q_func()->findChildren<QQuickPopup *>())
139 QQuickPopupPrivate::get(popup)->updateContentPalettes(parentPalette);
140 }
141
142 QQuickDeferredPointer<QQuickItem> background;
143 QQuickItem *appWindowContentItem = nullptr;
144 QQuickItem *menuBar = nullptr;
145 QQuickItem *header = nullptr;
146 QQuickItem *footer = nullptr;
149 QQuickItem *activeFocusControl = nullptr;
150 bool insideRelayout = false;
151 bool hasBackgroundWidth = false;
152 bool hasBackgroundHeight = false;
153};
154
156{
157 if (!item)
158 return;
159
160 item->setY(y);
162 if (!p->widthValid()) {
163 item->setWidth(width);
164 p->widthValidFlag = false;
165 }
166}
167
169{
172 return;
173
174 QScopedValueRollback<bool> guard(insideRelayout, true);
175 QQuickItem *content = q->contentItem();
176 qreal hh = header && header->isVisible() ? header->height() : 0;
177 qreal fh = footer && footer->isVisible() ? footer->height() : 0;
178 qreal mbh = menuBar && menuBar->isVisible() ? menuBar->height() : 0;
179
180 content->setY(mbh + hh);
181 content->setWidth(q->width());
182 content->setHeight(q->height() - mbh - hh - fh);
183
184 layoutItem(menuBar, -mbh - hh, q->width());
185 layoutItem(header, -hh, q->width());
186 layoutItem(footer, content->height(), q->width());
187
188 if (background) {
190 background->setWidth(q->width());
192 background->setHeight(q->height());
193 }
194}
195
197{
198 Q_UNUSED(diff);
199
200 if (!insideRelayout && item == background && change.sizeChange()) {
201 // Any time the background is resized (excluding our own resizing),
202 // we should respect it if it's explicit by storing the values of the flags.
204 hasBackgroundWidth = backgroundPrivate->widthValid();
205 hasBackgroundHeight = backgroundPrivate->heightValid();
206 }
207
208 relayout();
209}
210
216
222
228
230{
232 const bool changed = font != f;
233 font = f;
234
235 QQuickControlPrivate::updateFontRecur(q->QQuickWindow::contentItem(), f);
236
237 const QList<QQuickPopup *> popups = q->findChildren<QQuickPopup *>();
238 for (QQuickPopup *popup : popups)
239 QQuickControlPrivate::get(static_cast<QQuickControl *>(popup->popupItem()))->inheritFont(f);
240
241 if (changed)
242 emit q->fontChanged();
243}
244
250
252{
253 QQuickItem *item = window->activeFocusItem();
254 while (item) {
255 if (qobject_cast<QQuickControl *>(item) || qobject_cast<QQuickTextField *>(item) || qobject_cast<QQuickTextArea *>(item))
256 return item;
257 item = item->parentItem();
258 }
259 return item;
260}
261
267
269{
271 if (activeFocusControl != control) {
272 activeFocusControl = control;
273 emit q->activeFocusControlChanged();
274 }
275}
276
278{
280
281 // associate "top-level" popups with the window as soon as they are added to the default property
282 if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(obj))
283 QQuickPopupPrivate::get(popup)->setWindow(static_cast<QQuickApplicationWindow *>(prop->data));
284}
285
291
293{
296 return;
297
298 if (!background || complete)
300 if (complete)
302}
303
304QQuickApplicationWindow::QQuickApplicationWindow(QWindow *parent)
306{
307 connect(this, SIGNAL(activeFocusItemChanged()), this, SLOT(_q_updateActiveFocus()));
308}
309
310QQuickApplicationWindow::~QQuickApplicationWindow()
311{
313 d->setActiveFocusControl(nullptr);
314 disconnect(this, SIGNAL(activeFocusItemChanged()), this, SLOT(_q_updateActiveFocus()));
315 if (d->menuBar)
316 QQuickItemPrivate::get(d->menuBar)->removeItemChangeListener(d, ItemChanges);
317 if (d->header)
318 QQuickItemPrivate::get(d->header)->removeItemChangeListener(d, ItemChanges);
319 if (d->footer)
320 QQuickItemPrivate::get(d->footer)->removeItemChangeListener(d, ItemChanges);
321}
322
323QQuickApplicationWindowAttached *QQuickApplicationWindow::qmlAttachedProperties(QObject *object)
324{
325 return new QQuickApplicationWindowAttached(object);
326}
327
347{
349 if (!d->background)
350 d->executeBackground();
351 return d->background;
352}
353
354void QQuickApplicationWindow::setBackground(QQuickItem *background)
355{
357 if (d->background == background)
358 return;
359
360 if (!d->background.isExecuting())
361 d->cancelBackground();
362
363 if (d->background) {
364 d->hasBackgroundWidth = false;
365 d->hasBackgroundHeight = false;
366 }
368
369 d->background = background;
370
371 if (background) {
373
374 if (qFuzzyIsNull(background->z()))
375 background->setZ(-1);
376
378 d->hasBackgroundWidth = backgroundPrivate->widthValid();
379 d->hasBackgroundHeight = backgroundPrivate->heightValid();
380
382 d->relayout();
383 }
384 if (!d->background.isExecuting())
386}
387
410{
411 Q_D(const QQuickApplicationWindow);
412 return d->header;
413}
414
415void QQuickApplicationWindow::setHeader(QQuickItem *header)
416{
418 if (d->header == header)
419 return;
420
421 if (d->header) {
422 QQuickItemPrivate::get(d->header)->removeItemChangeListener(d, ItemChanges);
423 d->header->setParentItem(nullptr);
424 }
425 d->header = header;
426 if (header) {
429 p->addItemChangeListener(d, ItemChanges);
430 if (qFuzzyIsNull(header->z()))
431 header->setZ(1);
432 if (QQuickToolBar *toolBar = qobject_cast<QQuickToolBar *>(header))
433 toolBar->setPosition(QQuickToolBar::Header);
434#if QT_CONFIG(quicktemplates2_container)
435 else if (QQuickTabBar *tabBar = qobject_cast<QQuickTabBar *>(header))
436 tabBar->setPosition(QQuickTabBar::Header);
437 else if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(header))
438 buttonBox->setPosition(QQuickDialogButtonBox::Header);
439#endif
440 }
442 d->relayout();
444}
445
467{
468 Q_D(const QQuickApplicationWindow);
469 return d->footer;
470}
471
472void QQuickApplicationWindow::setFooter(QQuickItem *footer)
473{
475 if (d->footer == footer)
476 return;
477
478 if (d->footer) {
479 QQuickItemPrivate::get(d->footer)->removeItemChangeListener(d, ItemChanges);
480 d->footer->setParentItem(nullptr);
481 }
482 d->footer = footer;
483 if (footer) {
486 p->addItemChangeListener(d, ItemChanges);
487 if (qFuzzyIsNull(footer->z()))
488 footer->setZ(1);
489 if (QQuickToolBar *toolBar = qobject_cast<QQuickToolBar *>(footer))
490 toolBar->setPosition(QQuickToolBar::Footer);
491#if QT_CONFIG(quicktemplates2_container)
492 else if (QQuickTabBar *tabBar = qobject_cast<QQuickTabBar *>(footer))
493 tabBar->setPosition(QQuickTabBar::Footer);
494 else if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(footer))
495 buttonBox->setPosition(QQuickDialogButtonBox::Footer);
496#endif
497 }
499 d->relayout();
501}
502
532
545{
547 if (!d->appWindowContentItem) {
548 d->appWindowContentItem = new QQuickContentItem(this, QQuickWindow::contentItem());
549 d->appWindowContentItem->setFlag(QQuickItem::ItemIsFocusScope);
550 d->appWindowContentItem->setFocus(true);
551 d->relayout();
552 }
553 return d->appWindowContentItem;
554}
555
571{
572 Q_D(const QQuickApplicationWindow);
573 return d->activeFocusControl;
574}
575
593{
594 Q_D(const QQuickApplicationWindow);
595 return d->font;
596}
597
598void QQuickApplicationWindow::setFont(const QFont &font)
599{
601 if (d->font.resolveMask() == font.resolveMask() && d->font == font)
602 return;
603
605 d->setFont_helper(resolvedFont);
606}
607
608void QQuickApplicationWindow::resetFont()
609{
610 setFont(QFont());
611}
612
628{
629 Q_D(const QQuickApplicationWindow);
630 return d->locale;
631}
632
633void QQuickApplicationWindow::setLocale(const QLocale &locale)
634{
636 if (d->locale == locale)
637 return;
638
639 d->locale = locale;
641
642 // TODO: internal QQuickPopupManager that provides reliable access to all QQuickPopup instances
643 const QList<QQuickPopup *> popups = QQuickWindow::contentItem()->findChildren<QQuickPopup *>();
644 for (QQuickPopup *popup : popups)
645 QQuickControlPrivate::get(static_cast<QQuickControl *>(popup->popupItem()))->updateLocale(locale, false); // explicit=false
646
648}
649
650void QQuickApplicationWindow::resetLocale()
651{
652 setLocale(QLocale());
653}
654
674{
675 Q_D(const QQuickApplicationWindow);
676 return d->menuBar;
677}
678
679void QQuickApplicationWindow::setMenuBar(QQuickItem *menuBar)
680{
682 if (d->menuBar == menuBar)
683 return;
684
685 if (d->menuBar) {
686 QQuickItemPrivate::get(d->menuBar)->removeItemChangeListener(d, ItemChanges);
687 d->menuBar->setParentItem(nullptr);
688 }
689 d->menuBar = menuBar;
690 if (menuBar) {
693 p->addItemChangeListener(d, ItemChanges);
694 if (qFuzzyIsNull(menuBar->z()))
695 menuBar->setZ(2);
696 }
698 d->relayout();
699 emit menuBarChanged();
700}
701
703{
704 Q_D(const QQuickApplicationWindow);
705 return d->componentComplete;
706}
707
709{
711 d->componentComplete = false;
713 d->resolveFont();
714}
715
717{
719 d->componentComplete = true;
720 d->executeBackground(true);
722 d->relayout();
723}
724
731
733{
734public:
735 Q_DECLARE_PUBLIC(QQuickApplicationWindowAttached)
736
737 void windowChange(QQuickWindow *wnd);
738 void activeFocusChange();
739
742};
743
745{
747 if (window == wnd)
748 return;
749
750 QQuickApplicationWindow *oldWindow = qobject_cast<QQuickApplicationWindow *>(window);
751 if (oldWindow && !QQuickApplicationWindowPrivate::get(oldWindow))
752 oldWindow = nullptr; // being deleted (QTBUG-52731)
753
754 if (oldWindow) {
757 QObject::disconnect(oldWindow, &QQuickApplicationWindow::menuBarChanged,
763 } else if (window) {
764 disconnect(window, &QQuickWindow::activeFocusItemChanged,
766 }
767
768 QQuickApplicationWindow *newWindow = qobject_cast<QQuickApplicationWindow *>(wnd);
769 if (newWindow) {
772 QObject::connect(newWindow, &QQuickApplicationWindow::menuBarChanged,
778 } else if (wnd) {
779 connect(wnd, &QQuickWindow::activeFocusItemChanged,
781 }
782
783 window = wnd;
784 emit q->windowChanged();
785 emit q->contentItemChanged();
786
788 if ((oldWindow && oldWindow->menuBar()) || (newWindow && newWindow->menuBar()))
789 emit q->menuBarChanged();
790 if ((oldWindow && oldWindow->header()) || (newWindow && newWindow->header()))
791 emit q->headerChanged();
792 if ((oldWindow && oldWindow->footer()) || (newWindow && newWindow->footer()))
793 emit q->footerChanged();
794}
795
797{
799 QQuickItem *control = nullptr;
800 if (QQuickApplicationWindow *appWindow = qobject_cast<QQuickApplicationWindow *>(window))
801 control = appWindow->activeFocusControl();
802 else if (window)
804 if (activeFocusControl == control)
805 return;
806
807 activeFocusControl = control;
808 emit q->activeFocusControlChanged();
809}
810
813{
815 if (QQuickItem *item = qobject_cast<QQuickItem *>(parent)) {
816 d->windowChange(item->window());
818 if (!d->window) {
819 QQuickItem *p = item;
820 while (p) {
821 if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(p->parent())) {
822 d->windowChange(popup->window());
824 }
825 p = p->parentItem();
826 }
827 }
828 } else if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent)) {
829 d->windowChange(popup->window());
831 }
832}
833
844{
846 return qobject_cast<QQuickApplicationWindow *>(d->window);
847}
848
859{
861 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
862 return window->contentItem();
863 return nullptr;
864}
865
877{
879 return d->activeFocusControl;
880}
881
893{
895 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
896 return window->header();
897 return nullptr;
898}
899
911{
913 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
914 return window->footer();
915 return nullptr;
916}
917
930{
932 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
933 return window->menuBar();
934 return nullptr;
935}
936
938
939#include "moc_qquickapplicationwindow_p.cpp"
\reentrant
Definition qfont.h:22
QFont resolve(const QFont &) const
Returns a new QFont that has attributes copied from other that have not been previously set on this f...
Definition qfont.cpp:1893
uint resolveMask() const
Definition qfont.h:312
QGraphicsWidget * window() const
QGraphicsItem * parentItem() const
Returns a pointer to this item's parent item.
void setY(qreal y)
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
\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
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
QList< T > findChildren(QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
Returns all children of this object with the given name that can be cast to type T,...
Definition qobject.h:164
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
QQuickApplicationWindowAttached(QObject *parent=nullptr)
QQmlListProperty< QObject > contentData()
\qmlproperty list<QtObject> QtQuick.Controls::ApplicationWindow::contentData \qmldefault
void updateChildrenPalettes(const QPalette &parentPalette) override
void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override
void itemImplicitWidthChanged(QQuickItem *item) override
static void contentData_append(QQmlListProperty< QObject > *prop, QObject *obj)
static QQuickApplicationWindowPrivate * get(QQuickApplicationWindow *window)
QQuickDeferredPointer< QQuickItem > background
void executeBackground(bool complete=false)
void itemVisibilityChanged(QQuickItem *item) override
void itemImplicitHeightChanged(QQuickItem *item) override
void resizeEvent(QResizeEvent *event) override
Override this to handle resize events (ev).
void classBegin() override
Invoked after class creation, but before any properties have been set.
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
static void updateLocaleRecur(QQuickItem *item, const QLocale &l)
static void hideOldItem(QQuickItem *item)
static QQuickControlPrivate * get(QQuickControl *control)
static void updateFontRecur(QQuickItem *item, const QFont &font)
static void data_clear(QQmlListProperty< QObject > *)
static qsizetype data_count(QQmlListProperty< QObject > *)
\qmlproperty list<QtObject> QtQuick::Item::data \qmldefault
static QObject * data_at(QQmlListProperty< QObject > *, qsizetype)
static void data_append(QQmlListProperty< QObject > *, QObject *)
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:72
void setParentItem(QQuickItem *parent)
qreal z
\qmlproperty real QtQuick::Item::z
Definition qquickitem.h:74
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:73
bool isVisible() const
void setHeight(qreal)
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
void setZ(qreal)
void setWidth(qreal)
void setY(qreal)
static QQuickPopupPrivate * get(QQuickPopup *popup)
void windowChanged(QQuickWindow *window)
static QPalette palette(Scope scope)
static QFont font(Scope scope)
void updateChildrenPalettes(const QPalette &parentPalette) override
void classBegin() override
Invoked after class creation, but before any properties have been set.
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
QQuickItem * contentItem
\qmlattachedproperty Item Window::contentItem
\inmodule QtCore\reentrant
Definition qrect.h:484
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:548
\inmodule QtGui
Definition qwindow.h:63
virtual void resizeEvent(QResizeEvent *)
Override this to handle resize events (ev).
Definition qwindow.cpp:2462
Combined button and popup list for selecting options.
static QDBusError::ErrorType get(const char *name)
static QString header(const QString &name)
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLfloat GLfloat f
GLint GLsizei width
GLint y
struct _cl_event * event
GLhandleARB obj
[2]
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
static void layoutItem(QQuickItem *item, qreal y, qreal width)
static QT_BEGIN_NAMESPACE const QQuickItemPrivate::ChangeTypes ItemChanges
Styled top-level window with support for a header and footer.
static QQuickItem * findActiveFocusControl(QQuickWindow *window)
void quickCancelDeferred(QObject *object, const QString &property)
void quickCompleteDeferred(QObject *object, const QString &property, QQuickDeferredPointer< T > &delegate)
void quickBeginDeferred(QObject *object, const QString &property, QQuickDeferredPointer< T > &delegate)
#define emit
#define Q_UNUSED(x)
static QString backgroundName()
double qreal
Definition qtypes.h:187
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
myObject disconnect()
[26]
QGraphicsItem * item
aWidget window() -> setWindowTitle("New Window Title")
[2]
QMenuBar * menuBar
[0]