Qt 6.x
The Qt SDK
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 "qquicktabbar_p.h"
15
16#include <QtCore/private/qobject_p.h>
17#include <QtCore/qscopedvaluerollback.h>
18#include <QtQuick/private/qquickitem_p.h>
19#include <QtQuick/private/qquickitemchangelistener_p.h>
20#include <QtQuick/private/qquickwindowmodule_p_p.h>
21
23
28
87static const QQuickItemPrivate::ChangeTypes ItemChanges = QQuickItemPrivate::Visibility
89
90class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickApplicationWindowPrivate
93{
94 Q_DECLARE_PUBLIC(QQuickApplicationWindow)
95
96public:
98 {
99 complete = true;
100 }
101
103 {
104 return window->d_func();
105 }
106
107 QQmlListProperty<QObject> contentData();
108
109 void relayout();
110
111 void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override;
112 void itemVisibilityChanged(QQuickItem *item) override;
115 QPalette windowPalette() const override { return defaultPalette(); }
116
117 void updateFont(const QFont &f);
118 inline void setFont_helper(const QFont &f) {
119 if (font.resolveMask() == f.resolveMask() && font == f)
120 return;
121 updateFont(f);
122 }
123 void resolveFont();
124
125 void _q_updateActiveFocus();
126 void setActiveFocusControl(QQuickItem *item);
127
128 static void contentData_append(QQmlListProperty<QObject> *prop, QObject *obj);
129
130 void cancelBackground();
131 void executeBackground(bool complete = false);
132
134 void updateChildrenPalettes(const QPalette &parentPalette) override
135 {
136 // Update regular children
138
139 // And cover one special case
140 for (auto &&popup : q_func()->findChildren<QQuickPopup *>())
141 QQuickPopupPrivate::get(popup)->updateContentPalettes(parentPalette);
142 }
143
145 QQuickItem *appWindowContentItem = nullptr;
146 QQuickItem *menuBar = nullptr;
147 QQuickItem *header = nullptr;
148 QQuickItem *footer = nullptr;
151 QQuickItem *activeFocusControl = nullptr;
152 bool insideRelayout = 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{
171 if (!complete || insideRelayout)
172 return;
173
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 if (!p->widthValid() && qFuzzyIsNull(background->x())) {
191 background->setWidth(q->width());
192 p->widthValidFlag = false;
193 }
194 if (!p->heightValid() && qFuzzyIsNull(background->y())) {
195 background->setHeight(q->height());
196 p->heightValidFlag = false;
197 }
198 }
199}
200
202{
203 Q_UNUSED(item);
204 Q_UNUSED(change);
205 Q_UNUSED(diff);
206 relayout();
207}
208
210{
211 Q_UNUSED(item);
212 relayout();
213}
214
216{
217 Q_UNUSED(item);
218 relayout();
219}
220
222{
223 Q_UNUSED(item);
224 relayout();
225}
226
228{
230 const bool changed = font != f;
231 font = f;
232
233 QQuickControlPrivate::updateFontRecur(q->QQuickWindow::contentItem(), f);
234
235 const QList<QQuickPopup *> popups = q->findChildren<QQuickPopup *>();
236 for (QQuickPopup *popup : popups)
237 QQuickControlPrivate::get(static_cast<QQuickControl *>(popup->popupItem()))->inheritFont(f);
238
239 if (changed)
240 emit q->fontChanged();
241}
242
244{
246 setFont_helper(resolvedFont);
247}
248
250{
251 QQuickItem *item = window->activeFocusItem();
252 while (item) {
253 if (qobject_cast<QQuickControl *>(item) || qobject_cast<QQuickTextField *>(item) || qobject_cast<QQuickTextArea *>(item))
254 return item;
255 item = item->parentItem();
256 }
257 return item;
258}
259
261{
264}
265
267{
269 if (activeFocusControl != control) {
270 activeFocusControl = control;
271 emit q->activeFocusControlChanged();
272 }
273}
274
276{
278
279 // associate "top-level" popups with the window as soon as they are added to the default property
280 if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(obj))
281 QQuickPopupPrivate::get(popup)->setWindow(static_cast<QQuickApplicationWindow *>(prop->data));
282}
283
285{
288}
289
291{
294 return;
295
296 if (!background || complete)
298 if (complete)
300}
301
302QQuickApplicationWindow::QQuickApplicationWindow(QWindow *parent)
304{
305 connect(this, SIGNAL(activeFocusItemChanged()), this, SLOT(_q_updateActiveFocus()));
306}
307
308QQuickApplicationWindow::~QQuickApplicationWindow()
309{
311 d->setActiveFocusControl(nullptr);
312 disconnect(this, SIGNAL(activeFocusItemChanged()), this, SLOT(_q_updateActiveFocus()));
313 if (d->menuBar)
315 if (d->header)
317 if (d->footer)
319}
320
321QQuickApplicationWindowAttached *QQuickApplicationWindow::qmlAttachedProperties(QObject *object)
322{
323 return new QQuickApplicationWindowAttached(object);
324}
325
345{
347 if (!d->background)
348 d->executeBackground();
349 return d->background;
350}
351
352void QQuickApplicationWindow::setBackground(QQuickItem *background)
353{
355 if (d->background == background)
356 return;
357
358 if (!d->background.isExecuting())
359 d->cancelBackground();
360
362 d->background = background;
363 if (background) {
365 if (qFuzzyIsNull(background->z()))
366 background->setZ(-1);
368 d->relayout();
369 }
370 if (!d->background.isExecuting())
372}
373
396{
397 Q_D(const QQuickApplicationWindow);
398 return d->header;
399}
400
401void QQuickApplicationWindow::setHeader(QQuickItem *header)
402{
404 if (d->header == header)
405 return;
406
407 if (d->header) {
409 d->header->setParentItem(nullptr);
410 }
411 d->header = header;
412 if (header) {
415 p->addItemChangeListener(d, ItemChanges);
416 if (qFuzzyIsNull(header->z()))
417 header->setZ(1);
418 if (QQuickToolBar *toolBar = qobject_cast<QQuickToolBar *>(header))
419 toolBar->setPosition(QQuickToolBar::Header);
420 else if (QQuickTabBar *tabBar = qobject_cast<QQuickTabBar *>(header))
421 tabBar->setPosition(QQuickTabBar::Header);
422 else if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(header))
423 buttonBox->setPosition(QQuickDialogButtonBox::Header);
424 }
426 d->relayout();
428}
429
451{
452 Q_D(const QQuickApplicationWindow);
453 return d->footer;
454}
455
456void QQuickApplicationWindow::setFooter(QQuickItem *footer)
457{
459 if (d->footer == footer)
460 return;
461
462 if (d->footer) {
464 d->footer->setParentItem(nullptr);
465 }
466 d->footer = footer;
467 if (footer) {
470 p->addItemChangeListener(d, ItemChanges);
471 if (qFuzzyIsNull(footer->z()))
472 footer->setZ(1);
473 if (QQuickToolBar *toolBar = qobject_cast<QQuickToolBar *>(footer))
474 toolBar->setPosition(QQuickToolBar::Footer);
475 else if (QQuickTabBar *tabBar = qobject_cast<QQuickTabBar *>(footer))
476 tabBar->setPosition(QQuickTabBar::Footer);
477 else if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(footer))
478 buttonBox->setPosition(QQuickDialogButtonBox::Footer);
479 }
481 d->relayout();
483}
484
506{
508 return QQmlListProperty<QObject>(q->contentItem(), q,
513}
514
527{
529 if (!d->appWindowContentItem) {
530 d->appWindowContentItem = new QQuickContentItem(this, QQuickWindow::contentItem());
531 d->appWindowContentItem->setFlag(QQuickItem::ItemIsFocusScope);
532 d->appWindowContentItem->setFocus(true);
533 d->relayout();
534 }
535 return d->appWindowContentItem;
536}
537
553{
554 Q_D(const QQuickApplicationWindow);
555 return d->activeFocusControl;
556}
557
575{
576 Q_D(const QQuickApplicationWindow);
577 return d->font;
578}
579
580void QQuickApplicationWindow::setFont(const QFont &font)
581{
583 if (d->font.resolveMask() == font.resolveMask() && d->font == font)
584 return;
585
587 d->setFont_helper(resolvedFont);
588}
589
590void QQuickApplicationWindow::resetFont()
591{
592 setFont(QFont());
593}
594
610{
611 Q_D(const QQuickApplicationWindow);
612 return d->locale;
613}
614
615void QQuickApplicationWindow::setLocale(const QLocale &locale)
616{
618 if (d->locale == locale)
619 return;
620
621 d->locale = locale;
623
624 // TODO: internal QQuickPopupManager that provides reliable access to all QQuickPopup instances
626 for (QQuickPopup *popup : popups)
627 QQuickControlPrivate::get(static_cast<QQuickControl *>(popup->popupItem()))->updateLocale(locale, false); // explicit=false
628
630}
631
632void QQuickApplicationWindow::resetLocale()
633{
634 setLocale(QLocale());
635}
636
656{
657 Q_D(const QQuickApplicationWindow);
658 return d->menuBar;
659}
660
661void QQuickApplicationWindow::setMenuBar(QQuickItem *menuBar)
662{
664 if (d->menuBar == menuBar)
665 return;
666
667 if (d->menuBar) {
669 d->menuBar->setParentItem(nullptr);
670 }
671 d->menuBar = menuBar;
672 if (menuBar) {
675 p->addItemChangeListener(d, ItemChanges);
676 if (qFuzzyIsNull(menuBar->z()))
677 menuBar->setZ(2);
678 }
680 d->relayout();
681 emit menuBarChanged();
682}
683
685{
686 Q_D(const QQuickApplicationWindow);
687 return d->complete;
688}
689
691{
693 d->complete = false;
695 d->resolveFont();
696}
697
699{
701 d->complete = true;
702 d->executeBackground(true);
704 d->relayout();
705}
706
708{
711 d->relayout();
712}
713
715{
716public:
717 Q_DECLARE_PUBLIC(QQuickApplicationWindowAttached)
718
719 void windowChange(QQuickWindow *wnd);
720 void activeFocusChange();
721
724};
725
727{
729 if (window == wnd)
730 return;
731
732 QQuickApplicationWindow *oldWindow = qobject_cast<QQuickApplicationWindow *>(window);
733 if (oldWindow && !QQuickApplicationWindowPrivate::get(oldWindow))
734 oldWindow = nullptr; // being deleted (QTBUG-52731)
735
736 if (oldWindow) {
739 QObject::disconnect(oldWindow, &QQuickApplicationWindow::menuBarChanged,
745 } else if (window) {
746 disconnect(window, &QQuickWindow::activeFocusItemChanged,
748 }
749
750 QQuickApplicationWindow *newWindow = qobject_cast<QQuickApplicationWindow *>(wnd);
751 if (newWindow) {
754 QObject::connect(newWindow, &QQuickApplicationWindow::menuBarChanged,
760 } else if (wnd) {
761 connect(wnd, &QQuickWindow::activeFocusItemChanged,
763 }
764
765 window = wnd;
766 emit q->windowChanged();
767 emit q->contentItemChanged();
768
770 if ((oldWindow && oldWindow->menuBar()) || (newWindow && newWindow->menuBar()))
771 emit q->menuBarChanged();
772 if ((oldWindow && oldWindow->header()) || (newWindow && newWindow->header()))
773 emit q->headerChanged();
774 if ((oldWindow && oldWindow->footer()) || (newWindow && newWindow->footer()))
775 emit q->footerChanged();
776}
777
779{
781 QQuickItem *control = nullptr;
782 if (QQuickApplicationWindow *appWindow = qobject_cast<QQuickApplicationWindow *>(window))
783 control = appWindow->activeFocusControl();
784 else if (window)
786 if (activeFocusControl == control)
787 return;
788
789 activeFocusControl = control;
790 emit q->activeFocusControlChanged();
791}
792
795{
798 d->windowChange(item->window());
800 if (!d->window) {
801 QQuickItem *p = item;
802 while (p) {
803 if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(p->parent())) {
804 d->windowChange(popup->window());
806 }
807 p = p->parentItem();
808 }
809 }
810 } else if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent)) {
811 d->windowChange(popup->window());
813 }
814}
815
826{
828 return qobject_cast<QQuickApplicationWindow *>(d->window);
829}
830
841{
843 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
844 return window->contentItem();
845 return nullptr;
846}
847
859{
861 return d->activeFocusControl;
862}
863
875{
877 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
878 return window->header();
879 return nullptr;
880}
881
893{
895 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
896 return window->footer();
897 return nullptr;
898}
899
912{
914 if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
915 return window->menuBar();
916 return nullptr;
917}
918
920
921#include "moc_qquickapplicationwindow_p.cpp"
\reentrant
Definition qfont.h:20
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:1854
uint resolveMask() const
Definition qfont.h:251
QGraphicsWidget * window() const
QGraphicsItem * parentItem() const
Returns a pointer to this item's parent item.
void setY(qreal y)
Definition qlist.h:74
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:298
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3099
QList< T > findChildren(const QString &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:140
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:24
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)
virtual void itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &)
virtual void itemImplicitWidthChanged(QQuickItem *)
virtual void itemVisibilityChanged(QQuickItem *)
virtual void itemImplicitHeightChanged(QQuickItem *)
static void data_clear(QQmlListProperty< QObject > *)
void removeItemChangeListener(QQuickItemChangeListener *, ChangeTypes types)
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:64
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:73
void setParentItem(QQuickItem *parent)
qreal z
\qmlproperty real QtQuick::Item::z
Definition qquickitem.h:75
qreal y
Defines the item's y position relative to its parent.
Definition qquickitem.h:74
bool isVisible() const
void setHeight(qreal)
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
void setZ(qreal)
void setWidth(qreal)
void setY(qreal)
void setWindow(QQuickWindow *window)
static QQuickPopupPrivate * get(QQuickPopup *popup)
void updateContentPalettes(const QPalette &parentPalette)
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:483
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:547
\inmodule QtGui
Definition qwindow.h:63
virtual void resizeEvent(QResizeEvent *)
Override this to handle resize events (ev).
Definition qwindow.cpp:2384
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:303
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
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)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:483
#define emit
#define Q_UNUSED(x)
static QString backgroundName()
double qreal
Definition qtypes.h:92
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
myObject disconnect()
[26]
QGraphicsItem * item
aWidget window() -> setWindowTitle("New Window Title")
[2]
QMenuBar * menuBar
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent