1// Copyright (C) 2016 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
4#include "qwindow.h"
5
6#include <qpa/qplatformwindow.h>
7#include <qpa/qplatformintegration.h>
8#ifndef QT_NO_CONTEXTMENU
9#include <qpa/qplatformtheme.h>
10#endif
11#include "qsurfaceformat.h"
12#ifndef QT_NO_OPENGL
13#include <qpa/qplatformopenglcontext.h>
14#include "qopenglcontext.h"
15#include "qopenglcontext_p.h"
16#endif
17#include "qscreen.h"
18
19#include "qwindow_p.h"
20#include "qguiapplication_p.h"
21#if QT_CONFIG(accessibility)
22# include "qaccessible.h"
23#endif
24#include "qhighdpiscaling_p.h"
25#if QT_CONFIG(draganddrop)
26#include "qshapedpixmapdndwindow_p.h"
27#endif // QT_CONFIG(draganddrop)
28
29#include <private/qevent_p.h>
30
31#include <QtCore/QTimer>
32#include <QtCore/QDebug>
33
34#include <QStyleHints>
35#include <qpa/qplatformcursor.h>
36#include <qpa/qplatformwindow_p.h>
37
38QT_BEGIN_NAMESPACE
39
40/*!
41 \class QWindow
42 \inmodule QtGui
43 \since 5.0
44 \brief The QWindow class represents a window in the underlying windowing system.
45
46 A window that is supplied a parent becomes a native child window of
47 their parent window.
48
49 An application will typically use QWidget or QQuickView for its UI, and not
50 QWindow directly. Still, it is possible to render directly to a QWindow
51 with QBackingStore or QOpenGLContext, when wanting to keep dependencies to
52 a minimum or when wanting to use OpenGL directly. The
53 \l{Raster Window Example} and \l{OpenGL Window Example}
54 are useful reference examples for how to render to a QWindow using
55 either approach.
56
57 \section1 Resource Management
58
59 Windows can potentially use a lot of memory. A usual measurement is
60 width times height times color depth. A window might also include multiple
61 buffers to support double and triple buffering, as well as depth and stencil
62 buffers. To release a window's memory resources, call the destroy() function.
63
64 \section1 Content Orientation
65
66 QWindow has reportContentOrientationChange() that can be used to specify
67 the layout of the window contents in relation to the screen. The content
68 orientation is simply a hint to the windowing system about which
69 orientation the window contents are in. It's useful when you wish to keep
70 the same window size, but rotate the contents instead, especially when
71 doing rotation animations between different orientations. The windowing
72 system might use this value to determine the layout of system popups or
73 dialogs.
74
75 \section1 Visibility and Windowing System Exposure
76
77 By default, the window is not visible, and you must call setVisible(true),
78 or show() or similar to make it visible. To make a window hidden again,
79 call setVisible(false) or hide(). The visible property describes the state
80 the application wants the window to be in. Depending on the underlying
81 system, a visible window might still not be shown on the screen. It could,
82 for instance, be covered by other opaque windows or moved outside the
83 physical area of the screen. On windowing systems that have exposure
84 notifications, the isExposed() accessor describes whether the window should
85 be treated as directly visible on screen. The exposeEvent() function is
86 called whenever an area of the window is invalidated, for example due to the
87 exposure in the windowing system changing. On windowing systems that do not
88 make this information visible to the application, isExposed() will simply
89 return the same value as isVisible().
90
91 QWindow::Visibility queried through visibility() is a convenience API
92 combining the functions of visible() and windowStates().
93
94 \section1 Rendering
95
96 There are two Qt APIs that can be used to render content into a window,
97 QBackingStore for rendering with a QPainter and flushing the contents
98 to a window with type QSurface::RasterSurface, and QOpenGLContext for
99 rendering with OpenGL to a window with type QSurface::OpenGLSurface.
100
101 The application can start rendering as soon as isExposed() returns \c true,
102 and can keep rendering until it isExposed() returns \c false. To find out when
103 isExposed() changes, reimplement exposeEvent(). The window will always get
104 a resize event before the first expose event.
105
106 \section1 Initial Geometry
107
108 If the window's width and height are left uninitialized, the window will
109 get a reasonable default geometry from the platform window. If the position
110 is left uninitialized, then the platform window will allow the windowing
111 system to position the window. For example on X11, the window manager
112 usually does some kind of smart positioning to try to avoid having new
113 windows completely obscure existing windows. However setGeometry()
114 initializes both the position and the size, so if you want a fixed size but
115 an automatic position, you should call resize() or setWidth() and
116 setHeight() instead.
117*/
118
119/*!
120 Creates a window as a top level on the \a targetScreen.
121
122 The window is not shown until setVisible(true), show(), or similar is called.
123
124 \sa setScreen()
125*/
126QWindow::QWindow(QScreen *targetScreen)
127 : QObject(*new QWindowPrivate(), nullptr)
128 , QSurface(QSurface::Window)
129{
130 Q_D(QWindow);
131 d->init(targetScreen);
132}
133
134static QWindow *nonDesktopParent(QWindow *parent)
135{
136 if (parent && parent->type() == Qt::Desktop) {
137 qWarning(msg: "QWindows cannot be reparented into desktop windows");
138 return nullptr;
139 }
140
141 return parent;
142}
143
144/*!
145 Creates a window as a child of the given \a parent window.
146
147 The window will be embedded inside the parent window, its coordinates
148 relative to the parent.
149
150 The screen is inherited from the parent.
151
152 \sa setParent()
153*/
154QWindow::QWindow(QWindow *parent)
155 : QWindow(*new QWindowPrivate(), parent)
156{
157}
158
159/*!
160 Creates a window as a child of the given \a parent window with the \a dd
161 private implementation.
162
163 The window will be embedded inside the parent window, its coordinates
164 relative to the parent.
165
166 The screen is inherited from the parent.
167
168 \internal
169 \sa setParent()
170*/
171QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
172 : QObject(dd, nonDesktopParent(parent))
173 , QSurface(QSurface::Window)
174{
175 Q_D(QWindow);
176 d->init();
177}
178
179/*!
180 Destroys the window.
181*/
182QWindow::~QWindow()
183{
184 Q_D(QWindow);
185 d->destroy();
186 QGuiApplicationPrivate::window_list.removeAll(t: this);
187 if (!QGuiApplicationPrivate::is_app_closing)
188 QGuiApplicationPrivate::instance()->modalWindowList.removeOne(t: this);
189
190 // thse are normally cleared in destroy(), but the window may in
191 // some cases end up becoming the focus window again, or receive an enter
192 // event. Clear it again here as a workaround. See QTBUG-75326.
193 if (QGuiApplicationPrivate::focus_window == this)
194 QGuiApplicationPrivate::focus_window = nullptr;
195 if (QGuiApplicationPrivate::currentMouseWindow == this)
196 QGuiApplicationPrivate::currentMouseWindow = nullptr;
197 if (QGuiApplicationPrivate::currentMousePressWindow == this)
198 QGuiApplicationPrivate::currentMousePressWindow = nullptr;
199
200 d->isWindow = false;
201}
202
203QWindowPrivate::QWindowPrivate()
204 = default;
205
206QWindowPrivate::~QWindowPrivate()
207 = default;
208
209void QWindowPrivate::init(QScreen *targetScreen)
210{
211 Q_Q(QWindow);
212
213 isWindow = true;
214 parentWindow = static_cast<QWindow *>(q->QObject::parent());
215
216 QScreen *connectScreen = targetScreen ? targetScreen : QGuiApplication::primaryScreen();
217
218 if (!parentWindow)
219 connectToScreen(topLevelScreen: connectScreen);
220
221 // If your application aborts here, you are probably creating a QWindow
222 // before the screen list is populated.
223 if (Q_UNLIKELY(!parentWindow && !topLevelScreen)) {
224 qFatal(msg: "Cannot create window: no screens available");
225 }
226 QGuiApplicationPrivate::window_list.prepend(t: q);
227
228 requestedFormat = QSurfaceFormat::defaultFormat();
229 devicePixelRatio = connectScreen->devicePixelRatio();
230
231 QObject::connect(sender: q, signal: &QWindow::screenChanged, context: q, slot: [q, this](QScreen *){
232 // We may have changed scaling; trigger resize event if needed,
233 // except on Windows, where we send resize events during WM_DPICHANGED
234 // event handling. FIXME: unify DPI change handling across all platforms.
235#ifndef Q_OS_WIN
236 if (q->handle()) {
237 QWindowSystemInterfacePrivate::GeometryChangeEvent gce(q, QHighDpi::fromNativePixels(value: q->handle()->geometry(), context: q));
238 QGuiApplicationPrivate::processGeometryChangeEvent(e: &gce);
239 }
240#endif
241 updateDevicePixelRatio();
242 });
243}
244
245/*!
246 \enum QWindow::Visibility
247 \since 5.1
248
249 This enum describes what part of the screen the window occupies or should
250 occupy.
251
252 \value Windowed The window occupies part of the screen, but not necessarily
253 the entire screen. This state will occur only on windowing systems which
254 support showing multiple windows simultaneously. In this state it is
255 possible for the user to move and resize the window manually, if
256 WindowFlags permit it and if it is supported by the windowing system.
257
258 \value Minimized The window is reduced to an entry or icon on the task bar,
259 dock, task list or desktop, depending on how the windowing system handles
260 minimized windows.
261
262 \value Maximized The window occupies one entire screen, and the titlebar is
263 still visible. On most windowing systems this is the state achieved by
264 clicking the maximize button on the toolbar.
265
266 \value FullScreen The window occupies one entire screen, is not resizable,
267 and there is no titlebar. On some platforms which do not support showing
268 multiple simultaneous windows, this can be the usual visibility when the
269 window is not hidden.
270
271 \value AutomaticVisibility This means to give the window a default visible
272 state, which might be fullscreen or windowed depending on the platform.
273 It can be given as a parameter to setVisibility but will never be
274 read back from the visibility accessor.
275
276 \value Hidden The window is not visible in any way, however it may remember
277 a latent visibility which can be restored by setting AutomaticVisibility.
278*/
279
280/*!
281 \property QWindow::visibility
282 \brief the screen-occupation state of the window
283 \since 5.1
284
285 Visibility is whether the window should appear in the windowing system as
286 normal, minimized, maximized, fullscreen or hidden.
287
288 To set the visibility to AutomaticVisibility means to give the window
289 a default visible state, which might be fullscreen or windowed depending on
290 the platform.
291 When reading the visibility property you will always get the actual state,
292 never AutomaticVisibility.
293*/
294QWindow::Visibility QWindow::visibility() const
295{
296 Q_D(const QWindow);
297 return d->visibility;
298}
299
300void QWindow::setVisibility(Visibility v)
301{
302 switch (v) {
303 case Hidden:
304 hide();
305 break;
306 case AutomaticVisibility:
307 show();
308 break;
309 case Windowed:
310 showNormal();
311 break;
312 case Minimized:
313 showMinimized();
314 break;
315 case Maximized:
316 showMaximized();
317 break;
318 case FullScreen:
319 showFullScreen();
320 break;
321 default:
322 Q_ASSERT(false);
323 }
324}
325
326/*
327 Subclasses may override this function to run custom setVisible
328 logic. Subclasses that do so must call the base class implementation
329 at some point to make the native window visible, and must not
330 call QWindow::setVisble() since that will recurse back here.
331*/
332void QWindowPrivate::setVisible(bool visible)
333{
334 Q_Q(QWindow);
335
336 if (this->visible != visible) {
337 this->visible = visible;
338 emit q->visibleChanged(arg: visible);
339 updateVisibility();
340 } else if (platformWindow) {
341 // Visibility hasn't changed, and the platform window is in sync
342 return;
343 }
344
345 if (!platformWindow) {
346 // If we have a parent window, but the parent hasn't been created yet, we
347 // can defer creation until the parent is created or we're re-parented.
348 if (parentWindow && !parentWindow->handle())
349 return;
350
351 // We only need to create the window if it's being shown
352 if (visible) {
353 // FIXME: At this point we've already updated the visible state of
354 // the QWindow, so if the platform layer reads the window state during
355 // creation, and reflects that in the native window, it will end up
356 // with a visible window. This may in turn result in resize or expose
357 // events from the platform before we have sent the show event below.
358 q->create();
359 }
360 }
361
362 if (visible) {
363 // remove posted quit events when showing a new window
364 QCoreApplication::removePostedEvents(qApp, eventType: QEvent::Quit);
365
366 if (q->type() == Qt::Window) {
367 QGuiApplicationPrivate *app_priv = QGuiApplicationPrivate::instance();
368 QString &firstWindowTitle = app_priv->firstWindowTitle;
369 if (!firstWindowTitle.isEmpty()) {
370 q->setTitle(firstWindowTitle);
371 firstWindowTitle = QString();
372 }
373 if (!app_priv->forcedWindowIcon.isNull())
374 q->setIcon(app_priv->forcedWindowIcon);
375
376 // Handling of the -qwindowgeometry, -geometry command line arguments
377 static bool geometryApplied = false;
378 if (!geometryApplied) {
379 geometryApplied = true;
380 QGuiApplicationPrivate::applyWindowGeometrySpecificationTo(window: q);
381 }
382 }
383
384 QShowEvent showEvent;
385 QGuiApplication::sendEvent(receiver: q, event: &showEvent);
386 }
387
388 if (q->isModal()) {
389 if (visible)
390 QGuiApplicationPrivate::showModalWindow(window: q);
391 else
392 QGuiApplicationPrivate::hideModalWindow(window: q);
393 // QShapedPixmapWindow is used on some platforms for showing a drag pixmap, so don't block
394 // input to this window as it is performing a drag - QTBUG-63846
395 } else if (visible && QGuiApplication::modalWindow()
396#if QT_CONFIG(draganddrop)
397 && !qobject_cast<QShapedPixmapWindow *>(object: q)
398#endif // QT_CONFIG(draganddrop)
399 ) {
400 QGuiApplicationPrivate::updateBlockedStatus(window: q);
401 }
402
403#ifndef QT_NO_CURSOR
404 if (visible && (hasCursor || QGuiApplication::overrideCursor()))
405 applyCursor();
406#endif
407
408 if (platformWindow)
409 platformWindow->setVisible(visible);
410
411 if (!visible) {
412 QHideEvent hideEvent;
413 QGuiApplication::sendEvent(receiver: q, event: &hideEvent);
414 }
415}
416
417void QWindowPrivate::updateVisibility()
418{
419 Q_Q(QWindow);
420
421 QWindow::Visibility old = visibility;
422
423 if (!visible)
424 visibility = QWindow::Hidden;
425 else if (windowState & Qt::WindowMinimized)
426 visibility = QWindow::Minimized;
427 else if (windowState & Qt::WindowFullScreen)
428 visibility = QWindow::FullScreen;
429 else if (windowState & Qt::WindowMaximized)
430 visibility = QWindow::Maximized;
431 else
432 visibility = QWindow::Windowed;
433
434 if (visibility != old)
435 emit q->visibilityChanged(visibility);
436}
437
438void QWindowPrivate::updateSiblingPosition(SiblingPosition position)
439{
440 Q_Q(QWindow);
441
442 if (!q->parent())
443 return;
444
445 QObjectList &siblings = q->parent()->d_ptr->children;
446
447 const qsizetype siblingCount = siblings.size() - 1;
448 if (siblingCount == 0)
449 return;
450
451 const qsizetype currentPosition = siblings.indexOf(t: q);
452 Q_ASSERT(currentPosition >= 0);
453
454 const qsizetype targetPosition = position == PositionTop ? siblingCount : 0;
455
456 if (currentPosition == targetPosition)
457 return;
458
459 siblings.move(from: currentPosition, to: targetPosition);
460}
461
462bool QWindowPrivate::windowRecreationRequired(QScreen *newScreen) const
463{
464 Q_Q(const QWindow);
465 const QScreen *oldScreen = q->screen();
466 return oldScreen != newScreen && (platformWindow || !oldScreen)
467 && !(oldScreen && oldScreen->virtualSiblings().contains(t: newScreen));
468}
469
470void QWindowPrivate::disconnectFromScreen()
471{
472 if (topLevelScreen)
473 topLevelScreen = nullptr;
474}
475
476void QWindowPrivate::connectToScreen(QScreen *screen)
477{
478 disconnectFromScreen();
479 topLevelScreen = screen;
480}
481
482void QWindowPrivate::emitScreenChangedRecursion(QScreen *newScreen)
483{
484 Q_Q(QWindow);
485 emit q->screenChanged(screen: newScreen);
486 for (QObject *child : q->children()) {
487 if (child->isWindowType())
488 static_cast<QWindow *>(child)->d_func()->emitScreenChangedRecursion(newScreen);
489 }
490}
491
492void QWindowPrivate::setTopLevelScreen(QScreen *newScreen, bool recreate)
493{
494 Q_Q(QWindow);
495 if (parentWindow) {
496 qWarning() << q << '(' << newScreen << "): Attempt to set a screen on a child window.";
497 return;
498 }
499 if (newScreen != topLevelScreen) {
500 const bool shouldRecreate = recreate && windowRecreationRequired(newScreen);
501 const bool shouldShow = visibilityOnDestroy && !topLevelScreen;
502 if (shouldRecreate && platformWindow)
503 q->destroy();
504 connectToScreen(screen: newScreen);
505 if (shouldShow)
506 q->setVisible(true);
507 else if (newScreen && shouldRecreate)
508 create(recursive: true);
509 emitScreenChangedRecursion(newScreen);
510 }
511}
512
513void QWindowPrivate::create(bool recursive, WId nativeHandle)
514{
515 Q_Q(QWindow);
516 if (platformWindow)
517 return;
518
519 // avoid losing update requests when re-creating
520 const bool needsUpdate = updateRequestPending;
521 // the platformWindow, if there was one, is now gone, so make this flag reflect reality now
522 updateRequestPending = false;
523
524 if (q->parent())
525 q->parent()->create();
526
527 // QPlatformWindow will poll geometry() during construction below. Set the
528 // screen here so that high-dpi scaling will use the correct scale factor.
529 if (q->isTopLevel()) {
530 if (QScreen *screen = screenForGeometry(rect: geometry))
531 setTopLevelScreen(newScreen: screen, recreate: false);
532 }
533
534 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
535 platformWindow = nativeHandle ? platformIntegration->createForeignWindow(q, nativeHandle)
536 : platformIntegration->createPlatformWindow(window: q);
537 Q_ASSERT(platformWindow);
538
539 if (!platformWindow) {
540 qWarning() << "Failed to create platform window for" << q << "with flags" << q->flags();
541 return;
542 }
543
544 platformWindow->initialize();
545
546 QObjectList childObjects = q->children();
547 for (int i = 0; i < childObjects.size(); i ++) {
548 QObject *object = childObjects.at(i);
549 if (!object->isWindowType())
550 continue;
551
552 QWindow *childWindow = static_cast<QWindow *>(object);
553 if (recursive)
554 childWindow->d_func()->create(recursive);
555
556 // The child may have had deferred creation due to this window not being created
557 // at the time setVisible was called, so we re-apply the visible state, which
558 // may result in creating the child, and emitting the appropriate signals.
559 if (childWindow->isVisible())
560 childWindow->setVisible(true);
561
562 if (QPlatformWindow *childPlatformWindow = childWindow->d_func()->platformWindow)
563 childPlatformWindow->setParent(this->platformWindow);
564 }
565
566 QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceCreated);
567 QGuiApplication::sendEvent(receiver: q, event: &e);
568
569 updateDevicePixelRatio();
570
571 if (needsUpdate)
572 q->requestUpdate();
573}
574
575void QWindowPrivate::clearFocusObject()
576{
577}
578
579// Allows for manipulating the suggested geometry before a resize/move
580// event in derived classes for platforms that support it, for example to
581// implement heightForWidth().
582QRectF QWindowPrivate::closestAcceptableGeometry(const QRectF &rect) const
583{
584 Q_UNUSED(rect);
585 return QRectF();
586}
587
588void QWindowPrivate::setMinOrMaxSize(QSize *oldSizeMember, const QSize &size,
589 qxp::function_ref<void()> funcWidthChanged,
590 qxp::function_ref<void()> funcHeightChanged)
591{
592 Q_Q(QWindow);
593 Q_ASSERT(oldSizeMember);
594 const QSize adjustedSize =
595 size.expandedTo(otherSize: QSize(0, 0)).boundedTo(otherSize: QSize(QWINDOWSIZE_MAX, QWINDOWSIZE_MAX));
596 if (*oldSizeMember == adjustedSize)
597 return;
598 const bool widthChanged = adjustedSize.width() != oldSizeMember->width();
599 const bool heightChanged = adjustedSize.height() != oldSizeMember->height();
600 *oldSizeMember = adjustedSize;
601
602 if (platformWindow && q->isTopLevel())
603 platformWindow->propagateSizeHints();
604
605 if (widthChanged)
606 funcWidthChanged();
607 if (heightChanged)
608 funcHeightChanged();
609
610 // resize window if current size is outside of min and max limits
611 if (minimumSize.width() <= maximumSize.width()
612 || minimumSize.height() <= maximumSize.height()) {
613 const QSize currentSize = q->size();
614 const QSize boundedSize = currentSize.expandedTo(otherSize: minimumSize).boundedTo(otherSize: maximumSize);
615 if (currentSize != boundedSize)
616 q->resize(newSize: boundedSize);
617 }
618}
619
620/*!
621 Sets the \a surfaceType of the window.
622
623 Specifies whether the window is meant for raster rendering with
624 QBackingStore, or OpenGL rendering with QOpenGLContext.
625
626 The surfaceType will be used when the native surface is created
627 in the create() function. Calling this function after the native
628 surface has been created requires calling destroy() and create()
629 to release the old native surface and create a new one.
630
631 \sa QBackingStore, QOpenGLContext, create(), destroy()
632*/
633void QWindow::setSurfaceType(SurfaceType surfaceType)
634{
635 Q_D(QWindow);
636 d->surfaceType = surfaceType;
637}
638
639/*!
640 Returns the surface type of the window.
641
642 \sa setSurfaceType()
643*/
644QWindow::SurfaceType QWindow::surfaceType() const
645{
646 Q_D(const QWindow);
647 return d->surfaceType;
648}
649
650/*!
651 \property QWindow::visible
652 \brief whether the window is visible or not
653
654 This property controls the visibility of the window in the windowing system.
655
656 By default, the window is not visible, you must call setVisible(true), or
657 show() or similar to make it visible.
658
659 \note Hiding a window does not remove the window from the windowing system,
660 it only hides it. On windowing systems that give full screen applications a
661 dedicated desktop (such as macOS), hiding a full screen window will not remove
662 that desktop, but leave it blank. Another window from the same application
663 might be shown full screen, and will fill that desktop. Use QWindow::close to
664 completely remove a window from the windowing system.
665
666 \sa show()
667*/
668void QWindow::setVisible(bool visible)
669{
670 Q_D(QWindow);
671
672 d->setVisible(visible);
673}
674
675bool QWindow::isVisible() const
676{
677 Q_D(const QWindow);
678
679 return d->visible;
680}
681
682/*!
683 Allocates the platform resources associated with the window.
684
685 It is at this point that the surface format set using setFormat() gets resolved
686 into an actual native surface. However, the window remains hidden until setVisible() is called.
687
688 Note that it is not usually necessary to call this function directly, as it will be implicitly
689 called by show(), setVisible(), winId(), and other functions that require access to the platform
690 resources.
691
692 Call destroy() to free the platform resources if necessary.
693
694 \sa destroy()
695*/
696void QWindow::create()
697{
698 Q_D(QWindow);
699 d->create(recursive: false);
700}
701
702/*!
703 Returns the window's platform id.
704
705 \note This function will cause the platform window to be created if it is not already.
706 Returns 0, if the platform window creation failed.
707
708 For platforms where this id might be useful, the value returned
709 will uniquely represent the window inside the corresponding screen.
710
711 \sa screen()
712*/
713WId QWindow::winId() const
714{
715 Q_D(const QWindow);
716
717 if (!d->platformWindow)
718 const_cast<QWindow *>(this)->create();
719
720 if (!d->platformWindow)
721 return 0;
722
723 return d->platformWindow->winId();
724}
725
726 /*!
727 Returns the parent window, if any.
728
729 If \a mode is IncludeTransients, then the transient parent is returned
730 if there is no parent.
731
732 A window without a parent is known as a top level window.
733
734 \since 5.9
735*/
736QWindow *QWindow::parent(AncestorMode mode) const
737{
738 Q_D(const QWindow);
739 return d->parentWindow ? d->parentWindow : (mode == IncludeTransients ? transientParent() : nullptr);
740}
741
742/*!
743 Sets the \a parent Window. This will lead to the windowing system managing
744 the clip of the window, so it will be clipped to the \a parent window.
745
746 Setting \a parent to be \nullptr will make the window become a top level
747 window.
748
749 If \a parent is a window created by fromWinId(), then the current window
750 will be embedded inside \a parent, if the platform supports it.
751*/
752void QWindow::setParent(QWindow *parent)
753{
754 parent = nonDesktopParent(parent);
755
756 Q_D(QWindow);
757 if (d->parentWindow == parent)
758 return;
759
760 QScreen *newScreen = parent ? parent->screen() : screen();
761 if (d->windowRecreationRequired(newScreen)) {
762 qWarning() << this << '(' << parent << "): Cannot change screens (" << screen() << newScreen << ')';
763 return;
764 }
765
766 QObject::setParent(parent);
767 d->parentWindow = parent;
768
769 if (parent)
770 d->disconnectFromScreen();
771 else
772 d->connectToScreen(screen: newScreen);
773
774 // If we were set visible, but not created because we were a child, and we're now
775 // re-parented into a created parent, or to being a top level, we need re-apply the
776 // visibility state, which will also create.
777 if (isVisible() && (!parent || parent->handle()))
778 setVisible(true);
779
780 if (d->platformWindow) {
781 if (parent)
782 parent->create();
783
784 d->platformWindow->setParent(parent ? parent->d_func()->platformWindow : nullptr);
785 }
786
787 QGuiApplicationPrivate::updateBlockedStatus(window: this);
788}
789
790/*!
791 Returns whether the window is top level, i.e. has no parent window.
792*/
793bool QWindow::isTopLevel() const
794{
795 Q_D(const QWindow);
796 return d->parentWindow == nullptr;
797}
798
799/*!
800 Returns whether the window is modal.
801
802 A modal window prevents other windows from getting any input.
803
804 \sa QWindow::modality
805*/
806bool QWindow::isModal() const
807{
808 Q_D(const QWindow);
809 return d->modality != Qt::NonModal;
810}
811
812/*! \property QWindow::modality
813 \brief the modality of the window
814
815 A modal window prevents other windows from receiving input events. Qt
816 supports two types of modality: Qt::WindowModal and Qt::ApplicationModal.
817
818 By default, this property is Qt::NonModal
819
820 \sa Qt::WindowModality
821*/
822
823Qt::WindowModality QWindow::modality() const
824{
825 Q_D(const QWindow);
826 return d->modality;
827}
828
829void QWindow::setModality(Qt::WindowModality modality)
830{
831 Q_D(QWindow);
832 if (d->modality == modality)
833 return;
834 d->modality = modality;
835 emit modalityChanged(modality);
836}
837
838/*! \fn void QWindow::modalityChanged(Qt::WindowModality modality)
839
840 This signal is emitted when the Qwindow::modality property changes to \a modality.
841*/
842
843/*!
844 Sets the window's surface \a format.
845
846 The format determines properties such as color depth, alpha, depth and
847 stencil buffer size, etc. For example, to give a window a transparent
848 background (provided that the window system supports compositing, and
849 provided that other content in the window does not make it opaque again):
850
851 \code
852 QSurfaceFormat format;
853 format.setAlphaBufferSize(8);
854 window.setFormat(format);
855 \endcode
856
857 The surface format will be resolved in the create() function. Calling
858 this function after create() has been called will not re-resolve the
859 surface format of the native surface.
860
861 When the format is not explicitly set via this function, the format returned
862 by QSurfaceFormat::defaultFormat() will be used. This means that when having
863 multiple windows, individual calls to this function can be replaced by one
864 single call to QSurfaceFormat::setDefaultFormat() before creating the first
865 window.
866
867 \sa create(), destroy(), QSurfaceFormat::setDefaultFormat()
868*/
869void QWindow::setFormat(const QSurfaceFormat &format)
870{
871 Q_D(QWindow);
872 d->requestedFormat = format;
873}
874
875/*!
876 Returns the requested surface format of this window.
877
878 If the requested format was not supported by the platform implementation,
879 the requestedFormat will differ from the actual window format.
880
881 This is the value set with setFormat().
882
883 \sa setFormat(), format()
884 */
885QSurfaceFormat QWindow::requestedFormat() const
886{
887 Q_D(const QWindow);
888 return d->requestedFormat;
889}
890
891/*!
892 Returns the actual format of this window.
893
894 After the window has been created, this function will return the actual surface format
895 of the window. It might differ from the requested format if the requested format could
896 not be fulfilled by the platform. It might also be a superset, for example certain
897 buffer sizes may be larger than requested.
898
899 \note Depending on the platform, certain values in this surface format may still
900 contain the requested values, that is, the values that have been passed to
901 setFormat(). Typical examples are the OpenGL version, profile and options. These may
902 not get updated during create() since these are context specific and a single window
903 may be used together with multiple contexts over its lifetime. Use the
904 QOpenGLContext's format() instead to query such values.
905
906 \sa create(), requestedFormat(), QOpenGLContext::format()
907*/
908QSurfaceFormat QWindow::format() const
909{
910 Q_D(const QWindow);
911 if (d->platformWindow)
912 return d->platformWindow->format();
913 return d->requestedFormat;
914}
915
916/*!
917 \property QWindow::flags
918 \brief the window flags of the window
919
920 The window flags control the window's appearance in the windowing system,
921 whether it's a dialog, popup, or a regular window, and whether it should
922 have a title bar, etc.
923
924 The actual window flags might differ from the flags set with setFlags()
925 if the requested flags could not be fulfilled.
926
927 \sa setFlag()
928*/
929void QWindow::setFlags(Qt::WindowFlags flags)
930{
931 Q_D(QWindow);
932 if (d->windowFlags == flags)
933 return;
934
935 if (d->platformWindow)
936 d->platformWindow->setWindowFlags(flags);
937 d->windowFlags = flags;
938}
939
940Qt::WindowFlags QWindow::flags() const
941{
942 Q_D(const QWindow);
943 Qt::WindowFlags flags = d->windowFlags;
944
945 if (d->platformWindow && d->platformWindow->isForeignWindow())
946 flags |= Qt::ForeignWindow;
947
948 return flags;
949}
950
951/*!
952 \since 5.9
953
954 Sets the window flag \a flag on this window if \a on is true;
955 otherwise clears the flag.
956
957 \sa setFlags(), flags(), type()
958*/
959void QWindow::setFlag(Qt::WindowType flag, bool on)
960{
961 Q_D(QWindow);
962 if (on)
963 setFlags(d->windowFlags | flag);
964 else
965 setFlags(d->windowFlags & ~flag);
966}
967
968/*!
969 Returns the type of the window.
970
971 This returns the part of the window flags that represents
972 whether the window is a dialog, tooltip, popup, regular window, etc.
973
974 \sa flags(), setFlags()
975*/
976Qt::WindowType QWindow::type() const
977{
978 return static_cast<Qt::WindowType>(int(flags() & Qt::WindowType_Mask));
979}
980
981/*!
982 \property QWindow::title
983 \brief the window's title in the windowing system
984
985 The window title might appear in the title area of the window decorations,
986 depending on the windowing system and the window flags. It might also
987 be used by the windowing system to identify the window in other contexts,
988 such as in the task switcher.
989
990 \sa flags()
991*/
992void QWindow::setTitle(const QString &title)
993{
994 Q_D(QWindow);
995 bool changed = false;
996 if (d->windowTitle != title) {
997 d->windowTitle = title;
998 changed = true;
999 }
1000 if (d->platformWindow && type() != Qt::Desktop)
1001 d->platformWindow->setWindowTitle(title);
1002 if (changed)
1003 emit windowTitleChanged(title);
1004}
1005
1006QString QWindow::title() const
1007{
1008 Q_D(const QWindow);
1009 return d->windowTitle;
1010}
1011
1012/*!
1013 \brief set the file name this window is representing.
1014
1015 The windowing system might use \a filePath to display the
1016 path of the document this window is representing in the tile bar.
1017
1018*/
1019void QWindow::setFilePath(const QString &filePath)
1020{
1021 Q_D(QWindow);
1022 d->windowFilePath = filePath;
1023 if (d->platformWindow)
1024 d->platformWindow->setWindowFilePath(filePath);
1025}
1026
1027/*!
1028 \brief the file name this window is representing.
1029
1030 \sa setFilePath()
1031*/
1032QString QWindow::filePath() const
1033{
1034 Q_D(const QWindow);
1035 return d->windowFilePath;
1036}
1037
1038/*!
1039 \brief Sets the window's \a icon in the windowing system
1040
1041 The window icon might be used by the windowing system for example to
1042 decorate the window, and/or in the task switcher.
1043
1044 \note On \macos, the window title bar icon is meant for windows representing
1045 documents, and will only show up if a file path is also set.
1046
1047 \sa setFilePath()
1048*/
1049void QWindow::setIcon(const QIcon &icon)
1050{
1051 Q_D(QWindow);
1052 d->windowIcon = icon;
1053 if (d->platformWindow)
1054 d->platformWindow->setWindowIcon(icon);
1055 QEvent e(QEvent::WindowIconChange);
1056 QCoreApplication::sendEvent(receiver: this, event: &e);
1057}
1058
1059/*!
1060 \brief Returns the window's icon in the windowing system
1061
1062 \sa setIcon()
1063*/
1064QIcon QWindow::icon() const
1065{
1066 Q_D(const QWindow);
1067 if (d->windowIcon.isNull())
1068 return QGuiApplication::windowIcon();
1069 return d->windowIcon;
1070}
1071
1072/*!
1073 Raise the window in the windowing system.
1074
1075 Requests that the window be raised to appear above other windows.
1076*/
1077void QWindow::raise()
1078{
1079 Q_D(QWindow);
1080
1081 d->updateSiblingPosition(position: QWindowPrivate::PositionTop);
1082
1083 if (d->platformWindow)
1084 d->platformWindow->raise();
1085}
1086
1087/*!
1088 Lower the window in the windowing system.
1089
1090 Requests that the window be lowered to appear below other windows.
1091*/
1092void QWindow::lower()
1093{
1094 Q_D(QWindow);
1095
1096 d->updateSiblingPosition(position: QWindowPrivate::PositionBottom);
1097
1098 if (d->platformWindow)
1099 d->platformWindow->lower();
1100}
1101
1102/*!
1103 \brief Start a system-specific resize operation
1104 \since 5.15
1105
1106 Calling this will start an interactive resize operation on the window by platforms
1107 that support it. The actual behavior may vary depending on the platform. Usually,
1108 it will make the window resize so that its edge follows the mouse cursor.
1109
1110 On platforms that support it, this method of resizing windows is preferred over
1111 \c setGeometry, because it allows a more native look and feel of resizing windows, e.g.
1112 letting the window manager snap this window against other windows, or special resizing
1113 behavior with animations when dragged to the edge of the screen.
1114
1115 \a edges should either be a single edge, or two adjacent edges (a corner). Other values
1116 are not allowed.
1117
1118 Returns true if the operation was supported by the system.
1119*/
1120bool QWindow::startSystemResize(Qt::Edges edges)
1121{
1122 Q_D(QWindow);
1123 if (Q_UNLIKELY(!isVisible() || !d->platformWindow || d->maximumSize == d->minimumSize))
1124 return false;
1125
1126 const bool isSingleEdge = edges == Qt::TopEdge || edges == Qt::RightEdge || edges == Qt::BottomEdge || edges == Qt::LeftEdge;
1127 const bool isCorner =
1128 edges == (Qt::TopEdge | Qt::LeftEdge) ||
1129 edges == (Qt::TopEdge | Qt::RightEdge) ||
1130 edges == (Qt::BottomEdge | Qt::RightEdge) ||
1131 edges == (Qt::BottomEdge | Qt::LeftEdge);
1132
1133 if (Q_UNLIKELY(!isSingleEdge && !isCorner)) {
1134 qWarning() << "Invalid edges" << edges << "passed to QWindow::startSystemResize, ignoring.";
1135 return false;
1136 }
1137
1138 return d->platformWindow->startSystemResize(edges);
1139}
1140
1141/*!
1142 \brief Start a system-specific move operation
1143 \since 5.15
1144
1145 Calling this will start an interactive move operation on the window by platforms
1146 that support it. The actual behavior may vary depending on the platform. Usually,
1147 it will make the window follow the mouse cursor until a mouse button is released.
1148
1149 On platforms that support it, this method of moving windows is preferred over
1150 \c setPosition, because it allows a more native look-and-feel of moving windows, e.g.
1151 letting the window manager snap this window against other windows, or special tiling
1152 or resizing behavior with animations when dragged to the edge of the screen.
1153 Furthermore, on some platforms such as Wayland, \c setPosition is not supported, so
1154 this is the only way the application can influence its position.
1155
1156 Returns true if the operation was supported by the system.
1157*/
1158bool QWindow::startSystemMove()
1159{
1160 Q_D(QWindow);
1161 if (Q_UNLIKELY(!isVisible() || !d->platformWindow))
1162 return false;
1163
1164 return d->platformWindow->startSystemMove();
1165}
1166
1167/*!
1168 \property QWindow::opacity
1169 \brief The opacity of the window in the windowing system.
1170 \since 5.1
1171
1172 If the windowing system supports window opacity, this can be used to fade the
1173 window in and out, or to make it semitransparent.
1174
1175 A value of 1.0 or above is treated as fully opaque, whereas a value of 0.0 or below
1176 is treated as fully transparent. Values inbetween represent varying levels of
1177 translucency between the two extremes.
1178
1179 The default value is 1.0.
1180*/
1181void QWindow::setOpacity(qreal level)
1182{
1183 Q_D(QWindow);
1184 if (level == d->opacity)
1185 return;
1186 d->opacity = level;
1187 if (d->platformWindow) {
1188 d->platformWindow->setOpacity(level);
1189 emit opacityChanged(opacity: level);
1190 }
1191}
1192
1193qreal QWindow::opacity() const
1194{
1195 Q_D(const QWindow);
1196 return d->opacity;
1197}
1198
1199/*!
1200 Sets the mask of the window.
1201
1202 The mask is a hint to the windowing system that the application does not
1203 want to receive mouse or touch input outside the given \a region.
1204
1205 The window manager may or may not choose to display any areas of the window
1206 not included in the mask, thus it is the application's responsibility to
1207 clear to transparent the areas that are not part of the mask.
1208*/
1209void QWindow::setMask(const QRegion &region)
1210{
1211 Q_D(QWindow);
1212 if (d->platformWindow)
1213 d->platformWindow->setMask(QHighDpi::toNativeLocalRegion(pointRegion: region, window: this));
1214 d->mask = region;
1215}
1216
1217/*!
1218 Returns the mask set on the window.
1219
1220 The mask is a hint to the windowing system that the application does not
1221 want to receive mouse or touch input outside the given region.
1222*/
1223QRegion QWindow::mask() const
1224{
1225 Q_D(const QWindow);
1226 return d->mask;
1227}
1228
1229/*!
1230 Requests the window to be activated, i.e. receive keyboard focus.
1231
1232 \sa isActive(), QGuiApplication::focusWindow()
1233*/
1234void QWindow::requestActivate()
1235{
1236 Q_D(QWindow);
1237 if (flags() & Qt::WindowDoesNotAcceptFocus) {
1238 qWarning() << "requestActivate() called for " << this << " which has Qt::WindowDoesNotAcceptFocus set.";
1239 return;
1240 }
1241 if (d->platformWindow)
1242 d->platformWindow->requestActivateWindow();
1243}
1244
1245/*!
1246 Returns if this window is exposed in the windowing system.
1247
1248 When the window is not exposed, it is shown by the application
1249 but it is still not showing in the windowing system, so the application
1250 should minimize animations and other graphical activities.
1251
1252 An exposeEvent() is sent every time this value changes.
1253
1254 \sa exposeEvent()
1255*/
1256bool QWindow::isExposed() const
1257{
1258 Q_D(const QWindow);
1259 return d->exposed;
1260}
1261
1262/*!
1263 \property QWindow::active
1264 \brief the active status of the window
1265 \since 5.1
1266
1267 \sa requestActivate()
1268*/
1269
1270/*!
1271 Returns \c true if the window is active.
1272
1273 This is the case for the window that has input focus as well as windows
1274 that are in the same parent / transient parent chain as the focus window.
1275
1276 Typically active windows should appear active from a style perspective.
1277
1278 To get the window that currently has focus, use QGuiApplication::focusWindow().
1279
1280 \sa requestActivate()
1281*/
1282bool QWindow::isActive() const
1283{
1284 Q_D(const QWindow);
1285 if (!d->platformWindow)
1286 return false;
1287
1288 QWindow *focus = QGuiApplication::focusWindow();
1289
1290 // Means the whole application lost the focus
1291 if (!focus)
1292 return false;
1293
1294 if (focus == this)
1295 return true;
1296
1297 if (QWindow *p = parent(mode: IncludeTransients))
1298 return p->isActive();
1299 else
1300 return isAncestorOf(child: focus);
1301}
1302
1303/*!
1304 \property QWindow::contentOrientation
1305 \brief the orientation of the window's contents
1306
1307 This is a hint to the window manager in case it needs to display
1308 additional content like popups, dialogs, status bars, or similar
1309 in relation to the window.
1310
1311 The recommended orientation is QScreen::orientation() but
1312 an application doesn't have to support all possible orientations,
1313 and thus can opt to ignore the current screen orientation.
1314
1315 The difference between the window and the content orientation
1316 determines how much to rotate the content by. QScreen::angleBetween(),
1317 QScreen::transformBetween(), and QScreen::mapBetween() can be used
1318 to compute the necessary transform.
1319
1320 The default value is Qt::PrimaryOrientation
1321*/
1322void QWindow::reportContentOrientationChange(Qt::ScreenOrientation orientation)
1323{
1324 Q_D(QWindow);
1325 if (d->contentOrientation == orientation)
1326 return;
1327 if (d->platformWindow)
1328 d->platformWindow->handleContentOrientationChange(orientation);
1329 d->contentOrientation = orientation;
1330 emit contentOrientationChanged(orientation);
1331}
1332
1333Qt::ScreenOrientation QWindow::contentOrientation() const
1334{
1335 Q_D(const QWindow);
1336 return d->contentOrientation;
1337}
1338
1339/*!
1340 Returns the ratio between physical pixels and device-independent pixels
1341 for the window. This value is dependent on the screen the window is on,
1342 and may change when the window is moved.
1343
1344 Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays.
1345
1346 \note For windows not backed by a platform window, meaning that create() was not
1347 called, the function will fall back to the associated QScreen's device pixel ratio.
1348
1349 \sa QScreen::devicePixelRatio()
1350*/
1351qreal QWindow::devicePixelRatio() const
1352{
1353 Q_D(const QWindow);
1354 return d->devicePixelRatio;
1355}
1356
1357/*
1358 Updates the cached devicePixelRatio value by polling for a new value.
1359 Sends QEvent::DevicePixelRatioChange to the window if the DPR has changed.
1360*/
1361void QWindowPrivate::updateDevicePixelRatio()
1362{
1363 Q_Q(QWindow);
1364
1365 // If there is no platform window use the associated screen's devicePixelRatio,
1366 // which typically is the primary screen and will be correct for single-display
1367 // systems (a very common case).
1368 const qreal newDevicePixelRatio = platformWindow ?
1369 platformWindow->devicePixelRatio() * QHighDpiScaling::factor(context: q) : q->screen()->devicePixelRatio();
1370
1371 if (newDevicePixelRatio == devicePixelRatio)
1372 return;
1373
1374 devicePixelRatio = newDevicePixelRatio;
1375 QEvent dprChangeEvent(QEvent::DevicePixelRatioChange);
1376 QGuiApplication::sendEvent(receiver: q, event: &dprChangeEvent);
1377}
1378
1379Qt::WindowState QWindowPrivate::effectiveState(Qt::WindowStates state)
1380{
1381 if (state & Qt::WindowMinimized)
1382 return Qt::WindowMinimized;
1383 else if (state & Qt::WindowFullScreen)
1384 return Qt::WindowFullScreen;
1385 else if (state & Qt::WindowMaximized)
1386 return Qt::WindowMaximized;
1387 return Qt::WindowNoState;
1388}
1389
1390/*!
1391 \brief set the screen-occupation state of the window
1392
1393 The window \a state represents whether the window appears in the
1394 windowing system as maximized, minimized, fullscreen, or normal.
1395
1396 The enum value Qt::WindowActive is not an accepted parameter.
1397
1398 \sa showNormal(), showFullScreen(), showMinimized(), showMaximized(), setWindowStates()
1399*/
1400void QWindow::setWindowState(Qt::WindowState state)
1401{
1402 setWindowStates(state);
1403}
1404
1405/*!
1406 \brief set the screen-occupation state of the window
1407 \since 5.10
1408
1409 The window \a state represents whether the window appears in the
1410 windowing system as maximized, minimized and/or fullscreen.
1411
1412 The window can be in a combination of several states. For example, if
1413 the window is both minimized and maximized, the window will appear
1414 minimized, but clicking on the task bar entry will restore it to the
1415 maximized state.
1416
1417 The enum value Qt::WindowActive should not be set.
1418
1419 \sa showNormal(), showFullScreen(), showMinimized(), showMaximized()
1420 */
1421void QWindow::setWindowStates(Qt::WindowStates state)
1422{
1423 Q_D(QWindow);
1424 if (state & Qt::WindowActive) {
1425 qWarning(msg: "QWindow::setWindowStates does not accept Qt::WindowActive");
1426 state &= ~Qt::WindowActive;
1427 }
1428
1429 if (d->platformWindow)
1430 d->platformWindow->setWindowState(state);
1431
1432 auto originalEffectiveState = QWindowPrivate::effectiveState(state: d->windowState);
1433 d->windowState = state;
1434 auto newEffectiveState = QWindowPrivate::effectiveState(state: d->windowState);
1435 if (newEffectiveState != originalEffectiveState)
1436 emit windowStateChanged(windowState: newEffectiveState);
1437
1438 d->updateVisibility();
1439}
1440
1441/*!
1442 \brief the screen-occupation state of the window
1443
1444 \sa setWindowState(), windowStates()
1445*/
1446Qt::WindowState QWindow::windowState() const
1447{
1448 Q_D(const QWindow);
1449 return QWindowPrivate::effectiveState(state: d->windowState);
1450}
1451
1452/*!
1453 \brief the screen-occupation state of the window
1454 \since 5.10
1455
1456 The window can be in a combination of several states. For example, if
1457 the window is both minimized and maximized, the window will appear
1458 minimized, but clicking on the task bar entry will restore it to
1459 the maximized state.
1460
1461 \sa setWindowStates()
1462*/
1463Qt::WindowStates QWindow::windowStates() const
1464{
1465 Q_D(const QWindow);
1466 return d->windowState;
1467}
1468
1469/*!
1470 \fn QWindow::windowStateChanged(Qt::WindowState windowState)
1471
1472 This signal is emitted when the \a windowState changes, either
1473 by being set explicitly with setWindowStates(), or automatically when
1474 the user clicks one of the titlebar buttons or by other means.
1475*/
1476
1477/*!
1478 \property QWindow::transientParent
1479 \brief the window for which this window is a transient pop-up
1480 \since 5.13
1481
1482 This is a hint to the window manager that this window is a dialog or pop-up
1483 on behalf of the transient parent.
1484
1485 In order to cause the window to be centered above its transient \a parent by
1486 default, depending on the window manager, it may also be necessary to call
1487 setFlags() with a suitable \l Qt::WindowType (such as \c Qt::Dialog).
1488
1489 \sa parent()
1490*/
1491void QWindow::setTransientParent(QWindow *parent)
1492{
1493 Q_D(QWindow);
1494 if (parent && !parent->isTopLevel()) {
1495 qWarning() << parent << "must be a top level window.";
1496 return;
1497 }
1498 if (parent == this) {
1499 qWarning() << "transient parent" << parent << "cannot be same as window";
1500 return;
1501 }
1502
1503 d->transientParent = parent;
1504
1505 QGuiApplicationPrivate::updateBlockedStatus(window: this);
1506 emit transientParentChanged(transientParent: parent);
1507}
1508
1509QWindow *QWindow::transientParent() const
1510{
1511 Q_D(const QWindow);
1512 return d->transientParent.data();
1513}
1514
1515/*
1516 The setter for the QWindow::transientParent property.
1517 The only reason this exists is to set the transientParentPropertySet flag
1518 so that Qt Quick knows whether it was set programmatically (because of
1519 Window declaration context) or because the user set the property.
1520*/
1521void QWindowPrivate::setTransientParent(QWindow *parent)
1522{
1523 Q_Q(QWindow);
1524 q->setTransientParent(parent);
1525 transientParentPropertySet = true;
1526}
1527
1528/*!
1529 \enum QWindow::AncestorMode
1530
1531 This enum is used to control whether or not transient parents
1532 should be considered ancestors.
1533
1534 \value ExcludeTransients Transient parents are not considered ancestors.
1535 \value IncludeTransients Transient parents are considered ancestors.
1536*/
1537
1538/*!
1539 Returns \c true if the window is an ancestor of the given \a child. If \a mode
1540 is IncludeTransients, then transient parents are also considered ancestors.
1541*/
1542bool QWindow::isAncestorOf(const QWindow *child, AncestorMode mode) const
1543{
1544 if (child->parent() == this || (mode == IncludeTransients && child->transientParent() == this))
1545 return true;
1546
1547 if (QWindow *parent = child->parent(mode)) {
1548 if (isAncestorOf(child: parent, mode))
1549 return true;
1550 } else if (handle() && child->handle()) {
1551 if (handle()->isAncestorOf(child: child->handle()))
1552 return true;
1553 }
1554
1555 return false;
1556}
1557
1558/*!
1559 Returns the minimum size of the window.
1560
1561 \sa setMinimumSize()
1562*/
1563QSize QWindow::minimumSize() const
1564{
1565 Q_D(const QWindow);
1566 return d->minimumSize;
1567}
1568
1569/*!
1570 Returns the maximum size of the window.
1571
1572 \sa setMaximumSize()
1573*/
1574QSize QWindow::maximumSize() const
1575{
1576 Q_D(const QWindow);
1577 return d->maximumSize;
1578}
1579
1580/*!
1581 Returns the base size of the window.
1582
1583 \sa setBaseSize()
1584*/
1585QSize QWindow::baseSize() const
1586{
1587 Q_D(const QWindow);
1588 return d->baseSize;
1589}
1590
1591/*!
1592 Returns the size increment of the window.
1593
1594 \sa setSizeIncrement()
1595*/
1596QSize QWindow::sizeIncrement() const
1597{
1598 Q_D(const QWindow);
1599 return d->sizeIncrement;
1600}
1601
1602/*!
1603 Sets the minimum size of the window.
1604
1605 This is a hint to the window manager to prevent resizing below the specified \a size.
1606
1607 \sa setMaximumSize(), minimumSize()
1608*/
1609void QWindow::setMinimumSize(const QSize &size)
1610{
1611 Q_D(QWindow);
1612 d->setMinOrMaxSize(
1613 oldSizeMember: &d->minimumSize, size, funcWidthChanged: [this, d]() { emit minimumWidthChanged(arg: d->minimumSize.width()); },
1614 funcHeightChanged: [this, d]() { emit minimumHeightChanged(arg: d->minimumSize.height()); });
1615}
1616
1617/*!
1618 \property QWindow::x
1619 \brief the x position of the window's geometry
1620*/
1621void QWindow::setX(int arg)
1622{
1623 Q_D(QWindow);
1624 if (x() != arg)
1625 setGeometry(QRect(arg, y(), width(), height()));
1626 else
1627 d->positionAutomatic = false;
1628}
1629
1630/*!
1631 \property QWindow::y
1632 \brief the y position of the window's geometry
1633*/
1634void QWindow::setY(int arg)
1635{
1636 Q_D(QWindow);
1637 if (y() != arg)
1638 setGeometry(QRect(x(), arg, width(), height()));
1639 else
1640 d->positionAutomatic = false;
1641}
1642
1643/*!
1644 \property QWindow::width
1645 \brief the width of the window's geometry
1646*/
1647void QWindow::setWidth(int arg)
1648{
1649 if (width() != arg)
1650 resize(w: arg, h: height());
1651}
1652
1653/*!
1654 \property QWindow::height
1655 \brief the height of the window's geometry
1656*/
1657void QWindow::setHeight(int arg)
1658{
1659 if (height() != arg)
1660 resize(w: width(), h: arg);
1661}
1662
1663/*!
1664 \property QWindow::minimumWidth
1665 \brief the minimum width of the window's geometry
1666*/
1667void QWindow::setMinimumWidth(int w)
1668{
1669 setMinimumSize(QSize(w, minimumHeight()));
1670}
1671
1672/*!
1673 \property QWindow::minimumHeight
1674 \brief the minimum height of the window's geometry
1675*/
1676void QWindow::setMinimumHeight(int h)
1677{
1678 setMinimumSize(QSize(minimumWidth(), h));
1679}
1680
1681/*!
1682 Sets the maximum size of the window.
1683
1684 This is a hint to the window manager to prevent resizing above the specified \a size.
1685
1686 \sa setMinimumSize(), maximumSize()
1687*/
1688void QWindow::setMaximumSize(const QSize &size)
1689{
1690 Q_D(QWindow);
1691 d->setMinOrMaxSize(
1692 oldSizeMember: &d->maximumSize, size, funcWidthChanged: [this, d]() { emit maximumWidthChanged(arg: d->maximumSize.width()); },
1693 funcHeightChanged: [this, d]() { emit maximumHeightChanged(arg: d->maximumSize.height()); });
1694}
1695
1696/*!
1697 \property QWindow::maximumWidth
1698 \brief the maximum width of the window's geometry
1699*/
1700void QWindow::setMaximumWidth(int w)
1701{
1702 setMaximumSize(QSize(w, maximumHeight()));
1703}
1704
1705/*!
1706 \property QWindow::maximumHeight
1707 \brief the maximum height of the window's geometry
1708*/
1709void QWindow::setMaximumHeight(int h)
1710{
1711 setMaximumSize(QSize(maximumWidth(), h));
1712}
1713
1714/*!
1715 Sets the base \a size of the window.
1716
1717 The base size is used to calculate a proper window size if the
1718 window defines sizeIncrement().
1719
1720 \sa setMinimumSize(), setMaximumSize(), setSizeIncrement(), baseSize()
1721*/
1722void QWindow::setBaseSize(const QSize &size)
1723{
1724 Q_D(QWindow);
1725 if (d->baseSize == size)
1726 return;
1727 d->baseSize = size;
1728 if (d->platformWindow && isTopLevel())
1729 d->platformWindow->propagateSizeHints();
1730}
1731
1732/*!
1733 Sets the size increment (\a size) of the window.
1734
1735 When the user resizes the window, the size will move in steps of
1736 sizeIncrement().width() pixels horizontally and
1737 sizeIncrement().height() pixels vertically, with baseSize() as the
1738 basis.
1739
1740 By default, this property contains a size with zero width and height.
1741
1742 The windowing system might not support size increments.
1743
1744 \sa setBaseSize(), setMinimumSize(), setMaximumSize()
1745*/
1746void QWindow::setSizeIncrement(const QSize &size)
1747{
1748 Q_D(QWindow);
1749 if (d->sizeIncrement == size)
1750 return;
1751 d->sizeIncrement = size;
1752 if (d->platformWindow && isTopLevel())
1753 d->platformWindow->propagateSizeHints();
1754}
1755
1756/*!
1757 Sets the geometry of the window, excluding its window frame, to a
1758 rectangle constructed from \a posx, \a posy, \a w and \a h.
1759
1760 The geometry is in relation to the virtualGeometry() of its screen.
1761
1762 \sa geometry()
1763*/
1764void QWindow::setGeometry(int posx, int posy, int w, int h)
1765{
1766 setGeometry(QRect(posx, posy, w, h));
1767}
1768
1769/*!
1770 \brief Sets the geometry of the window, excluding its window frame, to \a rect.
1771
1772 The geometry is in relation to the virtualGeometry() of its screen.
1773
1774 \sa geometry()
1775*/
1776void QWindow::setGeometry(const QRect &rect)
1777{
1778 Q_D(QWindow);
1779 d->positionAutomatic = false;
1780 const QRect oldRect = geometry();
1781 if (rect == oldRect)
1782 return;
1783
1784 d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
1785 if (d->platformWindow) {
1786 QScreen *newScreen = d->screenForGeometry(rect);
1787 if (newScreen && isTopLevel())
1788 d->setTopLevelScreen(newScreen, recreate: true);
1789 d->platformWindow->setGeometry(QHighDpi::toNativeWindowGeometry(value: rect, context: this));
1790 } else {
1791 d->geometry = rect;
1792
1793 if (rect.x() != oldRect.x())
1794 emit xChanged(arg: rect.x());
1795 if (rect.y() != oldRect.y())
1796 emit yChanged(arg: rect.y());
1797 if (rect.width() != oldRect.width())
1798 emit widthChanged(arg: rect.width());
1799 if (rect.height() != oldRect.height())
1800 emit heightChanged(arg: rect.height());
1801 }
1802}
1803
1804/*
1805 This is equivalent to QPlatformWindow::screenForGeometry, but in platform
1806 independent coordinates. The duplication is unfortunate, but there is a
1807 chicken and egg problem here: we cannot convert to native coordinates
1808 before we know which screen we are on.
1809*/
1810QScreen *QWindowPrivate::screenForGeometry(const QRect &newGeometry) const
1811{
1812 Q_Q(const QWindow);
1813 QScreen *currentScreen = q->screen();
1814 QScreen *fallback = currentScreen;
1815 QPoint center = newGeometry.center();
1816 if (!q->parent() && currentScreen && !currentScreen->geometry().contains(p: center)) {
1817 const auto screens = currentScreen->virtualSiblings();
1818 for (QScreen* screen : screens) {
1819 if (screen->geometry().contains(p: center))
1820 return screen;
1821 if (screen->geometry().intersects(r: newGeometry))
1822 fallback = screen;
1823 }
1824 }
1825 return fallback;
1826}
1827
1828
1829/*!
1830 Returns the geometry of the window, excluding its window frame.
1831
1832 The geometry is in relation to the virtualGeometry() of its screen.
1833
1834 \sa frameMargins(), frameGeometry()
1835*/
1836QRect QWindow::geometry() const
1837{
1838 Q_D(const QWindow);
1839 if (d->platformWindow) {
1840 const auto nativeGeometry = d->platformWindow->geometry();
1841 return QHighDpi::fromNativeWindowGeometry(value: nativeGeometry, context: this);
1842 }
1843 return d->geometry;
1844}
1845
1846/*!
1847 Returns the window frame margins surrounding the window.
1848
1849 \sa geometry(), frameGeometry()
1850*/
1851QMargins QWindow::frameMargins() const
1852{
1853 Q_D(const QWindow);
1854 if (d->platformWindow)
1855 return QHighDpi::fromNativePixels(value: d->platformWindow->frameMargins(), context: this);
1856 return QMargins();
1857}
1858
1859/*!
1860 Returns the geometry of the window, including its window frame.
1861
1862 The geometry is in relation to the virtualGeometry() of its screen.
1863
1864 \sa geometry(), frameMargins()
1865*/
1866QRect QWindow::frameGeometry() const
1867{
1868 Q_D(const QWindow);
1869 if (d->platformWindow) {
1870 QMargins m = frameMargins();
1871 return QHighDpi::fromNativeWindowGeometry(value: d->platformWindow->geometry(), context: this).adjusted(xp1: -m.left(), yp1: -m.top(), xp2: m.right(), yp2: m.bottom());
1872 }
1873 return d->geometry;
1874}
1875
1876/*!
1877 Returns the top left position of the window, including its window frame.
1878
1879 This returns the same value as frameGeometry().topLeft().
1880
1881 \sa geometry(), frameGeometry()
1882*/
1883QPoint QWindow::framePosition() const
1884{
1885 Q_D(const QWindow);
1886 if (d->platformWindow) {
1887 QMargins margins = frameMargins();
1888 return QHighDpi::fromNativeWindowGeometry(value: d->platformWindow->geometry().topLeft(), context: this) - QPoint(margins.left(), margins.top());
1889 }
1890 return d->geometry.topLeft();
1891}
1892
1893/*!
1894 Sets the upper left position of the window (\a point) including its window frame.
1895
1896 The position is in relation to the virtualGeometry() of its screen.
1897
1898 \sa setGeometry(), frameGeometry()
1899*/
1900void QWindow::setFramePosition(const QPoint &point)
1901{
1902 Q_D(QWindow);
1903 d->positionPolicy = QWindowPrivate::WindowFrameInclusive;
1904 d->positionAutomatic = false;
1905 if (d->platformWindow) {
1906 d->platformWindow->setGeometry(QHighDpi::toNativeWindowGeometry(value: QRect(point, size()), context: this));
1907 } else {
1908 d->geometry.moveTopLeft(p: point);
1909 }
1910}
1911
1912/*!
1913 \brief set the position of the window on the desktop to \a pt
1914
1915 The position is in relation to the virtualGeometry() of its screen.
1916
1917 For interactively moving windows, see startSystemMove(). For interactively
1918 resizing windows, see startSystemResize().
1919
1920 \note Not all windowing systems support setting or querying top level window positions.
1921 On such a system, programmatically moving windows may not have any effect, and artificial
1922 values may be returned for the current positions, such as \c QPoint(0, 0).
1923
1924 \sa position(), startSystemMove()
1925*/
1926void QWindow::setPosition(const QPoint &pt)
1927{
1928 setGeometry(QRect(pt, size()));
1929}
1930
1931/*!
1932 \brief set the position of the window on the desktop to \a posx, \a posy
1933
1934 The position is in relation to the virtualGeometry() of its screen.
1935
1936 \sa position()
1937*/
1938void QWindow::setPosition(int posx, int posy)
1939{
1940 setPosition(QPoint(posx, posy));
1941}
1942
1943/*!
1944 \fn QPoint QWindow::position() const
1945 \brief Returns the position of the window on the desktop excluding any window frame
1946
1947 \note Not all windowing systems support setting or querying top level window positions.
1948 On such a system, programmatically moving windows may not have any effect, and artificial
1949 values may be returned for the current positions, such as \c QPoint(0, 0).
1950
1951 \sa setPosition()
1952*/
1953
1954/*!
1955 \fn QSize QWindow::size() const
1956 \brief Returns the size of the window excluding any window frame
1957
1958 \sa resize()
1959*/
1960
1961/*!
1962 set the size of the window, excluding any window frame, to a QSize
1963 constructed from width \a w and height \a h
1964
1965 For interactively resizing windows, see startSystemResize().
1966
1967 \sa size(), geometry()
1968*/
1969void QWindow::resize(int w, int h)
1970{
1971 resize(newSize: QSize(w, h));
1972}
1973
1974/*!
1975 \brief set the size of the window, excluding any window frame, to \a newSize
1976
1977 \sa size(), geometry()
1978*/
1979void QWindow::resize(const QSize &newSize)
1980{
1981 Q_D(QWindow);
1982 d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
1983 if (d->platformWindow) {
1984 d->platformWindow->setGeometry(
1985 QHighDpi::toNativeWindowGeometry(value: QRect(position(), newSize), context: this));
1986 } else {
1987 const QSize oldSize = d->geometry.size();
1988 d->geometry.setSize(newSize);
1989 if (newSize.width() != oldSize.width())
1990 emit widthChanged(arg: newSize.width());
1991 if (newSize.height() != oldSize.height())
1992 emit heightChanged(arg: newSize.height());
1993 }
1994}
1995
1996/*!
1997 Releases the native platform resources associated with this window.
1998
1999 \sa create()
2000*/
2001void QWindow::destroy()
2002{
2003 Q_D(QWindow);
2004 if (!d->platformWindow)
2005 return;
2006
2007 if (d->platformWindow->isForeignWindow())
2008 return;
2009
2010 d->destroy();
2011}
2012
2013void QWindowPrivate::destroy()
2014{
2015 if (!platformWindow)
2016 return;
2017
2018 Q_Q(QWindow);
2019 QObjectList childrenWindows = q->children();
2020 for (int i = 0; i < childrenWindows.size(); i++) {
2021 QObject *object = childrenWindows.at(i);
2022 if (object->isWindowType()) {
2023 QWindow *w = static_cast<QWindow*>(object);
2024 qt_window_private(window: w)->destroy();
2025 }
2026 }
2027
2028 bool wasVisible = q->isVisible();
2029 visibilityOnDestroy = wasVisible && platformWindow;
2030
2031 q->setVisible(false);
2032
2033 // Let subclasses act, typically by doing graphics resource cleaup, when
2034 // the window, to which graphics resource may be tied, is going away.
2035 //
2036 // NB! This is dysfunctional when destroy() is invoked from the dtor since
2037 // a reimplemented event() will not get called in the subclasses at that
2038 // stage. However, the typical QWindow cleanup involves either close() or
2039 // going through QWindowContainer, both of which will do an explicit, early
2040 // destroy(), which is good here.
2041
2042 QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed);
2043 QGuiApplication::sendEvent(receiver: q, event: &e);
2044
2045 // Unset platformWindow before deleting, so that the destructor of the
2046 // platform window does not recurse back into the platform window via
2047 // this window during destruction (e.g. as a result of platform events).
2048 delete std::exchange(obj&: platformWindow, new_val: nullptr);
2049
2050 if (QGuiApplicationPrivate::focus_window == q)
2051 QGuiApplicationPrivate::focus_window = q->parent();
2052 if (QGuiApplicationPrivate::currentMouseWindow == q)
2053 QGuiApplicationPrivate::currentMouseWindow = q->parent();
2054 if (QGuiApplicationPrivate::currentMousePressWindow == q)
2055 QGuiApplicationPrivate::currentMousePressWindow = q->parent();
2056
2057 for (int i = 0; i < QGuiApplicationPrivate::tabletDevicePoints.size(); ++i)
2058 if (QGuiApplicationPrivate::tabletDevicePoints.at(i).target == q)
2059 QGuiApplicationPrivate::tabletDevicePoints[i].target = q->parent();
2060
2061 resizeEventPending = true;
2062 receivedExpose = false;
2063 exposed = false;
2064}
2065
2066/*!
2067 Returns the platform window corresponding to the window.
2068
2069 \internal
2070*/
2071QPlatformWindow *QWindow::handle() const
2072{
2073 Q_D(const QWindow);
2074 return d->platformWindow;
2075}
2076
2077/*!
2078 Returns the platform surface corresponding to the window.
2079
2080 \internal
2081*/
2082QPlatformSurface *QWindow::surfaceHandle() const
2083{
2084 Q_D(const QWindow);
2085 return d->platformWindow;
2086}
2087
2088/*!
2089 Sets whether keyboard grab should be enabled or not (\a grab).
2090
2091 If the return value is true, the window receives all key events until
2092 setKeyboardGrabEnabled(false) is called; other windows get no key events at
2093 all. Mouse events are not affected. Use setMouseGrabEnabled() if you want
2094 to grab that.
2095
2096 \sa setMouseGrabEnabled()
2097*/
2098bool QWindow::setKeyboardGrabEnabled(bool grab)
2099{
2100 Q_D(QWindow);
2101 if (d->platformWindow)
2102 return d->platformWindow->setKeyboardGrabEnabled(grab);
2103 return false;
2104}
2105
2106/*!
2107 Sets whether mouse grab should be enabled or not (\a grab).
2108
2109 If the return value is true, the window receives all mouse events until setMouseGrabEnabled(false) is
2110 called; other windows get no mouse events at all. Keyboard events are not affected.
2111 Use setKeyboardGrabEnabled() if you want to grab that.
2112
2113 \sa setKeyboardGrabEnabled()
2114*/
2115bool QWindow::setMouseGrabEnabled(bool grab)
2116{
2117 Q_D(QWindow);
2118 if (d->platformWindow)
2119 return d->platformWindow->setMouseGrabEnabled(grab);
2120 return false;
2121}
2122
2123/*!
2124 Returns the screen on which the window is shown, or null if there is none.
2125
2126 For child windows, this returns the screen of the corresponding top level window.
2127
2128 \sa setScreen(), QScreen::virtualSiblings()
2129*/
2130QScreen *QWindow::screen() const
2131{
2132 Q_D(const QWindow);
2133 return d->parentWindow ? d->parentWindow->screen() : d->topLevelScreen.data();
2134}
2135
2136/*!
2137 Sets the screen on which the window should be shown.
2138
2139 If the window has been created, it will be recreated on the \a newScreen.
2140
2141 \note If the screen is part of a virtual desktop of multiple screens,
2142 the window will not move automatically to \a newScreen. To place the
2143 window relative to the screen, use the screen's topLeft() position.
2144
2145 This function only works for top level windows.
2146
2147 \sa screen(), QScreen::virtualSiblings()
2148*/
2149void QWindow::setScreen(QScreen *newScreen)
2150{
2151 Q_D(QWindow);
2152 if (!newScreen)
2153 newScreen = QGuiApplication::primaryScreen();
2154 d->setTopLevelScreen(newScreen, recreate: newScreen != nullptr);
2155}
2156
2157/*!
2158 \fn QWindow::screenChanged(QScreen *screen)
2159
2160 This signal is emitted when a window's \a screen changes, either
2161 by being set explicitly with setScreen(), or automatically when
2162 the window's screen is removed.
2163*/
2164
2165/*!
2166 Returns the accessibility interface for the object that the window represents
2167 \internal
2168 \sa QAccessible
2169 */
2170QAccessibleInterface *QWindow::accessibleRoot() const
2171{
2172 return nullptr;
2173}
2174
2175/*!
2176 \fn QWindow::focusObjectChanged(QObject *object)
2177
2178 This signal is emitted when the final receiver of events tied to focus
2179 is changed to \a object.
2180
2181 \sa focusObject()
2182*/
2183
2184/*!
2185 Returns the QObject that will be the final receiver of events tied focus, such
2186 as key events.
2187*/
2188QObject *QWindow::focusObject() const
2189{
2190 return const_cast<QWindow *>(this);
2191}
2192
2193/*!
2194 Shows the window.
2195
2196 This is equivalent to calling showFullScreen(), showMaximized(), or showNormal(),
2197 depending on the platform's default behavior for the window type and flags.
2198
2199 \sa showFullScreen(), showMaximized(), showNormal(), hide(), QStyleHints::showIsFullScreen(), flags()
2200*/
2201void QWindow::show()
2202{
2203 Qt::WindowState defaultState = QGuiApplicationPrivate::platformIntegration()->defaultWindowState(d_func()->windowFlags);
2204 if (defaultState == Qt::WindowFullScreen)
2205 showFullScreen();
2206 else if (defaultState == Qt::WindowMaximized)
2207 showMaximized();
2208 else
2209 showNormal();
2210}
2211
2212/*!
2213 Hides the window.
2214
2215 Equivalent to calling setVisible(false).
2216
2217 \sa show(), setVisible()
2218*/
2219void QWindow::hide()
2220{
2221 setVisible(false);
2222}
2223
2224/*!
2225 Shows the window as minimized.
2226
2227 Equivalent to calling setWindowStates(Qt::WindowMinimized) and then
2228 setVisible(true).
2229
2230 \sa setWindowStates(), setVisible()
2231*/
2232void QWindow::showMinimized()
2233{
2234 setWindowStates(Qt::WindowMinimized);
2235 setVisible(true);
2236}
2237
2238/*!
2239 Shows the window as maximized.
2240
2241 Equivalent to calling setWindowStates(Qt::WindowMaximized) and then
2242 setVisible(true).
2243
2244 \sa setWindowStates(), setVisible()
2245*/
2246void QWindow::showMaximized()
2247{
2248 setWindowStates(Qt::WindowMaximized);
2249 setVisible(true);
2250}
2251
2252/*!
2253 Shows the window as fullscreen.
2254
2255 Equivalent to calling setWindowStates(Qt::WindowFullScreen) and then
2256 setVisible(true).
2257
2258 See the \l{QWidget::showFullScreen()} documentation for platform-specific
2259 considerations and limitations.
2260
2261 \sa setWindowStates(), setVisible()
2262*/
2263void QWindow::showFullScreen()
2264{
2265 setWindowStates(Qt::WindowFullScreen);
2266 setVisible(true);
2267#if !defined Q_OS_QNX // On QNX this window will be activated anyway from libscreen
2268 // activating it here before libscreen activates it causes problems
2269 requestActivate();
2270#endif
2271}
2272
2273/*!
2274 Shows the window as normal, i.e. neither maximized, minimized, nor fullscreen.
2275
2276 Equivalent to calling setWindowStates(Qt::WindowNoState) and then
2277 setVisible(true).
2278
2279 \sa setWindowStates(), setVisible()
2280*/
2281void QWindow::showNormal()
2282{
2283 setWindowStates(Qt::WindowNoState);
2284 setVisible(true);
2285}
2286
2287/*!
2288 Close the window.
2289
2290 This closes the window, effectively calling destroy(), and potentially
2291 quitting the application. Returns \c true on success, false if it has a parent
2292 window (in which case the top level window should be closed instead).
2293
2294 \sa destroy(), QGuiApplication::quitOnLastWindowClosed(), closeEvent()
2295*/
2296bool QWindow::close()
2297{
2298 Q_D(QWindow);
2299 if (d->inClose)
2300 return true;
2301
2302 // Do not close non top level windows
2303 if (!isTopLevel())
2304 return false;
2305
2306 if (!d->platformWindow)
2307 return true;
2308
2309 // The window might be deleted during close,
2310 // as a result of delivering the close event.
2311 QPointer guard(this);
2312 d->inClose = true;
2313 bool success = d->platformWindow->close();
2314 if (guard)
2315 d->inClose = false;
2316
2317 return success;
2318}
2319
2320bool QWindowPrivate::participatesInLastWindowClosed() const
2321{
2322 Q_Q(const QWindow);
2323
2324 if (!q->isTopLevel())
2325 return false;
2326
2327 // Tool-tip widgets do not normally have Qt::WA_QuitOnClose,
2328 // but since we do not have a similar flag for non-widget
2329 // windows we need an explicit exclusion here as well.
2330 if (q->type() == Qt::ToolTip)
2331 return false;
2332
2333 // A window with a transient parent is not a primary window,
2334 // it's a secondary window.
2335 if (q->transientParent())
2336 return false;
2337
2338 return true;
2339}
2340
2341bool QWindowPrivate::treatAsVisible() const
2342{
2343 Q_Q(const QWindow);
2344 return q->isVisible();
2345}
2346
2347/*!
2348 The expose event (\a ev) is sent by the window system when a window moves
2349 between the un-exposed and exposed states.
2350
2351 An exposed window is potentially visible to the user. If the window is moved
2352 off screen, is made totally obscured by another window, is minimized, or
2353 similar, this function might be called and the value of isExposed() might
2354 change to false. You may use this event to limit expensive operations such
2355 as animations to only run when the window is exposed.
2356
2357 This event should not be used to paint. To handle painting implement
2358 paintEvent() instead.
2359
2360 A resize event will always be sent before the expose event the first time
2361 a window is shown.
2362
2363 \sa paintEvent(), isExposed()
2364*/
2365void QWindow::exposeEvent(QExposeEvent *ev)
2366{
2367 ev->ignore();
2368}
2369
2370/*!
2371 The paint event (\a ev) is sent by the window system whenever an area of
2372 the window needs a repaint, for example when initially showing the window,
2373 or due to parts of the window being uncovered by moving another window.
2374
2375 The application is expected to render into the window in response to the
2376 paint event, regardless of the exposed state of the window. For example,
2377 a paint event may be sent before the window is exposed, to prepare it for
2378 showing to the user.
2379
2380 \since 6.0
2381
2382 \sa exposeEvent()
2383*/
2384void QWindow::paintEvent(QPaintEvent *ev)
2385{
2386 ev->ignore();
2387}
2388
2389/*!
2390 Override this to handle window move events (\a ev).
2391*/
2392void QWindow::moveEvent(QMoveEvent *ev)
2393{
2394 ev->ignore();
2395}
2396
2397/*!
2398 Override this to handle resize events (\a ev).
2399
2400 The resize event is called whenever the window is resized in the windowing system,
2401 either directly through the windowing system acknowledging a setGeometry() or resize() request,
2402 or indirectly through the user resizing the window manually.
2403*/
2404void QWindow::resizeEvent(QResizeEvent *ev)
2405{
2406 ev->ignore();
2407}
2408
2409/*!
2410 Override this to handle show events (\a ev).
2411
2412 The function is called when the window has requested becoming visible.
2413
2414 If the window is successfully shown by the windowing system, this will
2415 be followed by a resize and an expose event.
2416*/
2417void QWindow::showEvent(QShowEvent *ev)
2418{
2419 ev->ignore();
2420}
2421
2422/*!
2423 Override this to handle hide events (\a ev).
2424
2425 The function is called when the window has requested being hidden in the
2426 windowing system.
2427*/
2428void QWindow::hideEvent(QHideEvent *ev)
2429{
2430 ev->ignore();
2431}
2432
2433/*!
2434 Override this to handle close events (\a ev).
2435
2436 The function is called when the window is requested to close. Call \l{QEvent::ignore()}
2437 on the event if you want to prevent the window from being closed.
2438
2439 \sa close()
2440*/
2441void QWindow::closeEvent(QCloseEvent *ev)
2442{
2443 Q_UNUSED(ev);
2444}
2445
2446/*!
2447 Override this to handle any event (\a ev) sent to the window.
2448 Return \c true if the event was recognized and processed.
2449
2450 Remember to call the base class version if you wish for mouse events,
2451 key events, resize events, etc to be dispatched as usual.
2452*/
2453bool QWindow::event(QEvent *ev)
2454{
2455 switch (ev->type()) {
2456 case QEvent::MouseMove:
2457 mouseMoveEvent(static_cast<QMouseEvent*>(ev));
2458 break;
2459
2460 case QEvent::MouseButtonPress:
2461 mousePressEvent(static_cast<QMouseEvent*>(ev));
2462 break;
2463
2464 case QEvent::MouseButtonRelease:
2465 mouseReleaseEvent(static_cast<QMouseEvent*>(ev));
2466 break;
2467
2468 case QEvent::MouseButtonDblClick:
2469 mouseDoubleClickEvent(static_cast<QMouseEvent*>(ev));
2470 break;
2471
2472 case QEvent::TouchBegin:
2473 case QEvent::TouchUpdate:
2474 case QEvent::TouchEnd:
2475 case QEvent::TouchCancel:
2476 touchEvent(static_cast<QTouchEvent *>(ev));
2477 break;
2478
2479 case QEvent::Move:
2480 moveEvent(ev: static_cast<QMoveEvent*>(ev));
2481 break;
2482
2483 case QEvent::Resize:
2484 resizeEvent(ev: static_cast<QResizeEvent*>(ev));
2485 break;
2486
2487 case QEvent::KeyPress:
2488 keyPressEvent(static_cast<QKeyEvent *>(ev));
2489 break;
2490
2491 case QEvent::KeyRelease:
2492 keyReleaseEvent(static_cast<QKeyEvent *>(ev));
2493 break;
2494
2495 case QEvent::FocusIn: {
2496 focusInEvent(static_cast<QFocusEvent *>(ev));
2497#if QT_CONFIG(accessibility)
2498 QAccessible::State state;
2499 state.active = true;
2500 QAccessibleStateChangeEvent event(this, state);
2501 QAccessible::updateAccessibility(event: &event);
2502#endif
2503 break; }
2504
2505 case QEvent::FocusOut: {
2506 focusOutEvent(static_cast<QFocusEvent *>(ev));
2507#if QT_CONFIG(accessibility)
2508 QAccessible::State state;
2509 state.active = true;
2510 QAccessibleStateChangeEvent event(this, state);
2511 QAccessible::updateAccessibility(event: &event);
2512#endif
2513 break; }
2514
2515#if QT_CONFIG(wheelevent)
2516 case QEvent::Wheel:
2517 wheelEvent(static_cast<QWheelEvent*>(ev));
2518 break;
2519#endif
2520
2521 case QEvent::Close: {
2522
2523 Q_D(QWindow);
2524 const bool wasVisible = d->treatAsVisible();
2525 const bool participatesInLastWindowClosed = d->participatesInLastWindowClosed();
2526
2527 // The window might be deleted in the close event handler
2528 QPointer<QWindow> deletionGuard(this);
2529 closeEvent(ev: static_cast<QCloseEvent*>(ev));
2530
2531 if (ev->isAccepted()) {
2532 if (deletionGuard)
2533 destroy();
2534 if (wasVisible && participatesInLastWindowClosed)
2535 QGuiApplicationPrivate::instance()->maybeLastWindowClosed();
2536 }
2537
2538 break;
2539 }
2540
2541 case QEvent::Expose:
2542 exposeEvent(ev: static_cast<QExposeEvent *>(ev));
2543 break;
2544
2545 case QEvent::Paint:
2546 paintEvent(ev: static_cast<QPaintEvent *>(ev));
2547 break;
2548
2549 case QEvent::Show:
2550 showEvent(ev: static_cast<QShowEvent *>(ev));
2551 break;
2552
2553 case QEvent::Hide:
2554 hideEvent(ev: static_cast<QHideEvent *>(ev));
2555 break;
2556
2557 case QEvent::ApplicationWindowIconChange:
2558 setIcon(icon());
2559 break;
2560
2561#if QT_CONFIG(tabletevent)
2562 case QEvent::TabletPress:
2563 case QEvent::TabletMove:
2564 case QEvent::TabletRelease:
2565 tabletEvent(static_cast<QTabletEvent *>(ev));
2566 break;
2567#endif
2568
2569 case QEvent::PlatformSurface: {
2570 if ((static_cast<QPlatformSurfaceEvent *>(ev))->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) {
2571#ifndef QT_NO_OPENGL
2572 QOpenGLContext *context = QOpenGLContext::currentContext();
2573 if (context && context->surface() == static_cast<QSurface *>(this))
2574 context->doneCurrent();
2575#endif
2576 }
2577 break;
2578 }
2579
2580 default:
2581 return QObject::event(event: ev);
2582 }
2583
2584#ifndef QT_NO_CONTEXTMENU
2585 /*
2586 QGuiApplicationPrivate::processContextMenuEvent blocks mouse-triggered
2587 context menu events that the QPA plugin might generate. In practice that
2588 never happens, as even on Windows WM_CONTEXTMENU is never generated by
2589 the OS (we never call the default window procedure that would do that in
2590 response to unhandled WM_RBUTTONUP).
2591
2592 So, we always have to syntheize QContextMenuEvent for mouse events anyway.
2593 QWidgetWindow synthesizes QContextMenuEvent similar to this code, and
2594 never calls QWindow::event, so we have to do it here as well.
2595
2596 This logic could be simplified by always synthesizing events in
2597 QGuiApplicationPrivate, or perhaps even in each QPA plugin. See QTBUG-93486.
2598 */
2599 static const QEvent::Type contextMenuTrigger =
2600 QGuiApplicationPrivate::platformTheme()->themeHint(hint: QPlatformTheme::ContextMenuOnMouseRelease).toBool() ?
2601 QEvent::MouseButtonRelease : QEvent::MouseButtonPress;
2602 auto asMouseEvent = [](QEvent *ev) {
2603 const auto t = ev->type();
2604 return t == QEvent::MouseButtonPress || t == QEvent::MouseButtonRelease
2605 ? static_cast<QMouseEvent *>(ev) : nullptr ;
2606 };
2607 if (QMouseEvent *me = asMouseEvent(ev); me &&
2608 ev->type() == contextMenuTrigger && me->button() == Qt::RightButton) {
2609 QContextMenuEvent e(QContextMenuEvent::Mouse, me->position().toPoint(),
2610 me->globalPosition().toPoint(), me->modifiers());
2611 QGuiApplication::sendEvent(receiver: this, event: &e);
2612 }
2613#endif
2614 return true;
2615}
2616
2617/*!
2618 Schedules a QEvent::UpdateRequest event to be delivered to this window.
2619
2620 The event is delivered in sync with the display vsync on platforms where
2621 this is possible. Otherwise, the event is delivered after a delay of at
2622 most 5 ms. If the window's associated screen reports a
2623 \l{QScreen::refreshRate()}{refresh rate} higher than 60 Hz, the interval is
2624 scaled down to a value smaller than 5. The additional time is there to give
2625 the event loop a bit of idle time to gather system events, and can be
2626 overridden using the QT_QPA_UPDATE_IDLE_TIME environment variable.
2627
2628 When driving animations, this function should be called once after drawing
2629 has completed. Calling this function multiple times will result in a single
2630 event being delivered to the window.
2631
2632 Subclasses of QWindow should reimplement event(), intercept the event and
2633 call the application's rendering code, then call the base class
2634 implementation.
2635
2636 \note The subclass' reimplementation of event() must invoke the base class
2637 implementation, unless it is absolutely sure that the event does not need to
2638 be handled by the base class. For example, the default implementation of
2639 this function relies on QEvent::Timer events. Filtering them away would
2640 therefore break the delivery of the update events.
2641
2642 \since 5.5
2643*/
2644void QWindow::requestUpdate()
2645{
2646 Q_ASSERT_X(QThread::currentThread() == QCoreApplication::instance()->thread(),
2647 "QWindow", "Updates can only be scheduled from the GUI (main) thread");
2648
2649 Q_D(QWindow);
2650 if (d->updateRequestPending || !d->platformWindow)
2651 return;
2652 d->updateRequestPending = true;
2653 d->platformWindow->requestUpdate();
2654}
2655
2656/*!
2657 Override this to handle key press events (\a ev).
2658
2659 \sa keyReleaseEvent()
2660*/
2661void QWindow::keyPressEvent(QKeyEvent *ev)
2662{
2663 ev->ignore();
2664}
2665
2666/*!
2667 Override this to handle key release events (\a ev).
2668
2669 \sa keyPressEvent()
2670*/
2671void QWindow::keyReleaseEvent(QKeyEvent *ev)
2672{
2673 ev->ignore();
2674}
2675
2676/*!
2677 Override this to handle focus in events (\a ev).
2678
2679 Focus in events are sent when the window receives keyboard focus.
2680
2681 \sa focusOutEvent()
2682*/
2683void QWindow::focusInEvent(QFocusEvent *ev)
2684{
2685 ev->ignore();
2686}
2687
2688/*!
2689 Override this to handle focus out events (\a ev).
2690
2691 Focus out events are sent when the window loses keyboard focus.
2692
2693 \sa focusInEvent()
2694*/
2695void QWindow::focusOutEvent(QFocusEvent *ev)
2696{
2697 ev->ignore();
2698}
2699
2700/*!
2701 Override this to handle mouse press events (\a ev).
2702
2703 \sa mouseReleaseEvent()
2704*/
2705void QWindow::mousePressEvent(QMouseEvent *ev)
2706{
2707 ev->ignore();
2708}
2709
2710/*!
2711 Override this to handle mouse release events (\a ev).
2712
2713 \sa mousePressEvent()
2714*/
2715void QWindow::mouseReleaseEvent(QMouseEvent *ev)
2716{
2717 ev->ignore();
2718}
2719
2720/*!
2721 Override this to handle mouse double click events (\a ev).
2722
2723 \sa mousePressEvent(), QStyleHints::mouseDoubleClickInterval()
2724*/
2725void QWindow::mouseDoubleClickEvent(QMouseEvent *ev)
2726{
2727 ev->ignore();
2728}
2729
2730/*!
2731 Override this to handle mouse move events (\a ev).
2732*/
2733void QWindow::mouseMoveEvent(QMouseEvent *ev)
2734{
2735 ev->ignore();
2736}
2737
2738#if QT_CONFIG(wheelevent)
2739/*!
2740 Override this to handle mouse wheel or other wheel events (\a ev).
2741*/
2742void QWindow::wheelEvent(QWheelEvent *ev)
2743{
2744 ev->ignore();
2745}
2746#endif // QT_CONFIG(wheelevent)
2747
2748/*!
2749 Override this to handle touch events (\a ev).
2750*/
2751void QWindow::touchEvent(QTouchEvent *ev)
2752{
2753 ev->ignore();
2754}
2755
2756#if QT_CONFIG(tabletevent)
2757/*!
2758 Override this to handle tablet press, move, and release events (\a ev).
2759
2760 Proximity enter and leave events are not sent to windows, they are
2761 delivered to the application instance.
2762*/
2763void QWindow::tabletEvent(QTabletEvent *ev)
2764{
2765 ev->ignore();
2766}
2767#endif
2768
2769/*!
2770 Override this to handle platform dependent events.
2771 Will be given \a eventType, \a message and \a result.
2772
2773 This might make your application non-portable.
2774
2775 Should return true only if the event was handled.
2776*/
2777
2778bool QWindow::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
2779{
2780 Q_UNUSED(eventType);
2781 Q_UNUSED(message);
2782 Q_UNUSED(result);
2783 return false;
2784}
2785
2786/*!
2787 \fn QPointF QWindow::mapToGlobal(const QPointF &pos) const
2788
2789 Translates the window coordinate \a pos to global screen
2790 coordinates. For example, \c{mapToGlobal(QPointF(0,0))} would give
2791 the global coordinates of the top-left pixel of the window.
2792
2793 \sa mapFromGlobal()
2794 \since 6.0
2795*/
2796QPointF QWindow::mapToGlobal(const QPointF &pos) const
2797{
2798 Q_D(const QWindow);
2799 // QTBUG-43252, prefer platform implementation for foreign windows.
2800 if (d->platformWindow
2801 && (d->platformWindow->isForeignWindow() || d->platformWindow->isEmbedded())) {
2802 return QHighDpi::fromNativeGlobalPosition(value: d->platformWindow->mapToGlobalF(pos: QHighDpi::toNativeLocalPosition(value: pos, context: this)), context: this);
2803 }
2804
2805 if (!QHighDpiScaling::isActive())
2806 return pos + d->globalPosition();
2807
2808 // The normal pos + windowGlobalPos calculation may give a point which is outside
2809 // screen geometry for windows which span multiple screens, due to the way QHighDpiScaling
2810 // creates gaps between screens in the the device indendent cooordinate system.
2811 //
2812 // Map the position (and the window's global position) to native coordinates, perform
2813 // the addition, and then map back to device independent coordinates.
2814 QPointF nativeLocalPos = QHighDpi::toNativeLocalPosition(value: pos, context: this);
2815 QPointF nativeWindowGlobalPos = QHighDpi::toNativeGlobalPosition(value: QPointF(d->globalPosition()), context: this);
2816 QPointF nativeGlobalPos = nativeLocalPos + nativeWindowGlobalPos;
2817 QPointF deviceIndependentGlobalPos = QHighDpi::fromNativeGlobalPosition(value: nativeGlobalPos, context: this);
2818 return deviceIndependentGlobalPos;
2819}
2820
2821/*!
2822 \overload
2823*/
2824QPoint QWindow::mapToGlobal(const QPoint &pos) const
2825{
2826 return mapToGlobal(pos: QPointF(pos)).toPoint();
2827}
2828
2829/*!
2830 \fn QPointF QWindow::mapFromGlobal(const QPointF &pos) const
2831
2832 Translates the global screen coordinate \a pos to window
2833 coordinates.
2834
2835 \sa mapToGlobal()
2836 \since 6.0
2837*/
2838QPointF QWindow::mapFromGlobal(const QPointF &pos) const
2839{
2840 Q_D(const QWindow);
2841 // QTBUG-43252, prefer platform implementation for foreign windows.
2842 if (d->platformWindow
2843 && (d->platformWindow->isForeignWindow() || d->platformWindow->isEmbedded())) {
2844 return QHighDpi::fromNativeLocalPosition(value: d->platformWindow->mapFromGlobalF(pos: QHighDpi::toNativeGlobalPosition(value: pos, context: this)), context: this);
2845 }
2846
2847 if (!QHighDpiScaling::isActive())
2848 return pos - d->globalPosition();
2849
2850 // Calculate local position in the native coordinate system. (See comment for the
2851 // corresponding mapToGlobal() code above).
2852 QPointF nativeGlobalPos = QHighDpi::toNativeGlobalPosition(value: pos, context: this);
2853 QPointF nativeWindowGlobalPos = QHighDpi::toNativeGlobalPosition(value: QPointF(d->globalPosition()), context: this);
2854 QPointF nativeLocalPos = nativeGlobalPos - nativeWindowGlobalPos;
2855 QPointF deviceIndependentLocalPos = QHighDpi::fromNativeLocalPosition(value: nativeLocalPos, context: this);
2856 return deviceIndependentLocalPos;
2857}
2858
2859/*!
2860 \overload
2861*/
2862QPoint QWindow::mapFromGlobal(const QPoint &pos) const
2863{
2864 return QWindow::mapFromGlobal(pos: QPointF(pos)).toPoint();
2865}
2866
2867QPoint QWindowPrivate::globalPosition() const
2868{
2869 Q_Q(const QWindow);
2870 QPoint offset = q->position();
2871 for (const QWindow *p = q->parent(); p; p = p->parent()) {
2872 QPlatformWindow *pw = p->handle();
2873 if (pw && (pw->isForeignWindow() || pw->isEmbedded())) {
2874 // Use mapToGlobal() for foreign windows
2875 offset += p->mapToGlobal(pos: QPoint(0, 0));
2876 break;
2877 } else {
2878 offset += p->position();
2879 }
2880 }
2881 return offset;
2882}
2883
2884Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
2885{
2886 return window->d_func();
2887}
2888
2889QWindow *QWindowPrivate::topLevelWindow(QWindow::AncestorMode mode) const
2890{
2891 Q_Q(const QWindow);
2892
2893 QWindow *window = const_cast<QWindow *>(q);
2894
2895 while (window) {
2896 QWindow *parent = window->parent(mode);
2897 if (!parent)
2898 break;
2899
2900 window = parent;
2901 }
2902
2903 return window;
2904}
2905
2906/*!
2907 Creates a local representation of a window created by another process or by
2908 using native libraries below Qt.
2909
2910 Given the handle \a id to a native window, this method creates a QWindow
2911 object which can be used to represent the window when invoking methods like
2912 setParent() and setTransientParent().
2913
2914 This can be used, on platforms which support it, to embed a QWindow inside a
2915 native window, or to embed a native window inside a QWindow.
2916
2917 If foreign windows are not supported or embedding the native window
2918 failed in the platform plugin, this function returns \nullptr.
2919
2920 \note The resulting QWindow should not be used to manipulate the underlying
2921 native window (besides re-parenting), or to observe state changes of the
2922 native window. Any support for these kind of operations is incidental, highly
2923 platform dependent and untested.
2924
2925 \sa setParent()
2926*/
2927QWindow *QWindow::fromWinId(WId id)
2928{
2929 if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(cap: QPlatformIntegration::ForeignWindows)) {
2930 qWarning(msg: "QWindow::fromWinId(): platform plugin does not support foreign windows.");
2931 return nullptr;
2932 }
2933
2934 QWindow *window = new QWindow;
2935 qt_window_private(window)->create(recursive: false, nativeHandle: id);
2936
2937 if (!window->handle()) {
2938 delete window;
2939 return nullptr;
2940 }
2941
2942 return window;
2943}
2944
2945/*!
2946 Causes an alert to be shown for \a msec milliseconds. If \a msec is \c 0 (the
2947 default), then the alert is shown indefinitely until the window becomes
2948 active again. This function has no effect on an active window.
2949
2950 In alert state, the window indicates that it demands attention, for example by
2951 flashing or bouncing the taskbar entry.
2952
2953 \since 5.1
2954*/
2955
2956void QWindow::alert(int msec)
2957{
2958 Q_D(QWindow);
2959 if (!d->platformWindow || d->platformWindow->isAlertState() || isActive())
2960 return;
2961 d->platformWindow->setAlertState(true);
2962 if (d->platformWindow->isAlertState() && msec)
2963 QTimer::singleShot(msec, receiver: this, SLOT(_q_clearAlert()));
2964}
2965
2966void QWindowPrivate::_q_clearAlert()
2967{
2968 if (platformWindow && platformWindow->isAlertState())
2969 platformWindow->setAlertState(false);
2970}
2971
2972#ifndef QT_NO_CURSOR
2973/*!
2974 \brief set the cursor shape for this window
2975
2976 The mouse \a cursor will assume this shape when it is over this
2977 window, unless an override cursor is set.
2978 See the \l{Qt::CursorShape}{list of predefined cursor objects} for a
2979 range of useful shapes.
2980
2981 If no cursor has been set, or after a call to unsetCursor(), the
2982 parent window's cursor is used.
2983
2984 By default, the cursor has the Qt::ArrowCursor shape.
2985
2986 Some underlying window implementations will reset the cursor if it
2987 leaves a window even if the mouse is grabbed. If you want to have
2988 a cursor set for all windows, even when outside the window, consider
2989 QGuiApplication::setOverrideCursor().
2990
2991 \sa QGuiApplication::setOverrideCursor()
2992*/
2993void QWindow::setCursor(const QCursor &cursor)
2994{
2995 Q_D(QWindow);
2996 d->setCursor(&cursor);
2997}
2998
2999/*!
3000 \brief Restores the default arrow cursor for this window.
3001 */
3002void QWindow::unsetCursor()
3003{
3004 Q_D(QWindow);
3005 d->setCursor(nullptr);
3006}
3007
3008/*!
3009 \brief the cursor shape for this window
3010
3011 \sa setCursor(), unsetCursor()
3012*/
3013QCursor QWindow::cursor() const
3014{
3015 Q_D(const QWindow);
3016 return d->cursor;
3017}
3018
3019void QWindowPrivate::setCursor(const QCursor *newCursor)
3020{
3021
3022 Q_Q(QWindow);
3023 if (newCursor) {
3024 const Qt::CursorShape newShape = newCursor->shape();
3025 if (newShape <= Qt::LastCursor && hasCursor && newShape == cursor.shape())
3026 return; // Unchanged and no bitmap/custom cursor.
3027 cursor = *newCursor;
3028 hasCursor = true;
3029 } else {
3030 if (!hasCursor)
3031 return;
3032 cursor = QCursor(Qt::ArrowCursor);
3033 hasCursor = false;
3034 }
3035 // Only attempt to emit signal if there is an actual platform cursor
3036 if (applyCursor()) {
3037 QEvent event(QEvent::CursorChange);
3038 QGuiApplication::sendEvent(receiver: q, event: &event);
3039 }
3040}
3041
3042// Apply the cursor and returns true iff the platform cursor exists
3043bool QWindowPrivate::applyCursor()
3044{
3045 Q_Q(QWindow);
3046 if (QScreen *screen = q->screen()) {
3047 if (QPlatformCursor *platformCursor = screen->handle()->cursor()) {
3048 if (!platformWindow)
3049 return true;
3050 QCursor *c = QGuiApplication::overrideCursor();
3051 if (c != nullptr && platformCursor->capabilities().testFlag(flag: QPlatformCursor::OverrideCursor))
3052 return true;
3053 if (!c && hasCursor)
3054 c = &cursor;
3055 platformCursor->changeCursor(windowCursor: c, window: q);
3056 return true;
3057 }
3058 }
3059 return false;
3060}
3061#endif // QT_NO_CURSOR
3062
3063void *QWindow::resolveInterface(const char *name, int revision) const
3064{
3065 using namespace QNativeInterface::Private;
3066
3067 auto *platformWindow = handle();
3068 Q_UNUSED(platformWindow);
3069 Q_UNUSED(name);
3070 Q_UNUSED(revision);
3071
3072#if defined(Q_OS_WIN)
3073 QT_NATIVE_INTERFACE_RETURN_IF(QWindowsWindow, platformWindow);
3074#endif
3075
3076#if QT_CONFIG(xcb)
3077 QT_NATIVE_INTERFACE_RETURN_IF(QXcbWindow, platformWindow);
3078#endif
3079
3080#if defined(Q_OS_MACOS)
3081 QT_NATIVE_INTERFACE_RETURN_IF(QCocoaWindow, platformWindow);
3082#endif
3083
3084#if defined(Q_OS_UNIX)
3085 QT_NATIVE_INTERFACE_RETURN_IF(QWaylandWindow, platformWindow);
3086#endif
3087
3088#if defined(Q_OS_WASM)
3089 QT_NATIVE_INTERFACE_RETURN_IF(QWasmWindow, platformWindow);
3090#endif
3091
3092 return nullptr;
3093}
3094
3095#ifndef QT_NO_DEBUG_STREAM
3096QDebug operator<<(QDebug debug, const QWindow *window)
3097{
3098 QDebugStateSaver saver(debug);
3099 debug.nospace();
3100 if (window) {
3101 debug << window->metaObject()->className() << '(' << (const void *)window;
3102 if (!window->objectName().isEmpty())
3103 debug << ", name=" << window->objectName();
3104 if (debug.verbosity() > 2) {
3105 const QRect geometry = window->geometry();
3106 if (window->isVisible())
3107 debug << ", visible";
3108 if (window->isExposed())
3109 debug << ", exposed";
3110 debug << ", state=" << window->windowState()
3111 << ", type=" << window->type() << ", flags=" << window->flags()
3112 << ", surface type=" << window->surfaceType();
3113 if (window->isTopLevel())
3114 debug << ", toplevel";
3115 debug << ", " << geometry.width() << 'x' << geometry.height()
3116 << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
3117 const QMargins margins = window->frameMargins();
3118 if (!margins.isNull())
3119 debug << ", margins=" << margins;
3120 debug << ", devicePixelRatio=" << window->devicePixelRatio();
3121 if (const QPlatformWindow *platformWindow = window->handle())
3122 debug << ", winId=0x" << Qt::hex << platformWindow->winId() << Qt::dec;
3123 if (const QScreen *screen = window->screen())
3124 debug << ", on " << screen->name();
3125 }
3126 debug << ')';
3127 } else {
3128 debug << "QWindow(0x0)";
3129 }
3130 return debug;
3131}
3132#endif // !QT_NO_DEBUG_STREAM
3133
3134#if QT_CONFIG(vulkan) || defined(Q_QDOC)
3135
3136/*!
3137 Associates this window with the specified Vulkan \a instance.
3138
3139 \a instance must stay valid as long as this QWindow instance exists.
3140 */
3141void QWindow::setVulkanInstance(QVulkanInstance *instance)
3142{
3143 Q_D(QWindow);
3144 d->vulkanInstance = instance;
3145}
3146
3147/*!
3148 \return the associated Vulkan instance if any was set, otherwise \nullptr.
3149 */
3150QVulkanInstance *QWindow::vulkanInstance() const
3151{
3152 Q_D(const QWindow);
3153 return d->vulkanInstance;
3154}
3155
3156#endif // QT_CONFIG(vulkan)
3157
3158QT_END_NAMESPACE
3159
3160#include "moc_qwindow.cpp"
3161

source code of qtbase/src/gui/kernel/qwindow.cpp