1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <qpa/qplatformwindow.h>
45 #include <qpa/qplatformintegration.h>
46 #include "qsurfaceformat.h"
48 #include <qpa/qplatformopenglcontext.h>
49 #include "qopenglcontext.h"
53 #include "qwindow_p.h"
54 #include "qguiapplication_p.h"
55 #include "qaccessible.h"
57 #include <private/qevent_p.h>
59 #include <QtCore/QDebug>
61 #include <QStyleHints>
62 #include <qpa/qplatformcursor.h>
69 \brief The QWindow class represents a window in the underlying windowing system.
71 A window that is supplied a parent becomes a native child window of
74 \section1 Resource management
76 Windows can potentially use a lot of memory. A usual measurement is
77 width times height times color depth. A window might also include multiple
78 buffers to support double and triple buffering, as well as depth and stencil
79 buffers. To release a window's memory resources, the destroy() function.
81 \section1 Window and content orientation
83 QWindow has reportContentOrientationChange() and
84 requestWindowOrientation() that can be used to specify the
85 layout of the window contents in relation to the screen. The
86 window orientation determines the actual buffer layout of the
87 window, and the windowing system uses this value to rotate the
88 window before it ends up on the display, and to ensure that input
89 coordinates are in the correct coordinate space relative to the
92 On the other hand, the content orientation is simply a hint to the
93 windowing system about which orientation the window contents are in.
94 It's useful when you wish to keep the same buffer layout, but rotate
95 the contents instead, especially when doing rotation animations
96 between different orientations. The windowing system might use this
97 value to determine the layout of system popups or dialogs.
99 \section1 Visibility and Windowing system exposure.
101 By default, the window is not visible, and you must call setVisible(true),
102 or show() or similar to make it visible. To make a window hidden again,
103 call setVisible(false) or hide(). The visible property describes the state
104 the application wants the window to be in. Depending on the underlying
105 system, a visible window might still not be shown on the screen. It could,
106 for instance, be covered by other opaque windows or moved outside the
107 physical area of the screen. On windowing systems that have exposure
108 notifications, the isExposed() accessor describes whether the window should
109 be treated as directly visible on screen. The exposeEvent() function is
110 called whenever the windows exposure in the windowing system changes. On
111 windowing systems that do not make this information visible to the
112 application, isExposed() will simply return the same value as isVisible().
116 There are two Qt APIs that can be used to render content into a window,
117 QBackingStore for rendering with a QPainter and flushing the contents
118 to a window with type QSurface::RasterSurface, and QOpenGLContext for
119 rendering with OpenGL to a window with type QSurface::OpenGLSurface.
121 The application can start rendering as soon as isExposed() returns true,
122 and can keep rendering until it isExposed() returns false. To find out when
123 isExposed() changes, reimplement exposeEvent(). The window will always get
124 a resize event before the first expose event.
128 Creates a window as a top level on the given screen.
130 The window is not shown until setVisible(true), show(), or similar is called.
134 QWindow::QWindow(QScreen *targetScreen)
135 : QObject(*new QWindowPrivate(), 0)
136 , QSurface(QSurface::Window)
139 d->screen = targetScreen;
141 d->screen = QGuiApplication::primaryScreen();
143 //if your applications aborts here, then chances are your creating a QWindow before the
144 //screen list is populated.
147 connect(d->screen, SIGNAL(destroyed(QObject *)), this, SLOT(screenDestroyed(QObject *)));
148 QGuiApplicationPrivate::window_list.prepend(this);
152 Creates a window as a child of the given \a parent window.
154 The window will be embedded inside the parent window, its coordinates relative to the parent.
156 The screen is inherited from the parent.
160 QWindow::QWindow(QWindow *parent)
161 : QObject(*new QWindowPrivate(), parent)
162 , QSurface(QSurface::Window)
165 d->parentWindow = parent;
167 d->screen = parent->screen();
169 d->screen = QGuiApplication::primaryScreen();
170 QGuiApplicationPrivate::window_list.prepend(this);
173 QWindow::QWindow(QWindowPrivate &dd, QWindow *parent)
174 : QObject(dd, parent)
175 , QSurface(QSurface::Window)
178 d->parentWindow = parent;
180 d->screen = parent->screen();
182 d->screen = QGuiApplication::primaryScreen();
183 QGuiApplicationPrivate::window_list.prepend(this);
191 if (QGuiApplicationPrivate::focus_window == this)
192 QGuiApplicationPrivate::focus_window = 0;
193 QGuiApplicationPrivate::window_list.removeAll(this);
198 Set the \a surfaceType of the window.
200 Specifies whether the window is meant for raster rendering with
201 QBackingStore, or OpenGL rendering with QOpenGLContext.
203 The surfaceType will be used when the native surface is created
204 in the create() function. Calling this function after the native
205 surface has been created requires calling destroy() and create()
206 to release the old native surface and create a new one.
208 \sa QBackingStore, QOpenGLContext, create(), destroy()
210 void QWindow::setSurfaceType(SurfaceType surfaceType)
213 d->surfaceType = surfaceType;
217 Returns the surface type of the window.
221 QWindow::SurfaceType QWindow::surfaceType() const
224 return d->surfaceType;
228 \property QWindow::visible
229 \brief whether the window is visible or not
231 This property controls the visibility of the window in the windowing system.
233 By default, the window is not visible, you must call setVisible(true), or
234 show() or similar to make it visible.
238 void QWindow::setVisible(bool visible)
242 if (d->visible == visible)
244 d->visible = visible;
245 emit visibleChanged(visible);
247 if (!d->platformWindow)
251 // remove posted quit events when showing a new window
252 QCoreApplication::removePostedEvents(qApp, QEvent::Quit);
254 QShowEvent showEvent;
255 QGuiApplication::sendEvent(this, &showEvent);
260 QGuiApplicationPrivate::showModalWindow(this);
262 QGuiApplicationPrivate::hideModalWindow(this);
265 d->platformWindow->setVisible(visible);
268 QHideEvent hideEvent;
269 QGuiApplication::sendEvent(this, &hideEvent);
273 bool QWindow::isVisible() const
281 Allocates the platform resources associated with the window.
283 It is at this point that the surface format set using setFormat() gets resolved
284 into an actual native surface. However, the window remains hidden until setVisible() is called.
286 Note that it is not usually necessary to call this function directly, as it will be implicitly
287 called by show(), setVisible(), and other functions that require access to the platform
290 Call destroy() to free the platform resources if necessary.
294 void QWindow::create()
297 if (!d->platformWindow) {
298 d->platformWindow = QGuiApplicationPrivate::platformIntegration()->createPlatformWindow(this);
299 QObjectList childObjects = children();
300 for (int i = 0; i < childObjects.size(); i ++) {
301 QObject *object = childObjects.at(i);
302 if(object->isWindowType()) {
303 QWindow *window = static_cast<QWindow *>(object);
304 if (window->d_func()->platformWindow)
305 window->d_func()->platformWindow->setParent(d->platformWindow);
312 Returns the window's platform id.
314 For platforms where this id might be useful, the value returned
315 will uniquely represent the window inside the corresponding screen.
319 WId QWindow::winId() const
322 if(!d->platformWindow)
323 const_cast<QWindow *>(this)->create();
325 WId id = d->platformWindow->winId();
326 // See the QPlatformWindow::winId() documentation
327 Q_ASSERT(id != WId(0));
332 Returns the parent window, if any.
334 A window without a parent is known as a top level window.
336 QWindow *QWindow::parent() const
339 return d->parentWindow;
343 Sets the parent Window. This will lead to the windowing system managing the clip of the window, so it will be clipped to the parent window.
345 Setting parent to be 0 will make the window become a top level window.
348 void QWindow::setParent(QWindow *parent)
352 QObject::setParent(parent);
354 if (d->platformWindow) {
355 if (parent && parent->d_func()->platformWindow) {
356 d->platformWindow->setParent(parent->d_func()->platformWindow);
358 d->platformWindow->setParent(0);
362 d->parentWindow = parent;
366 Returns whether the window is top level, i.e. has no parent window.
368 bool QWindow::isTopLevel() const
371 return d->parentWindow == 0;
375 Returns whether the window is modal.
377 A modal window prevents other windows from getting any input.
379 \sa QWindow::windowModality
381 bool QWindow::isModal() const
384 return d->modality != Qt::NonModal;
387 /*! \property QWindow::windowModality
388 \brief the modality of the window
390 A modal window prevents other windows from receiving input events. Qt
391 supports two types of modality: Qt::WindowModal and Qt::ApplicationModal.
393 By default, this property is Qt::NonModal
395 \sa Qt::WindowModality
398 Qt::WindowModality QWindow::windowModality() const
404 void QWindow::setWindowModality(Qt::WindowModality modality)
407 if (d->modality == modality)
409 d->modality = modality;
410 emit windowModalityChanged(modality);
413 /*! \fn void QWindow::windowModalityChanged(Qt::WindowModality windowModality)
415 This signal is emitted when the Qwindow::windowModality property changes to \a windowModality.
419 Sets the window's surface \a format.
421 The format determines properties such as color depth, alpha,
422 depth and stencil buffer size, etc.
424 The surface format will be resolved in the create() function. Calling
425 this function after create() has been called will not re-resolve the
426 surface format of the native surface.
428 \sa create(), destroy()
430 void QWindow::setFormat(const QSurfaceFormat &format)
433 d->requestedFormat = format;
437 Returns the requested surfaceformat of this window.
439 If the requested format was not supported by the platform implementation,
440 the requestedFormat will differ from the actual window format.
442 This is the value set with setFormat().
444 \sa setFormat(), format()
446 QSurfaceFormat QWindow::requestedFormat() const
449 return d->requestedFormat;
453 Returns the actual format of this window.
455 After the window has been created, this function will return the actual surface format
456 of the window. It might differ from the requested format if the requested format could
457 not be fulfilled by the platform.
459 \sa create(), requestedFormat()
461 QSurfaceFormat QWindow::format() const
464 if (d->platformWindow)
465 return d->platformWindow->format();
466 return d->requestedFormat;
470 Sets the window flags of the window to \a flags.
472 The window flags control the window's appearance in the windowing system,
473 whether it's a dialog, popup, or a regular window, and whether it should
474 have a title bar, etc.
478 void QWindow::setWindowFlags(Qt::WindowFlags flags)
481 if (d->platformWindow)
482 d->windowFlags = d->platformWindow->setWindowFlags(flags);
484 d->windowFlags = flags;
488 Returns the window flags of the window.
490 This might differ from the flags set with setWindowFlags() if the
491 requested flags could not be fulfilled.
495 Qt::WindowFlags QWindow::windowFlags() const
498 return d->windowFlags;
502 Returns the type of the window.
504 This returns the part of the window flags that represents
505 whether the window is a dialog, tooltip, popup, regular window, etc.
507 \sa windowFlags(), setWindowFlags()
509 Qt::WindowType QWindow::windowType() const
512 return static_cast<Qt::WindowType>(int(d->windowFlags & Qt::WindowType_Mask));
516 \property QWindow::windowTitle
517 \brief the window's title in the windowing system
519 The window title might appear in the title area of the window decorations,
520 depending on the windowing system and the window flags. It might also
521 be used by the windowing system to identify the window in other contexts,
522 such as in the task switcher.
526 void QWindow::setWindowTitle(const QString &title)
529 d->windowTitle = title;
530 if (d->platformWindow)
531 d->platformWindow->setWindowTitle(title);
534 QString QWindow::windowTitle() const
537 return d->windowTitle;
541 Sets the window icon to the given \a icon.
543 The window icon might be used by the windowing system for example to decorate the window,
544 or in the task switcher.
546 void QWindow::setWindowIcon(const QIcon &icon)
549 d->windowIcon = icon;
550 if (d->platformWindow)
551 d->platformWindow->setWindowIcon(icon);
554 QIcon QWindow::windowIcon() const
557 return d->windowIcon;
561 Raise the window in the windowing system.
563 Requests that the window be raised to appear above other windows.
565 void QWindow::raise()
568 if (d->platformWindow)
569 d->platformWindow->raise();
573 Lower the window in the windowing system.
575 Requests that the window be lowered to appear below other windows.
577 void QWindow::lower()
580 if (d->platformWindow)
581 d->platformWindow->lower();
585 Sets the window's opacity in the windowing system to \a level.
587 If the windowing system supports window opacity, this can be used to fade the
588 window in and out, or to make it semitransparent.
590 A value of 1.0 or above is treated as fully opaque, whereas a value of 0.0 or below
591 is treated as fully transparent. Values inbetween represent varying levels of
592 translucency between the two extremes.
594 void QWindow::setOpacity(qreal level)
597 if (d->platformWindow) {
598 d->platformWindow->setOpacity(level);
603 Requests the window to be activated, i.e. receive keyboard focus.
605 \sa isActive(), QGuiApplication::focusWindow()
607 void QWindow::requestActivateWindow()
610 if (d->platformWindow)
611 d->platformWindow->requestActivateWindow();
615 Returns if this window is exposed in the windowing system.
617 When the window is not exposed, it is shown by the application
618 but it is still not showing in the windowing system, so the application
619 should minimize rendering and other graphical activities.
621 An exposeEvent() is sent every time this value changes.
625 bool QWindow::isExposed() const
632 Returns true if the window should appear active from a style perspective.
634 This is the case for the window that has input focus as well as windows
635 that are in the same parent / transient parent chain as the focus window.
637 To get the window that currently has focus, use QGuiApplication::focusWindow().
639 bool QWindow::isActive() const
642 if (!d->platformWindow)
645 QWindow *focus = QGuiApplication::focusWindow();
647 // Means the whole application lost the focus
654 if (!parent() && !transientParent()) {
655 return isAncestorOf(focus);
657 return (parent() && parent()->isActive()) || (transientParent() && transientParent()->isActive());
662 \property QWindow::contentOrientation
663 \brief the orientation of the window's contents
665 This is a hint to the window manager in case it needs to display
666 additional content like popups, dialogs, status bars, or similar
667 in relation to the window.
669 The recommended orientation is QScreen::orientation() but
670 an application doesn't have to support all possible orientations,
671 and thus can opt to ignore the current screen orientation.
673 The difference between the window and the content orientation
674 determines how much to rotate the content by. QScreen::angleBetween(),
675 QScreen::transformBetween(), and QScreen::mapBetween() can be used
676 to compute the necessary transform.
678 The default value is Qt::PrimaryOrientation
680 \sa requestWindowOrientation(), QScreen::orientation()
682 void QWindow::reportContentOrientationChange(Qt::ScreenOrientation orientation)
685 if (d->contentOrientation == orientation)
687 if (!d->platformWindow)
689 Q_ASSERT(d->platformWindow);
690 d->contentOrientation = orientation;
691 d->platformWindow->handleContentOrientationChange(orientation);
692 emit contentOrientationChanged(orientation);
695 Qt::ScreenOrientation QWindow::contentOrientation() const
698 return d->contentOrientation;
702 Requests the given window orientation.
704 The window orientation specifies how the window should be rotated
705 by the window manager in order to be displayed. Input events will
706 be correctly mapped to the given orientation.
708 The return value is false if the system doesn't support the given
709 orientation (for example when requesting a portrait orientation
710 on a device that only handles landscape buffers, typically a desktop
713 If the return value is false, call windowOrientation() to get the actual
714 supported orientation.
716 \sa windowOrientation(), reportContentOrientationChange(), QScreen::orientation()
718 bool QWindow::requestWindowOrientation(Qt::ScreenOrientation orientation)
721 if (!d->platformWindow)
723 Q_ASSERT(d->platformWindow);
724 d->windowOrientation = d->platformWindow->requestWindowOrientation(orientation);
725 return d->windowOrientation == orientation;
729 Returns the actual window orientation.
731 The default value is Qt::PrimaryOrientation.
733 \sa requestWindowOrientation()
735 Qt::ScreenOrientation QWindow::windowOrientation() const
738 return d->windowOrientation;
742 Returns the window state.
746 Qt::WindowState QWindow::windowState() const
749 return d->windowState;
753 Sets the desired window \a state.
755 The window state represents whether the window appears in the
756 windowing system as maximized, minimized, fullscreen, or normal.
758 The enum value Qt::WindowActive is not an accepted parameter.
760 \sa windowState(), showNormal(), showFullScreen(), showMinimized(), showMaximized()
762 void QWindow::setWindowState(Qt::WindowState state)
764 if (state == Qt::WindowActive) {
765 qWarning() << "QWindow::setWindowState does not accept Qt::WindowActive";
770 if (d->platformWindow)
771 d->windowState = d->platformWindow->setWindowState(state);
773 d->windowState = state;
777 Sets the transient parent
779 This is a hint to the window manager that this window is a dialog or pop-up on behalf of the given window.
781 \sa transientParent(), parent()
783 void QWindow::setTransientParent(QWindow *parent)
786 d->transientParent = parent;
790 Returns the transient parent of the window.
792 \sa setTransientParent(), parent()
794 QWindow *QWindow::transientParent() const
797 return d->transientParent.data();
801 \enum QWindow::AncestorMode
803 This enum is used to control whether or not transient parents
804 should be considered ancestors.
806 \value ExcludeTransients Transient parents are not considered ancestors.
807 \value IncludeTransients Transient parents are considered ancestors.
811 Returns true if the window is an ancestor of the given child. If mode is
812 IncludeTransients transient parents are also considered ancestors.
814 bool QWindow::isAncestorOf(const QWindow *child, AncestorMode mode) const
816 if (child->parent() == this || (mode == IncludeTransients && child->transientParent() == this))
819 return (child->parent() && isAncestorOf(child->parent(), mode))
820 || (mode == IncludeTransients && child->transientParent() && isAncestorOf(child->transientParent(), mode));
824 Returns the minimum size of the window.
828 QSize QWindow::minimumSize() const
831 return d->minimumSize;
835 Returns the maximum size of the window.
839 QSize QWindow::maximumSize() const
842 return d->maximumSize;
846 Returns the base size of the window.
850 QSize QWindow::baseSize() const
857 Returns the size increment of the window.
859 \sa setSizeIncrement()
861 QSize QWindow::sizeIncrement() const
864 return d->sizeIncrement;
868 Sets the minimum size of the window.
870 This is a hint to the window manager to prevent resizing below the specified \a size.
872 \sa setMaximumSize(), minimumSize()
874 void QWindow::setMinimumSize(const QSize &size)
877 QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
878 if (d->minimumSize == adjustedSize)
880 d->minimumSize = adjustedSize;
881 if (d->platformWindow && isTopLevel())
882 d->platformWindow->propagateSizeHints();
886 Sets the maximum size of the window.
888 This is a hint to the window manager to prevent resizing above the specified \a size.
890 \sa setMinimumSize(), maximumSize()
892 void QWindow::setMaximumSize(const QSize &size)
895 QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX));
896 if (d->maximumSize == adjustedSize)
898 d->maximumSize = adjustedSize;
899 if (d->platformWindow && isTopLevel())
900 d->platformWindow->propagateSizeHints();
904 Sets the base size of the window.
906 The base size is used to calculate a proper window size if the
907 window defines sizeIncrement().
909 \sa setMinimumSize(), setMaximumSize(), setSizeIncrement(), baseSize()
911 void QWindow::setBaseSize(const QSize &size)
914 if (d->baseSize == size)
917 if (d->platformWindow && isTopLevel())
918 d->platformWindow->propagateSizeHints();
922 Sets the size increment of the window.
924 When the user resizes the window, the size will move in steps of
925 sizeIncrement().width() pixels horizontally and
926 sizeIncrement().height() pixels vertically, with baseSize() as the
929 By default, this property contains a size with zero width and height.
931 The windowing system might not support size increments.
933 \sa setBaseSize(), setMinimumSize(), setMaximumSize()
935 void QWindow::setSizeIncrement(const QSize &size)
938 if (d->sizeIncrement == size)
940 d->sizeIncrement = size;
941 if (d->platformWindow && isTopLevel())
942 d->platformWindow->propagateSizeHints();
946 Sets the geometry of the window, excluding its window frame, to \a rect.
948 To make sure the window is visible, make sure the geometry is within
949 the virtual geometry of its screen.
951 \sa geometry(), screen(), QScreen::virtualGeometry()
953 void QWindow::setGeometry(const QRect &rect)
956 if (rect == geometry())
958 QRect oldRect = geometry();
960 d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
961 if (d->platformWindow) {
962 d->platformWindow->setGeometry(rect);
967 if (rect.x() != oldRect.x())
968 emit xChanged(rect.x());
969 if (rect.y() != oldRect.y())
970 emit yChanged(rect.y());
971 if (rect.width() != oldRect.width())
972 emit widthChanged(rect.width());
973 if (rect.height() != oldRect.height())
974 emit heightChanged(rect.height());
979 \brief the x position of the window's geometry
984 \brief the y position of the window's geometry
988 \property QWindow::width
989 \brief the width of the window's geometry
993 \property QWindow::height
994 \brief the height of the window's geometry
998 Returns the geometry of the window, excluding its window frame.
1000 \sa frameMargins(), frameGeometry()
1002 QRect QWindow::geometry() const
1005 if (d->platformWindow)
1006 return d->platformWindow->geometry();
1011 Returns the window frame margins surrounding the window.
1013 \sa geometry(), frameGeometry()
1015 QMargins QWindow::frameMargins() const
1018 if (d->platformWindow)
1019 return d->platformWindow->frameMargins();
1024 Returns the geometry of the window, including its window frame.
1026 \sa geometry(), frameMargins()
1028 QRect QWindow::frameGeometry() const
1031 if (d->platformWindow) {
1032 QMargins m = frameMargins();
1033 return d->platformWindow->geometry().adjusted(-m.left(), -m.top(), m.right(), m.bottom());
1039 Returns the top left position of the window, including its window frame.
1041 This returns the same value as frameGeometry().topLeft().
1043 \sa geometry(), frameGeometry()
1045 QPoint QWindow::framePos() const
1048 if (d->platformWindow) {
1049 QMargins margins = frameMargins();
1050 return d->platformWindow->geometry().topLeft() - QPoint(margins.left(), margins.top());
1052 return d->geometry.topLeft();
1056 Sets the upper left position of the window including its window frame.
1058 \sa setGeometry(), frameGeometry()
1060 void QWindow::setFramePos(const QPoint &point)
1063 d->positionPolicy = QWindowPrivate::WindowFrameInclusive;
1064 if (d->platformWindow) {
1065 d->platformWindow->setGeometry(QRect(point, size()));
1067 d->geometry.setTopLeft(point);
1072 Sets the size of the window to be \a newSize.
1076 void QWindow::resize(const QSize &newSize)
1079 if (d->platformWindow) {
1080 d->platformWindow->setGeometry(QRect(pos(), newSize));
1082 d->geometry.setSize(newSize);
1087 Releases the native platform resources associated with this window.
1091 void QWindow::destroy()
1094 QObjectList childrenWindows = children();
1095 for (int i = 0; i < childrenWindows.size(); i++) {
1096 QObject *object = childrenWindows.at(i);
1097 if (object->isWindowType()) {
1098 QWindow *w = static_cast<QWindow*>(object);
1099 QGuiApplicationPrivate::window_list.removeAll(w);
1104 delete d->platformWindow;
1105 d->resizeEventPending = true;
1106 d->receivedExpose = false;
1108 d->platformWindow = 0;
1112 Returns the platform window corresponding to the window.
1116 QPlatformWindow *QWindow::handle() const
1119 return d->platformWindow;
1123 Returns the platform surface corresponding to the window.
1127 QPlatformSurface *QWindow::surfaceHandle() const
1130 return d->platformWindow;
1134 Set whether keyboard grab should be enabled or not.
1136 If the return value is true, the window receives all key events until setKeyboardGrabEnabled(false) is
1137 called; other windows get no key events at all. Mouse events are not affected.
1138 Use setMouseGrabEnabled() if you want to grab that.
1140 \sa setMouseGrabEnabled()
1142 bool QWindow::setKeyboardGrabEnabled(bool grab)
1145 if (d->platformWindow)
1146 return d->platformWindow->setKeyboardGrabEnabled(grab);
1151 Sets whether mouse grab should be enabled or not.
1153 If the return value is true, the window receives all mouse events until setMouseGrabEnabled(false) is
1154 called; other windows get no mouse events at all. Keyboard events are not affected.
1155 Use setKeyboardGrabEnabled() if you want to grab that.
1157 \sa setKeyboardGrabEnabled()
1159 bool QWindow::setMouseGrabEnabled(bool grab)
1162 if (d->platformWindow)
1163 return d->platformWindow->setMouseGrabEnabled(grab);
1168 Returns the screen on which the window is shown.
1170 The value returned will not change when the window is moved
1171 between virtual screens (as returned by QScreen::virtualSiblings()).
1173 \sa setScreen(), QScreen::virtualSiblings()
1175 QScreen *QWindow::screen() const
1182 Sets the screen on which the window should be shown.
1184 If the window has been created, it will be recreated on the new screen.
1186 Note that if the screen is part of a virtual desktop of multiple screens,
1187 the window can appear on any of the screens returned by QScreen::virtualSiblings().
1189 \sa screen(), QScreen::virtualSiblings()
1191 void QWindow::setScreen(QScreen *newScreen)
1195 newScreen = QGuiApplication::primaryScreen();
1196 if (newScreen != screen()) {
1197 const bool wasCreated = d->platformWindow != 0;
1201 disconnect(d->screen, SIGNAL(destroyed(QObject *)), this, SLOT(screenDestroyed(QObject *)));
1202 d->screen = newScreen;
1204 connect(d->screen, SIGNAL(destroyed(QObject *)), this, SLOT(screenDestroyed(QObject *)));
1208 emit screenChanged(newScreen);
1212 void QWindow::screenDestroyed(QObject *object)
1215 if (object == static_cast<QObject *>(d->screen))
1220 \fn QWindow::screenChanged(QScreen *screen)
1222 This signal is emitted when a window's screen changes, either
1223 by being set explicitly with setScreen(), or automatically when
1224 the window's screen is removed.
1228 Returns the accessibility interface for the object that the window represents
1232 QAccessibleInterface *QWindow::accessibleRoot() const
1238 \fn QWindow::focusObjectChanged(QObject *focusObject)
1240 This signal is emitted when final receiver of events tied to focus is changed.
1245 Returns the QObject that will be the final receiver of events tied focus, such
1248 QObject *QWindow::focusObject() const
1250 return const_cast<QWindow *>(this);
1256 This equivalent to calling showFullScreen() or showNormal(), depending
1257 on whether the platform defaults to windows being fullscreen or not.
1259 \sa showFullScreen(), showNormal(), hide(), QStyleHints::showIsFullScreen()
1261 void QWindow::show()
1263 if (qApp->styleHints()->showIsFullScreen())
1272 Equivalent to calling setVisible(false).
1274 \sa show(), setVisible()
1276 void QWindow::hide()
1282 Shows the window as minimized.
1284 Equivalent to calling setWindowState(Qt::WindowMinimized) and then
1287 \sa setWindowState(), setVisible()
1289 void QWindow::showMinimized()
1291 setWindowState(Qt::WindowMinimized);
1296 Shows the window as maximized.
1298 Equivalent to calling setWindowState(Qt::WindowMaximized) and then
1301 \sa setWindowState(), setVisible()
1303 void QWindow::showMaximized()
1305 setWindowState(Qt::WindowMaximized);
1310 Shows the window as fullscreen.
1312 Equivalent to calling setWindowState(Qt::WindowFullScreen) and then
1315 \sa setWindowState(), setVisible()
1317 void QWindow::showFullScreen()
1319 setWindowState(Qt::WindowFullScreen);
1321 requestActivateWindow();
1325 Shows the window as normal, i.e. neither maximized, minimized, nor fullscreen.
1327 Equivalent to calling setWindowState(Qt::WindowNoState) and then
1330 \sa setWindowState(), setVisible()
1332 void QWindow::showNormal()
1334 setWindowState(Qt::WindowNoState);
1341 This closes the window, effectively calling destroy(), and
1342 potentially quitting the application
1344 \sa destroy(), QGuiApplication::quitOnLastWindowClosed()
1346 bool QWindow::close()
1350 // Do not close non top level windows
1354 if (QGuiApplicationPrivate::focus_window == this)
1355 QGuiApplicationPrivate::focus_window = 0;
1357 QGuiApplicationPrivate::window_list.removeAll(this);
1359 d->maybeQuitOnLastWindowClosed();
1364 The expose event is sent by the window system whenever the window's
1365 exposure on screen changes.
1367 The application can start rendering into the window with QBackingStore
1368 and QOpenGLContext as soon as it gets an exposeEvent() such that
1369 isExposed() is true.
1371 If the window is moved off screen, is made totally obscured by another
1372 window, iconified or similar, this function might be called and the
1373 value of isExposed() might change to false. When this happens,
1374 an application should stop its rendering as it is no longer visible
1377 A resize event will always be sent before the expose event the first time
1382 void QWindow::exposeEvent(QExposeEvent *ev)
1388 Override this to handle mouse events.
1390 void QWindow::moveEvent(QMoveEvent *ev)
1396 Override this to handle resize events.
1398 The resize event is called whenever the window is resized in the windowing system,
1399 either directly through the windowing system acknowledging a setGeometry() or resize() request,
1400 or indirectly through the user resizing the window manually.
1402 void QWindow::resizeEvent(QResizeEvent *ev)
1408 Override this to handle show events.
1410 The function is called when the window has requested becoming visible.
1412 If the window is successfully shown by the windowing system, this will
1413 be followed by a resize and an expose event.
1415 void QWindow::showEvent(QShowEvent *ev)
1421 Override this to handle hide evens.
1423 The function is called when the window has requested being hidden in the
1426 void QWindow::hideEvent(QHideEvent *ev)
1432 Override this to handle any event sent to the window.
1434 Remember to call the base class version if you wish for mouse events,
1435 key events, resize events, etc to be dispatched as usual.
1437 bool QWindow::event(QEvent *ev)
1439 switch (ev->type()) {
1440 case QEvent::MouseMove:
1441 mouseMoveEvent(static_cast<QMouseEvent*>(ev));
1444 case QEvent::MouseButtonPress:
1445 mousePressEvent(static_cast<QMouseEvent*>(ev));
1448 case QEvent::MouseButtonRelease:
1449 mouseReleaseEvent(static_cast<QMouseEvent*>(ev));
1452 case QEvent::MouseButtonDblClick:
1453 mouseDoubleClickEvent(static_cast<QMouseEvent*>(ev));
1456 case QEvent::TouchBegin:
1457 case QEvent::TouchUpdate:
1458 case QEvent::TouchEnd:
1459 case QEvent::TouchCancel:
1460 touchEvent(static_cast<QTouchEvent *>(ev));
1464 moveEvent(static_cast<QMoveEvent*>(ev));
1467 case QEvent::Resize:
1468 resizeEvent(static_cast<QResizeEvent*>(ev));
1471 case QEvent::KeyPress:
1472 keyPressEvent(static_cast<QKeyEvent *>(ev));
1475 case QEvent::KeyRelease:
1476 keyReleaseEvent(static_cast<QKeyEvent *>(ev));
1479 case QEvent::FocusIn: {
1480 focusInEvent(static_cast<QFocusEvent *>(ev));
1481 #ifndef QT_NO_ACCESSIBILITY
1482 QAccessible::State state;
1483 state.active = true;
1484 QAccessibleStateChangeEvent event(this, state);
1485 QAccessible::updateAccessibility(&event);
1489 case QEvent::FocusOut: {
1490 focusOutEvent(static_cast<QFocusEvent *>(ev));
1491 #ifndef QT_NO_ACCESSIBILITY
1492 QAccessible::State state;
1493 state.active = true;
1494 QAccessibleStateChangeEvent event(this, state);
1495 QAccessible::updateAccessibility(&event);
1499 #ifndef QT_NO_WHEELEVENT
1501 wheelEvent(static_cast<QWheelEvent*>(ev));
1505 case QEvent::Close: {
1507 bool wasVisible = isVisible();
1510 d->maybeQuitOnLastWindowClosed();
1513 case QEvent::Expose:
1514 exposeEvent(static_cast<QExposeEvent *>(ev));
1518 showEvent(static_cast<QShowEvent *>(ev));
1522 hideEvent(static_cast<QHideEvent *>(ev));
1525 #ifndef QT_NO_TABLETEVENT
1526 case QEvent::TabletPress:
1527 case QEvent::TabletMove:
1528 case QEvent::TabletRelease:
1529 tabletEvent(static_cast<QTabletEvent *>(ev));
1534 return QObject::event(ev);
1540 Override this to handle key press events.
1544 void QWindow::keyPressEvent(QKeyEvent *ev)
1550 Override this to handle key release events.
1554 void QWindow::keyReleaseEvent(QKeyEvent *ev)
1560 Override this to handle focus in events.
1562 Focus in events are sent when the window receives keyboard focus.
1566 void QWindow::focusInEvent(QFocusEvent *ev)
1572 Override this to handle focus out events.
1574 Focus out events are sent when the window loses keyboard focus.
1578 void QWindow::focusOutEvent(QFocusEvent *ev)
1584 Override this to handle mouse press events.
1586 \sa mouseReleaseEvent()
1588 void QWindow::mousePressEvent(QMouseEvent *ev)
1594 Override this to handle mouse release events.
1596 \sa mousePressEvent()
1598 void QWindow::mouseReleaseEvent(QMouseEvent *ev)
1604 Override this to handle mouse double click events.
1606 \sa mousePressEvent(), QStyleHints::mouseDoubleClickInterval()
1608 void QWindow::mouseDoubleClickEvent(QMouseEvent *ev)
1614 Override this to handle mouse move events.
1616 void QWindow::mouseMoveEvent(QMouseEvent *ev)
1621 #ifndef QT_NO_WHEELEVENT
1623 Override this to handle mouse wheel or other wheel events.
1625 void QWindow::wheelEvent(QWheelEvent *ev)
1629 #endif //QT_NO_WHEELEVENT
1632 Override this to handle touch events.
1634 void QWindow::touchEvent(QTouchEvent *ev)
1639 #ifndef QT_NO_TABLETEVENT
1641 Override this to handle tablet press, move, and release events.
1643 Proximity enter and leave events are not sent to windows, they are
1644 delivered to the application instance.
1646 void QWindow::tabletEvent(QTabletEvent *ev)
1653 Override this to handle platform dependent events.
1655 This might make your application non-portable.
1657 bool QWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
1659 Q_UNUSED(eventType);
1666 \fn QPoint QWindow::mapToGlobal(const QPoint &pos) const
1668 Translates the window coordinate \a pos to global screen
1669 coordinates. For example, \c{mapToGlobal(QPoint(0,0))} would give
1670 the global coordinates of the top-left pixel of the window.
1674 QPoint QWindow::mapToGlobal(const QPoint &pos) const
1676 return pos + d_func()->globalPosition();
1681 \fn QPoint QWindow::mapFromGlobal(const QPoint &pos) const
1683 Translates the global screen coordinate \a pos to window
1688 QPoint QWindow::mapFromGlobal(const QPoint &pos) const
1690 return pos - d_func()->globalPosition();
1694 Q_GUI_EXPORT QWindowPrivate *qt_window_private(QWindow *window)
1696 return window->d_func();
1699 void QWindowPrivate::maybeQuitOnLastWindowClosed()
1703 // Attempt to close the application only if this has WA_QuitOnClose set and a non-visible parent
1704 bool quitOnClose = QGuiApplication::quitOnLastWindowClosed() && !q->parent();
1707 QWindowList list = QGuiApplication::topLevelWindows();
1708 bool lastWindowClosed = true;
1709 for (int i = 0; i < list.size(); ++i) {
1710 QWindow *w = list.at(i);
1711 if (!w->isVisible() || w->transientParent())
1713 lastWindowClosed = false;
1716 if (lastWindowClosed) {
1717 QGuiApplicationPrivate::emitLastWindowClosed();
1718 QCoreApplicationPrivate *applicationPrivate = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(QCoreApplication::instance()));
1719 applicationPrivate->maybeQuit();
1726 \property QWindow::cursor
1727 \brief the cursor shape for this window
1729 The mouse cursor will assume this shape when it is over this
1730 window, unless an override cursor is set.
1731 See the \l{Qt::CursorShape}{list of predefined cursor objects} for a
1732 range of useful shapes.
1734 By default, this property contains a cursor with the Qt::ArrowCursor
1737 Some underlying window implementations will reset the cursor if it
1738 leaves a window even if the mouse is grabbed. If you want to have
1739 a cursor set for all windows, even when outside the window, consider
1740 QGuiApplication::setOverrideCursor().
1742 \sa QGuiApplication::setOverrideCursor()
1745 #ifndef QT_NO_CURSOR
1746 QCursor QWindow::cursor() const
1752 void QWindow::setCursor(const QCursor &cursor)
1755 if (QPlatformCursor *platformCursor = d->screen->handle()->cursor()) {
1757 QCursor *oc = QGuiApplication::overrideCursor();
1758 QCursor c = oc ? *oc : d->cursor;
1759 platformCursor->changeCursor(&c, this);
1760 QEvent event(QEvent::CursorChange);
1761 QGuiApplication::sendEvent(this, &event);
1766 \brief Restores the default arrow cursor for this window.
1768 void QWindow::unsetCursor()
1771 if (QPlatformCursor *platformCursor = d->screen->handle()->cursor()) {
1772 d->cursor = Qt::ArrowCursor;
1773 QCursor *oc = QGuiApplication::overrideCursor();
1775 QCursor c = d->cursor;
1776 platformCursor->changeCursor(&c, this);
1778 QEvent event(QEvent::CursorChange);
1779 QGuiApplication::sendEvent(this, &event);