Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#include "qapplication.h"
8#include "qbrush.h"
9#include "qcursor.h"
10#include "private/qduplicatetracker_p.h"
11#include "qevent.h"
12#include "qlayout.h"
13#if QT_CONFIG(menu)
14#include "qmenu.h"
15#endif
16#include "qmetaobject.h"
17#include "qpixmap.h"
18#include "qpointer.h"
19#include "qstack.h"
20#include "qstyle.h"
21#include "qstylefactory.h"
22#include "qvariant.h"
23#include "qwidget.h"
24#include "qstyleoption.h"
25#include "qstylehints.h"
26#if QT_CONFIG(accessibility)
27# include "qaccessible.h"
28# include <private/qaccessiblecache_p.h>
29#endif
30#include <qpa/qplatformwindow.h>
31#include <qpa/qplatformwindow_p.h>
32#include "private/qwidgetwindow_p.h"
33#include "qpainter.h"
34#if QT_CONFIG(tooltip)
35#include "private/qtooltip_p.h"
36#endif
37#if QT_CONFIG(whatsthis)
38#include "qwhatsthis.h"
39#endif
40#include "qdebug.h"
41#if QT_CONFIG(style_stylesheet)
42#include "private/qstylesheetstyle_p.h"
43#endif
44#include "private/qstyle_p.h"
45#include "qfileinfo.h"
46#include "qscopeguard.h"
47#include <QtGui/qinputmethod.h>
48
49#if QT_CONFIG(graphicseffect)
50#include <private/qgraphicseffect_p.h>
51#endif
52#include <qbackingstore.h>
53#include <private/qwidgetrepaintmanager_p.h>
54#include <private/qpaintengine_raster_p.h>
55
56#include "qwidget_p.h"
57#include <QtGui/private/qwindow_p.h>
58#if QT_CONFIG(action)
59# include "QtGui/private/qaction_p.h"
60#endif
61#include "qlayout_p.h"
62#if QT_CONFIG(graphicsview)
63#include "QtWidgets/qgraphicsproxywidget.h"
64#include "QtWidgets/qgraphicsscene.h"
65#include "private/qgraphicsproxywidget_p.h"
66#include "private/qgraphicsview_p.h"
67#endif
68#include "QtWidgets/qabstractscrollarea.h"
69#include "private/qabstractscrollarea_p.h"
70#include "private/qevent_p.h"
71
72#include "private/qgesturemanager_p.h"
73
75
76#include <sstream>
77
78QT_BEGIN_NAMESPACE
79
80using namespace QNativeInterface::Private;
81using namespace Qt::StringLiterals;
82
83Q_LOGGING_CATEGORY(lcWidgetPainting, "qt.widgets.painting", QtWarningMsg);
84Q_LOGGING_CATEGORY(lcWidgetShowHide, "qt.widgets.showhide", QtWarningMsg);
85Q_STATIC_LOGGING_CATEGORY(lcWidgetWindow, "qt.widgets.window", QtWarningMsg);
86Q_STATIC_LOGGING_CATEGORY(lcWidgetFocus, "qt.widgets.focus")
87
88#ifndef QT_NO_DEBUG_STREAM
89namespace {
90 struct WidgetAttributes { const QWidget *widget; };
91 QDebug operator<<(QDebug debug, const WidgetAttributes &attributes);
92}
93#endif
94
95static inline bool qRectIntersects(const QRect &r1, const QRect &r2)
96{
97 return (qMax(r1.left(), r2.left()) <= qMin(r1.right(), r2.right()) &&
98 qMax(r1.top(), r2.top()) <= qMin(r1.bottom(), r2.bottom()));
99}
100
101extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); // qapplication.cpp
102
103static void setAttribute_internal(Qt::WidgetAttribute attribute,
104 bool on, QWidgetData *data, QWidgetPrivate *d);
105
106QWidgetPrivate::QWidgetPrivate(decltype(QObjectPrivateVersion) version)
107 : QObjectPrivate(version)
108 , focus_next(nullptr)
109 , focus_prev(nullptr)
110 , focus_child(nullptr)
111 , layout(nullptr)
112 , needsFlush(nullptr)
113 , redirectDev(nullptr)
114 , widgetItem(nullptr)
115 , extraPaintEngine(nullptr)
116 , polished(nullptr)
117 , graphicsEffect(nullptr)
118#if !defined(QT_NO_IM)
119 , imHints(Qt::ImhNone)
120#endif
121#if QT_CONFIG(tooltip)
122 , toolTipDuration(-1)
123#endif
124 , directFontResolveMask(0)
125 , inheritedFontResolveMask(0)
126 , directPaletteResolveMask(0)
127 , inheritedPaletteResolveMask(0)
128 , leftmargin(0)
129 , topmargin(0)
130 , rightmargin(0)
131 , bottommargin(0)
132 , leftLayoutItemMargin(0)
133 , topLayoutItemMargin(0)
134 , rightLayoutItemMargin(0)
135 , bottomLayoutItemMargin(0)
136 , hd(nullptr)
137 , size_policy(QSizePolicy::Preferred, QSizePolicy::Preferred)
138 , fg_role(QPalette::NoRole)
139 , bg_role(QPalette::NoRole)
140 , dirtyOpaqueChildren(1)
141 , isOpaque(0)
142 , retainSizeWhenHiddenChanged(0)
143 , inDirtyList(0)
144 , isScrolled(0)
145 , isMoved(0)
146 , usesDoubleBufferedGLContext(0)
147 , mustHaveWindowHandle(0)
148 , renderToTexture(0)
149 , textureChildSeen(0)
150#ifndef QT_NO_IM
151 , inheritsInputMethodHints(0)
152#endif
153 , renderToTextureReallyDirty(1)
154 , usesRhiFlush(0)
155 , childrenHiddenByWState(0)
156 , childrenShownByExpose(0)
157 , dontSetExplicitShowHide(0)
158 , inheritStyleRecursionGuard(0)
159#if defined(Q_OS_WIN)
160 , noPaintOnScreen(0)
161#endif
162{
163 if (Q_UNLIKELY(!qApp)) {
164 qFatal("QWidget: Must construct a QApplication before a QWidget");
165 return;
166 }
167
168 willBeWidget = true; // used in QObject's ctor
169 memset(high_attributes, 0, sizeof(high_attributes));
170
171#ifdef QWIDGET_EXTRA_DEBUG
172 static int count = 0;
173 qDebug() << "widgets" << ++count;
174#endif
175}
176
177
178QWidgetPrivate::~QWidgetPrivate()
179{
180 if (widgetItem)
181 widgetItem->wid = nullptr;
182
183 if (extra)
184 deleteExtra();
185}
186
187/*!
188 \internal
189*/
190void QWidgetPrivate::scrollChildren(int dx, int dy)
191{
192 Q_Q(QWidget);
193 if (q->children().size() > 0) { // scroll children
194 QPoint pd(dx, dy);
195 QObjectList childObjects = q->children();
196 for (int i = 0; i < childObjects.size(); ++i) { // move all children
197 QWidget *w = qobject_cast<QWidget*>(childObjects.at(i));
198 if (w && !w->isWindow()) {
199 QPoint oldp = w->pos();
200 QRect r(w->pos() + pd, w->size());
201 w->data->crect = r;
202 if (w->testAttribute(Qt::WA_WState_Created))
203 w->d_func()->setWSGeometry();
204 w->d_func()->setDirtyOpaqueRegion();
205 QMoveEvent e(r.topLeft(), oldp);
206 QCoreApplication::sendEvent(w, &e);
207 }
208 }
209 }
210}
211
212void QWidgetPrivate::setWSGeometry()
213{
214 Q_Q(QWidget);
215 if (QWindow *window = q->windowHandle())
216 window->setGeometry(data.crect);
217}
218
219void QWidgetPrivate::updateWidgetTransform(QEvent *event)
220{
221 Q_Q(QWidget);
222 if (q == QGuiApplication::focusObject() || event->type() == QEvent::FocusIn) {
223 QTransform t;
224 QPoint p = q->mapTo(q->topLevelWidget(), QPoint(0,0));
225 t.translate(p.x(), p.y());
226 QGuiApplication::inputMethod()->setInputItemTransform(t);
227 QGuiApplication::inputMethod()->setInputItemRectangle(q->rect());
228 QGuiApplication::inputMethod()->update(Qt::ImInputItemClipRectangle);
229 }
230}
231
232/*!
233 \property QWidget::autoFillBackground
234 \brief whether the widget background is filled automatically
235 \since 4.1
236
237 If enabled, this property will cause Qt to fill the background of the
238 widget before invoking the paint event. The color used is defined by the
239 QPalette::Window color role from the widget's \l{QPalette}{palette}.
240
241 In addition, Windows are always filled with QPalette::Window, unless the
242 WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.
243
244 This property cannot be turned off (i.e., set to false) if a widget's
245 parent has a static gradient for its background.
246
247 \warning Use this property with caution in conjunction with
248 \l{Qt Style Sheets}. When a widget has a style sheet with a valid
249 background or a border-image, this property is automatically disabled.
250
251 By default, this property is \c false.
252
253 \sa Qt::WA_OpaquePaintEvent, Qt::WA_NoSystemBackground,
254 {QWidget#Transparency and Double Buffering}{Transparency and Double Buffering}
255*/
256bool QWidget::autoFillBackground() const
257{
258 Q_D(const QWidget);
259 return d->extra && d->extra->autoFillBackground;
260}
261
262void QWidget::setAutoFillBackground(bool enabled)
263{
264 Q_D(QWidget);
265 if (!d->extra)
266 d->createExtra();
267 if (d->extra->autoFillBackground == enabled)
268 return;
269
270 d->extra->autoFillBackground = enabled;
271 d->updateIsOpaque();
272 update();
273 d->updateIsOpaque();
274}
275
276/*!
277 \class QWidget
278 \brief The QWidget class is the base class of all user interface objects.
279
280 \ingroup basicwidgets
281 \inmodule QtWidgets
282
283 The widget is the atom of the user interface: it receives mouse, keyboard
284 and other events from the window system, and paints a representation of
285 itself on the screen. Every widget is rectangular, and they are sorted in a
286 Z-order. A widget is clipped by its parent and by the widgets in front of
287 it.
288
289 A widget that is not embedded in a parent widget is called a window.
290 Usually, windows have a frame and a title bar, although it is also possible
291 to create windows without such decoration using suitable
292 \l{Qt::WindowFlags}{window flags}. In Qt, QMainWindow and the various
293 subclasses of QDialog are the most common window types.
294
295 Every widget's constructor accepts one or two standard arguments:
296
297 \list 1
298 \li \c{QWidget *parent = nullptr} is the parent of the new widget.
299 If it is \nullptr (the default), the new widget will be a window.
300 If not, it will be a child of \e parent, and be constrained by
301 \e parent's geometry (unless you specify Qt::Window as window flag).
302 \li \c{Qt::WindowFlags f = { }} (where available) sets the window flags;
303 the default is suitable for most widgets, but to get, for
304 example, a window without a window system frame, you must use
305 special flags.
306 \endlist
307
308 QWidget has many member functions, but some of them have little direct
309 functionality; for example, QWidget has a font property, but never uses
310 this itself. There are many subclasses that provide real functionality,
311 such as QLabel, QPushButton, QListWidget, and QTabWidget.
312
313
314 \section1 Top-Level and Child Widgets
315
316 A widget without a parent widget is always an independent window (top-level
317 widget). For these widgets, setWindowTitle() and setWindowIcon() set the
318 title bar and icon, respectively.
319
320 Non-window widgets are child widgets, displayed within their parent
321 widgets. Most widgets in Qt are mainly useful as child widgets. For
322 example, it is possible to display a button as a top-level window, but most
323 people prefer to put their buttons inside other widgets, such as QDialog.
324
325 \image parent-child-widgets.png
326 {Appointment widget with labeled child widgets}
327 \caption A parent widget containing various child widgets.
328
329 The diagram above shows a QGroupBox widget being used to hold various child
330 widgets in a layout provided by QGridLayout. The QLabel child widgets have
331 been outlined to indicate their full sizes.
332
333 If you want to use a QWidget to hold child widgets, you will usually want to
334 add a layout to the parent QWidget. See \l{Layout Management} for more
335 information.
336
337
338 \section1 Composite Widgets
339
340 When a widget is used as a container to group a number of child widgets, it
341 is known as a composite widget. These can be created by constructing a
342 widget with the required visual properties - a QFrame, for example - and
343 adding child widgets to it, usually managed by a layout.
344
345 Composite widgets can also be created by subclassing a standard widget,
346 such as QWidget or QFrame, and adding the necessary layout and child
347 widgets in the constructor of the subclass. Many of the \l{Qt Widgets Examples}
348 {examples provided with Qt} use this approach, and it is also covered in
349 the Qt \l{Widgets Tutorial}.
350
351
352 \section1 Custom Widgets and Painting
353
354 Since QWidget is a subclass of QPaintDevice, subclasses can be used to
355 display custom content that is composed using a series of painting
356 operations with an instance of the QPainter class. This approach contrasts
357 with the canvas-style approach used by the \l{Graphics View}
358 {Graphics View Framework} where items are added to a scene by the
359 application and are rendered by the framework itself.
360
361 Each widget performs all painting operations from within its paintEvent()
362 function. This is called whenever the widget needs to be redrawn, either
363 because of some external change or when requested by the application.
364
365 The \l{widgets/analogclock}{Analog Clock example} shows how a simple widget
366 can handle paint events.
367
368
369 \section1 Size Hints and Size Policies
370
371 When implementing a new widget, it is almost always useful to reimplement
372 sizeHint() to provide a reasonable default size for the widget and to set
373 the correct size policy with setSizePolicy().
374
375 By default, composite widgets that do not provide a size hint will be
376 sized according to the space requirements of their child widgets.
377
378 The size policy lets you supply good default behavior for the layout
379 management system, so that other widgets can contain and manage yours
380 easily. The default size policy indicates that the size hint represents
381 the preferred size of the widget, and this is often good enough for many
382 widgets.
383
384 \note The size of top-level widgets are constrained to 2/3 of the desktop's
385 height and width. You can resize() the widget manually if these bounds are
386 inadequate.
387
388
389 \section1 Events
390
391 Widgets respond to events that are typically caused by user actions. Qt
392 delivers events to widgets by calling specific event handler functions with
393 instances of QEvent subclasses containing information about each event.
394
395 If your widget only contains child widgets, you probably don't need to
396 implement any event handlers. If you want to detect a mouse click in a
397 child widget, call the child's underMouse() function inside the widget's
398 mousePressEvent().
399
400 The \l{widgets/scribble}{Scribble example} implements a wider set of
401 events to handle mouse movement, button presses, and window resizing.
402
403 You will need to supply the behavior and content for your own widgets, but
404 here is a brief overview of the events that are relevant to QWidget,
405 starting with the most common ones:
406
407 \list
408 \li paintEvent() is called whenever the widget needs to be repainted.
409 Every widget displaying custom content must implement it. Painting
410 using a QPainter can only take place in a paintEvent() or a
411 function called by a paintEvent().
412 \li resizeEvent() is called when the widget has been resized.
413 \li mousePressEvent() is called when a mouse button is pressed while
414 the mouse cursor is inside the widget, or when the widget has
415 grabbed the mouse using grabMouse(). Pressing the mouse without
416 releasing it is effectively the same as calling grabMouse().
417 \li mouseReleaseEvent() is called when a mouse button is released. A
418 widget receives mouse release events when it has received the
419 corresponding mouse press event. This means that if the user
420 presses the mouse inside \e your widget, then drags the mouse
421 somewhere else before releasing the mouse button, \e your widget
422 receives the release event. There is one exception: if a popup menu
423 appears while the mouse button is held down, this popup immediately
424 steals the mouse events.
425 \li mouseDoubleClickEvent() is called when the user double-clicks in
426 the widget. If the user double-clicks, the widget receives a mouse
427 press event, a mouse release event, this event instead of a second
428 mouse press event, and finally a second mouse release event.
429 (Some mouse move events may also be
430 received if the mouse is not held steady during this operation.) It
431 is \e{not possible} to distinguish a click from a double-click
432 until the second click arrives. (This is one reason why most GUI
433 books recommend that double-clicks be an extension of
434 single-clicks, rather than trigger a different action.)
435 \endlist
436
437 Widgets that accept keyboard input need to reimplement a few more event
438 handlers:
439
440 \list
441 \li keyPressEvent() is called whenever a key is pressed, and again when
442 a key has been held down long enough for it to auto-repeat. The
443 \uicontrol Tab and \uicontrol Shift+Tab keys are only passed to the widget if
444 they are not used by the focus-change mechanisms. To force those
445 keys to be processed by your widget, you must reimplement
446 QWidget::event().
447 \li focusInEvent() is called when the widget gains keyboard focus
448 (assuming you have called setFocusPolicy()). Well-behaved widgets
449 indicate that they own the keyboard focus in a clear but discreet
450 way.
451 \li focusOutEvent() is called when the widget loses keyboard focus.
452 \endlist
453
454 You may be required to also reimplement some of the less common event
455 handlers:
456
457 \list
458 \li mouseMoveEvent() is called whenever the mouse moves while a mouse
459 button is held down. This can be useful during drag and drop
460 operations. If you call \l{setMouseTracking()}{setMouseTracking}(true),
461 you get mouse move events even when no buttons are held down.
462 (See also the \l{Drag and Drop in Qt}{Drag and Drop} guide.)
463 \li keyReleaseEvent() is called whenever a key is released and while it
464 is held down (if the key is auto-repeating). In that case, the
465 widget will receive a pair of key release and key press event for
466 every repeat. The \uicontrol Tab and \uicontrol Shift+Tab keys are only passed
467 to the widget if they are not used by the focus-change mechanisms.
468 To force those keys to be processed by your widget, you must
469 reimplement QWidget::event().
470 \li wheelEvent() is called whenever the user turns the mouse wheel
471 while the widget has the focus.
472 \li enterEvent() is called when the mouse enters the widget's screen
473 space. (This excludes screen space owned by any of the widget's
474 children.)
475 \li leaveEvent() is called when the mouse leaves the widget's screen
476 space. If the mouse enters a child widget, it will not cause a
477 leaveEvent().
478 \li moveEvent() is called when the widget has been moved relative to
479 its parent.
480 \li closeEvent() is called when the user closes the widget (or when
481 close() is called).
482 \endlist
483
484 There are also some rather obscure events described in the documentation
485 for QEvent::Type. To handle these events, you need to reimplement event()
486 directly.
487
488 The default implementation of event() handles \uicontrol Tab and \uicontrol Shift+Tab
489 (to move the keyboard focus), and passes on most of the other events to
490 one of the more specialized handlers above.
491
492 Events and the mechanism used to deliver them are covered in
493 \l{The Event System}.
494
495 \section1 Groups of Functions and Properties
496
497 \table
498 \header \li Context \li Functions and Properties
499
500 \row \li Window functions \li
501 show(),
502 hide(),
503 raise(),
504 lower(),
505 close().
506
507 \row \li Top-level windows \li
508 \l windowModified, \l windowTitle, \l windowIcon,
509 \l isActiveWindow, activateWindow(), \l minimized, showMinimized(),
510 \l maximized, showMaximized(), \l fullScreen, showFullScreen(),
511 showNormal().
512
513 \row \li Window contents \li
514 update(),
515 repaint(),
516 scroll().
517
518 \row \li Geometry \li
519 \l pos, x(), y(), \l rect, \l size, width(), height(), move(), resize(),
520 \l sizePolicy, sizeHint(), minimumSizeHint(),
521 updateGeometry(), layout(),
522 \l frameGeometry, \l geometry, \l childrenRect, \l childrenRegion,
523 adjustSize(),
524 mapFromGlobal(), mapToGlobal(),
525 mapFromParent(), mapToParent(),
526 \l maximumSize, \l minimumSize, \l sizeIncrement,
527 \l baseSize, setFixedSize()
528
529 \row \li Mode \li
530 \l visible, isVisibleTo(),
531 \l enabled, isEnabledTo(),
532 \l modal,
533 isWindow(),
534 \l mouseTracking,
535 \l updatesEnabled,
536 visibleRegion().
537
538 \row \li Look and feel \li
539 style(),
540 setStyle(),
541 \l styleSheet,
542 \l cursor,
543 \l font,
544 \l palette,
545 backgroundRole(), setBackgroundRole(),
546 fontInfo(), fontMetrics().
547
548 \row \li Keyboard focus functions \li
549 \l focus, \l focusPolicy,
550 setFocus(), clearFocus(), setTabOrder(), setFocusProxy(),
551 focusNextChild(), focusPreviousChild().
552
553 \row \li Mouse and keyboard grabbing \li
554 grabMouse(), releaseMouse(),
555 grabKeyboard(), releaseKeyboard(),
556 mouseGrabber(), keyboardGrabber().
557
558 \row \li Event handlers \li
559 event(),
560 mousePressEvent(),
561 mouseReleaseEvent(),
562 mouseDoubleClickEvent(),
563 mouseMoveEvent(),
564 keyPressEvent(),
565 keyReleaseEvent(),
566 focusInEvent(),
567 focusOutEvent(),
568 wheelEvent(),
569 enterEvent(),
570 leaveEvent(),
571 paintEvent(),
572 moveEvent(),
573 resizeEvent(),
574 closeEvent(),
575 dragEnterEvent(),
576 dragMoveEvent(),
577 dragLeaveEvent(),
578 dropEvent(),
579 childEvent(),
580 showEvent(),
581 hideEvent(),
582 customEvent().
583 changeEvent(),
584
585 \row \li System functions \li
586 parentWidget(), window(), setParent(), winId(),
587 find(), metric().
588
589 \row \li Context menu \li
590 contextMenuPolicy, contextMenuEvent(),
591 customContextMenuRequested(), actions()
592
593 \row \li Interactive help \li
594 setToolTip(), setWhatsThis()
595
596 \endtable
597
598
599 \section1 Widget Style Sheets
600
601 In addition to the standard widget styles for each platform, widgets can
602 also be styled according to rules specified in a \l{styleSheet}
603 {style sheet}. This feature enables you to customize the appearance of
604 specific widgets to provide visual cues to users about their purpose. For
605 example, a button could be styled in a particular way to indicate that it
606 performs a destructive action.
607
608 The use of widget style sheets is described in more detail in the
609 \l{Qt Style Sheets} document.
610
611
612 \section1 Transparency and Double Buffering
613
614 QWidget automatically double-buffers its painting, so there
615 is no need to write double-buffering code in paintEvent() to avoid
616 flicker.
617
618 The contents of parent widgets are propagated by
619 default to each of their children as long as Qt::WA_PaintOnScreen is not
620 set. Custom widgets can be written to take advantage of this feature by
621 updating irregular regions (to create non-rectangular child widgets), or
622 painting with colors that have less than full alpha component. The
623 following diagram shows how attributes and properties of a custom widget
624 can be fine-tuned to achieve different effects.
625
626 \image propagation-custom.png
627 {Three pixmaps of a house with different background properties:
628 transparent, filled with white, and uninitialized}
629
630 In the above diagram, a semi-transparent rectangular child widget with an
631 area removed is constructed and added to a parent widget (a QLabel showing
632 a pixmap). Then, different properties and widget attributes are set to
633 achieve different effects:
634
635 \list
636 \li The left widget has no additional properties or widget attributes
637 set. This default state suits most custom widgets that have
638 transparency, are irregularly-shaped, or do not paint over their
639 entire area with an opaque brush.
640 \li The center widget has the \l autoFillBackground property set. This
641 property is used with custom widgets that rely on the widget to
642 supply a default background, and do not paint over their entire
643 area with an opaque brush.
644 \li The right widget has the Qt::WA_OpaquePaintEvent widget attribute
645 set. This indicates that the widget will paint over its entire area
646 with opaque colors. The widget's area will initially be
647 \e{uninitialized}, represented in the diagram with a red diagonal
648 grid pattern that shines through the overpainted area.
649 \endlist
650
651 To rapidly update custom widgets with simple background colors, such as
652 real-time plotting or graphing widgets, it is better to define a suitable
653 background color (using setBackgroundRole() with the
654 QPalette::Window role), set the \l autoFillBackground property, and only
655 implement the necessary drawing functionality in the widget's paintEvent().
656
657 To rapidly update custom widgets that constantly paint over their entire
658 areas with opaque content, for example, video streaming widgets, it is
659 better to set the widget's Qt::WA_OpaquePaintEvent, avoiding any unnecessary
660 overhead associated with repainting the widget's background.
661
662 If a widget has both the Qt::WA_OpaquePaintEvent widget attribute \e{and}
663 the \l autoFillBackground property set, the Qt::WA_OpaquePaintEvent
664 attribute takes precedence. Depending on your requirements, you should
665 choose either one of them.
666
667 The contents of parent widgets are also propagated to standard Qt widgets.
668 This can lead to some unexpected results if the parent widget is decorated
669 in a non-standard way, as shown in the diagram below.
670
671 \image propagation-standard.png
672 {One widget has a transparent background
673 and the other widget has a filled background}
674
675 The scope for customizing the painting behavior of standard Qt widgets,
676 without resorting to subclassing, is slightly less than that possible for
677 custom widgets. Usually, the desired appearance of a standard widget can be
678 achieved by setting its \l autoFillBackground property.
679
680
681 \section1 Creating Translucent Windows
682
683 You can create windows with translucent regions on window systems that
684 support compositing.
685
686 To enable this feature in a top-level widget, set its Qt::WA_TranslucentBackground
687 attribute with setAttribute() and ensure that its background is painted with
688 non-opaque colors in the regions you want to be partially transparent.
689
690 Platform notes:
691
692 \list
693 \li X11: This feature relies on the use of an X server that supports ARGB visuals
694 and a compositing window manager.
695 \li Windows: The widget needs to have the Qt::FramelessWindowHint window flag set
696 for the translucency to work.
697 \li \macos: The widget needs to have the Qt::FramelessWindowHint window flag set
698 for the translucency to work.
699 \endlist
700
701
702 \section1 Native Widgets vs Alien Widgets
703
704 Alien widgets are widgets unknown to the windowing system. They do not have
705 a native window handle associated with them. This feature significantly
706 speeds up widget painting, resizing, and removes flicker.
707
708 Should you require the old behavior with native windows, choose one of the
709 following options:
710
711 \list 1
712 \li Use the \c{QT_USE_NATIVE_WINDOWS=1} in your environment.
713 \li Set the Qt::AA_NativeWindows attribute on your application. All
714 widgets will be native widgets.
715 \li Set the Qt::WA_NativeWindow attribute on widgets: The widget itself
716 and all its ancestors will become native (unless
717 Qt::WA_DontCreateNativeAncestors is set).
718 \li Call QWidget::winId to enforce a native window (this implies 3).
719 \li Set the Qt::WA_PaintOnScreen attribute to enforce a native window
720 (this implies 3).
721 \endlist
722
723 \sa QEvent, QPainter, QGridLayout, QBoxLayout
724
725*/
726
727QWidgetMapper *QWidgetPrivate::mapper = nullptr; // widget with wid
728QWidgetSet *QWidgetPrivate::allWidgets = nullptr; // widgets with no wid
729
730
731/*****************************************************************************
732 QWidget member functions
733 *****************************************************************************/
734
735/*
736 Widget state flags:
737 \list
738 \li Qt::WA_WState_Created The widget has a valid winId().
739 \li Qt::WA_WState_Visible The widget is currently visible.
740 \li Qt::WA_WState_Hidden The widget is hidden, i.e. it won't
741 become visible unless you call show() on it. Qt::WA_WState_Hidden
742 implies !Qt::WA_WState_Visible.
743 \li Qt::WA_WState_CompressKeys Compress keyboard events.
744 \li Qt::WA_WState_BlockUpdates Repaints and updates are disabled.
745 \li Qt::WA_WState_InPaintEvent Currently processing a paint event.
746 \li Qt::WA_WState_Reparented The widget has been reparented.
747 \li Qt::WA_WState_ConfigPending A configuration (resize/move) event is pending.
748 \endlist
749*/
750
752{
753 /* this cleans up when the constructor throws an exception */
754 static inline void cleanup(QWidget *that, QWidgetPrivate *d)
755 {
756#ifdef QT_NO_EXCEPTIONS
757 Q_UNUSED(that);
758 Q_UNUSED(d);
759#else
760 QWidgetPrivate::allWidgets->remove(that);
761 d->removeFromFocusChain();
762#endif
763 }
764};
765
766/*!
767 Constructs a widget which is a child of \a parent, with widget
768 flags set to \a f.
769
770 If \a parent is \nullptr, the new widget becomes a window. If
771 \a parent is another widget, this widget becomes a child window
772 inside \a parent. The new widget is deleted when its \a parent is
773 deleted.
774
775 The widget flags argument, \a f, is normally 0, but it can be set
776 to customize the frame of a window (i.e. \a parent must be
777 \nullptr). To customize the frame, use a value composed
778 from the bitwise OR of any of the \l{Qt::WindowFlags}{window flags}.
779
780 If you add a child widget to an already visible widget you must
781 explicitly show the child to make it visible.
782
783 Note that the X11 version of Qt may not be able to deliver all
784 combinations of style flags on all systems. This is because on
785 X11, Qt can only ask the window manager, and the window manager
786 can override the application's settings. On Windows, Qt can set
787 whatever flags you want.
788
789 \sa windowFlags
790*/
791QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
792 : QWidget(*new QWidgetPrivate, parent, f)
793{
794}
795
796
797/*! \internal
798*/
799QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
800 : QObject(dd, nullptr), QPaintDevice()
801 , data(&dd.data)
802{
803 Q_D(QWidget);
804 QT_TRY {
805 d->init(parent, f);
806 } QT_CATCH(...) {
807 QWidgetExceptionCleaner::cleanup(this, d_func());
808 QT_RETHROW;
809 }
810}
811
812/*!
813 \internal
814*/
815int QWidget::devType() const
816{
817 return QInternal::Widget;
818}
819
820
821//### w is a "this" ptr, passed as a param because QWorkspace needs special logic
822void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
823{
824 bool customize = (flags & (Qt::CustomizeWindowHint
825 | Qt::FramelessWindowHint
826 | Qt::WindowTitleHint
827 | Qt::WindowSystemMenuHint
828 | Qt::WindowMinimizeButtonHint
829 | Qt::WindowMaximizeButtonHint
830 | Qt::WindowCloseButtonHint
831 | Qt::WindowContextHelpButtonHint));
832
833 uint type = (flags & Qt::WindowType_Mask);
834
835 if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
836 type = Qt::Window;
837 flags |= Qt::Window;
838 }
839
840 if (flags & Qt::CustomizeWindowHint) {
841 // modify window flags to make them consistent.
842 // Only enable this on non-Mac platforms. Since the old way of doing this would
843 // interpret WindowSystemMenuHint as a close button and we can't change that behavior
844 // we can't just add this in.
845 if ((flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint))
846# ifdef Q_OS_WIN
847 && type != Qt::Dialog // QTBUG-2027, allow for menu-less dialogs.
848# endif
849 ) {
850 flags |= Qt::WindowSystemMenuHint;
851 flags |= Qt::WindowTitleHint;
852 flags &= ~Qt::FramelessWindowHint;
853 }
854 } else if (customize && !(flags & Qt::FramelessWindowHint)) {
855 // if any of the window hints that affect the titlebar are set
856 // and the window is supposed to have frame, we add a titlebar
857 // and system menu by default.
858 flags |= Qt::WindowSystemMenuHint;
859 flags |= Qt::WindowTitleHint;
860 }
861 if (!customize) { // don't modify window flags if the user explicitly set them.
862 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
863 if (type != Qt::Dialog && type != Qt::Sheet && type != Qt::Tool)
864 flags |= Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowFullscreenButtonHint;
865 }
866 if (w->testAttribute(Qt::WA_TransparentForMouseEvents))
867 flags |= Qt::WindowTransparentForInput;
868}
869
870void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
871{
872 Q_Q(QWidget);
873 isWidget = true;
874 wasWidget = true;
875
876#if QT_DEPRECATED_SINCE(6, 11)
877 QT_IGNORE_DEPRECATIONS(
878 const bool isDesktopWindow = (f & Qt::WindowType_Mask) == Qt::WindowType::Desktop;
879 Q_ASSERT_X(!isDesktopWindow, Q_FUNC_INFO, "Qt::WindowType::Desktop is not allowed.");
880 if (isDesktopWindow)
881 f.setFlag(Qt::WindowType::Desktop, false);
882 )
883#endif
884
885 Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO, "Cannot parent a QWidget to itself");
886
887 if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
888 qFatal("QWidget: Cannot create a QWidget without QApplication");
889
890 Q_ASSERT(allWidgets);
891 if (allWidgets)
892 allWidgets->insert(q);
893
894#if QT_CONFIG(thread)
895 if (!parent) {
896 Q_ASSERT_X(QThread::isMainThread(), "QWidget",
897 "Widgets must be created in the GUI thread.");
898 }
899#endif
900
901 data.fstrut_dirty = true;
902
903 data.winid = 0;
904 data.widget_attributes = 0;
905 data.window_flags = f;
906 data.window_state = 0;
907 data.focus_policy = 0;
908 data.context_menu_policy = Qt::DefaultContextMenu;
909 data.window_modality = Qt::NonModal;
910
911 data.sizehint_forced = 0;
912 data.is_closing = false;
913 data.in_show = 0;
914 data.in_set_window_state = 0;
915 data.in_destructor = false;
916
917 // Widgets with Qt::MSWindowsOwnDC (typically QGLWidget) must have a window handle.
918 if (f & Qt::MSWindowsOwnDC) {
919 mustHaveWindowHandle = 1;
920 q->setAttribute(Qt::WA_NativeWindow);
921 }
922
923 q->setAttribute(Qt::WA_QuitOnClose); // might be cleared in adjustQuitOnCloseAttribute()
924 adjustQuitOnCloseAttribute();
925
926 q->setAttribute(Qt::WA_WState_Hidden);
927
928 //give potential windows a bigger "pre-initial" size; create() will give them a new size later
929 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
930 initFocusChain();
931
932 if (parentWidget)
933 q->setParent(parentWidget, data.window_flags);
934 else {
935 adjustFlags(data.window_flags, q);
936 resolveLayoutDirection();
937 // opaque system background?
938 const QBrush &background = q->palette().brush(QPalette::Window);
939 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
940 }
941 data.fnt = QFont(data.fnt, q);
942
943 q->setAttribute(Qt::WA_PendingMoveEvent);
944 q->setAttribute(Qt::WA_PendingResizeEvent);
945
946 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
947 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
948
949 QEvent e(QEvent::Create);
950 QCoreApplication::sendEvent(q, &e);
951 QCoreApplication::postEvent(q, new QEvent(QEvent::PolishRequest));
952
953 extraPaintEngine = nullptr;
954}
955
956void QWidgetPrivate::createRecursively()
957{
958 Q_Q(QWidget);
959 q->create(0, true, true);
960 for (int i = 0; i < children.size(); ++i) {
961 QWidget *child = qobject_cast<QWidget *>(children.at(i));
962 if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
963 child->d_func()->createRecursively();
964 }
965}
966
967QRhi *QWidgetPrivate::rhi() const
968{
969 Q_Q(const QWidget);
970 if (auto *backingStore = q->backingStore()) {
971 auto *window = windowHandle(WindowHandleMode::Closest);
972 return backingStore->handle()->rhi(window);
973 } else {
974 return nullptr;
975 }
976}
977
978/*!
979 \internal
980 Returns the closest parent widget that has a QWindow window handle
981
982 \note This behavior is different from nativeParentWidget(), which
983 returns the closest parent that has a QWindow window handle with
984 a created QPlatformWindow, and hence native window (winId).
985*/
986QWidget *QWidgetPrivate::closestParentWidgetWithWindowHandle() const
987{
988 Q_Q(const QWidget);
989 QWidget *parent = q->parentWidget();
990 while (parent && !parent->windowHandle())
991 parent = parent->parentWidget();
992 return parent;
993}
994
995QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode) const
996{
997 if (mode == WindowHandleMode::Direct || mode == WindowHandleMode::Closest) {
998 if (QTLWExtra *x = maybeTopData()) {
999 if (x->window != nullptr || mode == WindowHandleMode::Direct)
1000 return x->window;
1001 }
1002 }
1003 if (mode == WindowHandleMode::Closest) {
1004 // FIXME: Use closestParentWidgetWithWindowHandle instead
1005 if (auto nativeParent = q_func()->nativeParentWidget()) {
1006 if (auto window = nativeParent->windowHandle())
1007 return window;
1008 }
1009 }
1010 if (mode == WindowHandleMode::TopLevel || mode == WindowHandleMode::Closest) {
1011 if (auto topLevel = q_func()->topLevelWidget()) {
1012 if (auto window = topLevel ->windowHandle())
1013 return window;
1014 }
1015 }
1016 return nullptr;
1017}
1018
1019/*!
1020 \internal
1021
1022 Used by clients outside of widgets to get a handle to the
1023 closest QWindow without having to link to widgets.
1024*/
1025QWindow *QWidgetPrivate::_q_closestWindowHandle() const
1026{
1027 return windowHandle(QWidgetPrivate::WindowHandleMode::Closest);
1028}
1029
1030QScreen *QWidgetPrivate::associatedScreen() const
1031{
1032#if QT_CONFIG(graphicsview)
1033 // embedded widgets never have a screen associated, let QWidget::screen fall back to toplevel
1034 if (nearestGraphicsProxyWidget(q_func()))
1035 return nullptr;
1036#endif
1037 if (auto window = windowHandle(WindowHandleMode::Closest))
1038 return window->screen();
1039 return nullptr;
1040}
1041
1042// finds the first rhiconfig in the hierarchy that has enable==true
1043static bool q_evaluateRhiConfigRecursive(const QWidget *w, QPlatformBackingStoreRhiConfig *outConfig, QSurface::SurfaceType *outType)
1044{
1045 QPlatformBackingStoreRhiConfig config = QWidgetPrivate::get(w)->rhiConfig();
1046 if (config.isEnabled()) {
1047 if (outConfig)
1048 *outConfig = config;
1049 if (outType)
1050 *outType = QBackingStoreRhiSupport::surfaceTypeForConfig(config);
1051 return true;
1052 }
1053 for (const QObject *child : w->children()) {
1054 if (const QWidget *childWidget = qobject_cast<const QWidget *>(child)) {
1055 if (q_evaluateRhiConfigRecursive(childWidget, outConfig, outType)) {
1056 static bool optOut = qEnvironmentVariableIsSet("QT_WIDGETS_NO_CHILD_RHI");
1057 // Native child widgets should not trigger RHI for its parent
1058 // hierarchy, but will still flush the native child using RHI.
1059 if (!optOut && childWidget->testAttribute(Qt::WA_NativeWindow))
1060 continue;
1061
1062 return true;
1063 }
1064 }
1065 }
1066 return false;
1067}
1068
1069bool q_evaluateRhiConfig(const QWidget *w, QPlatformBackingStoreRhiConfig *outConfig, QSurface::SurfaceType *outType)
1070{
1071 // First, check env.vars. or other means that force the usage of rhi-based
1072 // flushing with a specific graphics API. This takes precedence over what
1073 // the widgets themselves declare. This is global, applying to all
1074 // top-levels.
1075 if (QBackingStoreRhiSupport::checkForceRhi(outConfig, outType)) {
1076 qCDebug(lcWidgetPainting) << "Tree with root" << w << "evaluated to forced flushing with QRhi";
1077 return true;
1078 }
1079
1080 // Otherwise, check the widget hierarchy to see if there is a child (or
1081 // ourselves) that declare the need for rhi-based composition.
1082 if (q_evaluateRhiConfigRecursive(w, outConfig, outType)) {
1083 qCDebug(lcWidgetPainting) << "Tree with root" << w << "evaluates to flushing with QRhi";
1084 return true;
1085 }
1086
1087 return false;
1088}
1089
1090// ### fixme: Qt 6: Remove parameter window from QWidget::create()
1091
1092/*!
1093 Creates a new widget window.
1094
1095 The parameters \a window, \a initializeWindow, and \a destroyOldWindow
1096 are ignored in Qt 5. Please use QWindow::fromWinId() to create a
1097 QWindow wrapping a foreign window and pass it to
1098 QWidget::createWindowContainer() instead.
1099
1100 \sa createWindowContainer(), QWindow::fromWinId()
1101*/
1102
1103void QWidget::create(WId window, bool initializeWindow, bool destroyOldWindow)
1104{
1105 Q_UNUSED(initializeWindow);
1106 Q_UNUSED(destroyOldWindow);
1107
1108 Q_D(QWidget);
1109 if (Q_UNLIKELY(window))
1110 qWarning("QWidget::create(): Parameter 'window' does not have any effect.");
1111 if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
1112 return;
1113
1114 if (d->data.in_destructor)
1115 return;
1116
1117 Qt::WindowType type = windowType();
1118 Qt::WindowFlags &flags = data->window_flags;
1119
1120 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1121 type = Qt::Window;
1122 flags |= Qt::Window;
1123 }
1124
1125 if (QWidget *parent = parentWidget()) {
1126 if (type & Qt::Window) {
1127 if (!parent->testAttribute(Qt::WA_WState_Created))
1128 parent->createWinId();
1129 } else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
1130 && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
1131 // We're about to create a native child widget that doesn't have a native parent;
1132 // enforce a native handle for the parent unless the Qt::WA_DontCreateNativeAncestors
1133 // attribute is set.
1134 d->createWinId();
1135 // Nothing more to do.
1136 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1137 Q_ASSERT(internalWinId());
1138 return;
1139 }
1140 }
1141
1142
1143 static const bool paintOnScreenEnv = qEnvironmentVariableIntValue("QT_ONSCREEN_PAINT") > 0;
1144 if (paintOnScreenEnv)
1145 setAttribute(Qt::WA_PaintOnScreen);
1146
1147 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1148 setAttribute(Qt::WA_NativeWindow);
1149
1150 if (isWindow()
1151#if QT_CONFIG(graphicsview)
1152 && !graphicsProxyWidget()
1153#endif
1154 ) {
1155 // Make top levels automatically respect safe areas by default
1156 auto *topExtra = d->maybeTopData();
1157 if (!topExtra || !topExtra->explicitContentsMarginsRespectsSafeArea) {
1158 setAttribute_internal(Qt::WA_ContentsMarginsRespectsSafeArea,
1159 true, data, d);
1160 }
1161 }
1162
1163 d->updateIsOpaque();
1164
1165 setAttribute(Qt::WA_WState_Created); // set created flag
1166 d->create();
1167
1168 // A real toplevel window needs a paint manager
1169 if (isWindow())
1170 d->topData()->repaintManager.reset(new QWidgetRepaintManager(this));
1171
1172 d->setModal_sys();
1173
1174 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1175 setAttribute(Qt::WA_DropSiteRegistered, true);
1176
1177 // need to force the resting of the icon after changing parents
1178 if (testAttribute(Qt::WA_SetWindowIcon))
1179 d->setWindowIcon_sys();
1180
1181 if (isWindow() && !d->topData()->iconText.isEmpty())
1182 d->setWindowIconText_helper(d->topData()->iconText);
1183 if (isWindow() && !d->topData()->caption.isEmpty())
1184 d->setWindowTitle_helper(d->topData()->caption);
1185 if (isWindow() && !d->topData()->filePath.isEmpty())
1186 d->setWindowFilePath_helper(d->topData()->filePath);
1187 d->updateSystemBackground();
1188
1189 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1190 d->setWindowIcon_sys();
1191
1192 // Frame strut update needed in cases where there are native widgets such as QGLWidget,
1193 // as those force native window creation on their ancestors before they are shown.
1194 // If the strut is not updated, any subsequent move of the top level window before show
1195 // will cause window frame to be ignored when positioning the window.
1196 // Note that this only helps on platforms that handle window creation synchronously.
1197 d->updateFrameStrut();
1198}
1199
1201{
1202 QObjectList children = parentWidget->children();
1203 for (int i = 0; i < children.size(); i++) {
1204 if (children.at(i)->isWidgetType()) {
1205 const QWidget *childWidget = qobject_cast<const QWidget *>(children.at(i));
1206 if (childWidget) { // should not be necessary
1207 if (childWidget->testAttribute(Qt::WA_NativeWindow)) {
1208 if (!childWidget->internalWinId())
1209 childWidget->winId();
1210 if (childWidget->windowHandle()) {
1211 if (childWidget->isWindow()) {
1212 childWidget->windowHandle()->setTransientParent(parentWidget->window()->windowHandle());
1213 } else {
1214 childWidget->windowHandle()->setParent(childWidget->nativeParentWidget()->windowHandle());
1215 }
1216 }
1217 } else {
1219 }
1220 }
1221 }
1222 }
1223
1224}
1225
1226void QWidgetPrivate::create()
1227{
1228 Q_Q(QWidget);
1229
1230 if (!q->testAttribute(Qt::WA_NativeWindow) && !q->isWindow())
1231 return; // we only care about real toplevels
1232
1233 QWidgetWindow *win = topData()->window;
1234 // topData() ensures the extra is created but does not ensure 'window' is non-null
1235 // in case the extra was already valid.
1236 if (!win) {
1237 createTLSysExtra();
1238 Q_ASSERT(topData()->window);
1239 win = topData()->window;
1240 }
1241
1242 const auto dynamicPropertyNames = q->dynamicPropertyNames();
1243 for (const QByteArray &propertyName : dynamicPropertyNames) {
1244 if (!qstrncmp(propertyName, "_q_platform_", 12))
1245 win->setProperty(propertyName, q->property(propertyName));
1246 }
1247
1248 Qt::WindowFlags &flags = data.window_flags;
1249
1250#if defined(QT_PLATFORM_UIKIT)
1251 if (q->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
1252 flags |= Qt::ExpandedClientAreaHint;
1253#endif
1254
1255 if (q->testAttribute(Qt::WA_ShowWithoutActivating))
1256 win->setProperty("_q_showWithoutActivating", QVariant(true));
1257 if (q->testAttribute(Qt::WA_MacAlwaysShowToolWindow))
1258 win->setProperty("_q_macAlwaysShowToolWindow", QVariant(true));
1259 win->setFlags(flags);
1260 fixPosIncludesFrame();
1261 if (q->testAttribute(Qt::WA_Moved)
1262 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
1263 win->setGeometry(q->geometry());
1264 else
1265 win->resize(q->size());
1266 if (win->isTopLevel()) {
1267 QScreen *targetScreen = topData()->initialScreen;
1268 topData()->initialScreen = nullptr;
1269 if (!targetScreen)
1270 targetScreen = q->screen();
1271 win->setScreen(targetScreen);
1272 }
1273
1274 QSurfaceFormat format = win->requestedFormat();
1275 if ((flags & Qt::Window) && win->surfaceType() != QSurface::OpenGLSurface
1276 && q->testAttribute(Qt::WA_TranslucentBackground)) {
1277 format.setAlphaBufferSize(8);
1278 }
1279 win->setFormat(format);
1280
1281 if (QWidget *nativeParent = q->nativeParentWidget()) {
1282 if (nativeParent->windowHandle()) {
1283 if (flags & Qt::Window) {
1284 // When setting the transient parent for a top-level window, skip
1285 // past any popup-based windows (Popup, Tool, ToolTip, SplashScreen)
1286 // in the native parent chain. These window types are typically short-
1287 // lived and dismissed as a side-effect of showing other top-level
1288 // windows. On some platforms (e.g., Wayland), closing a parent window
1289 // sends close events to all registered transient children; if a stable
1290 // dialog retained a popup-based parent, it could be unexpectedly closed
1291 // when that parent is dismissed.
1292 //
1293 // Popup-based windows that are themselves children of popup-based
1294 // parents intentionally retain the popup parent relationship so they
1295 // stay visually stacked and are dismissed together.
1296 const auto winType = Qt::WindowType((flags & Qt::WindowType_Mask).toInt());
1297 auto isPopupBased = [](Qt::WindowType type) {
1298 return type == Qt::Popup || type == Qt::Tool || type == Qt::ToolTip
1299 || type == Qt::SplashScreen;
1300 };
1301
1302 QWindow *transientParent = nativeParent->window()->windowHandle();
1303 if (!isPopupBased(winType)) {
1304 // Walk up past popup-based ancestors to find a stable parent.
1305 while (transientParent) {
1306 const Qt::WindowType tp = transientParent->type();
1307 if (!isPopupBased(tp))
1308 break;
1309 transientParent = transientParent->transientParent();
1310 }
1311 }
1312 win->setTransientParent(transientParent);
1313 win->setParent(nullptr);
1314 } else {
1315 win->setTransientParent(nullptr);
1316 win->setParent(nativeParent->windowHandle());
1317 }
1318 }
1319 }
1320
1321 qt_window_private(win)->positionPolicy = topData()->posIncludesFrame ?
1322 QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
1323
1324 win->create();
1325 // Enable nonclient-area events for QDockWidget and other NonClientArea-mouse event processing.
1326 if (QPlatformWindow *platformWindow = win->handle())
1327 platformWindow->setFrameStrutEventsEnabled(true);
1328
1329 data.window_flags = win->flags();
1330
1331#if QT_CONFIG(xcb)
1332 if (!topData()->role.isNull()) {
1333 if (auto *xcbWindow = dynamic_cast<QXcbWindow*>(win->handle()))
1334 xcbWindow->setWindowRole(topData()->role);
1335 }
1336#endif
1337#if QT_CONFIG(wayland)
1338 if (!topData()->role.isNull()) {
1339 if (auto *waylandWindow = dynamic_cast<QWaylandWindow*>(win->handle()))
1340 waylandWindow->setSessionRestoreId(topData()->role);
1341 }
1342#endif
1343
1344 QBackingStore *store = q->backingStore();
1345 usesRhiFlush = false;
1346
1347 if (!store && q->isWindow())
1348 q->setBackingStore(new QBackingStore(win));
1349
1350 QPlatformBackingStoreRhiConfig rhiConfig;
1351 usesRhiFlush = q_evaluateRhiConfig(q, &rhiConfig, nullptr);
1352 if (usesRhiFlush && q->backingStore()) {
1353 // Trigger creation of support infrastructure up front,
1354 // now that we have a specific RHI configuration.
1355 q->backingStore()->handle()->createRhi(win, rhiConfig);
1356 }
1357
1358 setWindowModified_helper();
1359
1360 if (win->handle()) {
1361 WId id = win->winId();
1362 // See the QPlatformWindow::winId() documentation
1363 Q_ASSERT(id != WId(0));
1364 setWinId(id);
1365 }
1366 setNetWmWindowTypes(true); // do nothing if none of WA_X11NetWmWindowType* is set
1367
1368 // Check children and create windows for them if necessary
1369 q_createNativeChildrenAndSetParent(q);
1370
1371 if (extra && !extra->mask.isEmpty())
1372 setMask_sys(extra->mask);
1373
1374 if (data.crect.width() == 0 || data.crect.height() == 0) {
1375 q->setAttribute(Qt::WA_OutsideWSRange, true);
1376 } else {
1377 q->setAttribute(Qt::WA_OutsideWSRange, false);
1378 if (q->isVisible()) {
1379 // If widget is already shown, set window visible, too
1380 win->setNativeWindowVisibility(true);
1381 }
1382 }
1383}
1384
1385#ifdef Q_OS_WIN
1386static const char activeXNativeParentHandleProperty[] = "_q_embedded_native_parent_handle";
1387#endif
1388
1389void QWidgetPrivate::createTLSysExtra()
1390{
1391 Q_Q(QWidget);
1392 if (!extra->topextra->window && (q->testAttribute(Qt::WA_NativeWindow) || q->isWindow())) {
1393 extra->topextra->window = new QWidgetWindow(q);
1394 if (extra->minw || extra->minh)
1395 extra->topextra->window->setMinimumSize(QSize(extra->minw, extra->minh));
1396 if (extra->maxw != QWIDGETSIZE_MAX || extra->maxh != QWIDGETSIZE_MAX)
1397 extra->topextra->window->setMaximumSize(QSize(extra->maxw, extra->maxh));
1398 if (extra->topextra->opacity != 255 && q->isWindow())
1399 extra->topextra->window->setOpacity(qreal(extra->topextra->opacity) / qreal(255));
1400
1401#if QT_CONFIG(tooltip)
1402 const bool isTipLabel = qobject_cast<const QTipLabel *>(q) != nullptr;
1403#endif
1404 const bool isAlphaWidget = !isTipLabel && q->inherits("QAlphaWidget");
1405#ifdef Q_OS_WIN
1406 // Pass on native parent handle for Widget embedded into Active X.
1407 const QVariant activeXNativeParentHandle = q->property(activeXNativeParentHandleProperty);
1408 if (activeXNativeParentHandle.isValid())
1409 extra->topextra->window->setProperty(activeXNativeParentHandleProperty, activeXNativeParentHandle);
1410 if (isTipLabel || isAlphaWidget)
1411 extra->topextra->window->setProperty("_q_windowsDropShadow", QVariant(true));
1412#endif
1413 if (isTipLabel || isAlphaWidget || q->inherits("QRollEffect"))
1414 qt_window_private(extra->topextra->window)->setAutomaticPositionAndResizeEnabled(false);
1415
1416 updateIsTranslucent();
1417 }
1418
1419}
1420
1421/*!
1422 Destroys the widget.
1423
1424 All this widget's children are deleted first. The application
1425 exits if this widget is the main widget.
1426*/
1427
1428QWidget::~QWidget()
1429{
1430 Q_D(QWidget);
1431 d->data.in_destructor = true;
1432
1433#if QT_CONFIG(accessibility)
1434 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing && QAccessible::isActive())
1435 QAccessibleCache::instance()->sendObjectDestroyedEvent(this);
1436#endif
1437
1438#if defined (QT_CHECK_STATE)
1439 if (Q_UNLIKELY(paintingActive()))
1440 qWarning("QWidget: %s (%s) deleted while being painted", className(), name());
1441#endif
1442
1443#ifndef QT_NO_GESTURES
1444 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1445 // \forall Qt::GestureType type : ungrabGesture(type) (inlined)
1446 for (auto it = d->gestureContext.keyBegin(), end = d->gestureContext.keyEnd(); it != end; ++it)
1447 manager->cleanupCachedGestures(this, *it);
1448 }
1449 d->gestureContext.clear();
1450#endif
1451
1452#if QT_CONFIG(action)
1453 // remove all actions from this widget
1454 for (auto action : std::as_const(d->actions)) {
1455 QActionPrivate *apriv = action->d_func();
1456 apriv->associatedObjects.removeAll(this);
1457 }
1458 d->actions.clear();
1459#endif
1460
1461#ifndef QT_NO_SHORTCUT
1462 // Remove all shortcuts grabbed by this
1463 // widget, unless application is closing
1464 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1465 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0, this, QKeySequence());
1466#endif
1467
1468 // delete layout while we still are a valid widget
1469 delete d->layout;
1470 d->layout = nullptr;
1471
1472 // Remove this from focus list
1473 d->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency);
1474
1475 QT_TRY {
1476#if QT_CONFIG(graphicsview)
1477 const QWidget* w = this;
1478 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
1479 w = w->d_func()->extra->focus_proxy;
1480 QWidget *window = w->window();
1481 QWExtra *e = window ? window->d_func()->extra.get() : nullptr ;
1482 if (!e || !e->proxyWidget || (w->parentWidget() && w->parentWidget()->d_func()->focus_child == this))
1483#endif
1484 clearFocus();
1485 } QT_CATCH(...) {
1486 // swallow this problem because we are in a destructor
1487 }
1488
1489 d->setDirtyOpaqueRegion();
1490
1491 if (isWindow() && isVisible() && internalWinId()) {
1492 QT_TRY {
1493 d->close();
1494 } QT_CATCH(...) {
1495 // if we're out of memory, at least hide the window.
1496 QT_TRY {
1497 hide();
1498 } QT_CATCH(...) {
1499 // and if that also doesn't work, then give up
1500 }
1501 }
1502 } else if (isVisible()) {
1503 qApp->d_func()->sendSyntheticEnterLeave(this);
1504 }
1505
1506 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
1507 repaintManager->removeDirtyWidget(this);
1508 if (testAttribute(Qt::WA_StaticContents))
1509 repaintManager->removeStaticWidget(this);
1510 }
1511
1512 delete d->needsFlush;
1513 d->needsFlush = nullptr;
1514
1515 // The next 20 lines are duplicated from QObject, but required here
1516 // since QWidget deletes is children itself
1517 bool blocked = d->blockSig;
1518 d->blockSig = 0; // unblock signals so we always emit destroyed()
1519
1520 if (d->isSignalConnected(0)) {
1521 QT_TRY {
1522 emit destroyed(this);
1523 } QT_CATCH(...) {
1524 // all the signal/slots connections are still in place - if we don't
1525 // quit now, we will crash pretty soon.
1526 qWarning("Detected an unexpected exception in ~QWidget while emitting destroyed().");
1527 QT_RETHROW;
1528 }
1529 }
1530
1531 if (d->declarativeData) {
1532 d->wasDeleted = true; // needed, so that destroying the declarative data does the right thing
1533 if (QAbstractDeclarativeData::destroyed)
1534 QAbstractDeclarativeData::destroyed(d->declarativeData, this);
1535 d->declarativeData = nullptr; // don't activate again in ~QObject
1536 d->wasDeleted = false;
1537 }
1538
1539 d->blockSig = blocked;
1540
1541 if (!d->children.isEmpty())
1542 d->deleteChildren();
1543
1544 QCoreApplication::removePostedEvents(this);
1545
1546 QT_TRY {
1547 destroy(); // platform-dependent cleanup
1548 } QT_CATCH(...) {
1549 // if this fails we can't do anything about it but at least we are not allowed to throw.
1550 }
1551 --QWidgetPrivate::instanceCounter;
1552
1553 if (QWidgetPrivate::allWidgets) // might have been deleted by ~QApplication
1554 QWidgetPrivate::allWidgets->remove(this);
1555
1556 QT_TRY {
1557 QEvent e(QEvent::Destroy);
1558 QCoreApplication::sendEvent(this, &e);
1559 } QT_CATCH(const std::exception&) {
1560 // if this fails we can't do anything about it but at least we are not allowed to throw.
1561 }
1562
1563#if QT_CONFIG(graphicseffect)
1564 delete d->graphicsEffect;
1565#endif
1566 d->deleteExtra();
1567
1568 d->isWidget = false;
1569}
1570
1571int QWidgetPrivate::instanceCounter = 0; // Current number of widget instances
1572int QWidgetPrivate::maxInstances = 0; // Maximum number of widget instances
1573
1574void QWidgetPrivate::setWinId(WId id) // set widget identifier
1575{
1576 Q_Q(QWidget);
1577 if (mapper && data.winid) {
1578 mapper->remove(data.winid);
1579 }
1580
1581 const WId oldWinId = data.winid;
1582
1583 data.winid = id;
1584 if (mapper && id) {
1585 mapper->insert(data.winid, q);
1586 }
1587
1588 if (oldWinId != id) {
1589 QEvent e(QEvent::WinIdChange);
1590 QCoreApplication::sendEvent(q, &e);
1591 }
1592}
1593
1594void QWidgetPrivate::createTLExtra()
1595{
1596 if (!extra)
1597 createExtra();
1598 if (!extra->topextra) {
1599 extra->topextra = std::make_unique<QTLWExtra>();
1600 QTLWExtra* x = extra->topextra.get();
1601 x->backingStore = nullptr;
1602 x->sharedPainter = nullptr;
1603 x->incw = x->inch = 0;
1604 x->basew = x->baseh = 0;
1605 x->frameStrut.setCoords(0, 0, 0, 0);
1606 x->normalGeometry = QRect(0,0,-1,-1);
1607 x->savedFlags = { };
1608 x->opacity = 255;
1609 x->posIncludesFrame = 0;
1610 x->sizeAdjusted = false;
1611 x->embedded = 0;
1612 x->explicitContentsMarginsRespectsSafeArea = 0;
1613 x->window = nullptr;
1614 x->initialScreen = nullptr;
1615
1616#ifdef QWIDGET_EXTRA_DEBUG
1617 static int count = 0;
1618 qDebug() << "tlextra" << ++count;
1619#endif
1620 }
1621}
1622
1623/*!
1624 \internal
1625 Creates the widget extra data.
1626*/
1627
1628void QWidgetPrivate::createExtra()
1629{
1630 if (!extra) { // if not exists
1631 extra = std::make_unique<QWExtra>();
1632 extra->glContext = nullptr;
1633#if QT_CONFIG(graphicsview)
1634 extra->proxyWidget = nullptr;
1635#endif
1636 extra->minw = 0;
1637 extra->minh = 0;
1638 extra->maxw = QWIDGETSIZE_MAX;
1639 extra->maxh = QWIDGETSIZE_MAX;
1640 extra->customDpiX = 0;
1641 extra->customDpiY = 0;
1642 extra->explicitMinSize = 0;
1643 extra->explicitMaxSize = 0;
1644 extra->autoFillBackground = 0;
1645 extra->nativeChildrenForced = 0;
1646 extra->inRenderWithPainter = 0;
1647 extra->hasWindowContainer = false;
1648 extra->hasMask = 0;
1649 createSysExtra();
1650#ifdef QWIDGET_EXTRA_DEBUG
1651 static int count = 0;
1652 qDebug() << "extra" << ++count;
1653#endif
1654 }
1655}
1656
1657void QWidgetPrivate::createSysExtra()
1658{
1659}
1660
1661/*!
1662 \internal
1663 Deletes the widget extra data.
1664*/
1665
1666void QWidgetPrivate::deleteExtra()
1667{
1668 if (extra) { // if exists
1669 deleteSysExtra();
1670#if QT_CONFIG(style_stylesheet)
1671 // dereference the stylesheet style
1672 if (QStyleSheetStyle *proxy = qt_styleSheet(extra->style))
1673 proxy->deref();
1674#endif
1675 if (extra->topextra)
1676 deleteTLSysExtra();
1677
1678 // extra->xic destroyed in QWidget::destroy()
1679 extra.reset();
1680 }
1681}
1682
1683void QWidgetPrivate::deleteSysExtra()
1684{
1685}
1686
1687void QWidgetPrivate::deleteTLSysExtra()
1688{
1689 Q_Q(QWidget);
1690 if (extra && extra->topextra) {
1691 if (extra->hasWindowContainer)
1692 QWindowContainer::toplevelAboutToBeDestroyed(q);
1693
1694 delete extra->topextra->window;
1695 extra->topextra->window = nullptr;
1696 }
1697}
1698
1699/*
1700 Returns \c region of widgets above this which overlap with
1701 \a rect, which is in parent's coordinate system (same as crect).
1702*/
1703
1704QRegion QWidgetPrivate::overlappedRegion(const QRect &rect, bool breakAfterFirst) const
1705{
1706 Q_Q(const QWidget);
1707
1708 const QWidget *w = q;
1709 QRect r = rect;
1710 QPoint p;
1711 QRegion region;
1712 while (w) {
1713 if (w->isWindow())
1714 break;
1715 QWidgetPrivate *pd = w->parentWidget()->d_func();
1716 bool above = false;
1717 for (int i = 0; i < pd->children.size(); ++i) {
1718 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
1719 if (!sibling || !sibling->isVisible() || sibling->isWindow())
1720 continue;
1721 if (!above) {
1722 above = (sibling == w);
1723 continue;
1724 }
1725
1726 const QRect siblingRect = sibling->d_func()->effectiveRectFor(sibling->data->crect);
1727 if (qRectIntersects(siblingRect, r)) {
1728 const auto &siblingExtra = sibling->d_func()->extra;
1729 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
1730 && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
1731 continue;
1732 }
1733 region += siblingRect.translated(-p);
1734 if (breakAfterFirst)
1735 break;
1736 }
1737 }
1738 w = w->parentWidget();
1739 r.translate(pd->data.crect.topLeft());
1740 p += pd->data.crect.topLeft();
1741 }
1742 return region;
1743}
1744
1745void QWidgetPrivate::syncBackingStore()
1746{
1747 if (shouldPaintOnScreen()) {
1748 paintOnScreen(dirty);
1749 dirty = QRegion();
1750 } else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1751 repaintManager->sync();
1752 }
1753}
1754
1755void QWidgetPrivate::syncBackingStore(const QRegion &region)
1756{
1757 if (shouldPaintOnScreen())
1758 paintOnScreen(region);
1759 else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1760 repaintManager->sync(q_func(), region);
1761 }
1762}
1763
1764void QWidgetPrivate::paintOnScreen(const QRegion &rgn)
1765{
1766 if (data.in_destructor)
1767 return;
1768
1769 if (shouldDiscardSyncRequest())
1770 return;
1771
1772 Q_Q(QWidget);
1773 if (q->testAttribute(Qt::WA_StaticContents)) {
1774 if (!extra)
1775 createExtra();
1776 extra->staticContentsSize = data.crect.size();
1777 }
1778
1779 QPaintEngine *engine = q->paintEngine();
1780
1781 // QGLWidget does not support partial updates if:
1782 // 1) The context is double buffered
1783 // 2) The context is single buffered and auto-fill background is enabled.
1784 const bool noPartialUpdateSupport = (engine && (engine->type() == QPaintEngine::OpenGL
1785 || engine->type() == QPaintEngine::OpenGL2))
1786 && (usesDoubleBufferedGLContext || q->autoFillBackground());
1787 QRegion toBePainted(noPartialUpdateSupport ? q->rect() : rgn);
1788
1789 toBePainted &= clipRect();
1790 clipToEffectiveMask(toBePainted);
1791 if (toBePainted.isEmpty())
1792 return; // Nothing to repaint.
1793
1794 drawWidget(q, toBePainted, QPoint(), QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen, nullptr);
1795
1796 if (Q_UNLIKELY(q->paintingActive()))
1797 qWarning("QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
1798}
1799
1800void QWidgetPrivate::setUpdatesEnabled_helper(bool enable)
1801{
1802 Q_Q(QWidget);
1803
1804 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
1805 return; // nothing we can do
1806
1807 if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
1808 return; // nothing to do
1809
1810 q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
1811 if (enable)
1812 q->update();
1813
1814 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
1815 for (int i = 0; i < children.size(); ++i) {
1816 QWidget *w = qobject_cast<QWidget *>(children.at(i));
1817 if (w && !w->isWindow() && !w->testAttribute(attribute))
1818 w->d_func()->setUpdatesEnabled_helper(enable);
1819 }
1820}
1821
1822/*!
1823 \internal
1824
1825 Propagate this widget's palette to all children, except style sheet
1826 widgets, and windows that don't enable window propagation (palettes don't
1827 normally propagate to windows).
1828*/
1829void QWidgetPrivate::propagatePaletteChange()
1830{
1831 Q_Q(QWidget);
1832 // Propagate a new inherited mask to all children.
1833#if QT_CONFIG(graphicsview)
1834 if (!q->parentWidget() && extra && extra->proxyWidget) {
1835 QGraphicsProxyWidget *p = extra->proxyWidget;
1836 inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolveMask();
1837 } else
1838#endif // QT_CONFIG(graphicsview)
1839 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
1840 inheritedPaletteResolveMask = 0;
1841 }
1842
1843 directPaletteResolveMask = data.pal.resolveMask();
1844 auto mask = directPaletteResolveMask | inheritedPaletteResolveMask;
1845
1846 const bool useStyleSheetPropagationInWidgetStyles =
1847 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
1848
1849 QEvent pc(QEvent::PaletteChange);
1850 QCoreApplication::sendEvent(q, &pc);
1851 for (int i = 0; i < children.size(); ++i) {
1852 QWidget *w = qobject_cast<QWidget*>(children.at(i));
1853 if (w && (!w->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
1854 && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
1855 QWidgetPrivate *wd = w->d_func();
1856 wd->inheritedPaletteResolveMask = mask;
1857 wd->resolvePalette();
1858 }
1859 }
1860}
1861
1862/*
1863 Returns the widget's clipping rectangle.
1864*/
1865QRect QWidgetPrivate::clipRect() const
1866{
1867 Q_Q(const QWidget);
1868 const QWidget * w = q;
1869 if (!w->isVisible())
1870 return QRect();
1871 QRect r = effectiveRectFor(q->rect());
1872 int ox = 0;
1873 int oy = 0;
1874 while (w
1875 && w->isVisible()
1876 && !w->isWindow()
1877 && w->parentWidget()) {
1878 ox -= w->x();
1879 oy -= w->y();
1880 w = w->parentWidget();
1881 r &= QRect(ox, oy, w->width(), w->height());
1882 }
1883 return r;
1884}
1885
1886/*
1887 Returns the widget's clipping region (without siblings).
1888*/
1889QRegion QWidgetPrivate::clipRegion() const
1890{
1891 Q_Q(const QWidget);
1892 if (!q->isVisible())
1893 return QRegion();
1894 QRegion r(q->rect());
1895 const QWidget * w = q;
1896 const QWidget *ignoreUpTo;
1897 int ox = 0;
1898 int oy = 0;
1899 while (w
1900 && w->isVisible()
1901 && !w->isWindow()
1902 && w->parentWidget()) {
1903 ox -= w->x();
1904 oy -= w->y();
1905 ignoreUpTo = w;
1906 w = w->parentWidget();
1907 r &= QRegion(ox, oy, w->width(), w->height());
1908
1909 int i = 0;
1910 while(w->d_func()->children.at(i++) != static_cast<const QObject *>(ignoreUpTo))
1911 ;
1912 for ( ; i < w->d_func()->children.size(); ++i) {
1913 if (QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
1914 if (sibling->isVisible() && !sibling->isWindow()) {
1915 QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
1916 sibling->width(), sibling->height());
1917 if (qRectIntersects(siblingRect, q->rect()))
1918 r -= QRegion(siblingRect);
1919 }
1920 }
1921 }
1922 }
1923 return r;
1924}
1925
1926void QWidgetPrivate::setSystemClip(QPaintEngine *paintEngine, qreal devicePixelRatio, const QRegion &region)
1927{
1928// Transform the system clip region from device-independent pixels to device pixels
1929 QTransform scaleTransform;
1930 scaleTransform.scale(devicePixelRatio, devicePixelRatio);
1931
1932 paintEngine->d_func()->baseSystemClip = region;
1933 paintEngine->d_func()->setSystemTransform(scaleTransform);
1934
1935}
1936
1937#if QT_CONFIG(graphicseffect)
1938void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
1939{
1940 Q_Q(QWidget);
1941 QWidget *w = q;
1942 do {
1943 if (w->graphicsEffect()) {
1944 QWidgetEffectSourcePrivate *sourced =
1945 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
1946 if (!sourced->updateDueToGraphicsEffect)
1947 w->graphicsEffect()->source()->d_func()->invalidateCache();
1948 }
1949 w = w->parentWidget();
1950 } while (w);
1951}
1952#endif // QT_CONFIG(graphicseffect)
1953
1954void QWidgetPrivate::setDirtyOpaqueRegion()
1955{
1956 Q_Q(QWidget);
1957
1958 dirtyOpaqueChildren = true;
1959
1960#if QT_CONFIG(graphicseffect)
1961 invalidateGraphicsEffectsRecursively();
1962#endif // QT_CONFIG(graphicseffect)
1963
1964 if (q->isWindow())
1965 return;
1966
1967 QWidget *parent = q->parentWidget();
1968 if (!parent)
1969 return;
1970
1971 // TODO: instead of setting dirtyflag, manipulate the dirtyregion directly?
1972 QWidgetPrivate *pd = parent->d_func();
1973 if (!pd->dirtyOpaqueChildren)
1974 pd->setDirtyOpaqueRegion();
1975}
1976
1977const QRegion &QWidgetPrivate::getOpaqueChildren() const
1978{
1979 if (!dirtyOpaqueChildren)
1980 return opaqueChildren;
1981
1982 QWidgetPrivate *that = const_cast<QWidgetPrivate*>(this);
1983 that->opaqueChildren = QRegion();
1984
1985 for (int i = 0; i < children.size(); ++i) {
1986 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1987 if (!child || !child->isVisible() || child->isWindow())
1988 continue;
1989
1990 const QPoint offset = child->geometry().topLeft();
1991 QWidgetPrivate *childd = child->d_func();
1992 QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
1993 if (childd->extra && childd->extra->hasMask)
1994 r &= childd->extra->mask;
1995 if (r.isEmpty())
1996 continue;
1997 r.translate(offset);
1998 that->opaqueChildren += r;
1999 }
2000
2001 that->opaqueChildren &= q_func()->rect();
2002 that->dirtyOpaqueChildren = false;
2003
2004 return that->opaqueChildren;
2005}
2006
2007void QWidgetPrivate::subtractOpaqueChildren(QRegion &source, const QRect &clipRect) const
2008{
2009 if (children.isEmpty() || clipRect.isEmpty())
2010 return;
2011
2012 const QRegion &r = getOpaqueChildren();
2013 if (!r.isEmpty())
2014 source -= (r & clipRect);
2015}
2016
2017//subtract any relatives that are higher up than me --- this is too expensive !!!
2018void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion, bool *hasDirtySiblingsAbove,
2019 bool alsoNonOpaque) const
2020{
2021 Q_Q(const QWidget);
2022 static int disableSubtractOpaqueSiblings = qEnvironmentVariableIntValue("QT_NO_SUBTRACTOPAQUESIBLINGS");
2023 if (disableSubtractOpaqueSiblings || q->isWindow())
2024 return;
2025
2026 QRect clipBoundingRect;
2027 bool dirtyClipBoundingRect = true;
2028
2029 QRegion parentClip;
2030 bool dirtyParentClip = true;
2031
2032 QPoint parentOffset = data.crect.topLeft();
2033
2034 const QWidget *w = q;
2035
2036 while (w) {
2037 if (w->isWindow())
2038 break;
2039 QWidgetPrivate *pd = w->parentWidget()->d_func();
2040 const int myIndex = pd->children.indexOf(const_cast<QWidget *>(w));
2041 const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
2042 for (int i = myIndex + 1; i < pd->children.size(); ++i) {
2043 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
2044 if (!sibling || !sibling->isVisible() || sibling->isWindow())
2045 continue;
2046
2047 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2048 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2049 continue;
2050
2051 if (dirtyClipBoundingRect) {
2052 clipBoundingRect = sourceRegion.boundingRect();
2053 dirtyClipBoundingRect = false;
2054 }
2055
2056 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2057 continue;
2058
2059 if (dirtyParentClip) {
2060 parentClip = sourceRegion.translated(parentOffset);
2061 dirtyParentClip = false;
2062 }
2063
2064 const QPoint siblingPos(sibling->data->crect.topLeft());
2065 const QRect siblingClipRect(sibling->d_func()->clipRect());
2066 QRegion siblingDirty(parentClip);
2067 siblingDirty &= (siblingClipRect.translated(siblingPos));
2068 const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
2069 && !sibling->d_func()->graphicsEffect;
2070 if (hasMask)
2071 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2072 if (siblingDirty.isEmpty())
2073 continue;
2074
2075 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2076 if (hasMask) {
2077 siblingDirty.translate(-parentOffset);
2078 sourceRegion -= siblingDirty;
2079 } else {
2080 sourceRegion -= siblingGeometry.translated(-parentOffset);
2081 }
2082 } else {
2083 if (hasDirtySiblingsAbove)
2084 *hasDirtySiblingsAbove = true;
2085 if (sibling->d_func()->children.isEmpty())
2086 continue;
2087 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2088 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2089 sourceRegion -= opaqueSiblingChildren;
2090 }
2091 if (sourceRegion.isEmpty())
2092 return;
2093
2094 dirtyClipBoundingRect = true;
2095 dirtyParentClip = true;
2096 }
2097
2098 w = w->parentWidget();
2099 parentOffset += pd->data.crect.topLeft();
2100 dirtyParentClip = true;
2101 }
2102}
2103
2104void QWidgetPrivate::clipToEffectiveMask(QRegion &region) const
2105{
2106 Q_Q(const QWidget);
2107
2108 const QWidget *w = q;
2109 QPoint offset;
2110
2111#if QT_CONFIG(graphicseffect)
2112 if (graphicsEffect && !w->isWindow()) {
2113 w = q->parentWidget();
2114 offset -= data.crect.topLeft();
2115 }
2116#endif // QT_CONFIG(graphicseffect)
2117
2118 while (w) {
2119 const QWidgetPrivate *wd = w->d_func();
2120 if (wd->extra && wd->extra->hasMask)
2121 region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
2122 if (w->isWindow())
2123 return;
2124 offset -= wd->data.crect.topLeft();
2125 w = w->parentWidget();
2126 }
2127}
2128
2129bool QWidgetPrivate::shouldPaintOnScreen() const
2130{
2131#if defined(QT_NO_BACKINGSTORE)
2132 return true;
2133#else
2134 Q_Q(const QWidget);
2135 if (q->testAttribute(Qt::WA_PaintOnScreen)
2136 || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
2137 return true;
2138 }
2139
2140 return false;
2141#endif
2142}
2143
2144void QWidgetPrivate::updateIsOpaque()
2145{
2146 // hw: todo: only needed if opacity actually changed
2147 setDirtyOpaqueRegion();
2148
2149#if QT_CONFIG(graphicseffect)
2150 if (graphicsEffect) {
2151 // ### We should probably add QGraphicsEffect::isOpaque at some point.
2152 setOpaque(false);
2153 return;
2154 }
2155#endif // QT_CONFIG(graphicseffect)
2156
2157 Q_Q(QWidget);
2158 if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
2159 setOpaque(true);
2160 return;
2161 }
2162
2163 const QPalette &pal = q->palette();
2164
2165 if (q->autoFillBackground()) {
2166 const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
2167 if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
2168 setOpaque(true);
2169 return;
2170 }
2171 }
2172
2173 if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
2174 const QBrush &windowBrush = q->palette().brush(QPalette::Window);
2175 if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
2176 setOpaque(true);
2177 return;
2178 }
2179 }
2180 setOpaque(false);
2181}
2182
2183void QWidgetPrivate::setOpaque(bool opaque)
2184{
2185 if (isOpaque != opaque) {
2186 isOpaque = opaque;
2187 updateIsTranslucent();
2188 }
2189}
2190
2191void QWidgetPrivate::updateIsTranslucent()
2192{
2193 Q_Q(QWidget);
2194 if (QWindow *window = q->windowHandle()) {
2195 QSurfaceFormat format = window->format();
2196 const int oldAlpha = format.alphaBufferSize();
2197 const int newAlpha = q->testAttribute(Qt::WA_TranslucentBackground) ? 8 : -1;
2198 if (oldAlpha != newAlpha) {
2199 // QTBUG-85714: Do this only when the QWindow has not yet been create()'ed yet.
2200 //
2201 // If that is not the case, then the setFormat() is not just futile
2202 // but downright dangerous. Futile because the format matters only
2203 // when creating the native window, no point in changing it
2204 // afterwards. Dangerous because a QOpenGLContext or something else
2205 // may eventually query the QWindow's format(), in order to ensure
2206 // compatibility (in terms of native concepts such as pixel format,
2207 // EGLConfig, etc.), and if we change it here, then the returned
2208 // format does not describe reality anymore. (reality being the
2209 // settings with which the native resource was created).
2210 //
2211 // Whereas if one does a destroy()-create() then this all here
2212 // won't matter because the format is updated in
2213 // QWidgetPrivate::create() again.
2214 //
2215 if (!window->handle()) {
2216 format.setAlphaBufferSize(newAlpha);
2217 window->setFormat(format);
2218 }
2219 }
2220 }
2221}
2222
2223static inline void fillRegion(QPainter *painter, const QRegion &rgn, const QBrush &brush)
2224{
2225 Q_ASSERT(painter);
2226
2227 if (brush.style() == Qt::TexturePattern) {
2228 const QRect rect(rgn.boundingRect());
2229 painter->setClipRegion(rgn);
2230 painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
2231 } else if (brush.gradient()
2232 && (brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode
2233 || brush.gradient()->coordinateMode() == QGradient::ObjectMode)) {
2234 painter->save();
2235 painter->setClipRegion(rgn);
2236 painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
2237 painter->restore();
2238 } else {
2239 for (const QRect &rect : rgn)
2240 painter->fillRect(rect, brush);
2241 }
2242}
2243
2244bool QWidgetPrivate::updateBrushOrigin(QPainter *painter, const QBrush &brush) const
2245{
2246#if QT_CONFIG(scrollarea)
2247 Q_Q(const QWidget);
2248 //If we are painting the viewport of a scrollarea, we must apply an offset to the brush in case we are drawing a texture
2249 if (brush.style() == Qt::NoBrush || brush.style() == Qt::SolidPattern)
2250 return false;
2251 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
2252 if (scrollArea && scrollArea->viewport() == q) {
2253 QObjectData *scrollPrivate = static_cast<QWidget *>(scrollArea)->d_ptr.data();
2254 QAbstractScrollAreaPrivate *priv = static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
2255 painter->setBrushOrigin(-priv->contentsOffset());
2256 }
2257#endif // QT_CONFIG(scrollarea)
2258 return true;
2259}
2260
2261void QWidgetPrivate::paintBackground(QPainter *painter, const QRegion &rgn, DrawWidgetFlags flags) const
2262{
2263 Q_Q(const QWidget);
2264
2265 bool brushOriginSet = false;
2266 const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
2267
2268 if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
2269 const QBrush bg = q->palette().brush(QPalette::Window);
2270 if (!brushOriginSet)
2271 brushOriginSet = updateBrushOrigin(painter, bg);
2272 if (!(flags & DontSetCompositionMode)) {
2273 //copy alpha straight in
2274 QPainter::CompositionMode oldMode = painter->compositionMode();
2275 painter->setCompositionMode(QPainter::CompositionMode_Source);
2276 fillRegion(painter, rgn, bg);
2277 painter->setCompositionMode(oldMode);
2278 } else {
2279 fillRegion(painter, rgn, bg);
2280 }
2281 }
2282
2283 if (q->autoFillBackground()) {
2284 if (!brushOriginSet)
2285 brushOriginSet = updateBrushOrigin(painter, autoFillBrush);
2286 fillRegion(painter, rgn, autoFillBrush);
2287 }
2288
2289 if (q->testAttribute(Qt::WA_StyledBackground)) {
2290 painter->setClipRegion(rgn);
2291 QStyleOption opt;
2292 opt.initFrom(q);
2293 q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
2294 }
2295}
2296
2297/*
2298 \internal
2299 This function is called when a widget is hidden or destroyed.
2300 It resets some application global pointers that should only refer active,
2301 visible widgets.
2302*/
2303
2305
2306void QWidgetPrivate::deactivateWidgetCleanup()
2307{
2308 Q_Q(QWidget);
2309 // If this was the active application window, reset it
2310 if (QApplication::activeWindow() == q)
2311 QApplicationPrivate::setActiveWindow(nullptr);
2312 // If the is the active mouse press widget, reset it
2313 if (q == qt_button_down)
2314 qt_button_down = nullptr;
2315}
2316
2317
2318/*!
2319 Returns a pointer to the widget with window identifier/handle \a
2320 id.
2321
2322 The window identifier type depends on the underlying window
2323 system, see \c qwindowdefs.h for the actual definition. If there
2324 is no widget with this identifier, \nullptr is returned.
2325*/
2326
2327QWidget *QWidget::find(WId id)
2328{
2329 return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) : nullptr;
2330}
2331
2332
2333
2334/*!
2335 \fn WId QWidget::internalWinId() const
2336 \internal
2337 Returns the window system identifier of the widget, or 0 if the widget is not created yet.
2338
2339*/
2340
2341/*!
2342 \fn WId QWidget::winId() const
2343
2344 Returns the window system identifier of the widget.
2345
2346 Portable in principle, but if you use it you are probably about to
2347 do something non-portable. Be careful.
2348
2349 If a widget is non-native (alien) and winId() is invoked on it, that widget
2350 will be provided a native handle.
2351
2352 This value may change at run-time. An event with type QEvent::WinIdChange
2353 will be sent to the widget following a change in window system identifier.
2354
2355 \sa find()
2356*/
2357WId QWidget::winId() const
2358{
2359 if (!data->in_destructor
2360 && (!testAttribute(Qt::WA_WState_Created) || !internalWinId()))
2361 {
2362 QWidget *that = const_cast<QWidget*>(this);
2363 that->setAttribute(Qt::WA_NativeWindow);
2364 that->d_func()->createWinId();
2365 return that->data->winid;
2366 }
2367 return data->winid;
2368}
2369
2370void QWidgetPrivate::createWinId()
2371{
2372 Q_Q(QWidget);
2373
2374 const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
2375 if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
2376 if (!q->isWindow()) {
2377 QWidget *parent = q->parentWidget();
2378 QWidgetPrivate *pd = parent->d_func();
2379 if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
2380 parent->setAttribute(Qt::WA_NativeWindow);
2381 if (!parent->internalWinId()) {
2382 pd->createWinId();
2383 }
2384
2385 for (int i = 0; i < pd->children.size(); ++i) {
2386 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
2387 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
2388 || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
2389 w->create();
2390 }
2391 }
2392 } else {
2393 q->create();
2394 }
2395 }
2396}
2397
2398/*!
2399\internal
2400Ensures that the widget is set on the screen point is on. This is handy getting a correct
2401size hint before a resize in e.g QMenu and QToolTip.
2402Returns if the screen was changed.
2403*/
2404
2405bool QWidgetPrivate::setScreenForPoint(const QPoint &pos)
2406{
2407 Q_Q(QWidget);
2408 if (!q->isWindow())
2409 return false;
2410 // Find the screen for pos and make the widget understand it is on that screen.
2411 return setScreen(QGuiApplication::screenAt(pos));
2412}
2413
2414/*!
2415\internal
2416Ensures that the widget's QWindow is set to be on the given \a screen.
2417Returns true if the screen was changed.
2418*/
2419
2420bool QWidgetPrivate::setScreen(QScreen *screen)
2421{
2422 Q_Q(QWidget);
2423 if (!screen || !q->isWindow())
2424 return false;
2425 const QScreen *currentScreen = windowHandle() ? windowHandle()->screen() : nullptr;
2426 if (currentScreen != screen) {
2427 topData()->initialScreen = screen;
2428 if (windowHandle())
2429 windowHandle()->setScreen(screen);
2430 return true;
2431 }
2432 return false;
2433}
2434
2435/*!
2436\internal
2437Ensures that the widget has a window system identifier, i.e. that it is known to the windowing system.
2438
2439*/
2440
2441void QWidget::createWinId()
2442{
2443 Q_D(QWidget);
2444 d->createWinId();
2445}
2446
2447/*!
2448 \since 4.4
2449
2450 Returns the effective window system identifier of the widget, i.e. the
2451 native parent's window system identifier.
2452
2453 If the widget is native, this function returns the native widget ID.
2454 Otherwise, the window ID of the first native parent widget, i.e., the
2455 top-level widget that contains this widget, is returned.
2456
2457 \note We recommend that you do not store this value as it is likely to
2458 change at run-time.
2459
2460 \sa nativeParentWidget()
2461*/
2462WId QWidget::effectiveWinId() const
2463{
2464 const WId id = internalWinId();
2465 if (id || !testAttribute(Qt::WA_WState_Created))
2466 return id;
2467 if (const QWidget *realParent = nativeParentWidget())
2468 return realParent->internalWinId();
2469 return 0;
2470}
2471
2472/*!
2473 If this is a native widget, return the associated QWindow.
2474 Otherwise return null.
2475
2476 Native widgets include toplevel widgets, QGLWidget, and child widgets
2477 on which winId() was called.
2478
2479 \since 5.0
2480
2481 \sa winId(), screen()
2482*/
2483QWindow *QWidget::windowHandle() const
2484{
2485 Q_D(const QWidget);
2486 return d->windowHandle();
2487}
2488
2489/*!
2490 Returns the screen the widget is on.
2491
2492 \since 5.14
2493
2494 \sa windowHandle()
2495*/
2496QScreen *QWidget::screen() const
2497{
2498 Q_D(const QWidget);
2499 if (auto associatedScreen = d->associatedScreen())
2500 return associatedScreen;
2501 if (auto topLevel = window()) {
2502 if (auto topData = qt_widget_private(topLevel)->topData()) {
2503 if (topData->initialScreen)
2504 return topData->initialScreen;
2505 }
2506 if (auto screenByPos = QGuiApplication::screenAt(topLevel->geometry().center()))
2507 return screenByPos;
2508 }
2509 return QGuiApplication::primaryScreen();
2510}
2511
2512/*!
2513 Sets the screen on which the widget should be shown to \a screen.
2514
2515 Setting the screen only makes sense for windows. If necessary, the widget's
2516 window will get recreated on \a screen.
2517
2518 \note If the screen is part of a virtual desktop of multiple screens,
2519 the window will not move automatically to \a screen. To place the
2520 window relative to the screen, use the screen's topLeft() position.
2521
2522 \sa QWindow::setScreen()
2523*/
2524void QWidget::setScreen(QScreen *screen)
2525{
2526 Q_D(QWidget);
2527 d->setScreen(screen);
2528}
2529
2530#if QT_CONFIG(style_stylesheet)
2531
2532/*!
2533 \property QWidget::styleSheet
2534 \brief the widget's style sheet
2535 \since 4.2
2536
2537 The style sheet contains a textual description of customizations to the
2538 widget's style, as described in the \l{Qt Style Sheets} document.
2539
2540 Setting a style sheet on a widget makes the widget and its children use a
2541 style sheet style that wraps the current \l{style()}{style}. For when to
2542 use style sheets, see \l{Styling Approaches for Qt Widgets}.
2543
2544 \sa setStyle(), QApplication::styleSheet, {Qt Style Sheets},
2545 {Styling Approaches for Qt Widgets}
2546*/
2547QString QWidget::styleSheet() const
2548{
2549 Q_D(const QWidget);
2550 if (!d->extra)
2551 return QString();
2552 return d->extra->styleSheet;
2553}
2554
2555void QWidget::setStyleSheet(const QString& styleSheet)
2556{
2557 Q_D(QWidget);
2558 if (data->in_destructor)
2559 return;
2560 d->createExtra();
2561
2562 QStyleSheetStyle *proxy = qt_styleSheet(d->extra->style);
2563 d->extra->styleSheet = styleSheet;
2564 if (styleSheet.isEmpty()) { // stylesheet removed
2565 if (!proxy)
2566 return;
2567
2568 d->inheritStyle();
2569 return;
2570 }
2571
2572 if (proxy) { // style sheet update
2573 bool repolish = d->polished;
2574 if (!repolish) {
2575 const auto childWidgets = findChildren<QWidget*>();
2576 for (auto child : childWidgets) {
2577 repolish = child->d_func()->polished;
2578 if (repolish)
2579 break;
2580 }
2581 }
2582 if (repolish)
2583 proxy->repolish(this);
2584 return;
2585 }
2586
2587 if (testAttribute(Qt::WA_SetStyle)) {
2588 d->setStyle_helper(new QStyleSheetStyle(d->extra->style), true);
2589 } else {
2590 d->setStyle_helper(new QStyleSheetStyle(nullptr), true);
2591 }
2592}
2593
2594#endif // QT_NO_STYLE_STYLESHEET
2595
2596/*!
2597 \sa QWidget::setStyle(), QApplication::setStyle(), QApplication::style()
2598*/
2599
2600QStyle *QWidget::style() const
2601{
2602 Q_D(const QWidget);
2603
2604 if (d->extra && d->extra->style)
2605 return d->extra->style;
2606 return QApplication::style();
2607}
2608
2609/*!
2610 Sets the widget's GUI style to \a style. The ownership of the style
2611 object is not transferred.
2612
2613 If no style is set, the widget uses the application's style,
2614 QApplication::style() instead.
2615
2616 Setting a widget's style has no effect on existing or future child
2617 widgets.
2618
2619 \warning This function is particularly useful for demonstration
2620 purposes, where you want to show Qt's styling capabilities. Real
2621 applications should avoid it and use one consistent GUI style
2622 instead.
2623
2624 \sa style(), QStyle, QApplication::style(), QApplication::setStyle()
2625*/
2626
2627void QWidget::setStyle(QStyle *style)
2628{
2629 Q_D(QWidget);
2630 setAttribute(Qt::WA_SetStyle, style != nullptr);
2631 d->createExtra();
2632#if QT_CONFIG(style_stylesheet)
2633 if (QStyleSheetStyle *styleSheetStyle = qt_styleSheet(style)) {
2634 //if for some reason someone try to set a QStyleSheetStyle, ref it
2635 //(this may happen for example in QButtonDialogBox which propagates its style)
2636 styleSheetStyle->ref();
2637 d->setStyle_helper(style, false);
2638 } else if (qt_styleSheet(d->extra->style) || !qApp->styleSheet().isEmpty()) {
2639 // if we have an application stylesheet or have a proxy already, propagate
2640 d->setStyle_helper(new QStyleSheetStyle(style), true);
2641 } else
2642#endif
2643 d->setStyle_helper(style, false);
2644}
2645
2646void QWidgetPrivate::setStyle_helper(QStyle *newStyle, bool propagate)
2647{
2648 Q_Q(QWidget);
2649 QStyle *oldStyle = q->style();
2650
2651 createExtra();
2652
2653#if QT_CONFIG(style_stylesheet)
2654 QPointer<QStyle> origStyle = extra->style;
2655#endif
2656 extra->style = newStyle;
2657
2658 // repolish
2659 if (polished) {
2660 oldStyle->unpolish(q);
2661 q->style()->polish(q);
2662 }
2663
2664 if (propagate) {
2665 // We copy the list because the order may be modified
2666 const QObjectList childrenList = children;
2667 for (int i = 0; i < childrenList.size(); ++i) {
2668 QWidget *c = qobject_cast<QWidget*>(childrenList.at(i));
2669 if (c)
2670 c->d_func()->inheritStyle();
2671 }
2672 }
2673
2674#if QT_CONFIG(style_stylesheet)
2675 if (!qt_styleSheet(newStyle)) {
2676 if (const QStyleSheetStyle* cssStyle = qt_styleSheet(origStyle)) {
2677 cssStyle->clearWidgetFont(q);
2678 }
2679 }
2680#endif
2681
2682 QEvent e(QEvent::StyleChange);
2683 QCoreApplication::sendEvent(q, &e);
2684
2685#if QT_CONFIG(style_stylesheet)
2686 // dereference the old stylesheet style
2687 if (QStyleSheetStyle *proxy = qt_styleSheet(origStyle))
2688 proxy->deref();
2689#endif
2690}
2691
2692// Inherits style from the current parent and propagates it as necessary
2693void QWidgetPrivate::inheritStyle()
2694{
2695#if QT_CONFIG(style_stylesheet)
2696 Q_Q(QWidget);
2697
2698 QStyle *extraStyle = extra ? (QStyle*)extra->style : nullptr;
2699
2700 QStyleSheetStyle *proxy = qt_styleSheet(extraStyle);
2701
2702 if (!q->styleSheet().isEmpty()) {
2703 Q_ASSERT(proxy);
2704 proxy->repolish(q);
2705 return;
2706 }
2707 if (inheritStyleRecursionGuard)
2708 return;
2709 inheritStyleRecursionGuard = true;
2710 const auto resetGuard = qScopeGuard([&]() {
2711 inheritStyleRecursionGuard = false;
2712 });
2713
2714 QStyle *origStyle = proxy ? proxy->base : extraStyle;
2715 QWidget *parent = q->parentWidget();
2716 QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style : nullptr;
2717 // If we have stylesheet on app or parent has stylesheet style, we need
2718 // to be running a proxy
2719 if (!qApp->styleSheet().isEmpty() || qt_styleSheet(parentStyle)) {
2720 QStyle *newStyle = parentStyle;
2721 if (q->testAttribute(Qt::WA_SetStyle) && qt_styleSheet(origStyle) == nullptr)
2722 newStyle = new QStyleSheetStyle(origStyle);
2723 else if (auto *styleSheetStyle = qt_styleSheet(origStyle))
2724 newStyle = styleSheetStyle;
2725 else if (QStyleSheetStyle *newProxy = qt_styleSheet(parentStyle))
2726 newProxy->ref();
2727
2728 setStyle_helper(newStyle, true);
2729 return;
2730 }
2731
2732 // So, we have no stylesheet on parent/app and we have an empty stylesheet
2733 // we just need our original style back
2734 if (origStyle == extraStyle) // is it any different?
2735 return;
2736
2737 // We could have inherited the proxy from our parent (which has a custom style)
2738 // In such a case we need to start following the application style (i.e revert
2739 // the propagation behavior of QStyleSheetStyle)
2740 if (!q->testAttribute(Qt::WA_SetStyle))
2741 origStyle = nullptr;
2742
2743 setStyle_helper(origStyle, true);
2744#endif // QT_NO_STYLE_STYLESHEET
2745}
2746
2747
2748/*!
2749 \fn bool QWidget::isWindow() const
2750
2751 Returns \c true if the widget is an independent window, otherwise
2752 returns \c false.
2753
2754 A window is a widget that isn't visually the child of any other
2755 widget and that usually has a frame and a
2756 \l{QWidget::setWindowTitle()}{window title}.
2757
2758 A window can have a \l{QWidget::parentWidget()}{parent widget}.
2759 It will then be grouped with its parent and deleted when the
2760 parent is deleted, minimized when the parent is minimized etc. If
2761 supported by the window manager, it will also have a common
2762 taskbar entry with its parent.
2763
2764 QDialog and QMainWindow widgets are by default windows, even if a
2765 parent widget is specified in the constructor. This behavior is
2766 specified by the Qt::Window flag.
2767
2768 \sa window(), isModal(), parentWidget()
2769*/
2770
2771/*!
2772 \property QWidget::modal
2773 \brief whether the widget is a modal widget
2774
2775 This property only makes sense for windows. A modal widget
2776 prevents widgets in all other windows from getting any input.
2777
2778 By default, this property is \c false.
2779
2780 \sa isWindow(), windowModality, QDialog
2781*/
2782
2783/*!
2784 \property QWidget::windowModality
2785 \brief which windows are blocked by the modal widget
2786 \since 4.1
2787
2788 This property only makes sense for windows. A modal widget
2789 prevents widgets in other windows from getting input. The value of
2790 this property controls which windows are blocked when the widget
2791 is visible. Changing this property while the window is visible has
2792 no effect; you must hide() the widget first, then show() it again.
2793
2794 By default, this property is Qt::NonModal.
2795
2796 \sa isWindow(), QWidget::modal, QDialog
2797*/
2798
2799Qt::WindowModality QWidget::windowModality() const
2800{
2801 return static_cast<Qt::WindowModality>(data->window_modality);
2802}
2803
2804void QWidget::setWindowModality(Qt::WindowModality windowModality)
2805{
2806 data->window_modality = windowModality;
2807 // setModal_sys() will be called by setAttribute()
2808 setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
2809 setAttribute(Qt::WA_SetWindowModality, true);
2810}
2811
2812void QWidgetPrivate::setModal_sys()
2813{
2814 Q_Q(QWidget);
2815 if (q->windowHandle())
2816 q->windowHandle()->setModality(q->windowModality());
2817}
2818
2819/*!
2820 \fn bool QWidget::underMouse() const
2821
2822 Returns \c true if the widget is under the mouse cursor; otherwise
2823 returns \c false.
2824
2825 This value is not updated properly during drag and drop
2826 operations.
2827
2828 \sa enterEvent(), leaveEvent()
2829*/
2830
2831/*!
2832 \property QWidget::minimized
2833 \brief whether this widget is minimized (iconified)
2834
2835 This property is only relevant for windows.
2836
2837 By default, this property is \c false.
2838
2839 \sa showMinimized(), visible, show(), hide(), showNormal(), maximized
2840*/
2841bool QWidget::isMinimized() const
2842{ return data->window_state & Qt::WindowMinimized; }
2843
2844/*!
2845 Shows the widget minimized, as an icon.
2846
2847 Calling this function only affects \l{isWindow()}{windows}.
2848
2849 \sa showNormal(), showMaximized(), show(), hide(), isVisible(),
2850 isMinimized()
2851*/
2852void QWidget::showMinimized()
2853{
2854 bool isMin = isMinimized();
2855 if (isMin && isVisible())
2856 return;
2857
2858 ensurePolished();
2859
2860 if (!isMin)
2861 setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
2862 setVisible(true);
2863}
2864
2865/*!
2866 \property QWidget::maximized
2867 \brief whether this widget is maximized
2868
2869 This property is only relevant for windows.
2870
2871 \note Due to limitations on some window systems, this does not always
2872 report the expected results (e.g., if the user on X11 maximizes the
2873 window via the window manager, Qt has no way of distinguishing this
2874 from any other resize). This is expected to improve as window manager
2875 protocols evolve.
2876
2877 By default, this property is \c false.
2878
2879 \sa windowState(), showMaximized(), visible, show(), hide(), showNormal(), minimized
2880*/
2881bool QWidget::isMaximized() const
2882{ return data->window_state & Qt::WindowMaximized; }
2883
2884
2885
2886/*!
2887 Returns the current window state. The window state is a OR'ed
2888 combination of Qt::WindowState: Qt::WindowMinimized,
2889 Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.
2890
2891 \sa Qt::WindowState, setWindowState()
2892 */
2893Qt::WindowStates QWidget::windowState() const
2894{
2895 return Qt::WindowStates(data->window_state);
2896}
2897
2898/*!\internal
2899
2900 The function sets the window state on child widgets similar to
2901 setWindowState(). The difference is that the window state changed
2902 event has the isOverride() flag set. It exists mainly to keep
2903 QWorkspace working.
2904 */
2905void QWidget::overrideWindowState(Qt::WindowStates newstate)
2906{
2907 QWindowStateChangeEvent e(Qt::WindowStates(data->window_state), true);
2908 data->window_state = newstate;
2909 QCoreApplication::sendEvent(this, &e);
2910}
2911
2912/*!
2913 \fn void QWidget::setWindowState(Qt::WindowStates windowState)
2914
2915 Sets the window state to \a windowState. The window state is a OR'ed
2916 combination of Qt::WindowState: Qt::WindowMinimized,
2917 Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.
2918
2919 If the window is not visible (i.e. isVisible() returns \c false), the
2920 window state will take effect when show() is called. For visible
2921 windows, the change is immediate. For example, to toggle between
2922 full-screen and normal mode, use the following code:
2923
2924 \snippet code/src_gui_kernel_qwidget.cpp 0
2925
2926 To restore and activate a minimized window (while
2927 preserving its maximized and/or full-screen state), use the following:
2928
2929 \snippet code/src_gui_kernel_qwidget.cpp 1
2930
2931 Calling this function will hide the widget. You must call show() to make
2932 the widget visible again.
2933
2934 \note On some window systems Qt::WindowActive is not immediate, and may be
2935 ignored in certain cases.
2936
2937 When the window state changes, the widget receives a changeEvent()
2938 of type QEvent::WindowStateChange.
2939
2940 \sa Qt::WindowState, windowState()
2941*/
2942void QWidget::setWindowState(Qt::WindowStates newstate)
2943{
2944 Q_D(QWidget);
2945 Qt::WindowStates oldstate = windowState();
2946 if (newstate.testFlag(Qt::WindowMinimized)) // QTBUG-46763
2947 newstate.setFlag(Qt::WindowActive, false);
2948 if (oldstate == newstate)
2949 return;
2950 if (isWindow() && !testAttribute(Qt::WA_WState_Created))
2951 create();
2952
2953 data->window_state = newstate;
2954 data->in_set_window_state = 1;
2955 if (isWindow()) {
2956 // Ensure the initial size is valid, since we store it as normalGeometry below.
2957 if (!testAttribute(Qt::WA_Resized) && !isVisible())
2958 adjustSize();
2959
2960 d->createTLExtra();
2961 if (!(oldstate & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)))
2962 d->topData()->normalGeometry = geometry();
2963
2964 Q_ASSERT(windowHandle());
2965 windowHandle()->setWindowStates(newstate & ~Qt::WindowActive);
2966 }
2967 data->in_set_window_state = 0;
2968
2969 if (newstate & Qt::WindowActive)
2970 activateWindow();
2971
2972 QWindowStateChangeEvent e(oldstate);
2973 QCoreApplication::sendEvent(this, &e);
2974}
2975
2976/*!
2977 \property QWidget::fullScreen
2978 \brief whether the widget is shown in full screen mode
2979
2980 A widget in full screen mode occupies the whole screen area and does not
2981 display window decorations, such as a title bar.
2982
2983 By default, this property is \c false.
2984
2985 \sa windowState(), minimized, maximized
2986*/
2987bool QWidget::isFullScreen() const
2988{ return data->window_state & Qt::WindowFullScreen; }
2989
2990/*!
2991 Shows the widget in full-screen mode.
2992
2993 Calling this function only affects \l{isWindow()}{windows}.
2994
2995 To return from full-screen mode, call showNormal() or close().
2996
2997 \note Full-screen mode works fine under Windows, but has certain
2998 problems under X. These problems are due to limitations of the
2999 ICCCM protocol that specifies the communication between X11
3000 clients and the window manager. ICCCM simply does not understand
3001 the concept of non-decorated full-screen windows. Therefore, the
3002 best you can do is to request a borderless window and place and
3003 resize it to fill the entire screen. Depending on the window
3004 manager, this may or may not work. The borderless window is
3005 requested using MOTIF hints, which are at least partially
3006 supported by virtually all modern window managers.
3007
3008 An alternative would be to bypass the window manager entirely and
3009 create a window with the Qt::X11BypassWindowManagerHint flag. This
3010 has other severe problems though, like broken keyboard focus
3011 and very strange effects on desktop changes or when the user raises
3012 other windows.
3013
3014 X11 window managers that follow modern post-ICCCM specifications
3015 support full-screen mode properly.
3016
3017 On macOS, showing a window full screen puts the entire application in
3018 full-screen mode, providing it with a dedicated desktop. Showing another
3019 window while the application runs in full-screen mode might automatically
3020 make that window full screen as well. To prevent that, exit full-screen
3021 mode by calling showNormal() or by close() on the full screen window
3022 before showing another window.
3023
3024 \sa showNormal(), showMaximized(), show(), isVisible(), close()
3025*/
3026void QWidget::showFullScreen()
3027{
3028 ensurePolished();
3029
3030 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
3031 | Qt::WindowFullScreen);
3032 setVisible(true);
3033#if !defined Q_OS_QNX // On QNX this window will be activated anyway from libscreen
3034 // activating it here before libscreen activates it causes problems
3035 activateWindow();
3036#endif
3037}
3038
3039/*!
3040 Shows the widget maximized.
3041
3042 Calling this function only affects \l{isWindow()}{windows}.
3043
3044 On X11, this function may not work properly with certain window
3045 managers. See the \l{Window Geometry} documentation for an explanation.
3046
3047 \sa setWindowState(), showNormal(), showMinimized(), show(), hide(), isVisible()
3048*/
3049void QWidget::showMaximized()
3050{
3051 ensurePolished();
3052
3053 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
3054 | Qt::WindowMaximized);
3055 setVisible(true);
3056}
3057
3058/*!
3059 Restores the widget after it has been maximized or minimized.
3060
3061 Calling this function only affects \l{isWindow()}{windows}.
3062
3063 \sa setWindowState(), showMinimized(), showMaximized(), show(), hide(), isVisible()
3064*/
3065void QWidget::showNormal()
3066{
3067 ensurePolished();
3068
3069 setWindowState(windowState() & ~(Qt::WindowMinimized
3070 | Qt::WindowMaximized
3071 | Qt::WindowFullScreen));
3072 setVisible(true);
3073}
3074
3075/*!
3076 Returns \c true if this widget would become enabled if \a ancestor is
3077 enabled; otherwise returns \c false.
3078
3079
3080
3081 This is the case if neither the widget itself nor every parent up
3082 to but excluding \a ancestor has been explicitly disabled.
3083
3084 isEnabledTo(0) returns false if this widget or any if its ancestors
3085 was explicitly disabled.
3086
3087 The word ancestor here means a parent widget within the same window.
3088
3089 Therefore isEnabledTo(0) stops at this widget's window, unlike
3090 isEnabled() which also takes parent windows into considerations.
3091
3092 \sa setEnabled(), enabled
3093*/
3094
3095bool QWidget::isEnabledTo(const QWidget *ancestor) const
3096{
3097 const QWidget * w = this;
3098 while (!w->testAttribute(Qt::WA_ForceDisabled)
3099 && !w->isWindow()
3100 && w->parentWidget()
3101 && w->parentWidget() != ancestor)
3102 w = w->parentWidget();
3103 return !w->testAttribute(Qt::WA_ForceDisabled);
3104}
3105
3106#if QT_CONFIG(action)
3107/*!
3108 Appends the action \a action to this widget's list of actions.
3109
3110 All QWidgets have a list of \l{QAction}s. However, they can be
3111 represented graphically in many different ways. The default use of
3112 the QAction list (as returned by actions()) is to create a context
3113 QMenu.
3114
3115 A QWidget should only have one of each action and adding an action
3116 it already has will not cause the same action to be in the widget twice.
3117
3118 The ownership of \a action is not transferred to this QWidget.
3119
3120 \sa removeAction(), insertAction(), actions(), QMenu
3121*/
3122void QWidget::addAction(QAction *action)
3123{
3124 insertAction(nullptr, action);
3125}
3126
3127/*!
3128 Appends the actions \a actions to this widget's list of actions.
3129
3130 \sa removeAction(), QMenu, addAction()
3131*/
3132void QWidget::addActions(const QList<QAction *> &actions)
3133{
3134 for(int i = 0; i < actions.size(); i++)
3135 insertAction(nullptr, actions.at(i));
3136}
3137
3138/*!
3139 Inserts the action \a action to this widget's list of actions,
3140 before the action \a before. It appends the action if \a before is \nullptr or
3141 \a before is not a valid action for this widget.
3142
3143 A QWidget should only have one of each action.
3144
3145 \sa removeAction(), addAction(), QMenu, contextMenuPolicy, actions()
3146*/
3147void QWidget::insertAction(QAction *before, QAction *action)
3148{
3149 if (Q_UNLIKELY(!action)) {
3150 qWarning("QWidget::insertAction: Attempt to insert null action");
3151 return;
3152 }
3153
3154 Q_D(QWidget);
3155 if (d->actions.contains(action))
3156 removeAction(action);
3157
3158 int pos = d->actions.indexOf(before);
3159 if (pos < 0) {
3160 before = nullptr;
3161 pos = d->actions.size();
3162 }
3163 d->actions.insert(pos, action);
3164
3165 QActionPrivate *apriv = action->d_func();
3166 apriv->associatedObjects.append(this);
3167
3168 QActionEvent e(QEvent::ActionAdded, action, before);
3169 QCoreApplication::sendEvent(this, &e);
3170}
3171
3172/*!
3173 Inserts the actions \a actions to this widget's list of actions,
3174 before the action \a before. It appends the action if \a before is \nullptr or
3175 \a before is not a valid action for this widget.
3176
3177 A QWidget can have at most one of each action.
3178
3179 \sa removeAction(), QMenu, insertAction(), contextMenuPolicy
3180*/
3181void QWidget::insertActions(QAction *before, const QList<QAction*> &actions)
3182{
3183 for(int i = 0; i < actions.size(); ++i)
3184 insertAction(before, actions.at(i));
3185}
3186
3187/*!
3188 Removes the action \a action from this widget's list of actions.
3189 \sa insertAction(), actions()
3190*/
3191void QWidget::removeAction(QAction *action)
3192{
3193 if (!action)
3194 return;
3195
3196 Q_D(QWidget);
3197
3198 QActionPrivate *apriv = action->d_func();
3199 apriv->associatedObjects.removeAll(this);
3200
3201 if (d->actions.removeAll(action)) {
3202 QActionEvent e(QEvent::ActionRemoved, action);
3203 QCoreApplication::sendEvent(this, &e);
3204 }
3205}
3206
3207/*!
3208 Returns the (possibly empty) list of this widget's actions.
3209
3210 \sa contextMenuPolicy, insertAction(), removeAction()
3211*/
3212QList<QAction*> QWidget::actions() const
3213{
3214 Q_D(const QWidget);
3215 return d->actions;
3216}
3217
3218/*!
3219 \fn QAction *QWidget::addAction(const QString &text);
3220 \fn QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut);
3221 \fn QAction *QWidget::addAction(const QIcon &icon, const QString &text);
3222 \fn QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut);
3223
3224 \since 6.3
3225
3226 These convenience functions create a new action with text \a text,
3227 icon \a icon and shortcut \a shortcut, if any.
3228
3229 The functions add the newly created action to the widget's
3230 list of actions, and return it.
3231
3232 QWidget takes ownership of the returned QAction.
3233*/
3234QAction *QWidget::addAction(const QString &text)
3235{
3236 QAction *ret = new QAction(text, this);
3237 addAction(ret);
3238 return ret;
3239}
3240
3241QAction *QWidget::addAction(const QIcon &icon, const QString &text)
3242{
3243 QAction *ret = new QAction(icon, text, this);
3244 addAction(ret);
3245 return ret;
3246}
3247
3248#if QT_CONFIG(shortcut)
3249QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut)
3250{
3251 QAction *ret = addAction(text);
3252 ret->setShortcut(shortcut);
3253 return ret;
3254}
3255
3256QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut)
3257{
3258 QAction *ret = addAction(icon, text);
3259 ret->setShortcut(shortcut);
3260 return ret;
3261}
3262#endif
3263
3264/*!
3265 \fn QAction *QWidget::addAction(const QString &text, const QObject *receiver, const char* member, Qt::ConnectionType type)
3266 \fn QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, Qt::ConnectionType type)
3267 \fn QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut, const QObject *receiver, const char* member, Qt::ConnectionType type)
3268 \fn QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut, const QObject *receiver, const char* member, Qt::ConnectionType type)
3269
3270 \overload
3271 \since 6.3
3272
3273 This convenience function creates a new action with the text \a
3274 text, icon \a icon, and shortcut \a shortcut, if any.
3275
3276 The action's \l{QAction::triggered()}{triggered()} signal is connected
3277 to the \a receiver's \a member slot. The function adds the newly created
3278 action to the widget's list of actions and returns it.
3279
3280 QWidget takes ownership of the returned QAction.
3281*/
3282QAction *QWidget::addAction(const QString &text, const QObject *receiver, const char* member,
3283 Qt::ConnectionType type)
3284{
3285 QAction *action = addAction(text);
3286 QObject::connect(action, SIGNAL(triggered(bool)), receiver, member, type);
3287 return action;
3288}
3289
3290QAction *QWidget::addAction(const QIcon &icon, const QString &text,
3291 const QObject *receiver, const char* member,
3292 Qt::ConnectionType type)
3293{
3294 QAction *action = addAction(icon, text);
3295 QObject::connect(action, SIGNAL(triggered(bool)), receiver, member, type);
3296 return action;
3297}
3298
3299#if QT_CONFIG(shortcut)
3300QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut,
3301 const QObject *receiver, const char* member,
3302 Qt::ConnectionType type)
3303{
3304 QAction *action = addAction(text, receiver, member, type);
3305 action->setShortcut(shortcut);
3306 return action;
3307}
3308
3309QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut,
3310 const QObject *receiver, const char* member,
3311 Qt::ConnectionType type)
3312{
3313 QAction *action = addAction(icon, text, receiver, member, type);
3314 action->setShortcut(shortcut);
3315 return action;
3316}
3317#endif // QT_CONFIG(shortcut)
3318
3319/*!
3320 \fn template<typename...Args, typename = compatible_action_slot_args<Args...>> QAction *QWidget::addAction(const QString &text, Args&&...args)
3321 \fn template<typename...Args, typename = compatible_action_slot_args<Args...>> QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut, Args&&...args)
3322 \fn template<typename...Args, typename = compatible_action_slot_args<Args...>> QAction *QWidget::addAction(const QIcon &icon, const QString &text, Args&&...args)
3323 \fn template<typename...Args, typename = compatible_action_slot_args<Args...>> QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut, Args&&...args)
3324
3325 \since 6.3
3326 \overload
3327
3328 These convenience functions create a new action with the text \a text,
3329 icon \a icon, and shortcut \a shortcut, if any.
3330
3331 The action's \l{QAction::triggered()}{triggered()} signal is connected
3332 as if by a call to QObject::connect(action, &QAction::triggered, args...),
3333 perfectly forwarding \a args, including a possible Qt::ConnectionType.
3334
3335 The function adds the newly created action to the widget's list of
3336 actions and returns it.
3337
3338 QWidget takes ownership of the returned QAction.
3339*/
3340#endif // QT_NO_ACTION
3341
3342/*!
3343 \property QWidget::enabled
3344 \brief whether the widget is enabled
3345
3346 In general an enabled widget handles keyboard and mouse events; a disabled
3347 widget does not. An exception is made with \l{QAbstractButton}.
3348
3349 Some widgets display themselves differently when they are
3350 disabled. For example a button might draw its label grayed out. If
3351 your widget needs to know when it becomes enabled or disabled, you
3352 can use the changeEvent() with type QEvent::EnabledChange.
3353
3354 Disabling a widget implicitly disables all its children. Enabling
3355 respectively enables all child widgets unless they have been
3356 explicitly disabled. It it not possible to explicitly enable a child
3357 widget which is not a window while its parent widget remains disabled.
3358
3359 By default, this property is \c true.
3360
3361 \sa isEnabledTo(), QKeyEvent, QMouseEvent, changeEvent()
3362*/
3363void QWidget::setEnabled(bool enable)
3364{
3365 Q_D(QWidget);
3366
3367#if QT_CONFIG(accessibility)
3368 const bool wasEnabled = !testAttribute(Qt::WA_ForceDisabled);
3369#endif
3370
3371 setAttribute(Qt::WA_ForceDisabled, !enable);
3372 d->setEnabled_helper(enable);
3373
3374#if QT_CONFIG(accessibility)
3375 // A widget is enabled if it's parent and itself is enabled.
3376 // We do not send state changed events recursively. It is up
3377 // to the receiver of the events to check children if required.
3378 if (QAccessible::isActive() && wasEnabled != enable) {
3379 QAccessible::State states;
3380 states.disabled = 1;
3381 QAccessibleStateChangeEvent scEvent(this, states);
3382 QAccessible::updateAccessibility(&scEvent);
3383 }
3384#endif
3385}
3386
3387void QWidgetPrivate::setEnabled_helper(bool enable)
3388{
3389 Q_Q(QWidget);
3390
3391 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
3392 return; // nothing we can do
3393
3394 if (enable != q->testAttribute(Qt::WA_Disabled))
3395 return; // nothing to do
3396
3397 q->setAttribute(Qt::WA_Disabled, !enable);
3398 updateSystemBackground();
3399
3400 if (!enable && q->window()->focusWidget() == q) {
3401 bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
3402 if (!parentIsEnabled || !q->focusNextChild())
3403 q->clearFocus();
3404 }
3405
3406 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
3407 for (int i = 0; i < children.size(); ++i) {
3408 QWidget *w = qobject_cast<QWidget *>(children.at(i));
3409 if (w && !w->testAttribute(attribute))
3410 w->d_func()->setEnabled_helper(enable);
3411 }
3412#ifndef QT_NO_CURSOR
3413 if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
3414 // enforce the windows behavior of clearing the cursor on
3415 // disabled widgets
3416 qt_qpa_set_cursor(q, false);
3417 }
3418#endif
3419#ifndef QT_NO_IM
3420 if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
3421 QWidget *focusWidget = effectiveFocusWidget();
3422
3423 if (enable) {
3424 if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
3425 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3426 } else {
3427 QGuiApplication::inputMethod()->commit();
3428 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3429 }
3430 }
3431#endif //QT_NO_IM
3432 QEvent e(QEvent::EnabledChange);
3433 QCoreApplication::sendEvent(q, &e);
3434}
3435
3436/*!
3437 \property QWidget::acceptDrops
3438 \brief whether drop events are enabled for this widget
3439
3440 Setting this property to true announces to the system that this
3441 widget \e may be able to accept drop events.
3442
3443 \warning Do not modify this property in a drag and drop event handler.
3444
3445 By default, this property is \c false.
3446
3447 \sa {Drag and Drop in Qt}{Drag and Drop}
3448*/
3449bool QWidget::acceptDrops() const
3450{
3451 return testAttribute(Qt::WA_AcceptDrops);
3452}
3453
3454void QWidget::setAcceptDrops(bool on)
3455{
3456 setAttribute(Qt::WA_AcceptDrops, on);
3457
3458}
3459
3460/*!
3461 Disables widget input events if \a disable is true; otherwise
3462 enables input events.
3463
3464 See the \l enabled documentation for more information.
3465
3466 \sa isEnabledTo(), QKeyEvent, QMouseEvent, changeEvent()
3467*/
3468void QWidget::setDisabled(bool disable)
3469{
3470 setEnabled(!disable);
3471}
3472
3473/*!
3474 \property QWidget::frameGeometry
3475 \brief geometry of the widget relative to its parent including any
3476 window frame
3477
3478 See the \l{Window Geometry} documentation for an overview of geometry
3479 issues with windows.
3480
3481 By default, this property contains a value that depends on the user's
3482 platform and screen geometry.
3483
3484 \sa geometry(), x(), y(), pos()
3485*/
3486QRect QWidget::frameGeometry() const
3487{
3488 Q_D(const QWidget);
3489 if (isWindow() && ! (windowType() == Qt::Popup)) {
3490 QRect fs = d->frameStrut();
3491 return QRect(data->crect.x() - fs.left(),
3492 data->crect.y() - fs.top(),
3493 data->crect.width() + fs.left() + fs.right(),
3494 data->crect.height() + fs.top() + fs.bottom());
3495 }
3496 return data->crect;
3497}
3498
3499/*!
3500 \property QWidget::x
3501
3502 \brief the x coordinate of the widget relative to its parent including
3503 any window frame
3504
3505 See the \l{Window Geometry} documentation for an overview of geometry
3506 issues with windows.
3507
3508 By default, this property has a value of 0.
3509
3510 \sa frameGeometry, y, pos
3511*/
3512int QWidget::x() const
3513{
3514 Q_D(const QWidget);
3515 if (isWindow() && ! (windowType() == Qt::Popup))
3516 return data->crect.x() - d->frameStrut().left();
3517 return data->crect.x();
3518}
3519
3520/*!
3521 \property QWidget::y
3522 \brief the y coordinate of the widget relative to its parent and
3523 including any window frame
3524
3525 See the \l{Window Geometry} documentation for an overview of geometry
3526 issues with windows.
3527
3528 By default, this property has a value of 0.
3529
3530 \sa frameGeometry, x, pos
3531*/
3532int QWidget::y() const
3533{
3534 Q_D(const QWidget);
3535 if (isWindow() && ! (windowType() == Qt::Popup))
3536 return data->crect.y() - d->frameStrut().top();
3537 return data->crect.y();
3538}
3539
3540/*!
3541 \property QWidget::pos
3542 \brief the position of the widget within its parent widget
3543
3544 If the widget is a window, the position is that of the widget on
3545 the desktop, including its frame.
3546
3547 When changing the position, the widget, if visible, receives a
3548 move event (moveEvent()) immediately. If the widget is not
3549 currently visible, it is guaranteed to receive an event before it
3550 is shown.
3551
3552 By default, this property contains a position that refers to the
3553 origin.
3554
3555 \warning Calling move() or setGeometry() inside moveEvent() can
3556 lead to infinite recursion.
3557
3558 See the \l{Window Geometry} documentation for an overview of geometry
3559 issues with windows.
3560
3561 \note Not all windowing systems support setting or querying top level window positions.
3562 On such a system, programmatically moving windows may not have any effect, and artificial
3563 values may be returned for the current positions, such as \c QPoint(0, 0).
3564
3565 \sa frameGeometry, size, x(), y()
3566*/
3567QPoint QWidget::pos() const
3568{
3569 Q_D(const QWidget);
3570 QPoint result = data->crect.topLeft();
3571 if (isWindow() && ! (windowType() == Qt::Popup))
3572 if (!d->maybeTopData() || !d->maybeTopData()->posIncludesFrame)
3573 result -= d->frameStrut().topLeft();
3574 return result;
3575}
3576
3577/*!
3578 \property QWidget::geometry
3579 \brief the geometry of the widget relative to its parent and
3580 excluding the window frame
3581
3582 If the widget is a top-level widget (a window) its geometry is
3583 given in global screen coordinates.
3584
3585 When changing the geometry, the widget, if visible, receives a
3586 move event (moveEvent()) and/or a resize event (resizeEvent())
3587 immediately. If the widget is not currently visible, it is
3588 guaranteed to receive appropriate events before it is shown.
3589
3590 The size component is adjusted if it lies outside the range
3591 defined by minimumSize() and maximumSize().
3592
3593 \warning Calling setGeometry() inside resizeEvent() or moveEvent()
3594 can lead to infinite recursion.
3595
3596 See the \l{Window Geometry} documentation for an overview of geometry
3597 issues with windows.
3598
3599 By default, this property contains a value that depends on the user's
3600 platform and screen geometry.
3601
3602 \sa frameGeometry(), rect(), move(), resize(), moveEvent(),
3603 resizeEvent(), minimumSize(), maximumSize()
3604*/
3605
3606/*!
3607 \property QWidget::size
3608 \brief the size of the widget excluding any window frame
3609
3610 If the widget is visible when it is being resized, it receives a resize event
3611 (resizeEvent()) immediately. If the widget is not currently
3612 visible, it is guaranteed to receive an event before it is shown.
3613
3614 The size is adjusted if it lies outside the range defined by
3615 minimumSize() and maximumSize().
3616
3617 By default, this property contains a value that depends on the user's
3618 platform and screen geometry.
3619
3620 \warning Calling resize() or setGeometry() inside resizeEvent() can
3621 lead to infinite recursion.
3622
3623 \note Setting the size to \c{QSize(0, 0)} will cause the widget to not
3624 appear on screen. This also applies to windows.
3625
3626 \sa pos, geometry, minimumSize, maximumSize, resizeEvent(), adjustSize()
3627*/
3628
3629/*!
3630 \property QWidget::width
3631 \brief the width of the widget excluding any window frame
3632
3633 See the \l{Window Geometry} documentation for an overview of geometry
3634 issues with windows.
3635
3636 \note Do not use this function to find the width of a screen on
3637 a multi-screen desktop. See QScreen for details.
3638
3639 By default, this property contains a value that depends on the user's
3640 platform and screen geometry.
3641
3642 \sa geometry, height, size
3643*/
3644
3645/*!
3646 \property QWidget::height
3647 \brief the height of the widget excluding any window frame
3648
3649 See the \l{Window Geometry} documentation for an overview of geometry
3650 issues with windows.
3651
3652 By default, this property contains a value that depends on the user's
3653 platform and \l{QScreen::geometry}{screen geometry}.
3654
3655 \sa geometry, width, size
3656*/
3657
3658/*!
3659 \property QWidget::rect
3660 \brief the internal geometry of the widget excluding any window
3661 frame
3662
3663 The rect property equals QRect(0, 0, width(), height()).
3664
3665 See the \l{Window Geometry} documentation for an overview of geometry
3666 issues with windows.
3667
3668 By default, this property contains a value that depends on the user's
3669 platform and screen geometry.
3670
3671 \sa size
3672*/
3673
3674/*!
3675 \property QWidget::normalGeometry
3676
3677 \brief the geometry of the widget as it will appear when shown as
3678 a normal (not maximized or full screen) top-level widget
3679
3680 If the widget is already in this state the normal geometry will
3681 reflect the widget's current geometry().
3682
3683 For child widgets this property always holds an empty rectangle.
3684
3685 By default, this property contains an empty rectangle.
3686
3687 \sa QWidget::windowState(), QWidget::geometry
3688*/
3689QRect QWidget::normalGeometry() const
3690{
3691 Q_D(const QWidget);
3692 if (!isWindow())
3693 return QRect();
3694
3695 if (!isMaximized() && !isFullScreen())
3696 return geometry();
3697
3698 return d->topData()->normalGeometry;
3699}
3700
3701
3702/*!
3703 \property QWidget::childrenRect
3704 \brief the bounding rectangle of the widget's children
3705
3706 Hidden children are excluded.
3707
3708 By default, for a widget with no children, this property contains a
3709 rectangle with zero width and height located at the origin.
3710
3711 \sa childrenRegion(), geometry()
3712*/
3713
3714QRect QWidget::childrenRect() const
3715{
3716 Q_D(const QWidget);
3717 QRect r(0, 0, 0, 0);
3718 for (int i = 0; i < d->children.size(); ++i) {
3719 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3720 if (w && !w->isWindow() && !w->isHidden())
3721 r |= w->geometry();
3722 }
3723 return r;
3724}
3725
3726/*!
3727 \property QWidget::childrenRegion
3728 \brief the combined region occupied by the widget's children
3729
3730 Hidden children are excluded.
3731
3732 By default, for a widget with no children, this property contains an
3733 empty region.
3734
3735 \sa childrenRect(), geometry(), mask()
3736*/
3737
3738QRegion QWidget::childrenRegion() const
3739{
3740 Q_D(const QWidget);
3741 QRegion r;
3742 for (int i = 0; i < d->children.size(); ++i) {
3743 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3744 if (w && !w->isWindow() && !w->isHidden()) {
3745 QRegion mask = w->mask();
3746 if (mask.isEmpty())
3747 r |= w->geometry();
3748 else
3749 r |= mask.translated(w->pos());
3750 }
3751 }
3752 return r;
3753}
3754
3755
3756/*!
3757 \property QWidget::minimumSize
3758 \brief the widget's minimum size
3759
3760 The widget cannot be resized to a smaller size than the minimum
3761 widget size. The widget's size is forced to the minimum size if
3762 the current size is smaller.
3763
3764 The minimum size set by this function will override the minimum size
3765 defined by QLayout. To unset the minimum size, use a
3766 value of \c{QSize(0, 0)}.
3767
3768 By default, this property contains a size with zero width and height.
3769
3770 \sa minimumWidth, minimumHeight, maximumSize, sizeIncrement
3771*/
3772
3773QSize QWidget::minimumSize() const
3774{
3775 Q_D(const QWidget);
3776 return d->extra ? QSize(d->extra->minw, d->extra->minh) : QSize(0, 0);
3777}
3778
3779/*!
3780 \property QWidget::maximumSize
3781 \brief the widget's maximum size in pixels
3782
3783 The widget cannot be resized to a larger size than the maximum
3784 widget size.
3785
3786 By default, this property contains a size in which both width and height
3787 have values of 16777215.
3788
3789 \note The definition of the \c QWIDGETSIZE_MAX macro limits the maximum size
3790 of widgets.
3791
3792 \sa maximumWidth, maximumHeight, minimumSize, sizeIncrement
3793*/
3794
3795QSize QWidget::maximumSize() const
3796{
3797 Q_D(const QWidget);
3798 return d->extra ? QSize(d->extra->maxw, d->extra->maxh)
3800}
3801
3802
3803/*!
3804 \property QWidget::minimumWidth
3805 \brief the widget's minimum width in pixels
3806
3807 This property corresponds to the width held by the \l minimumSize property.
3808
3809 By default, this property has a value of 0.
3810
3811 \sa minimumSize, minimumHeight
3812*/
3813
3814/*!
3815 \property QWidget::minimumHeight
3816 \brief the widget's minimum height in pixels
3817
3818 This property corresponds to the height held by the \l minimumSize property.
3819
3820 By default, this property has a value of 0.
3821
3822 \sa minimumSize, minimumWidth
3823*/
3824
3825/*!
3826 \property QWidget::maximumWidth
3827 \brief the widget's maximum width in pixels
3828
3829 This property corresponds to the width held by the \l maximumSize property.
3830
3831 By default, this property contains a value of 16777215.
3832
3833 \note The definition of the \c QWIDGETSIZE_MAX macro limits the maximum size
3834 of widgets.
3835
3836 \sa maximumSize, maximumHeight
3837*/
3838
3839/*!
3840 \property QWidget::maximumHeight
3841 \brief the widget's maximum height in pixels
3842
3843 This property corresponds to the height held by the \l maximumSize property.
3844
3845 By default, this property contains a value of 16777215.
3846
3847 \note The definition of the \c QWIDGETSIZE_MAX macro limits the maximum size
3848 of widgets.
3849
3850 \sa maximumSize, maximumWidth
3851*/
3852
3853/*!
3854 \property QWidget::sizeIncrement
3855 \brief the size increment of the widget
3856
3857 When the user resizes the window, the size will move in steps of
3858 sizeIncrement().width() pixels horizontally and
3859 sizeIncrement.height() pixels vertically, with baseSize() as the
3860 basis. Preferred widget sizes are for non-negative integers \e i
3861 and \e j:
3862 \snippet code/src_gui_kernel_qwidget.cpp 2
3863
3864 Note that while you can set the size increment for all widgets, it
3865 only affects windows.
3866
3867 By default, this property contains a size with zero width and height.
3868
3869 \warning The size increment has no effect under Windows, and may
3870 be disregarded by the window manager on X11.
3871
3872 \sa size, minimumSize, maximumSize
3873*/
3874QSize QWidget::sizeIncrement() const
3875{
3876 Q_D(const QWidget);
3877 return (d->extra && d->extra->topextra)
3878 ? QSize(d->extra->topextra->incw, d->extra->topextra->inch)
3879 : QSize(0, 0);
3880}
3881
3882/*!
3883 \property QWidget::baseSize
3884 \brief the base size of the widget
3885
3886 The base size is used to calculate a proper widget size if the
3887 widget defines sizeIncrement().
3888
3889 By default, for a newly-created widget, this property contains a size with
3890 zero width and height.
3891
3892 \sa setSizeIncrement()
3893*/
3894
3895QSize QWidget::baseSize() const
3896{
3897 Q_D(const QWidget);
3898 return (d->extra && d->extra->topextra)
3899 ? QSize(d->extra->topextra->basew, d->extra->topextra->baseh)
3900 : QSize(0, 0);
3901}
3902
3903bool QWidgetPrivate::setMinimumSize_helper(int &minw, int &minh)
3904{
3905 Q_Q(QWidget);
3906
3907 int mw = minw, mh = minh;
3908 if (mw == QWIDGETSIZE_MAX)
3909 mw = 0;
3910 if (mh == QWIDGETSIZE_MAX)
3911 mh = 0;
3912 if (Q_UNLIKELY(minw > QWIDGETSIZE_MAX || minh > QWIDGETSIZE_MAX)) {
3913 qWarning("QWidget::setMinimumSize: (%s/%s) "
3914 "The largest allowed size is (%d,%d)",
3915 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), QWIDGETSIZE_MAX,
3917 minw = mw = qMin<int>(minw, QWIDGETSIZE_MAX);
3918 minh = mh = qMin<int>(minh, QWIDGETSIZE_MAX);
3919 }
3920 if (Q_UNLIKELY(minw < 0 || minh < 0)) {
3921 qWarning("QWidget::setMinimumSize: (%s/%s) Negative sizes (%d,%d) "
3922 "are not possible",
3923 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), minw, minh);
3924 minw = mw = qMax(minw, 0);
3925 minh = mh = qMax(minh, 0);
3926 }
3927 createExtra();
3928 if (extra->minw == mw && extra->minh == mh)
3929 return false;
3930 extra->minw = mw;
3931 extra->minh = mh;
3932 extra->explicitMinSize = (mw ? Qt::Horizontal : 0) | (mh ? Qt::Vertical : 0);
3933 return true;
3934}
3935
3936void QWidgetPrivate::setConstraints_sys()
3937{
3938 Q_Q(QWidget);
3939 if (extra && q->windowHandle()) {
3940 QWindow *win = q->windowHandle();
3941 QWindowPrivate *winp = qt_window_private(win);
3942
3943 winp->minimumSize = QSize(extra->minw, extra->minh);
3944 winp->maximumSize = QSize(extra->maxw, extra->maxh);
3945
3946 if (extra->topextra) {
3947 winp->baseSize = QSize(extra->topextra->basew, extra->topextra->baseh);
3948 winp->sizeIncrement = QSize(extra->topextra->incw, extra->topextra->inch);
3949 }
3950
3951 if (winp->platformWindow) {
3952 fixPosIncludesFrame();
3953 winp->platformWindow->propagateSizeHints();
3954 }
3955 }
3956}
3957
3958/*!
3959 \overload
3960
3961 This function corresponds to setMinimumSize(QSize(minw, minh)).
3962 Sets the minimum width to \a minw and the minimum height to \a
3963 minh.
3964*/
3965
3966void QWidget::setMinimumSize(int minw, int minh)
3967{
3968 Q_D(QWidget);
3969 if (!d->setMinimumSize_helper(minw, minh))
3970 return;
3971
3972 if (isWindow())
3973 d->setConstraints_sys();
3974 if (minw > width() || minh > height()) {
3975 bool resized = testAttribute(Qt::WA_Resized);
3976 bool maximized = isMaximized();
3977 resize(qMax(minw,width()), qMax(minh,height()));
3978 setAttribute(Qt::WA_Resized, resized); //not a user resize
3979 if (maximized)
3980 data->window_state = data->window_state | Qt::WindowMaximized;
3981 }
3982#if QT_CONFIG(graphicsview)
3983 if (d->extra) {
3984 if (d->extra->proxyWidget)
3985 d->extra->proxyWidget->setMinimumSize(minw, minh);
3986 }
3987#endif
3988 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
3989}
3990
3991bool QWidgetPrivate::setMaximumSize_helper(int &maxw, int &maxh)
3992{
3993 Q_Q(QWidget);
3994 if (Q_UNLIKELY(maxw > QWIDGETSIZE_MAX || maxh > QWIDGETSIZE_MAX)) {
3995 qWarning("QWidget::setMaximumSize: (%s/%s) "
3996 "The largest allowed size is (%d,%d)",
3997 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), QWIDGETSIZE_MAX,
3999 maxw = qMin<int>(maxw, QWIDGETSIZE_MAX);
4000 maxh = qMin<int>(maxh, QWIDGETSIZE_MAX);
4001 }
4002 if (Q_UNLIKELY(maxw < 0 || maxh < 0)) {
4003 qWarning("QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) "
4004 "are not possible",
4005 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), maxw, maxh);
4006 maxw = qMax(maxw, 0);
4007 maxh = qMax(maxh, 0);
4008 }
4009 createExtra();
4010 if (extra->maxw == maxw && extra->maxh == maxh)
4011 return false;
4012 extra->maxw = maxw;
4013 extra->maxh = maxh;
4014 extra->explicitMaxSize = (maxw != QWIDGETSIZE_MAX ? Qt::Horizontal : 0) |
4015 (maxh != QWIDGETSIZE_MAX ? Qt::Vertical : 0);
4016 return true;
4017}
4018
4019/*!
4020 \overload
4021
4022 This function corresponds to setMaximumSize(QSize(\a maxw, \a
4023 maxh)). Sets the maximum width to \a maxw and the maximum height
4024 to \a maxh.
4025*/
4026void QWidget::setMaximumSize(int maxw, int maxh)
4027{
4028 Q_D(QWidget);
4029 if (!d->setMaximumSize_helper(maxw, maxh))
4030 return;
4031
4032 if (isWindow())
4033 d->setConstraints_sys();
4034 if (maxw < width() || maxh < height()) {
4035 bool resized = testAttribute(Qt::WA_Resized);
4036 resize(qMin(maxw,width()), qMin(maxh,height()));
4037 setAttribute(Qt::WA_Resized, resized); //not a user resize
4038 }
4039
4040#if QT_CONFIG(graphicsview)
4041 if (d->extra) {
4042 if (d->extra->proxyWidget)
4043 d->extra->proxyWidget->setMaximumSize(maxw, maxh);
4044 }
4045#endif
4046
4047 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4048}
4049
4050/*!
4051 \overload
4052
4053 Sets the x (width) size increment to \a w and the y (height) size
4054 increment to \a h.
4055*/
4056void QWidget::setSizeIncrement(int w, int h)
4057{
4058 Q_D(QWidget);
4059 d->createTLExtra();
4060 QTLWExtra* x = d->topData();
4061 if (x->incw == w && x->inch == h)
4062 return;
4063 x->incw = w;
4064 x->inch = h;
4065 if (isWindow())
4066 d->setConstraints_sys();
4067}
4068
4069/*!
4070 \overload
4071
4072 This corresponds to setBaseSize(QSize(\a basew, \a baseh)). Sets
4073 the widgets base size to width \a basew and height \a baseh.
4074*/
4075void QWidget::setBaseSize(int basew, int baseh)
4076{
4077 Q_D(QWidget);
4078 d->createTLExtra();
4079 QTLWExtra* x = d->topData();
4080 if (x->basew == basew && x->baseh == baseh)
4081 return;
4082 x->basew = basew;
4083 x->baseh = baseh;
4084 if (isWindow())
4085 d->setConstraints_sys();
4086}
4087
4088/*!
4089 Sets both the minimum and maximum sizes of the widget to \a s,
4090 thereby preventing it from ever growing or shrinking.
4091
4092 This will override the default size constraints set by QLayout.
4093
4094 To remove constraints, set the size to QWIDGETSIZE_MAX.
4095
4096 Alternatively, if you want the widget to have a
4097 fixed size based on its contents, you can call
4098 QLayout::setSizeConstraint(QLayout::SetFixedSize);
4099
4100 \sa maximumSize, minimumSize
4101*/
4102
4103void QWidget::setFixedSize(const QSize & s)
4104{
4105 setFixedSize(s.width(), s.height());
4106}
4107
4108
4109/*!
4110 \fn void QWidget::setFixedSize(int w, int h)
4111 \overload
4112
4113 Sets the width of the widget to \a w and the height to \a h.
4114*/
4115
4116void QWidget::setFixedSize(int w, int h)
4117{
4118 Q_D(QWidget);
4119 bool minSizeSet = d->setMinimumSize_helper(w, h);
4120 bool maxSizeSet = d->setMaximumSize_helper(w, h);
4121 if (!minSizeSet && !maxSizeSet)
4122 return;
4123
4124 if (isWindow())
4125 d->setConstraints_sys();
4126 else
4127 d->updateGeometry_helper(true);
4128
4129 if (w != QWIDGETSIZE_MAX || h != QWIDGETSIZE_MAX)
4130 resize(w, h);
4131}
4132
4133void QWidget::setMinimumWidth(int w)
4134{
4135 Q_D(QWidget);
4136 d->createExtra();
4137 uint expl = d->extra->explicitMinSize | (w ? Qt::Horizontal : 0);
4138 setMinimumSize(w, minimumSize().height());
4139 d->extra->explicitMinSize = expl;
4140}
4141
4142void QWidget::setMinimumHeight(int h)
4143{
4144 Q_D(QWidget);
4145 d->createExtra();
4146 uint expl = d->extra->explicitMinSize | (h ? Qt::Vertical : 0);
4147 setMinimumSize(minimumSize().width(), h);
4148 d->extra->explicitMinSize = expl;
4149}
4150
4151void QWidget::setMaximumWidth(int w)
4152{
4153 Q_D(QWidget);
4154 d->createExtra();
4155 uint expl = d->extra->explicitMaxSize | (w == QWIDGETSIZE_MAX ? 0 : Qt::Horizontal);
4156 setMaximumSize(w, maximumSize().height());
4157 d->extra->explicitMaxSize = expl;
4158}
4159
4160void QWidget::setMaximumHeight(int h)
4161{
4162 Q_D(QWidget);
4163 d->createExtra();
4164 uint expl = d->extra->explicitMaxSize | (h == QWIDGETSIZE_MAX ? 0 : Qt::Vertical);
4165 setMaximumSize(maximumSize().width(), h);
4166 d->extra->explicitMaxSize = expl;
4167}
4168
4169/*!
4170 Sets both the minimum and maximum width of the widget to \a w
4171 without changing the heights. Provided for convenience.
4172
4173 \sa sizeHint(), minimumSize(), maximumSize(), setFixedSize()
4174*/
4175
4176void QWidget::setFixedWidth(int w)
4177{
4178 Q_D(QWidget);
4179 d->createExtra();
4180 uint explMin = d->extra->explicitMinSize | Qt::Horizontal;
4181 uint explMax = d->extra->explicitMaxSize | Qt::Horizontal;
4182 setMinimumSize(w, minimumSize().height());
4183 setMaximumSize(w, maximumSize().height());
4184 d->extra->explicitMinSize = explMin;
4185 d->extra->explicitMaxSize = explMax;
4186}
4187
4188
4189/*!
4190 Sets both the minimum and maximum heights of the widget to \a h
4191 without changing the widths. Provided for convenience.
4192
4193 \sa sizeHint(), minimumSize(), maximumSize(), setFixedSize()
4194*/
4195
4196void QWidget::setFixedHeight(int h)
4197{
4198 Q_D(QWidget);
4199 d->createExtra();
4200 uint explMin = d->extra->explicitMinSize | Qt::Vertical;
4201 uint explMax = d->extra->explicitMaxSize | Qt::Vertical;
4202 setMinimumSize(minimumSize().width(), h);
4203 setMaximumSize(maximumSize().width(), h);
4204 d->extra->explicitMinSize = explMin;
4205 d->extra->explicitMaxSize = explMax;
4206}
4207
4208
4209/*!
4210 Translates the widget coordinate \a pos to the coordinate system
4211 of \a parent. The \a parent must not be \nullptr and must be a parent
4212 of the calling widget.
4213
4214 \sa mapFrom(), mapToParent(), mapToGlobal(), underMouse()
4215 \since 6.0
4216*/
4217
4218QPointF QWidget::mapTo(const QWidget *parent, const QPointF &pos) const
4219{
4220 QPointF p = pos;
4221 if (parent) {
4222 const QWidget * w = this;
4223 while (w != parent) {
4224 p = w->mapToParent(p);
4225 w = w->parentWidget();
4226 if (!w) {
4227 qWarning("QWidget::mapTo(): parent must be in parent hierarchy");
4228 break;
4229 }
4230 }
4231 }
4232 return p;
4233}
4234
4235/*!
4236 \overload
4237*/
4238QPoint QWidget::mapTo(const QWidget *parent, const QPoint &pos) const
4239{
4240 return mapTo(parent, QPointF(pos)).toPoint();
4241}
4242
4243/*!
4244 Translates the widget coordinate \a pos from the coordinate system
4245 of \a parent to this widget's coordinate system. The \a parent
4246 must not be \nullptr and must be a parent of the calling widget.
4247
4248 \sa mapTo(), mapFromParent(), mapFromGlobal(), underMouse()
4249 \since 6.0
4250*/
4251
4252QPointF QWidget::mapFrom(const QWidget *parent, const QPointF &pos) const
4253{
4254 QPointF p(pos);
4255 if (parent) {
4256 const QWidget * w = this;
4257 while (w != parent) {
4258 p = w->mapFromParent(p);
4259 w = w->parentWidget();
4260 if (!w) {
4261 qWarning("QWidget::mapFrom(): parent must be in parent hierarchy");
4262 break;
4263 }
4264 }
4265 }
4266 return p;
4267}
4268
4269/*!
4270 \overload
4271*/
4272QPoint QWidget::mapFrom(const QWidget *parent, const QPoint &pos) const
4273{
4274 return mapFrom(parent, QPointF(pos)).toPoint();
4275}
4276
4277/*!
4278 Translates the widget coordinate \a pos to a coordinate in the
4279 parent widget.
4280
4281 Same as mapToGlobal() if the widget has no parent.
4282
4283 \sa mapFromParent(), mapTo(), mapToGlobal(), underMouse()
4284 \since 6.0
4285*/
4286
4287QPointF QWidget::mapToParent(const QPointF &pos) const
4288{
4289 return pos + QPointF(data->crect.topLeft());
4290}
4291
4292/*!
4293 \overload
4294*/
4295QPoint QWidget::mapToParent(const QPoint &pos) const
4296{
4297 return pos + data->crect.topLeft();
4298}
4299
4300/*!
4301 Translates the parent widget coordinate \a pos to widget
4302 coordinates.
4303
4304 Same as mapFromGlobal() if the widget has no parent.
4305
4306 \sa mapToParent(), mapFrom(), mapFromGlobal(), underMouse()
4307 \since 6.0
4308*/
4309
4310QPointF QWidget::mapFromParent(const QPointF &pos) const
4311{
4312 return pos - QPointF(data->crect.topLeft());
4313}
4314
4315/*!
4316 \overload
4317*/
4318QPoint QWidget::mapFromParent(const QPoint &pos) const
4319{
4320 return pos - data->crect.topLeft();
4321}
4322
4323
4324/*!
4325 Returns the window for this widget, i.e. the next ancestor widget
4326 that has (or could have) a window-system frame.
4327
4328 If the widget is a window, the widget itself is returned.
4329
4330 Typical usage is changing the window title:
4331
4332 \snippet code/src_gui_kernel_qwidget.cpp 3
4333
4334 \sa isWindow()
4335*/
4336
4337QWidget *QWidget::window() const
4338{
4339 QWidget *w = const_cast<QWidget *>(this);
4340 QWidget *p = w->parentWidget();
4341 while (!w->isWindow() && p) {
4342 w = p;
4343 p = p->parentWidget();
4344 }
4345 return w;
4346}
4347
4348/*!
4349 \since 4.4
4350
4351 Returns the native parent for this widget, i.e. the next ancestor widget
4352 that has a system identifier, or \nullptr if it does not have any native
4353 parent.
4354
4355 \sa effectiveWinId()
4356*/
4357QWidget *QWidget::nativeParentWidget() const
4358{
4359 QWidget *parent = parentWidget();
4360 while (parent && !parent->internalWinId())
4361 parent = parent->parentWidget();
4362 return parent;
4363}
4364
4365/*! \fn QWidget *QWidget::topLevelWidget() const
4366 \deprecated
4367
4368 Use window() instead.
4369*/
4370
4371
4372
4373/*!
4374 Returns the background role of the widget.
4375
4376 The background role defines the brush from the widget's \l palette that
4377 is used to render the background.
4378
4379 If no explicit background role is set, the widget inherits its parent
4380 widget's background role.
4381
4382 \sa setBackgroundRole(), foregroundRole()
4383 */
4384QPalette::ColorRole QWidget::backgroundRole() const
4385{
4386
4387 const QWidget *w = this;
4388 do {
4389 QPalette::ColorRole role = w->d_func()->bg_role;
4390 if (role != QPalette::NoRole)
4391 return role;
4392 if (w->isWindow() || w->windowType() == Qt::SubWindow)
4393 break;
4394 w = w->parentWidget();
4395 } while (w);
4396 return QPalette::Window;
4397}
4398
4399/*!
4400 Sets the background role of the widget to \a role.
4401
4402 The background role defines the brush from the widget's \l palette that
4403 is used to render the background.
4404
4405 If \a role is QPalette::NoRole, then the widget inherits its
4406 parent's background role.
4407
4408 Note that styles are free to choose any color from the palette.
4409 You can modify the palette or set a style sheet if you don't
4410 achieve the result you want with setBackgroundRole().
4411
4412 \sa backgroundRole(), foregroundRole()
4413 */
4414
4415void QWidget::setBackgroundRole(QPalette::ColorRole role)
4416{
4417 Q_D(QWidget);
4418 d->bg_role = role;
4419 d->updateSystemBackground();
4420 d->propagatePaletteChange();
4421 d->updateIsOpaque();
4422}
4423
4424/*!
4425 Returns the foreground role.
4426
4427 The foreground role defines the color from the widget's \l palette that
4428 is used to draw the foreground.
4429
4430 If no explicit foreground role is set, the function returns a role
4431 that contrasts with the background role.
4432
4433 \sa setForegroundRole(), backgroundRole()
4434 */
4435QPalette::ColorRole QWidget::foregroundRole() const
4436{
4437 Q_D(const QWidget);
4438 QPalette::ColorRole rl = QPalette::ColorRole(d->fg_role);
4439 if (rl != QPalette::NoRole)
4440 return rl;
4441 QPalette::ColorRole role = QPalette::WindowText;
4442 switch (backgroundRole()) {
4443 case QPalette::Button:
4444 role = QPalette::ButtonText;
4445 break;
4446 case QPalette::Base:
4447 role = QPalette::Text;
4448 break;
4449 case QPalette::Dark:
4450 case QPalette::Shadow:
4451 role = QPalette::Light;
4452 break;
4453 case QPalette::Highlight:
4454 role = QPalette::HighlightedText;
4455 break;
4456 case QPalette::ToolTipBase:
4457 role = QPalette::ToolTipText;
4458 break;
4459 default:
4460 ;
4461 }
4462 return role;
4463}
4464
4465/*!
4466 Sets the foreground role of the widget to \a role.
4467
4468 The foreground role defines the color from the widget's \l palette that
4469 is used to draw the foreground.
4470
4471 If \a role is QPalette::NoRole, the widget uses a foreground role
4472 that contrasts with the background role.
4473
4474 Note that styles are free to choose any color from the palette.
4475 You can modify the palette or set a style sheet if you don't
4476 achieve the result you want with setForegroundRole().
4477
4478 \sa foregroundRole(), backgroundRole()
4479 */
4480void QWidget::setForegroundRole(QPalette::ColorRole role)
4481{
4482 Q_D(QWidget);
4483 d->fg_role = role;
4484 d->updateSystemBackground();
4485 d->propagatePaletteChange();
4486}
4487
4488/*!
4489 \property QWidget::palette
4490 \brief the widget's palette
4491
4492 This property describes the widget's palette. The palette is used by the
4493 widget's style when rendering standard components, and is available as a
4494 means to ensure that custom widgets can maintain consistency with the
4495 native platform's look and feel. It's common that different platforms, or
4496 different styles, have different palettes.
4497
4498 When you assign a new palette to a widget, the color roles from this
4499 palette are combined with the widget's default palette to form the
4500 widget's final palette. The palette entry for the widget's background role
4501 is used to fill the widget's background (see QWidget::autoFillBackground),
4502 and the foreground role initializes QPainter's pen.
4503
4504 The default depends on the system environment. QApplication maintains a
4505 system/theme palette which serves as a default for all widgets. There may
4506 also be special palette defaults for certain types of widgets (e.g., on
4507 Windows Vista, all classes that derive from QMenuBar have a special
4508 default palette). You can also define default palettes for widgets
4509 yourself by passing a custom palette and the name of a widget to
4510 QApplication::setPalette(). Finally, the style always has the option of
4511 polishing the palette as it's assigned (see QStyle::polish()).
4512
4513 QWidget propagates explicit palette roles from parent to child. If you
4514 assign a brush or color to a specific role on a palette and assign that
4515 palette to a widget, that role will propagate to all the widget's
4516 children, overriding any system defaults for that role. Note that palettes
4517 by default don't propagate to windows (see isWindow()) unless the
4518 Qt::WA_WindowPropagation attribute is enabled.
4519
4520 QWidget's palette propagation is similar to its font propagation.
4521
4522 The current style, which is used to render the content of all standard Qt
4523 widgets, is free to choose colors and brushes from the widget palette, or,
4524 in some cases, to ignore the palette (partially, or completely). In
4525 particular, certain styles like GTK style, Mac style, and Windows Vista
4526 style, depend on third party APIs to render the content of widgets,
4527 and these styles typically do not follow the palette. Because of this,
4528 assigning roles to a widget's palette is not guaranteed to change the
4529 appearance of the widget. Instead, you may choose to apply a \l {styleSheet}.
4530
4531 \warning Do not use this function in conjunction with \l{Qt Style Sheets}.
4532 When using style sheets, the palette of a widget can be customized using
4533 the "color", "background-color", "selection-color",
4534 "selection-background-color" and "alternate-background-color".
4535
4536 \sa QGuiApplication::palette(), QWidget::font(), {Qt Style Sheets}
4537*/
4538const QPalette &QWidget::palette() const
4539{
4540 if (!isEnabled()) {
4541 data->pal.setCurrentColorGroup(QPalette::Disabled);
4542 } else if ((!isVisible() || isActiveWindow())
4543#if defined(Q_OS_WIN)
4544 && !QApplicationPrivate::isBlockedByModal(const_cast<QWidget *>(this))
4545#endif
4546 ) {
4547 data->pal.setCurrentColorGroup(QPalette::Active);
4548 } else {
4549 data->pal.setCurrentColorGroup(QPalette::Inactive);
4550 }
4551 return data->pal;
4552}
4553
4554void QWidget::setPalette(const QPalette &palette)
4555{
4556 Q_D(QWidget);
4557 setAttribute(Qt::WA_SetPalette, palette.resolveMask() != 0);
4558
4559 // Determine which palette is inherited from this widget's ancestors and
4560 // QApplication::palette, resolve this against \a palette (attributes from
4561 // the inherited palette are copied over this widget's palette). Then
4562 // propagate this palette to this widget's children.
4563 QPalette naturalPalette = d->naturalWidgetPalette(d->inheritedPaletteResolveMask);
4564 QPalette resolvedPalette = palette.resolve(naturalPalette);
4565 d->setPalette_helper(resolvedPalette);
4566}
4567
4568/*!
4569 \internal
4570
4571 Returns the palette that the widget \a w inherits from its ancestors and
4572 QApplication::palette. \a inheritedMask is the combination of the widget's
4573 ancestors palette request masks (i.e., which attributes from the parent
4574 widget's palette are implicitly imposed on this widget by the user). Note
4575 that this font does not take into account the palette set on \a w itself.
4576*/
4577QPalette QWidgetPrivate::naturalWidgetPalette(QPalette::ResolveMask inheritedMask) const
4578{
4579 Q_Q(const QWidget);
4580
4581 const bool useStyleSheetPropagationInWidgetStyles =
4582 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4583
4584 QPalette naturalPalette = QApplication::palette(q);
4585 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4586 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4587#if QT_CONFIG(graphicsview)
4588 || (extra && extra->proxyWidget)
4589#endif // QT_CONFIG(graphicsview)
4590 )) {
4591 if (QWidget *p = q->parentWidget()) {
4592 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4593 if (!naturalPalette.isCopyOf(QGuiApplication::palette())) {
4594 QPalette inheritedPalette = p->palette();
4595 inheritedPalette.setResolveMask(inheritedMask);
4596 naturalPalette = inheritedPalette.resolve(naturalPalette);
4597 } else {
4598 naturalPalette = p->palette();
4599 }
4600 }
4601 }
4602#if QT_CONFIG(graphicsview)
4603 else if (extra && extra->proxyWidget) {
4604 QPalette inheritedPalette = extra->proxyWidget->palette();
4605 inheritedPalette.setResolveMask(inheritedMask);
4606 naturalPalette = inheritedPalette.resolve(naturalPalette);
4607 }
4608#endif // QT_CONFIG(graphicsview)
4609 }
4610 naturalPalette.setResolveMask(0);
4611 return naturalPalette;
4612}
4613/*!
4614 \internal
4615
4616 Determine which palette is inherited from this widget's ancestors and
4617 QApplication::palette, resolve this against this widget's palette
4618 (attributes from the inherited palette are copied over this widget's
4619 palette). Then propagate this palette to this widget's children.
4620*/
4621void QWidgetPrivate::resolvePalette()
4622{
4623 QPalette naturalPalette = naturalWidgetPalette(inheritedPaletteResolveMask);
4624 QPalette resolvedPalette = data.pal.resolve(naturalPalette);
4625 setPalette_helper(resolvedPalette);
4626}
4627
4628void QWidgetPrivate::setPalette_helper(const QPalette &palette)
4629{
4630 Q_Q(QWidget);
4631 if (data.pal == palette && data.pal.resolveMask() == palette.resolveMask())
4632 return;
4633 data.pal = palette;
4634 updateSystemBackground();
4635 propagatePaletteChange();
4636 updateIsOpaque();
4637 q->update();
4638 updateIsOpaque();
4639}
4640
4641void QWidgetPrivate::updateSystemBackground()
4642{
4643}
4644
4645/*!
4646 \property QWidget::font
4647 \brief the font currently set for the widget
4648
4649 This property describes the widget's requested font. The font is used by
4650 the widget's style when rendering standard components, and is available as
4651 a means to ensure that custom widgets can maintain consistency with the
4652 native platform's look and feel. It's common that different platforms, or
4653 different styles, define different fonts for an application.
4654
4655 When you assign a new font to a widget, the properties from this font are
4656 combined with the widget's default font to form the widget's final
4657 font. You can call fontInfo() to get a copy of the widget's final
4658 font. The final font is also used to initialize QPainter's font.
4659
4660 The default depends on the system environment. QApplication maintains a
4661 system/theme font which serves as a default for all widgets. There may
4662 also be special font defaults for certain types of widgets. You can also
4663 define default fonts for widgets yourself by passing a custom font and the
4664 name of a widget to QApplication::setFont(). Finally, the font is matched
4665 against Qt's font database to find the best match.
4666
4667 QWidget propagates explicit font properties from parent to child. If you
4668 change a specific property on a font and assign that font to a widget,
4669 that property will propagate to all the widget's children, overriding any
4670 system defaults for that property. Note that fonts by default don't
4671 propagate to windows (see isWindow()) unless the Qt::WA_WindowPropagation
4672 attribute is enabled.
4673
4674 QWidget's font propagation is similar to its palette propagation.
4675
4676 The current style, which is used to render the content of all standard Qt
4677 widgets, is free to choose to use the widget font, or in some cases, to
4678 ignore it (partially, or completely). In particular, certain styles like
4679 GTK style, Mac style, and Windows Vista style, apply special
4680 modifications to the widget font to match the platform's native look and
4681 feel. Because of this, assigning properties to a widget's font is not
4682 guaranteed to change the appearance of the widget. Instead, you may choose
4683 to apply a \l styleSheet.
4684
4685 \note If \l{Qt Style Sheets} are used on the same widget as setFont(),
4686 style sheets will take precedence if the settings conflict.
4687
4688 \sa fontInfo(), fontMetrics()
4689*/
4690
4691void QWidget::setFont(const QFont &font)
4692{
4693 Q_D(QWidget);
4694
4695#if QT_CONFIG(style_stylesheet)
4696 const QStyleSheetStyle* style;
4697 if (d->extra && (style = qt_styleSheet(d->extra->style)))
4698 style->saveWidgetFont(this, font);
4699#endif
4700
4701 setAttribute(Qt::WA_SetFont, font.resolveMask() != 0);
4702
4703 // Determine which font is inherited from this widget's ancestors and
4704 // QApplication::font, resolve this against \a font (attributes from the
4705 // inherited font are copied over). Then propagate this font to this
4706 // widget's children.
4707 QFont naturalFont = d->naturalWidgetFont(d->inheritedFontResolveMask);
4708 QFont resolvedFont = font.resolve(naturalFont);
4709 d->setFont_helper(resolvedFont);
4710}
4711
4712/*
4713 \internal
4714
4715 Returns the font that the widget \a w inherits from its ancestors and
4716 QApplication::font. \a inheritedMask is the combination of the widget's
4717 ancestors font request masks (i.e., which attributes from the parent
4718 widget's font are implicitly imposed on this widget by the user). Note
4719 that this font does not take into account the font set on \a w itself.
4720
4721 ### Stylesheet has a different font propagation mechanism. When a stylesheet
4722 is applied, fonts are not propagated anymore
4723*/
4724QFont QWidgetPrivate::naturalWidgetFont(uint inheritedMask) const
4725{
4726 Q_Q(const QWidget);
4727
4728 const bool useStyleSheetPropagationInWidgetStyles =
4729 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4730
4731 QFont naturalFont = QApplication::font(q);
4732 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4733 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4734#if QT_CONFIG(graphicsview)
4735 || (extra && extra->proxyWidget)
4736#endif // QT_CONFIG(graphicsview)
4737 )) {
4738 if (QWidget *p = q->parentWidget()) {
4739 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4740 if (!naturalFont.isCopyOf(QApplication::font())) {
4741 if (inheritedMask != 0) {
4742 QFont inheritedFont = p->font();
4743 inheritedFont.setResolveMask(inheritedMask);
4744 naturalFont = inheritedFont.resolve(naturalFont);
4745 } // else nothing to do (naturalFont = naturalFont)
4746 } else {
4747 naturalFont = p->font();
4748 }
4749 }
4750 }
4751#if QT_CONFIG(graphicsview)
4752 else if (extra && extra->proxyWidget) {
4753 if (inheritedMask != 0) {
4754 QFont inheritedFont = extra->proxyWidget->font();
4755 inheritedFont.setResolveMask(inheritedMask);
4756 naturalFont = inheritedFont.resolve(naturalFont);
4757 } // else nothing to do (naturalFont = naturalFont)
4758 }
4759#endif // QT_CONFIG(graphicsview)
4760 }
4761 naturalFont.setResolveMask(0);
4762 return naturalFont;
4763}
4764
4765/*!
4766 \internal
4767
4768 Returns a font suitable for inheritance, where only locally set attributes are considered resolved.
4769*/
4770QFont QWidgetPrivate::localFont() const
4771{
4772 QFont localfont = data.fnt;
4773 localfont.setResolveMask(directFontResolveMask);
4774 return localfont;
4775}
4776
4777/*!
4778 \internal
4779
4780 Determine which font is implicitly imposed on this widget by its ancestors
4781 and QApplication::font, resolve this against its own font (attributes from
4782 the implicit font are copied over). Then propagate this font to this
4783 widget's children.
4784*/
4785void QWidgetPrivate::resolveFont()
4786{
4787 QFont naturalFont = naturalWidgetFont(inheritedFontResolveMask);
4788 QFont resolvedFont = localFont().resolve(naturalFont);
4789 setFont_helper(resolvedFont);
4790}
4791
4792/*!
4793 \internal
4794
4795 Assign \a font to this widget, and propagate it to all children, except
4796 style sheet widgets (handled differently) and windows that don't enable
4797 window propagation. \a implicitMask is the union of all ancestor widgets'
4798 font request masks, and determines which attributes from this widget's
4799 font should propagate.
4800*/
4801void QWidgetPrivate::updateFont(const QFont &font)
4802{
4803 Q_Q(QWidget);
4804#if QT_CONFIG(style_stylesheet)
4805 const QStyleSheetStyle* cssStyle;
4806 cssStyle = extra ? qt_styleSheet(extra->style) : nullptr;
4807 const bool useStyleSheetPropagationInWidgetStyles =
4808 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4809#endif
4810
4811 data.fnt = QFont(font, q);
4812
4813 // Combine new mask with natural mask and propagate to children.
4814#if QT_CONFIG(graphicsview)
4815 if (!q->parentWidget() && extra && extra->proxyWidget) {
4816 QGraphicsProxyWidget *p = extra->proxyWidget;
4817 inheritedFontResolveMask = p->d_func()->inheritedFontResolveMask | p->font().resolveMask();
4818 } else
4819#endif // QT_CONFIG(graphicsview)
4820 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
4821 inheritedFontResolveMask = 0;
4822 }
4823 uint newMask = data.fnt.resolveMask() | inheritedFontResolveMask;
4824 // Set the font as also having resolved inherited traits, so the result of reading QWidget::font()
4825 // isn't all weak information, but save the original mask to be able to let new changes on the
4826 // parent widget font propagate correctly.
4827 directFontResolveMask = data.fnt.resolveMask();
4828 data.fnt.setResolveMask(newMask);
4829
4830 for (int i = 0; i < children.size(); ++i) {
4831 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4832 if (w) {
4833 if (0) {
4834#if QT_CONFIG(style_stylesheet)
4835 } else if (!useStyleSheetPropagationInWidgetStyles && w->testAttribute(Qt::WA_StyleSheet)) {
4836 // Style sheets follow a different font propagation scheme.
4837 if (cssStyle)
4838 cssStyle->updateStyleSheetFont(w);
4839#endif
4840 } else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
4841 // Propagate font changes.
4842 QWidgetPrivate *wd = w->d_func();
4843 wd->inheritedFontResolveMask = newMask;
4844 wd->resolveFont();
4845 }
4846 }
4847 }
4848
4849#if QT_CONFIG(style_stylesheet)
4850 if (!useStyleSheetPropagationInWidgetStyles && cssStyle) {
4851 cssStyle->updateStyleSheetFont(q);
4852 }
4853#endif
4854
4855 QEvent e(QEvent::FontChange);
4856 QCoreApplication::sendEvent(q, &e);
4857}
4858
4859void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
4860{
4861 Q_Q(QWidget);
4862
4863 if ( (direction == Qt::RightToLeft) == q->testAttribute(Qt::WA_RightToLeft))
4864 return;
4865 q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
4866 if (!children.isEmpty()) {
4867 for (int i = 0; i < children.size(); ++i) {
4868 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4869 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_SetLayoutDirection))
4870 w->d_func()->setLayoutDirection_helper(direction);
4871 }
4872 }
4873 QEvent e(QEvent::LayoutDirectionChange);
4874 QCoreApplication::sendEvent(q, &e);
4875}
4876
4877void QWidgetPrivate::resolveLayoutDirection()
4878{
4879 Q_Q(const QWidget);
4880 if (!q->testAttribute(Qt::WA_SetLayoutDirection))
4881 setLayoutDirection_helper(q->isWindow() ? QGuiApplication::layoutDirection() : q->parentWidget()->layoutDirection());
4882}
4883
4884/*!
4885 \property QWidget::layoutDirection
4886
4887 \brief the layout direction for this widget.
4888
4889 \note This method no longer affects text layout direction since Qt 4.7.
4890
4891 By default, this property is set to Qt::LeftToRight.
4892
4893 When the layout direction is set on a widget, it will propagate to
4894 the widget's children, but not to a child that is a window and not
4895 to a child for which setLayoutDirection() has been explicitly
4896 called. Also, child widgets added \e after setLayoutDirection()
4897 has been called for the parent do not inherit the parent's layout
4898 direction.
4899
4900
4901 \sa QApplication::layoutDirection
4902*/
4903void QWidget::setLayoutDirection(Qt::LayoutDirection direction)
4904{
4905 Q_D(QWidget);
4906
4907 if (direction == Qt::LayoutDirectionAuto) {
4908 unsetLayoutDirection();
4909 return;
4910 }
4911
4912 setAttribute(Qt::WA_SetLayoutDirection);
4913 d->setLayoutDirection_helper(direction);
4914}
4915
4916Qt::LayoutDirection QWidget::layoutDirection() const
4917{
4918 return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
4919}
4920
4921void QWidget::unsetLayoutDirection()
4922{
4923 Q_D(QWidget);
4924 setAttribute(Qt::WA_SetLayoutDirection, false);
4925 d->resolveLayoutDirection();
4926}
4927
4928/*!
4929 \fn QFontMetrics QWidget::fontMetrics() const
4930
4931 Returns the font metrics for the widget's current font.
4932 Equivalent to \c QFontMetrics(widget->font()).
4933
4934 \sa font(), fontInfo(), setFont()
4935*/
4936
4937/*!
4938 \fn QFontInfo QWidget::fontInfo() const
4939
4940 Returns the font info for the widget's current font.
4941 Equivalent to \c QFontInfo(widget->font()).
4942
4943 \sa font(), fontMetrics(), setFont()
4944*/
4945
4946
4947/*!
4948 \property QWidget::cursor
4949 \brief the cursor shape for this widget
4950
4951 The mouse cursor will assume this shape when it's over this
4952 widget. See the \l{Qt::CursorShape}{list of predefined cursor objects} for a range of useful shapes.
4953
4954 An editor widget might use an I-beam cursor:
4955 \snippet code/src_gui_kernel_qwidget.cpp 6
4956
4957 If no cursor has been set, or after a call to unsetCursor(), the
4958 parent's cursor is used.
4959
4960 By default, this property contains a cursor with the Qt::ArrowCursor
4961 shape.
4962
4963 Some underlying window implementations will reset the cursor if it
4964 leaves a widget even if the mouse is grabbed. If you want to have
4965 a cursor set for all widgets, even when outside the window, consider
4966 QGuiApplication::setOverrideCursor().
4967
4968 \sa QGuiApplication::setOverrideCursor()
4969*/
4970
4971#ifndef QT_NO_CURSOR
4972QCursor QWidget::cursor() const
4973{
4974 Q_D(const QWidget);
4975 if (testAttribute(Qt::WA_SetCursor))
4976 return (d->extra && d->extra->curs)
4977 ? *d->extra->curs
4978 : QCursor(Qt::ArrowCursor);
4979 if (isWindow() || !parentWidget())
4980 return QCursor(Qt::ArrowCursor);
4981 return parentWidget()->cursor();
4982}
4983
4984void QWidget::setCursor(const QCursor &cursor)
4985{
4986 Q_D(QWidget);
4987 if (cursor.shape() != Qt::ArrowCursor
4988 || (d->extra && d->extra->curs))
4989 {
4990 d->createExtra();
4991 d->extra->curs = std::make_unique<QCursor>(cursor);
4992 }
4993 setAttribute(Qt::WA_SetCursor);
4994 d->setCursor_sys(cursor);
4995
4996 QEvent event(QEvent::CursorChange);
4997 QCoreApplication::sendEvent(this, &event);
4998}
4999
5000void QWidgetPrivate::setCursor_sys(const QCursor &cursor)
5001{
5002 Q_UNUSED(cursor);
5003 Q_Q(QWidget);
5004 qt_qpa_set_cursor(q, false);
5005}
5006
5007void QWidget::unsetCursor()
5008{
5009 Q_D(QWidget);
5010 if (d->extra)
5011 d->extra->curs.reset();
5012 if (!isWindow())
5013 setAttribute(Qt::WA_SetCursor, false);
5014 d->unsetCursor_sys();
5015
5016 QEvent event(QEvent::CursorChange);
5017 QCoreApplication::sendEvent(this, &event);
5018}
5019
5020void QWidgetPrivate::unsetCursor_sys()
5021{
5022 Q_Q(QWidget);
5023 qt_qpa_set_cursor(q, false);
5024}
5025
5026static inline void applyCursor(QWidget *w, const QCursor &c)
5027{
5028 if (QWindow *window = w->windowHandle())
5029 window->setCursor(c);
5030}
5031
5032static inline void unsetCursor(QWidget *w)
5033{
5034 if (QWindow *window = w->windowHandle())
5035 window->unsetCursor();
5036}
5037
5038void qt_qpa_set_cursor(QWidget *w, bool force)
5039{
5040 if (!w->testAttribute(Qt::WA_WState_Created))
5041 return;
5042
5043 static QPointer<QWidget> lastUnderMouse = nullptr;
5044 if (force) {
5045 lastUnderMouse = w;
5046 } else if (lastUnderMouse) {
5047 const WId lastWinId = lastUnderMouse->effectiveWinId();
5048 const WId winId = w->effectiveWinId();
5049 if (lastWinId && lastWinId == winId)
5050 w = lastUnderMouse;
5051 } else if (!w->internalWinId()) {
5052 return; // The mouse is not under this widget, and it's not native, so don't change it.
5053 }
5054
5055 while (!w->internalWinId() && w->parentWidget() && !w->isWindow()
5056 && !w->testAttribute(Qt::WA_SetCursor))
5057 w = w->parentWidget();
5058
5059 QWidget *nativeParent = w;
5060 if (!w->internalWinId())
5061 nativeParent = w->nativeParentWidget();
5062 if (!nativeParent || !nativeParent->internalWinId())
5063 return;
5064
5065 if (w->isWindow() || w->testAttribute(Qt::WA_SetCursor)) {
5066 if (w->isEnabled())
5067 applyCursor(nativeParent, w->cursor());
5068 else
5069 // Enforce the windows behavior of clearing the cursor on
5070 // disabled widgets.
5071 unsetCursor(nativeParent);
5072 } else {
5073 unsetCursor(nativeParent);
5074 }
5075}
5076#endif
5077
5078/*!
5079 \enum QWidget::RenderFlag
5080
5081 This enum describes how to render the widget when calling QWidget::render().
5082
5083 \value DrawWindowBackground If you enable this option, the widget's background
5084 is rendered into the target even if autoFillBackground is not set. By default,
5085 this option is enabled.
5086
5087 \value DrawChildren If you enable this option, the widget's children
5088 are rendered recursively into the target. By default, this option is enabled.
5089
5090 \value IgnoreMask If you enable this option, the widget's QWidget::mask()
5091 is ignored when rendering into the target. By default, this option is disabled.
5092
5093 \since 4.3
5094*/
5095
5096/*!
5097 \since 4.3
5098
5099 Renders the \a sourceRegion of this widget into the \a target
5100 using \a renderFlags to determine how to render. Rendering
5101 starts at \a targetOffset in the \a target. For example:
5102
5103 \snippet code/src_gui_kernel_qwidget.cpp 7
5104
5105 If \a sourceRegion is a null region, this function will use QWidget::rect() as
5106 the region, i.e. the entire widget.
5107
5108 Ensure that you call QPainter::end() for the \a target device's
5109 active painter (if any) before rendering. For example:
5110
5111 \snippet code/src_gui_kernel_qwidget.cpp 8
5112
5113 \note To obtain the contents of a QOpenGLWidget, use QOpenGLWidget::grabFramebuffer()
5114 instead.
5115*/
5116void QWidget::render(QPaintDevice *target, const QPoint &targetOffset,
5117 const QRegion &sourceRegion, RenderFlags renderFlags)
5118{
5119 QPainter p(target);
5120 render(&p, targetOffset, sourceRegion, renderFlags);
5121}
5122
5123/*!
5124 \overload
5125
5126 Renders the widget into the \a painter's QPainter::device().
5127
5128 Transformations and settings applied to the \a painter will be used
5129 when rendering.
5130
5131 \note The \a painter must be active. On \macos the widget will be
5132 rendered into a QPixmap and then drawn by the \a painter.
5133
5134 \sa QPainter::device()
5135*/
5136void QWidget::render(QPainter *painter, const QPoint &targetOffset,
5137 const QRegion &sourceRegion, RenderFlags renderFlags)
5138{
5139 if (Q_UNLIKELY(!painter)) {
5140 qWarning("QWidget::render: Null pointer to painter");
5141 return;
5142 }
5143
5144 if (Q_UNLIKELY(!painter->isActive())) {
5145 qWarning("QWidget::render: Cannot render with an inactive painter");
5146 return;
5147 }
5148
5149 const qreal opacity = painter->opacity();
5150 if (qFuzzyIsNull(opacity))
5151 return; // Fully transparent.
5152
5153 Q_D(QWidget);
5154 const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
5155 const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
5156 : sourceRegion;
5157 if (toBePainted.isEmpty())
5158 return;
5159
5160 if (!d->extra)
5161 d->createExtra();
5162 d->extra->inRenderWithPainter = true;
5163
5164 QPaintEngine *engine = painter->paintEngine();
5165 Q_ASSERT(engine);
5166 QPaintEnginePrivate *enginePriv = engine->d_func();
5167 Q_ASSERT(enginePriv);
5168 QPaintDevice *target = engine->paintDevice();
5169 Q_ASSERT(target);
5170
5171 // Render via a pixmap when dealing with non-opaque painters or printers.
5172 if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
5173 d->render_helper(painter, targetOffset, toBePainted, renderFlags);
5174 d->extra->inRenderWithPainter = inRenderWithPainter;
5175 return;
5176 }
5177
5178 // Set new shared painter.
5179 QPainter *oldPainter = d->sharedPainter();
5180 d->setSharedPainter(painter);
5181
5182 // Save current system clip, viewport and transform,
5183 const QTransform oldTransform = enginePriv->systemTransform;
5184 const QRegion oldSystemClip = enginePriv->systemClip;
5185 const QRegion oldBaseClip = enginePriv->baseSystemClip;
5186 const QRegion oldSystemViewport = enginePriv->systemViewport;
5187 const Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection();
5188
5189 // This ensures that all painting triggered by render() is clipped to the current engine clip.
5190 if (painter->hasClipping()) {
5191 const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
5192 enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
5193 } else {
5194 enginePriv->setSystemViewport(oldSystemClip);
5195 }
5196 painter->setLayoutDirection(layoutDirection());
5197
5198 d->render(target, targetOffset, toBePainted, renderFlags);
5199
5200 // Restore system clip, viewport and transform.
5201 enginePriv->baseSystemClip = oldBaseClip;
5202 enginePriv->setSystemTransformAndViewport(oldTransform, oldSystemViewport);
5203 enginePriv->systemStateChanged();
5204 painter->setLayoutDirection(oldLayoutDirection);
5205
5206 // Restore shared painter.
5207 d->setSharedPainter(oldPainter);
5208
5209 d->extra->inRenderWithPainter = inRenderWithPainter;
5210}
5211
5212static void sendResizeEvents(QWidget *target)
5213{
5214 QResizeEvent e(target->size(), QSize());
5215 QCoreApplication::sendEvent(target, &e);
5216
5217 const QObjectList children = target->children();
5218 for (int i = 0; i < children.size(); ++i) {
5219 if (!children.at(i)->isWidgetType())
5220 continue;
5221 QWidget *child = static_cast<QWidget*>(children.at(i));
5222 if (!child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
5223 sendResizeEvents(child);
5224 }
5225}
5226
5227/*!
5228 \since 5.0
5229
5230 Renders the widget into a pixmap restricted by the
5231 given \a rectangle. If the widget has any children, then
5232 they are also painted in the appropriate positions.
5233
5234 If a rectangle with an invalid size is specified (the default),
5235 the entire widget is painted.
5236
5237 \sa render(), QPixmap
5238*/
5239QPixmap QWidget::grab(const QRect &rectangle)
5240{
5241 Q_D(QWidget);
5242 if (testAttribute(Qt::WA_PendingResizeEvent) || !testAttribute(Qt::WA_WState_Created))
5243 sendResizeEvents(this);
5244
5245 const QWidget::RenderFlags renderFlags = QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask;
5246
5247 const bool oldDirtyOpaqueChildren = d->dirtyOpaqueChildren;
5248 QRect r(rectangle);
5249 if (r.width() < 0 || r.height() < 0) {
5250 // For grabbing widgets that haven't been shown yet,
5251 // we trigger the layouting mechanism to determine the widget's size.
5252 r = d->prepareToRender(QRegion(), renderFlags).boundingRect();
5253 r.setTopLeft(rectangle.topLeft());
5254 }
5255
5256 if (!r.intersects(rect()))
5257 return QPixmap();
5258
5259 const qreal dpr = devicePixelRatio();
5260 QPixmap res((QSizeF(r.size()) * dpr).toSize());
5261 res.setDevicePixelRatio(dpr);
5262 if (!d->isOpaque || res.hasAlphaChannel())
5263 res.fill(Qt::transparent);
5264 d->render(&res, QPoint(), QRegion(r), renderFlags);
5265
5266 d->dirtyOpaqueChildren = oldDirtyOpaqueChildren;
5267 return res;
5268}
5269
5270/*!
5271 \brief The graphicsEffect function returns a pointer to the
5272 widget's graphics effect.
5273
5274 If the widget has no graphics effect, \nullptr is returned.
5275
5276 \since 4.6
5277
5278 \sa setGraphicsEffect()
5279*/
5280#if QT_CONFIG(graphicseffect)
5281QGraphicsEffect *QWidget::graphicsEffect() const
5282{
5283 Q_D(const QWidget);
5284 return d->graphicsEffect;
5285}
5286#endif // QT_CONFIG(graphicseffect)
5287
5288/*!
5289
5290 \brief The setGraphicsEffect function is for setting the widget's graphics effect.
5291
5292 Sets \a effect as the widget's effect. If there already is an effect installed
5293 on this widget, QWidget will delete the existing effect before installing
5294 the new \a effect.
5295
5296 If \a effect is the installed effect on a different widget, setGraphicsEffect() will remove
5297 the effect from the widget and install it on this widget.
5298
5299 QWidget takes ownership of \a effect.
5300
5301 \note This function will apply the effect on itself and all its children.
5302
5303 \note Graphics effects are not supported for OpenGL-based widgets, such as QGLWidget,
5304 QOpenGLWidget and QQuickWidget.
5305
5306 \since 4.6
5307
5308 \sa graphicsEffect()
5309*/
5310#if QT_CONFIG(graphicseffect)
5311void QWidget::setGraphicsEffect(QGraphicsEffect *effect)
5312{
5313 Q_D(QWidget);
5314 if (d->graphicsEffect == effect)
5315 return;
5316
5317 if (d->graphicsEffect) {
5318 d->invalidateBackingStore(rect());
5319 delete d->graphicsEffect;
5320 d->graphicsEffect = nullptr;
5321 }
5322
5323 if (effect) {
5324 // Set new effect.
5325 QGraphicsEffectSourcePrivate *sourced = new QWidgetEffectSourcePrivate(this);
5326 QGraphicsEffectSource *source = new QGraphicsEffectSource(*sourced);
5327 d->graphicsEffect = effect;
5328 effect->d_func()->setGraphicsEffectSource(source);
5329 update();
5330 }
5331
5332 d->updateIsOpaque();
5333}
5334#endif // QT_CONFIG(graphicseffect)
5335
5336bool QWidgetPrivate::isAboutToShow() const
5337{
5338 if (data.in_show)
5339 return true;
5340
5341 Q_Q(const QWidget);
5342 if (q->isHidden())
5343 return false;
5344
5345 // The widget will be shown if any of its ancestors are about to show.
5346 QWidget *parent = q->parentWidget();
5347 return parent ? parent->d_func()->isAboutToShow() : false;
5348}
5349
5350QRegion QWidgetPrivate::prepareToRender(const QRegion &region, QWidget::RenderFlags renderFlags)
5351{
5352 Q_Q(QWidget);
5353 const bool isVisible = q->isVisible();
5354
5355 // Make sure the widget is laid out correctly.
5356 if (!isVisible && !isAboutToShow()) {
5357 QWidget *topLevel = q->window();
5358 (void)topLevel->d_func()->topData(); // Make sure we at least have top-data.
5359 topLevel->ensurePolished();
5360
5361 // Invalidate the layout of hidden ancestors (incl. myself) and pretend
5362 // they're not explicitly hidden.
5363 QWidget *widget = q;
5364 QWidgetList hiddenWidgets;
5365 while (widget) {
5366 if (widget->isHidden()) {
5367 widget->setAttribute(Qt::WA_WState_Hidden, false);
5368 hiddenWidgets.append(widget);
5369 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5370 widget->d_func()->updateGeometry_helper(true);
5371 }
5372 widget = widget->parentWidget();
5373 }
5374
5375 // Activate top-level layout.
5376 if (topLevel->d_func()->layout)
5377 topLevel->d_func()->layout->activate();
5378
5379 // Adjust size if necessary.
5380 QTLWExtra *topLevelExtra = topLevel->d_func()->maybeTopData();
5381 if (topLevelExtra && !topLevelExtra->sizeAdjusted
5382 && !topLevel->testAttribute(Qt::WA_Resized)) {
5383 topLevel->adjustSize();
5384 topLevel->setAttribute(Qt::WA_Resized, false);
5385 }
5386
5387 // Activate child layouts.
5388 topLevel->d_func()->activateChildLayoutsRecursively();
5389
5390 // We're not cheating with WA_WState_Hidden anymore.
5391 for (int i = 0; i < hiddenWidgets.size(); ++i) {
5392 QWidget *widget = hiddenWidgets.at(i);
5393 widget->setAttribute(Qt::WA_WState_Hidden);
5394 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5395 widget->parentWidget()->d_func()->layout->invalidate();
5396 }
5397 } else if (isVisible) {
5398 q->window()->d_func()->sendPendingMoveAndResizeEvents(true, true);
5399 }
5400
5401 // Calculate the region to be painted.
5402 QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
5403 if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
5404 toBePainted &= extra->mask;
5405 return toBePainted;
5406}
5407
5408void QWidgetPrivate::render_helper(QPainter *painter, const QPoint &targetOffset, const QRegion &toBePainted,
5409 QWidget::RenderFlags renderFlags)
5410{
5411 Q_ASSERT(painter);
5412 Q_ASSERT(!toBePainted.isEmpty());
5413
5414 Q_Q(QWidget);
5415 const QTransform originalTransform = painter->worldTransform();
5416 const bool useDeviceCoordinates = originalTransform.isScaling();
5417 if (!useDeviceCoordinates) {
5418 // Render via a pixmap.
5419 const QRect rect = toBePainted.boundingRect();
5420 const QSize size = rect.size();
5421 if (size.isNull())
5422 return;
5423
5424 const qreal pixmapDevicePixelRatio = painter->device()->devicePixelRatio();
5425 QPixmap pixmap(size * pixmapDevicePixelRatio);
5426 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio);
5427
5428 if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
5429 pixmap.fill(Qt::transparent);
5430 q->render(&pixmap, QPoint(), toBePainted, renderFlags);
5431
5432 const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
5433 painter->setRenderHints(QPainter::SmoothPixmapTransform, true);
5434
5435 painter->drawPixmap(targetOffset, pixmap);
5436
5437 if (restore)
5438 painter->setRenderHints(QPainter::SmoothPixmapTransform, false);
5439
5440 } else {
5441 // Render via a pixmap in device coordinates (to avoid pixmap scaling).
5442 QTransform transform = originalTransform;
5443 transform.translate(targetOffset.x(), targetOffset.y());
5444
5445 QPaintDevice *device = painter->device();
5446 Q_ASSERT(device);
5447
5448 // Calculate device rect.
5449 const QRectF rect(toBePainted.boundingRect());
5450 QRect deviceRect = transform.mapRect(QRectF(0, 0, rect.width(), rect.height())).toAlignedRect();
5451 deviceRect &= QRect(0, 0, device->width(), device->height());
5452
5453 QPixmap pixmap(deviceRect.size());
5454 pixmap.fill(Qt::transparent);
5455
5456 // Create a pixmap device coordinate painter.
5457 QPainter pixmapPainter(&pixmap);
5458 pixmapPainter.setRenderHints(painter->renderHints());
5459 transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
5460 pixmapPainter.setTransform(transform);
5461
5462 q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
5463 pixmapPainter.end();
5464
5465 // And then draw the pixmap.
5466 painter->setTransform(QTransform());
5467 painter->drawPixmap(deviceRect.topLeft(), pixmap);
5468 painter->setTransform(originalTransform);
5469 }
5470}
5471
5472void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QPoint &offset, DrawWidgetFlags flags,
5473 QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5474{
5475 if (rgn.isEmpty())
5476 return;
5477
5478 Q_Q(QWidget);
5479
5480 qCInfo(lcWidgetPainting) << "Drawing" << rgn << "of" << q << "at" << offset
5481 << "into paint device" << pdev << "with" << flags;
5482
5483 const bool asRoot = flags & DrawAsRoot;
5484 bool onScreen = shouldPaintOnScreen();
5485
5486#if QT_CONFIG(graphicseffect)
5487 if (graphicsEffect && graphicsEffect->isEnabled()) {
5488 QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
5489 QWidgetEffectSourcePrivate *sourced = static_cast<QWidgetEffectSourcePrivate *>
5490 (source->d_func());
5491 if (!sourced->context) {
5492 const QRegion effectRgn((flags & UseEffectRegionBounds) ? rgn.boundingRect() : rgn);
5493 QWidgetPaintContext context(pdev, effectRgn, offset, flags, sharedPainter, repaintManager);
5494 sourced->context = &context;
5495 if (!sharedPainter) {
5496 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), effectRgn.translated(offset));
5497 QPainter p(pdev);
5498 p.translate(offset);
5499 context.painter = &p;
5500 graphicsEffect->draw(&p);
5501 setSystemClip(pdev->paintEngine(), 1, QRegion());
5502 } else {
5503 context.painter = sharedPainter;
5504 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
5505 sourced->invalidateCache();
5506 sourced->lastEffectTransform = sharedPainter->worldTransform();
5507 }
5508 sharedPainter->save();
5509 sharedPainter->translate(offset);
5510 setSystemClip(sharedPainter->paintEngine(), sharedPainter->device()->devicePixelRatio(), effectRgn.translated(offset));
5511 graphicsEffect->draw(sharedPainter);
5512 setSystemClip(sharedPainter->paintEngine(), 1, QRegion());
5513 sharedPainter->restore();
5514 }
5515 sourced->context = nullptr;
5516
5517 if (repaintManager)
5518 repaintManager->markNeedsFlush(q, effectRgn, offset);
5519
5520 return;
5521 }
5522 }
5523#endif // QT_CONFIG(graphicseffect)
5524 flags = flags & ~UseEffectRegionBounds;
5525
5526 const bool alsoOnScreen = flags & DrawPaintOnScreen;
5527 const bool recursive = flags & DrawRecursive;
5528 const bool alsoInvisible = flags & DrawInvisible;
5529
5530 Q_ASSERT(sharedPainter ? sharedPainter->isActive() : true);
5531
5532 QRegion toBePainted(rgn);
5533 if (asRoot && !alsoInvisible)
5534 toBePainted &= clipRect(); //(rgn & visibleRegion());
5535 if (!(flags & DontSubtractOpaqueChildren))
5536 subtractOpaqueChildren(toBePainted, q->rect());
5537
5538 if (!toBePainted.isEmpty()) {
5539 if (!onScreen || alsoOnScreen) {
5540 //update the "in paint event" flag
5541 if (Q_UNLIKELY(q->testAttribute(Qt::WA_WState_InPaintEvent)))
5542 qWarning("QWidget::repaint: Recursive repaint detected");
5543 q->setAttribute(Qt::WA_WState_InPaintEvent);
5544
5545 //clip away the new area
5546 QPaintEngine *paintEngine = pdev->paintEngine();
5547 if (paintEngine) {
5548 setRedirected(pdev, -offset);
5549
5550 if (sharedPainter)
5551 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted);
5552 else
5553 paintEngine->d_func()->systemRect = q->data->crect;
5554
5555 //paint the background
5556 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
5557 && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
5558 beginBackingStorePainting();
5559 QPainter p(q);
5560 p.setRenderHint(QPainter::SmoothPixmapTransform);
5561 paintBackground(&p, toBePainted, (asRoot || onScreen) ? (flags | DrawAsRoot) : DrawWidgetFlags());
5562 endBackingStorePainting();
5563 }
5564
5565 if (!sharedPainter)
5566 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted.translated(offset));
5567
5568 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
5569 beginBackingStorePainting();
5570 QPainter p(q);
5571 QColor tint = q->palette().window().color();
5572 tint.setAlphaF(.6f);
5573 p.fillRect(toBePainted.boundingRect(), tint);
5574 endBackingStorePainting();
5575 }
5576 }
5577
5578#if 0
5579 qDebug() << "painting" << q << "opaque ==" << isOpaque();
5580 qDebug() << "clipping to" << toBePainted << "location == " << offset
5581 << "geometry ==" << QRect(q->mapTo(q->window(), QPoint(0, 0)), q->size());
5582#endif
5583
5584 bool skipPaintEvent = false;
5585 if (renderToTexture) {
5586 // This widget renders into a texture which is composed later. We just need to
5587 // punch a hole in the backingstore, so the texture will be visible.
5588 beginBackingStorePainting();
5589 if (!q->testAttribute(Qt::WA_AlwaysStackOnTop) && repaintManager) {
5590 QPainter p(q);
5591 p.setCompositionMode(QPainter::CompositionMode_Source);
5592 p.fillRect(q->rect(), Qt::transparent);
5593 } else if (!repaintManager) {
5594 // We are not drawing to a backingstore: fall back to QImage
5595 QImage img = grabFramebuffer();
5596 // grabFramebuffer() always sets the format to RGB32
5597 // regardless of whether it is transparent or not.
5598 if (img.format() == QImage::Format_RGB32)
5599 img.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied);
5600 QPainter p(q);
5601 p.drawImage(q->rect(), img);
5602 skipPaintEvent = true;
5603 }
5604 endBackingStorePainting();
5605 if (renderToTextureReallyDirty)
5606 renderToTextureReallyDirty = 0;
5607 else
5608 skipPaintEvent = true;
5609 }
5610
5611 if (!skipPaintEvent) {
5612 //actually send the paint event
5613 sendPaintEvent(toBePainted);
5614 }
5615
5616 if (repaintManager)
5617 repaintManager->markNeedsFlush(q, toBePainted, offset);
5618
5619 //restore
5620 if (paintEngine) {
5621 restoreRedirected();
5622 if (!sharedPainter)
5623 paintEngine->d_func()->systemRect = QRect();
5624 else
5625 paintEngine->d_func()->currentClipDevice = nullptr;
5626
5627 setSystemClip(pdev->paintEngine(), 1, QRegion());
5628 }
5629 q->setAttribute(Qt::WA_WState_InPaintEvent, false);
5630 if (Q_UNLIKELY(q->paintingActive()))
5631 qWarning("QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
5632
5633 if (paintEngine && paintEngine->autoDestruct()) {
5634 delete paintEngine;
5635 }
5636 } else if (q->isWindow()) {
5637 QPaintEngine *engine = pdev->paintEngine();
5638 if (engine) {
5639 QPainter p(pdev);
5640 p.setClipRegion(toBePainted);
5641 const QBrush bg = q->palette().brush(QPalette::Window);
5642 if (bg.style() == Qt::TexturePattern)
5643 p.drawTiledPixmap(q->rect(), bg.texture());
5644 else
5645 p.fillRect(q->rect(), bg);
5646
5647 if (engine->autoDestruct())
5648 delete engine;
5649 }
5650 }
5651 }
5652
5653 if (recursive && !children.isEmpty()) {
5654 paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot,
5655 sharedPainter, repaintManager);
5656 }
5657}
5658
5659void QWidgetPrivate::sendPaintEvent(const QRegion &toBePainted)
5660{
5661 Q_Q(QWidget);
5662 QPaintEvent e(toBePainted);
5663 QCoreApplication::sendSpontaneousEvent(q, &e);
5664
5665 if (renderToTexture)
5666 resolveSamples();
5667}
5668
5669void QWidgetPrivate::render(QPaintDevice *target, const QPoint &targetOffset,
5670 const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
5671{
5672 if (Q_UNLIKELY(!target)) {
5673 qWarning("QWidget::render: null pointer to paint device");
5674 return;
5675 }
5676
5677 const bool inRenderWithPainter = extra && extra->inRenderWithPainter;
5678 QRegion paintRegion = !inRenderWithPainter
5679 ? prepareToRender(sourceRegion, renderFlags)
5680 : sourceRegion;
5681 if (paintRegion.isEmpty())
5682 return;
5683
5684 QPainter *oldSharedPainter = inRenderWithPainter ? sharedPainter() : nullptr;
5685
5686 // Use the target's shared painter if set (typically set when doing
5687 // "other->render(widget);" in the widget's paintEvent.
5688 if (target->devType() == QInternal::Widget) {
5689 QWidgetPrivate *targetPrivate = static_cast<QWidget *>(target)->d_func();
5690 if (targetPrivate->extra && targetPrivate->extra->inRenderWithPainter) {
5691 QPainter *targetPainter = targetPrivate->sharedPainter();
5692 if (targetPainter && targetPainter->isActive())
5693 setSharedPainter(targetPainter);
5694 }
5695 }
5696
5697 // Use the target's redirected device if set and adjust offset and paint
5698 // region accordingly. This is typically the case when people call render
5699 // from the paintEvent.
5700 QPoint offset = targetOffset;
5701 offset -= paintRegion.boundingRect().topLeft();
5702 QPoint redirectionOffset;
5703 QPaintDevice *redirected = nullptr;
5704
5705 if (target->devType() == QInternal::Widget)
5706 redirected = static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
5707
5708 if (redirected) {
5709 target = redirected;
5710 offset -= redirectionOffset;
5711 }
5712
5713 if (!inRenderWithPainter) { // Clip handled by shared painter (in qpainter.cpp).
5714 if (QPaintEngine *targetEngine = target->paintEngine()) {
5715 const QRegion targetSystemClip = targetEngine->systemClip();
5716 if (!targetSystemClip.isEmpty())
5717 paintRegion &= targetSystemClip.translated(-offset);
5718 }
5719 }
5720
5721 // Set backingstore flags.
5722 DrawWidgetFlags flags = DrawPaintOnScreen | DrawInvisible;
5723 if (renderFlags & QWidget::DrawWindowBackground)
5724 flags |= DrawAsRoot;
5725
5726 if (renderFlags & QWidget::DrawChildren)
5727 flags |= DrawRecursive;
5728 else
5729 flags |= DontSubtractOpaqueChildren;
5730
5731 flags |= DontSetCompositionMode;
5732
5733 // Render via backingstore.
5734 drawWidget(target, paintRegion, offset, flags, sharedPainter());
5735
5736 // Restore shared painter.
5737 if (oldSharedPainter)
5738 setSharedPainter(oldSharedPainter);
5739}
5740
5741void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev, const QObjectList& siblings, int index, const QRegion &rgn,
5742 const QPoint &offset, DrawWidgetFlags flags
5743 , QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5744{
5745 QWidget *w = nullptr;
5746 QRect boundingRect;
5747 bool dirtyBoundingRect = true;
5748 const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
5749 const bool excludeNativeChildren = (flags & DontDrawNativeChildren);
5750
5751 do {
5752 QWidget *x = qobject_cast<QWidget*>(siblings.at(index));
5753 if (x && !(exludeOpaqueChildren && x->d_func()->isOpaque) && !x->isHidden() && !x->isWindow()
5754 && !(excludeNativeChildren && x->internalWinId())) {
5755 if (dirtyBoundingRect) {
5756 boundingRect = rgn.boundingRect();
5757 dirtyBoundingRect = false;
5758 }
5759
5760 if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
5761 w = x;
5762 break;
5763 }
5764 }
5765 --index;
5766 } while (index >= 0);
5767
5768 if (!w)
5769 return;
5770
5771 QWidgetPrivate *wd = w->d_func();
5772 const QPoint widgetPos(w->data->crect.topLeft());
5773 const bool hasMask = wd->extra && wd->extra->hasMask && !wd->graphicsEffect;
5774 if (index > 0) {
5775 QRegion wr(rgn);
5776 if (wd->isOpaque)
5777 wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
5778 paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags,
5779 sharedPainter, repaintManager);
5780 }
5781
5782 if (w->updatesEnabled()
5783#if QT_CONFIG(graphicsview)
5784 && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
5785#endif // QT_CONFIG(graphicsview)
5786 ) {
5787 QRegion wRegion(rgn);
5788 wRegion &= wd->effectiveRectFor(w->data->crect);
5789 wRegion.translate(-widgetPos);
5790 if (hasMask)
5791 wRegion &= wd->extra->mask;
5792 wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, repaintManager);
5793 }
5794}
5795
5796#if QT_CONFIG(graphicseffect)
5797QRectF QWidgetEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system) const
5798{
5799 if (system != Qt::DeviceCoordinates)
5800 return m_widget->rect();
5801
5802 if (Q_UNLIKELY(!context)) {
5803 // Device coordinates without context not yet supported.
5804 qWarning("QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
5805 return QRectF();
5806 }
5807
5808 return context->painter->worldTransform().mapRect(m_widget->rect());
5809}
5810
5811void QWidgetEffectSourcePrivate::draw(QPainter *painter)
5812{
5813 if (!context || context->painter != painter) {
5814 m_widget->render(painter);
5815 return;
5816 }
5817
5818 // The region saved in the context is neither clipped to the rect
5819 // nor the mask, so we have to clip it here before calling drawWidget.
5820 QRegion toBePainted = context->rgn;
5821 toBePainted &= m_widget->rect();
5822 QWidgetPrivate *wd = qt_widget_private(m_widget);
5823 if (wd->extra && wd->extra->hasMask)
5824 toBePainted &= wd->extra->mask;
5825
5826 wd->drawWidget(context->pdev, toBePainted, context->offset, context->flags,
5827 context->sharedPainter, context->repaintManager);
5828}
5829
5830QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
5831 QGraphicsEffect::PixmapPadMode mode) const
5832{
5833 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
5834 if (Q_UNLIKELY(!context && deviceCoordinates)) {
5835 // Device coordinates without context not yet supported.
5836 qWarning("QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
5837 return QPixmap();
5838 }
5839
5840 QPoint pixmapOffset;
5841 QRectF sourceRect = m_widget->rect();
5842
5843 if (deviceCoordinates) {
5844 const QTransform &painterTransform = context->painter->worldTransform();
5845 sourceRect = painterTransform.mapRect(sourceRect);
5846 pixmapOffset = painterTransform.map(pixmapOffset);
5847 }
5848
5849 QRect effectRect;
5850
5851 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect)
5852 effectRect = m_widget->graphicsEffect()->boundingRectFor(sourceRect).toAlignedRect();
5853 else if (mode == QGraphicsEffect::PadToTransparentBorder)
5854 effectRect = sourceRect.adjusted(-1, -1, 1, 1).toAlignedRect();
5855 else
5856 effectRect = sourceRect.toAlignedRect();
5857
5858 if (offset)
5859 *offset = effectRect.topLeft();
5860
5861 pixmapOffset -= effectRect.topLeft();
5862
5863 qreal dpr(1.0);
5864 if (const auto *paintDevice = context->painter->device())
5865 dpr = paintDevice->devicePixelRatio();
5866 else
5867 qWarning("QWidgetEffectSourcePrivate::pixmap: Painter not active");
5868 QPixmap pixmap(effectRect.size() * dpr);
5869 pixmap.setDevicePixelRatio(dpr);
5870
5871 pixmap.fill(Qt::transparent);
5872 m_widget->render(&pixmap, pixmapOffset, QRegion(), QWidget::DrawChildren);
5873 return pixmap;
5874}
5875#endif // QT_CONFIG(graphicseffect)
5876
5877#if QT_CONFIG(graphicsview)
5878/*!
5879 \internal
5880
5881 Finds the nearest widget embedded in a graphics proxy widget along the chain formed by this
5882 widget and its ancestors. The search starts at \a origin (inclusive).
5883 If successful, the function returns the proxy that embeds the widget, or \nullptr if no
5884 embedded widget was found.
5885*/
5886QGraphicsProxyWidget *QWidgetPrivate::nearestGraphicsProxyWidget(const QWidget *origin)
5887{
5888 if (origin) {
5889 const auto &extra = origin->d_func()->extra;
5890 if (extra && extra->proxyWidget)
5891 return extra->proxyWidget;
5892 return nearestGraphicsProxyWidget(origin->parentWidget());
5893 }
5894 return nullptr;
5895}
5896#endif
5897
5898/*!
5899 \property QWidget::locale
5900 \brief the widget's locale
5901 \since 4.3
5902
5903 As long as no special locale has been set, this is either
5904 the parent's locale or (if this widget is a top level widget),
5905 the default locale.
5906
5907 If the widget displays dates or numbers, these should be formatted
5908 using the widget's locale.
5909
5910 \sa QLocale, QLocale::setDefault()
5911*/
5912
5913void QWidgetPrivate::setLocale_helper(const QLocale &loc, bool forceUpdate)
5914{
5915 Q_Q(QWidget);
5916 if (locale == loc && !forceUpdate)
5917 return;
5918
5919 locale = loc;
5920
5921 if (!children.isEmpty()) {
5922 for (int i = 0; i < children.size(); ++i) {
5923 QWidget *w = qobject_cast<QWidget*>(children.at(i));
5924 if (!w)
5925 continue;
5926 if (w->testAttribute(Qt::WA_SetLocale))
5927 continue;
5928 if (w->isWindow() && !w->testAttribute(Qt::WA_WindowPropagation))
5929 continue;
5930 w->d_func()->setLocale_helper(loc, forceUpdate);
5931 }
5932 }
5933 QEvent e(QEvent::LocaleChange);
5934 QCoreApplication::sendEvent(q, &e);
5935}
5936
5937void QWidget::setLocale(const QLocale &locale)
5938{
5939 Q_D(QWidget);
5940
5941 setAttribute(Qt::WA_SetLocale);
5942 d->setLocale_helper(locale);
5943}
5944
5945QLocale QWidget::locale() const
5946{
5947 Q_D(const QWidget);
5948
5949 return d->locale;
5950}
5951
5952void QWidgetPrivate::resolveLocale()
5953{
5954 Q_Q(const QWidget);
5955
5956 if (!q->testAttribute(Qt::WA_SetLocale)) {
5957 QWidget *parent = q->parentWidget();
5958 setLocale_helper(!parent || (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
5959 ? QLocale() : parent->locale());
5960 }
5961}
5962
5963void QWidget::unsetLocale()
5964{
5965 Q_D(QWidget);
5966 setAttribute(Qt::WA_SetLocale, false);
5967 d->resolveLocale();
5968}
5969
5970/*!
5971 \property QWidget::windowTitle
5972 \brief the window title (caption)
5973
5974 This property only makes sense for top-level widgets, such as
5975 windows and dialogs. If no caption has been set, the title is based of the
5976 \l windowFilePath. If neither of these is set, then the title is
5977 an empty string.
5978
5979 If you use the \l windowModified mechanism, the window title must
5980 contain a "[*]" placeholder, which indicates where the '*' should
5981 appear. Normally, it should appear right after the file name
5982 (e.g., "document1.txt[*] - Text Editor"). If the \l
5983 windowModified property is \c false (the default), the placeholder
5984 is simply removed.
5985
5986 On some desktop platforms (including Windows and Unix), the application name
5987 (from QGuiApplication::applicationDisplayName) is added at the end of the
5988 window title, if set. This is done by the QPA plugin, so it is shown to the
5989 user, but isn't part of the windowTitle string.
5990
5991 \sa windowIcon, windowModified, windowFilePath
5992*/
5993QString QWidget::windowTitle() const
5994{
5995 Q_D(const QWidget);
5996 if (d->extra && d->extra->topextra) {
5997 if (!d->extra->topextra->caption.isEmpty())
5998 return d->extra->topextra->caption;
5999 if (!d->extra->topextra->filePath.isEmpty())
6000 return QFileInfo(d->extra->topextra->filePath).fileName() + "[*]"_L1;
6001 }
6002 return QString();
6003}
6004
6005/*!
6006 Returns a modified window title with the [*] place holder
6007 replaced according to the rules described in QWidget::setWindowTitle
6008
6009 This function assumes that "[*]" can be quoted by another
6010 "[*]", so it will replace two place holders by one and
6011 a single last one by either "*" or nothing depending on
6012 the modified flag.
6013
6014 \internal
6015*/
6016QString qt_setWindowTitle_helperHelper(const QString &title, const QWidget *widget)
6017{
6018 Q_ASSERT(widget);
6019
6020 QString cap = title;
6021 if (cap.isEmpty())
6022 return cap;
6023
6024 const auto placeHolder = "[*]"_L1;
6025 int index = cap.indexOf(placeHolder);
6026
6027 // here the magic begins
6028 while (index != -1) {
6029 index += placeHolder.size();
6030 int count = 1;
6031 while (cap.indexOf(placeHolder, index) == index) {
6032 ++count;
6033 index += placeHolder.size();
6034 }
6035
6036 if (count%2) { // odd number of [*] -> replace last one
6037 int lastIndex = cap.lastIndexOf(placeHolder, index - 1);
6038 if (widget->isWindowModified()
6039 && widget->style()->styleHint(QStyle::SH_TitleBar_ModifyNotification, nullptr, widget))
6040 cap.replace(lastIndex, 3, QWidget::tr("*"));
6041 else
6042 cap.remove(lastIndex, 3);
6043 }
6044
6045 index = cap.indexOf(placeHolder, index);
6046 }
6047
6048 cap.replace("[*][*]"_L1, placeHolder);
6049
6050 return cap;
6051}
6052
6053void QWidgetPrivate::setWindowTitle_helper(const QString &title)
6054{
6055 Q_Q(QWidget);
6056 if (q->testAttribute(Qt::WA_WState_Created))
6057 setWindowTitle_sys(qt_setWindowTitle_helperHelper(title, q));
6058}
6059
6060void QWidgetPrivate::setWindowTitle_sys(const QString &caption)
6061{
6062 Q_Q(QWidget);
6063 if (!q->isWindow())
6064 return;
6065
6066 if (QWindow *window = q->windowHandle())
6067 {
6068#if QT_CONFIG(accessibility)
6069 QString oldAccessibleName;
6070 const QAccessibleInterface *accessible = QAccessible::isActive()
6071 ? QAccessible::queryAccessibleInterface(q)
6072 : nullptr;
6073 if (accessible)
6074 oldAccessibleName = accessible->text(QAccessible::Name);
6075#endif
6076
6077 window->setTitle(caption);
6078
6079#if QT_CONFIG(accessibility)
6080 if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) {
6081 QAccessibleEvent event(q, QAccessible::NameChanged);
6082 QAccessible::updateAccessibility(&event);
6083 }
6084#endif
6085 }
6086}
6087
6088void QWidgetPrivate::setWindowIconText_helper(const QString &title)
6089{
6090 Q_Q(QWidget);
6091 if (q->testAttribute(Qt::WA_WState_Created))
6092 setWindowIconText_sys(qt_setWindowTitle_helperHelper(title, q));
6093}
6094
6095void QWidgetPrivate::setWindowIconText_sys(const QString &iconText)
6096{
6097#if QT_CONFIG(xcb)
6098 Q_Q(QWidget);
6099 // ### The QWidget property is deprecated, but the XCB window function is not.
6100 // It should remain available for the rare application that needs it.
6101 if (QWindow *window = q->windowHandle()) {
6102 if (auto *xcbWindow = dynamic_cast<QXcbWindow*>(window->handle()))
6103 xcbWindow->setWindowIconText(iconText);
6104 }
6105#else
6106 Q_UNUSED(iconText);
6107#endif
6108}
6109
6110/*!
6111 \fn void QWidget::windowIconTextChanged(const QString &iconText)
6112
6113 This signal is emitted when the window's icon text has changed, with the
6114 new \a iconText as an argument.
6115
6116 \since 5.2
6117 \deprecated
6118
6119 This signal is deprecated.
6120*/
6121
6122void QWidget::setWindowIconText(const QString &iconText)
6123{
6124 if (QWidget::windowIconText() == iconText)
6125 return;
6126
6127 Q_D(QWidget);
6128 d->topData()->iconText = iconText;
6129 d->setWindowIconText_helper(iconText);
6130
6131 QEvent e(QEvent::IconTextChange);
6132 QCoreApplication::sendEvent(this, &e);
6133
6134 emit windowIconTextChanged(iconText);
6135}
6136
6137/*!
6138 \fn void QWidget::windowTitleChanged(const QString &title)
6139
6140 This signal is emitted when the window's title has changed, with the
6141 new \a title as an argument.
6142
6143 \since 5.2
6144*/
6145
6146void QWidget::setWindowTitle(const QString &title)
6147{
6148 if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
6149 return;
6150
6151 Q_D(QWidget);
6152 d->topData()->caption = title;
6153 d->setWindowTitle_helper(title);
6154
6155 QEvent e(QEvent::WindowTitleChange);
6156 QCoreApplication::sendEvent(this, &e);
6157
6158 emit windowTitleChanged(title);
6159}
6160
6161
6162/*!
6163 \property QWidget::windowIcon
6164 \brief the widget's icon
6165
6166 This property only makes sense for windows. If no icon
6167 has been set, windowIcon() returns the application icon
6168 (QApplication::windowIcon()).
6169
6170 \note On \macos, window icons represent the active document,
6171 and will not be displayed unless a file path has also been
6172 set using setWindowFilePath.
6173
6174 \sa windowTitle, setWindowFilePath
6175*/
6176QIcon QWidget::windowIcon() const
6177{
6178 const QWidget *w = this;
6179 while (w) {
6180 const QWidgetPrivate *d = w->d_func();
6181 if (d->extra && d->extra->topextra && d->extra->topextra->icon)
6182 return *d->extra->topextra->icon;
6183 w = w->parentWidget();
6184 }
6185 return QApplication::windowIcon();
6186}
6187
6188void QWidgetPrivate::setWindowIcon_helper()
6189{
6190 Q_Q(QWidget);
6191 QEvent e(QEvent::WindowIconChange);
6192
6193 // Do not send the event if the widget is a top level.
6194 // In that case, setWindowIcon_sys does it, and event propagation from
6195 // QWidgetWindow to the top level QWidget ensures that the event reaches
6196 // the top level anyhow
6197 if (!q->windowHandle())
6198 QCoreApplication::sendEvent(q, &e);
6199 for (int i = 0; i < children.size(); ++i) {
6200 QWidget *w = qobject_cast<QWidget *>(children.at(i));
6201 if (w && !w->isWindow())
6202 QCoreApplication::sendEvent(w, &e);
6203 }
6204}
6205
6206/*!
6207 \fn void QWidget::windowIconChanged(const QIcon &icon)
6208
6209 This signal is emitted when the window's icon has changed, with the
6210 new \a icon as an argument.
6211
6212 \since 5.2
6213*/
6214
6215void QWidget::setWindowIcon(const QIcon &icon)
6216{
6217 Q_D(QWidget);
6218
6219 setAttribute(Qt::WA_SetWindowIcon, !icon.isNull());
6220 d->createTLExtra();
6221
6222 if (!d->extra->topextra->icon)
6223 d->extra->topextra->icon = std::make_unique<QIcon>(icon);
6224 else
6225 *d->extra->topextra->icon = icon;
6226
6227 d->setWindowIcon_sys();
6228 d->setWindowIcon_helper();
6229
6230 emit windowIconChanged(icon);
6231}
6232
6233void QWidgetPrivate::setWindowIcon_sys()
6234{
6235 Q_Q(QWidget);
6236 if (QWindow *window = q->windowHandle())
6237 window->setIcon(q->windowIcon());
6238}
6239
6240/*!
6241 \property QWidget::windowIconText
6242 \brief the text to be displayed on the icon of a minimized window
6243
6244 This property only makes sense for windows. If no icon
6245 text has been set, this accessor returns an empty string.
6246 It is only implemented on the X11 platform, and only certain
6247 window managers use this window property.
6248
6249 \deprecated
6250 This property is deprecated.
6251
6252 \sa windowIcon, windowTitle
6253*/
6254
6255QString QWidget::windowIconText() const
6256{
6257 Q_D(const QWidget);
6258 return (d->extra && d->extra->topextra) ? d->extra->topextra->iconText : QString();
6259}
6260
6261/*!
6262 \property QWidget::windowFilePath
6263 \since 4.4
6264 \brief the file path associated with a widget
6265
6266 This property only makes sense for windows. It associates a file path with
6267 a window. If you set the file path, but have not set the window title, Qt
6268 sets the window title to the file name of the specified path, obtained using
6269 QFileInfo::fileName().
6270
6271 If the window title is set at any point, then the window title takes precedence and
6272 will be shown instead of the file path string.
6273
6274 Additionally, on \macos, this has an added benefit that it sets the
6275 \l{http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/XHIGWindows/chapter_17_section_3.html}{proxy icon}
6276 for the window, assuming that the file path exists.
6277
6278 If no file path is set, this property contains an empty string.
6279
6280 By default, this property contains an empty string.
6281
6282 \sa windowTitle, windowIcon
6283*/
6284
6285QString QWidget::windowFilePath() const
6286{
6287 Q_D(const QWidget);
6288 return (d->extra && d->extra->topextra) ? d->extra->topextra->filePath : QString();
6289}
6290
6291void QWidget::setWindowFilePath(const QString &filePath)
6292{
6293 if (filePath == windowFilePath())
6294 return;
6295
6296 Q_D(QWidget);
6297
6298 d->createTLExtra();
6299 d->extra->topextra->filePath = filePath;
6300 d->setWindowFilePath_helper(filePath);
6301}
6302
6303void QWidgetPrivate::setWindowFilePath_helper(const QString &filePath)
6304{
6305 if (extra->topextra && extra->topextra->caption.isEmpty()) {
6306#ifdef Q_OS_MACOS
6307 setWindowTitle_helper(QFileInfo(filePath).fileName());
6308#else
6309 Q_Q(QWidget);
6310 Q_UNUSED(filePath);
6311 setWindowTitle_helper(q->windowTitle());
6312#endif
6313 }
6314#ifdef Q_OS_MACOS
6315 setWindowFilePath_sys(filePath);
6316#endif
6317}
6318
6319void QWidgetPrivate::setWindowFilePath_sys(const QString &filePath)
6320{
6321 Q_Q(QWidget);
6322 if (!q->isWindow())
6323 return;
6324
6325 if (QWindow *window = q->windowHandle())
6326 window->setFilePath(filePath);
6327}
6328
6329/*!
6330 Returns the window's role, or an empty string.
6331
6332 \sa windowIcon, windowTitle
6333*/
6334
6335QString QWidget::windowRole() const
6336{
6337 Q_D(const QWidget);
6338 return (d->extra && d->extra->topextra) ? d->extra->topextra->role : QString();
6339}
6340
6341/*!
6342 Sets the window's role to \a role. This only makes sense for
6343 windows on X11.
6344*/
6345void QWidget::setWindowRole(const QString &role)
6346{
6347#if QT_CONFIG(xcb) || QT_CONFIG(wayland)
6348 Q_D(QWidget);
6349 d->createTLExtra();
6350 d->topData()->role = role;
6351#else
6352 Q_UNUSED(role);
6353#endif
6354
6355 if (windowHandle()) {
6356#if QT_CONFIG(xcb)
6357 if (auto *xcbWindow = dynamic_cast<QXcbWindow*>(windowHandle()->handle()))
6358 xcbWindow->setWindowRole(role);
6359#endif
6360#if QT_CONFIG(wayland)
6361 if (auto *waylandWindow = dynamic_cast<QWaylandWindow*>(windowHandle()->handle()))
6362 waylandWindow->setSessionRestoreId(role);
6363#endif
6364 }
6365}
6366
6367/*!
6368 \property QWidget::mouseTracking
6369 \brief whether mouse tracking is enabled for the widget
6370
6371 If mouse tracking is disabled (the default), the widget only
6372 receives mouse move events when at least one mouse button is
6373 pressed while the mouse is being moved.
6374
6375 If mouse tracking is enabled, the widget receives mouse move
6376 events even if no buttons are pressed.
6377
6378 \sa mouseMoveEvent()
6379*/
6380
6381/*!
6382 \property QWidget::tabletTracking
6383 \brief whether tablet tracking is enabled for the widget
6384 \since 5.9
6385
6386 If tablet tracking is disabled (the default), the widget only
6387 receives tablet move events when the stylus is in contact with
6388 the tablet, or at least one stylus button is pressed,
6389 while the stylus is being moved.
6390
6391 If tablet tracking is enabled, the widget receives tablet move
6392 events even while hovering in proximity. This is useful for
6393 monitoring position as well as the auxiliary properties such
6394 as rotation and tilt, and providing feedback in the UI.
6395
6396 \sa tabletEvent()
6397*/
6398
6399
6400/*!
6401 Sets the widget's focus proxy to widget \a w. If \a w is \nullptr, the
6402 function resets this widget to have no focus proxy.
6403
6404 Some widgets can "have focus", but create a child widget, such as
6405 QLineEdit, to actually handle the focus. In this case, the widget
6406 can set the line edit to be its focus proxy.
6407
6408 setFocusProxy() sets the widget which will actually get focus when
6409 "this widget" gets it. If there is a focus proxy, setFocus() and
6410 hasFocus() operate on the focus proxy. If "this widget" is the focus
6411 widget, then setFocusProxy() moves focus to the new focus proxy.
6412
6413 \sa focusProxy()
6414*/
6415
6416void QWidget::setFocusProxy(QWidget * w)
6417{
6418 Q_D(QWidget);
6419 if (!w && !d->extra)
6420 return;
6421
6422 for (QWidget* fp = w; fp; fp = fp->focusProxy()) {
6423 if (Q_UNLIKELY(fp == this)) {
6424 qWarning("QWidget: %s (%s) already in focus proxy chain", metaObject()->className(), objectName().toLocal8Bit().constData());
6425 return;
6426 }
6427 }
6428
6429 const bool moveFocusToProxy = (QApplicationPrivate::focus_widget == this);
6430
6431 d->createExtra();
6432 d->extra->focus_proxy = w;
6433
6434 if (w && isAncestorOf(w)) {
6435 // If the focus proxy is a child of this (so this is a compound widget), then
6436 // we need to make sure that this widget is immediately in front of its own children
6437 // in the focus chain. Otherwise focusNextPrev_helper might jump over unrelated
6438 // widgets that are positioned between this compound widget, and its proxy in
6439 // the focus chain.
6440 const QWidget *parentOfW = w->parentWidget();
6441 Q_ASSERT(parentOfW); // can't be nullptr since we are an ancestor of w
6442 QWidget *firstChild = nullptr;
6443 const auto childList = children();
6444 for (QObject *child : childList) {
6445 if ((firstChild = qobject_cast<QWidget *>(child)))
6446 break;
6447 }
6448 Q_ASSERT(firstChild); // can't be nullptr since w is a child
6449 d->insertIntoFocusChainBefore(firstChild);
6450 } else if (w && w->isAncestorOf(this)) {
6451 // If the focus proxy is a parent, 'this' has to be inserted directly after its parent in the focus chain
6452 // remove it from the chain and insert this into the focus chain after its parent
6453
6454 // is this the case already?
6455 QWidget *parentsNext = w->nextInFocusChain();
6456 if (parentsNext == this) {
6457 // nothing to do.
6458 Q_ASSERT(previousInFocusChain() == w);
6459 } else {
6460 d->QWidgetPrivate::insertIntoFocusChainAfter(w);
6461 }
6462 }
6463
6464 if (moveFocusToProxy)
6465 setFocus(Qt::OtherFocusReason);
6466}
6467
6468
6469/*!
6470 Returns the focus proxy, or \nullptr if there is no focus proxy.
6471
6472 \sa setFocusProxy()
6473*/
6474
6475QWidget *QWidget::focusProxy() const
6476{
6477 Q_D(const QWidget);
6478 return d->extra ? d->extra->focus_proxy.data() : nullptr;
6479}
6480
6481
6482/*!
6483 \property QWidget::focus
6484 \brief whether this widget (or its focus proxy) has the keyboard
6485 input focus
6486
6487 By default, this property is \c false.
6488
6489 \note Obtaining the value of this property for a widget is effectively equivalent
6490 to checking whether QApplication::focusWidget() refers to the widget.
6491
6492 \sa setFocus(), clearFocus(), setFocusPolicy(), QApplication::focusWidget()
6493*/
6494bool QWidget::hasFocus() const
6495{
6496 const QWidget* w = this;
6497 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
6498 w = w->d_func()->extra->focus_proxy;
6499#if QT_CONFIG(graphicsview)
6500 if (QWidget *window = w->window()) {
6501 const auto &e = window->d_func()->extra;
6502 if (e && e->proxyWidget && e->proxyWidget->hasFocus() && window->focusWidget() == w)
6503 return true;
6504 }
6505#endif // QT_CONFIG(graphicsview)
6506 return (QApplication::focusWidget() == w);
6507}
6508
6509/*!
6510 Gives the keyboard input focus to this widget (or its focus
6511 proxy) if this widget or one of its parents is the \l{isActiveWindow()}{active window}. The \a reason argument will
6512 be passed into any focus event sent from this function, it is used
6513 to give an explanation of what caused the widget to get focus.
6514 If the window is not active, the widget will be given the focus when
6515 the window becomes active.
6516
6517 First, a focus about to change event is sent to the focus widget (if any) to
6518 tell it that it is about to lose the focus. Then focus is changed, a
6519 focus out event is sent to the previous focus item and a focus in event is sent
6520 to the new item to tell it that it just received the focus.
6521 (Nothing happens if the focus in and focus out widgets are the
6522 same.)
6523
6524 \note On embedded platforms, setFocus() will not cause an input panel
6525 to be opened by the input method. If you want this to happen, you
6526 have to send a QEvent::RequestSoftwareInputPanel event to the
6527 widget yourself.
6528
6529 setFocus() gives focus to a widget regardless of its focus policy,
6530 but does not clear any keyboard grab (see grabKeyboard()).
6531
6532 Be aware that if the widget is hidden, it will not accept focus
6533 until it is shown.
6534
6535 \warning If you call setFocus() in a function which may itself be
6536 called from focusOutEvent() or focusInEvent(), you may get an
6537 infinite recursion.
6538
6539 \sa hasFocus(), clearFocus(), focusInEvent(), focusOutEvent(),
6540 setFocusPolicy(), focusWidget(), QApplication::focusWidget(), grabKeyboard(),
6541 grabMouse(), {Keyboard Focus in Widgets}, QEvent::RequestSoftwareInputPanel
6542*/
6543
6544void QWidget::setFocus(Qt::FocusReason reason)
6545{
6546 if (!isEnabled())
6547 return;
6548
6549 QWidget *f = d_func()->deepestFocusProxy();
6550 if (!f)
6551 f = this;
6552
6553 if (QApplication::focusWidget() == f)
6554 return;
6555
6556#if QT_CONFIG(graphicsview)
6557 QWidget *previousProxyFocus = nullptr;
6558 if (const auto &topData = window()->d_func()->extra) {
6559 if (topData->proxyWidget && topData->proxyWidget->hasFocus()) {
6560 previousProxyFocus = topData->proxyWidget->widget()->focusWidget();
6561 if (previousProxyFocus && previousProxyFocus->focusProxy())
6562 previousProxyFocus = previousProxyFocus->focusProxy();
6563 if (previousProxyFocus == f && !topData->proxyWidget->d_func()->proxyIsGivingFocus)
6564 return;
6565 }
6566 }
6567#endif
6568
6569#if QT_CONFIG(graphicsview)
6570 // Update proxy state
6571 if (const auto &topData = window()->d_func()->extra) {
6572 if (topData->proxyWidget && !topData->proxyWidget->hasFocus()) {
6573 f->d_func()->updateFocusChild();
6574 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 1;
6575 topData->proxyWidget->setFocus(reason);
6576 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 0;
6577 }
6578 }
6579#endif
6580
6581 if (f->isActiveWindow()) {
6582 QWidget *prev = QApplicationPrivate::focus_widget;
6583 if (prev) {
6584 if (reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
6585 && prev->testAttribute(Qt::WA_InputMethodEnabled)) {
6586 QGuiApplication::inputMethod()->commit();
6587 }
6588
6589 if (reason != Qt::NoFocusReason) {
6590 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason);
6591 QCoreApplication::sendEvent(prev, &focusAboutToChange);
6592 }
6593 }
6594
6595 f->d_func()->updateFocusChild();
6596
6597 QApplicationPrivate::setFocusWidget(f, reason);
6598#if QT_CONFIG(accessibility)
6599 // menus update the focus manually and this would create bogus events
6600 if (!(f->inherits("QMenuBar") || f->inherits("QMenu") || f->inherits("QMenuItem")))
6601 {
6602 QAccessibleEvent event(f, QAccessible::Focus);
6603 QAccessible::updateAccessibility(&event);
6604 }
6605#endif
6606#if QT_CONFIG(graphicsview)
6607 if (const auto &topData = window()->d_func()->extra) {
6608 if (topData->proxyWidget) {
6609 if (previousProxyFocus && previousProxyFocus != f) {
6610 // Send event to self
6611 QFocusEvent event(QEvent::FocusOut, reason);
6612 QPointer<QWidget> that = previousProxyFocus;
6613 QCoreApplication::sendEvent(previousProxyFocus, &event);
6614 if (that)
6615 QCoreApplication::sendEvent(that->style(), &event);
6616 }
6617 if (!isHidden()) {
6618#if QT_CONFIG(graphicsview)
6619 // Update proxy state
6620 if (const auto &topData = window()->d_func()->extra)
6621 if (topData->proxyWidget && topData->proxyWidget->hasFocus())
6622 topData->proxyWidget->d_func()->updateProxyInputMethodAcceptanceFromWidget();
6623#endif
6624 // Send event to self
6625 QFocusEvent event(QEvent::FocusIn, reason);
6626 QPointer<QWidget> that = f;
6627 QCoreApplication::sendEvent(f, &event);
6628 if (that)
6629 QCoreApplication::sendEvent(that->style(), &event);
6630 }
6631 }
6632 }
6633#endif
6634 } else {
6635 f->d_func()->updateFocusChild();
6636 }
6637}
6638
6639
6640/*!\internal
6641 * A focus proxy can have its own focus proxy, which can have its own
6642 * proxy, and so on. This helper function returns the widget that sits
6643 * at the bottom of the proxy chain, and therefore the one that should
6644 * normally get focus if this widget receives a focus request.
6645 */
6646QWidget *QWidgetPrivate::deepestFocusProxy() const
6647{
6648 Q_Q(const QWidget);
6649
6650 QWidget *focusProxy = q->focusProxy();
6651 if (!focusProxy)
6652 return nullptr;
6653
6654 while (QWidget *nextFocusProxy = focusProxy->focusProxy())
6655 focusProxy = nextFocusProxy;
6656
6657 return focusProxy;
6658}
6659
6660static inline bool isEmbedded(const QWindow *w)
6661{
6662 const auto platformWindow = w->handle();
6663 return platformWindow && platformWindow->isEmbedded();
6664}
6665
6666void QWidgetPrivate::setFocus_sys()
6667{
6668 Q_Q(QWidget);
6669 // Embedded native widget may have taken the focus; get it back to toplevel
6670 // if that is the case (QTBUG-25852), unless widget is a window container.
6671 if (extra && extra->hasWindowContainer)
6672 return;
6673 // Do not activate in case the popup menu opens another application (QTBUG-70810)
6674 // unless the application is embedded (QTBUG-71991).
6675 if (QWindow *nativeWindow = q->testAttribute(Qt::WA_WState_Created) ? q->window()->windowHandle() : nullptr) {
6676 if (nativeWindow->type() != Qt::Popup && nativeWindow != QGuiApplication::focusWindow()
6677 && (QGuiApplication::applicationState() == Qt::ApplicationActive
6678 || QCoreApplication::testAttribute(Qt::AA_PluginApplication)
6679 || isEmbedded(nativeWindow))) {
6680 nativeWindow->requestActivate();
6681 }
6682 }
6683}
6684
6685// updates focus_child on parent widgets to point into this widget
6686void QWidgetPrivate::updateFocusChild()
6687{
6688 Q_Q(QWidget);
6689
6690 QWidget *w = q;
6691 if (q->isHidden()) {
6692 while (w && w->isHidden()) {
6693 w->d_func()->focus_child = q;
6694 w = w->isWindow() ? nullptr : w->parentWidget();
6695 }
6696 } else {
6697 while (w) {
6698 w->d_func()->focus_child = q;
6699 w = w->isWindow() ? nullptr : w->parentWidget();
6700 }
6701 }
6702
6703 if (QTLWExtra *extra = q->window()->d_func()->maybeTopData()) {
6704 if (extra->window)
6705 emit extra->window->focusObjectChanged(q);
6706 }
6707}
6708
6709/*!
6710 \fn void QWidget::setFocus()
6711 \overload
6712
6713 Gives the keyboard input focus to this widget (or its focus
6714 proxy) if this widget or one of its parents is the
6715 \l{isActiveWindow()}{active window}.
6716*/
6717
6718/*!
6719 Takes keyboard input focus from the widget.
6720
6721 If the widget has active focus, a \l{focusOutEvent()}{focus out event} is sent to this widget to tell it that it has
6722 lost the focus.
6723
6724 This widget must enable focus setting to get the keyboard
6725 input focus; that is, it must call setFocusPolicy().
6726
6727 \sa hasFocus(), setFocus(), focusInEvent(), focusOutEvent(),
6728 setFocusPolicy(), QApplication::focusWidget()
6729*/
6730
6731void QWidget::clearFocus()
6732{
6733 if (hasFocus()) {
6734 if (testAttribute(Qt::WA_InputMethodEnabled))
6735 QGuiApplication::inputMethod()->commit();
6736
6737 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange);
6738 QCoreApplication::sendEvent(this, &focusAboutToChange);
6739 }
6740
6741 QTLWExtra *extra = window()->d_func()->maybeTopData();
6742 QObject *originalFocusObject = nullptr;
6743 if (extra && extra->window) {
6744 originalFocusObject = extra->window->focusObject();
6745 // the window's focus object might already be nullptr if we are in the destructor, but we still
6746 // need to update QGuiApplication and input context if we have a focus widget.
6747 if (!originalFocusObject)
6748 originalFocusObject = focusWidget();
6749 }
6750
6751 QWidget *w = this;
6752 while (w) {
6753 // Just like setFocus(), we update (clear) the focus_child of our parents
6754 if (w->d_func()->focus_child == this)
6755 w->d_func()->focus_child = nullptr;
6756 w = w->parentWidget();
6757 }
6758
6759 // We've potentially cleared the focus_child of our parents, so we need
6760 // to report this to the rest of Qt. Note that the focus_child is not the same
6761 // thing as the application's focusWidget, which is why this piece of code is
6762 // not inside a hasFocus() block.
6763 if (originalFocusObject && originalFocusObject != extra->window->focusObject())
6764 emit extra->window->focusObjectChanged(extra->window->focusObject());
6765
6766#if QT_CONFIG(graphicsview)
6767 const auto &topData = d_func()->extra;
6768 if (topData && topData->proxyWidget)
6769 topData->proxyWidget->clearFocus();
6770#endif
6771
6772 if (hasFocus()) {
6773 // Update proxy state
6774 QApplicationPrivate::setFocusWidget(nullptr, Qt::OtherFocusReason);
6775#if QT_CONFIG(accessibility)
6776 QAccessibleEvent event(this, QAccessible::Focus);
6777 QAccessible::updateAccessibility(&event);
6778#endif
6779 }
6780}
6781
6782
6783/*!
6784 \fn bool QWidget::focusNextChild()
6785
6786 Finds a new widget to give the keyboard focus to, as appropriate
6787 for \uicontrol Tab, and returns \c true if it can find a new widget, or
6788 false if it can't.
6789
6790 \sa focusPreviousChild()
6791*/
6792
6793/*!
6794 \fn bool QWidget::focusPreviousChild()
6795
6796 Finds a new widget to give the keyboard focus to, as appropriate
6797 for \uicontrol Shift+Tab, and returns \c true if it can find a new widget,
6798 or false if it can't.
6799
6800 \sa focusNextChild()
6801*/
6802
6803/*!
6804 Finds a new widget to give the keyboard focus to, as appropriate
6805 for Tab and Shift+Tab, and returns \c true if it can find a new
6806 widget, or false if it can't.
6807
6808 If \a next is true, this function searches forward, if \a next
6809 is false, it searches backward.
6810
6811 Sometimes, you will want to reimplement this function. For
6812 example, a web browser might reimplement it to move its "current
6813 active link" forward or backward, and call
6814 focusNextPrevChild() only when it reaches the last or
6815 first link on the "page".
6816
6817 Child widgets call focusNextPrevChild() on their parent widgets,
6818 but only the window that contains the child widgets decides where
6819 to redirect focus. By reimplementing this function for an object,
6820 you thus gain control of focus traversal for all child widgets.
6821
6822 \sa focusNextChild(), focusPreviousChild()
6823*/
6824
6825bool QWidget::focusNextPrevChild(bool next)
6826{
6827 QWidget* p = parentWidget();
6828 bool isSubWindow = (windowType() == Qt::SubWindow);
6829 if (!isWindow() && !isSubWindow && p)
6830 return p->focusNextPrevChild(next);
6831#if QT_CONFIG(graphicsview)
6832 Q_D(QWidget);
6833 if (d->extra && d->extra->proxyWidget)
6834 return d->extra->proxyWidget->focusNextPrevChild(next);
6835#endif
6836
6837 bool wrappingOccurred = false;
6838 QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(this, next,
6839 &wrappingOccurred);
6840 if (!w) return false;
6841
6842 Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
6843
6844 /* If we are about to wrap the focus chain, give the platform
6845 * implementation a chance to alter the wrapping behavior. This is
6846 * especially needed when the window is embedded in a window created by
6847 * another process.
6848 */
6849 if (wrappingOccurred) {
6850 QWindow *window = windowHandle();
6851 if (window != nullptr) {
6852 QWindowPrivate *winp = qt_window_private(window);
6853
6854 if (winp->platformWindow != nullptr) {
6855 QFocusEvent event(QEvent::FocusIn, reason);
6856 event.ignore();
6857 winp->platformWindow->windowEvent(&event);
6858 if (event.isAccepted()) return true;
6859 }
6860 }
6861 }
6862
6863 w->setFocus(reason);
6864 return true;
6865}
6866
6867/*!
6868 Returns the last child of this widget that setFocus had been
6869 called on. For top level widgets this is the widget that will get
6870 focus in case this window gets activated
6871
6872 This is not the same as QApplication::focusWidget(), which returns
6873 the focus widget in the currently active window.
6874*/
6875
6876QWidget *QWidget::focusWidget() const
6877{
6878 return const_cast<QWidget *>(d_func()->focus_child);
6879}
6880
6881QObject *QWidgetPrivate::focusObject()
6882{
6883 Q_Q(QWidget);
6884 QWidget *proxy = deepestFocusProxy();
6885 return proxy ? proxy : q;
6886}
6887
6888/*!
6889 Returns the next widget in this widget's focus chain.
6890
6891 \sa previousInFocusChain()
6892*/
6893QWidget *QWidget::nextInFocusChain() const
6894{
6895 Q_D(const QWidget);
6896 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Next);
6897}
6898
6899/*!
6900 \brief The previousInFocusChain function returns the previous
6901 widget in this widget's focus chain.
6902
6903 \sa nextInFocusChain()
6904
6905 \since 4.6
6906*/
6907QWidget *QWidget::previousInFocusChain() const
6908{
6909 Q_D(const QWidget);
6910 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Previous);
6911}
6912
6913/*!
6914 \property QWidget::isActiveWindow
6915 \brief whether this widget's window is the active window
6916
6917 The active window is the window that contains the widget that has
6918 keyboard focus (The window may still have focus if it has no
6919 widgets or none of its widgets accepts keyboard focus).
6920
6921 When popup windows are visible, this property is \c true for both the
6922 active window \e and for the popup.
6923
6924 By default, this property is \c false.
6925
6926 \sa activateWindow(), QApplication::activeWindow()
6927*/
6928bool QWidget::isActiveWindow() const
6929{
6930 QWidget *tlw = window();
6931 if (tlw == QApplication::activeWindow() || (isVisible() && (tlw->windowType() == Qt::Popup)))
6932 return true;
6933
6934#if QT_CONFIG(graphicsview)
6935 if (const auto &tlwExtra = tlw->d_func()->extra) {
6936 if (isVisible() && tlwExtra->proxyWidget)
6937 return tlwExtra->proxyWidget->isActiveWindow();
6938 }
6939#endif
6940
6941 if (style()->styleHint(QStyle::SH_Widget_ShareActivation, nullptr, this)) {
6942 if (tlw->windowType() == Qt::Tool &&
6943 !tlw->isModal() &&
6944 (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow()))
6945 return true;
6946 QWidget *w = QApplication::activeWindow();
6947 while(w && tlw->windowType() == Qt::Tool &&
6948 !w->isModal() && w->parentWidget()) {
6949 w = w->parentWidget()->window();
6950 if (w == tlw)
6951 return true;
6952 }
6953 }
6954
6955 // Check for an active window container
6956 if (QWindow *ww = QGuiApplication::focusWindow()) {
6957 while (ww) {
6958 QWidgetWindow *qww = qobject_cast<QWidgetWindow *>(ww);
6959 QWindowContainer *qwc = qww ? qobject_cast<QWindowContainer *>(qww->widget()) : 0;
6960 if (qwc && qwc->topLevelWidget() == tlw)
6961 return true;
6962 ww = ww->parent();
6963 }
6964 }
6965
6966 // Check if platform adaptation thinks the window is active. This is necessary for
6967 // example in case of ActiveQt servers that are embedded into another application.
6968 // Those are separate processes that are not part of the parent application Qt window/widget
6969 // hierarchy, so they need to rely on native methods to determine if they are part of the
6970 // active window.
6971 if (const QWindow *w = tlw->windowHandle()) {
6972 if (w->handle())
6973 return w->handle()->isActive();
6974 }
6975
6976 return false;
6977}
6978
6979/*!
6980 \fn void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets)
6981 \overload
6982 \since 6.6
6983
6984 Sets the tab order for the widgets in the \a widgets list by calling
6985 \l{QWidget::setTabOrder(QWidget *, QWidget *)} for each consecutive
6986 pair of widgets.
6987
6988 Instead of setting up each pair manually like this:
6989
6990 \snippet code/src_gui_kernel_qwidget.cpp 9
6991
6992 you can call:
6993
6994 \snippet code/src_gui_kernel_qwidget.cpp 9.list
6995
6996 The call does not create a closed tab focus loop. If there are more widgets
6997 with \l{Qt::TabFocus} focus policy, tabbing on \c{d} will move focus to one
6998 of those widgets, not back to \c{a}.
6999
7000 \sa setFocusPolicy(), setFocusProxy(), {Keyboard Focus in Widgets}
7001*/
7002
7003/*!
7004 Puts the \a second widget after the \a first widget in the focus order.
7005
7006 It effectively removes the \a second widget from its focus chain and
7007 inserts it after the \a first widget.
7008
7009 Note that since the tab order of the \a second widget is changed, you
7010 should order a chain like this:
7011
7012 \snippet code/src_gui_kernel_qwidget.cpp 9
7013
7014 \e not like this:
7015
7016 \snippet code/src_gui_kernel_qwidget.cpp 10
7017
7018 If \a first or \a second has a focus proxy, setTabOrder()
7019 correctly substitutes the proxy.
7020
7021 \note Since Qt 5.10: A widget that has a child as focus proxy is understood as
7022 a compound widget. When setting a tab order between one or two compound widgets, the
7023 local tab order inside each will be preserved. This means that if both widgets are
7024 compound widgets, the resulting tab order will be from the last child inside
7025 \a first, to the first child inside \a second.
7026
7027 \sa setFocusPolicy(), setFocusProxy(), {Keyboard Focus in Widgets}
7028*/
7029void QWidget::setTabOrder(QWidget* first, QWidget *second)
7030{
7031 if (!first || !second || first == second
7032 || first->focusPolicy() == Qt::NoFocus
7033 || second->focusPolicy() == Qt::NoFocus)
7034 return;
7035
7036 if (Q_UNLIKELY(first->window() != second->window())) {
7037 qWarning("QWidget::setTabOrder: 'first' and 'second' must be in the same window");
7038 return;
7039 }
7040
7041 const auto determineLastFocusChild = [](QWidget *target, QWidget *noFurtherThan)
7042 {
7043 // Since we need to repeat the same logic for both 'first' and 'second', we add a function that
7044 // determines the last focus child for a widget, taking proxies and compound widgets into account.
7045 // If the target is not a compound widget (it doesn't have a focus proxy that points to a child),
7046 // 'lastFocusChild' will be set to the target itself.
7047 QWidget *lastFocusChild = target;
7048
7049 QWidget *focusProxy = target->d_func()->deepestFocusProxy();
7050 if (!focusProxy) {
7051 // QTBUG-81097: Another case is possible here. We can have a child
7052 // widget, that sets its focusProxy() to the parent (target).
7053 // An example of such widget is a QLineEdit, nested into
7054 // a QAbstractSpinBox. In this case such widget should be considered
7055 // the last focus child.
7056 for (auto *object : target->children()) {
7057 QWidget *w = qobject_cast<QWidget*>(object);
7058 if (w && w->focusProxy() == target) {
7059 lastFocusChild = w;
7060 break;
7061 }
7062 }
7063 } else if (target->isAncestorOf(focusProxy)) {
7064 lastFocusChild = focusProxy;
7065 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
7066 focusNext != focusProxy && target->isAncestorOf(focusNext) && focusNext->window() == focusProxy->window();
7067 focusNext = focusNext->nextInFocusChain()) {
7068 if (focusNext == noFurtherThan)
7069 break;
7070 if (focusNext->focusPolicy() != Qt::NoFocus)
7071 lastFocusChild = focusNext;
7072 }
7073 }
7074 return lastFocusChild;
7075 };
7076 // detect inflection in case we have compound widgets
7077 QWidget *lastFocusChildOfFirst = determineLastFocusChild(first, second);
7078 if (lastFocusChildOfFirst == second)
7079 lastFocusChildOfFirst = first;
7080 QWidget *lastFocusChildOfSecond = determineLastFocusChild(second, first);
7081 if (lastFocusChildOfSecond == first)
7082 lastFocusChildOfSecond = second;
7083
7084 // Return if only NoFocus widgets are between first and second
7085 QWidget *oldPrev = second->previousInFocusChain();
7086 QWidget *prevWithFocus = oldPrev;
7087 while (prevWithFocus->focusPolicy() == Qt::NoFocus)
7088 prevWithFocus = prevWithFocus->previousInFocusChain();
7089 if (prevWithFocus == first)
7090 return;
7091 const QWidgetList chain = QWidgetPrivate::takeFromFocusChain(second, lastFocusChildOfSecond);
7092 QWidgetPrivate::insertIntoFocusChain(chain, QWidgetPrivate::FocusDirection::Next, lastFocusChildOfFirst);
7093}
7094
7095void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets)
7096{
7097 QWidget *prev = nullptr;
7098 for (const auto &widget : widgets) {
7099 if (!prev) {
7100 prev = widget;
7101 } else {
7102 QWidget::setTabOrder(prev, widget);
7103 prev = widget;
7104 }
7105 }
7106}
7107
7108
7109/*!\internal
7110
7111 Moves the relevant subwidgets of this widget from the \a oldtlw's
7112 tab chain to that of the new parent, if there's anything to move and
7113 we're really moving
7114
7115 This function is called from QWidget::reparent() *after* the widget
7116 has been reparented.
7117
7118 \sa reparent()
7119*/
7120
7121void QWidgetPrivate::reparentFocusWidgets(QWidget * oldtlw)
7122{
7123 Q_Q(QWidget);
7124 if (oldtlw == q->window())
7125 return; // nothing to do
7126
7127 if (focus_child)
7128 focus_child->clearFocus();
7129
7130 reparentFocusChildren(QWidgetPrivate::FocusDirection::Next);
7131}
7132
7133/*!
7134 \property QWidget::frameSize
7135 \brief the size of the widget including any window frame
7136
7137 By default, this property contains a value that depends on the user's
7138 platform and screen geometry.
7139*/
7140QSize QWidget::frameSize() const
7141{
7142 Q_D(const QWidget);
7143 if (isWindow() && !(windowType() == Qt::Popup)) {
7144 QRect fs = d->frameStrut();
7145 return QSize(data->crect.width() + fs.left() + fs.right(),
7146 data->crect.height() + fs.top() + fs.bottom());
7147 }
7148 return data->crect.size();
7149}
7150
7151/*! \fn void QWidget::move(int x, int y)
7152
7153 \overload
7154
7155 This corresponds to move(QPoint(\a x, \a y)).
7156*/
7157
7158void QWidget::move(const QPoint &p)
7159{
7160 Q_D(QWidget);
7161 setAttribute(Qt::WA_Moved);
7162 if (testAttribute(Qt::WA_WState_Created)) {
7163 if (isWindow())
7164 d->topData()->posIncludesFrame = false;
7165 d->setGeometry_sys(p.x() + geometry().x() - QWidget::x(),
7166 p.y() + geometry().y() - QWidget::y(),
7167 width(), height(), true);
7168 d->setDirtyOpaqueRegion();
7169 } else {
7170 // no frame yet: see also QWidgetPrivate::fixPosIncludesFrame(), QWindowPrivate::PositionPolicy.
7171 if (isWindow())
7172 d->topData()->posIncludesFrame = true;
7173 data->crect.moveTopLeft(p); // no frame yet
7174 setAttribute(Qt::WA_PendingMoveEvent);
7175 }
7176
7177 if (d->extra && d->extra->hasWindowContainer)
7178 QWindowContainer::parentWasMoved(this);
7179}
7180
7181// move() was invoked with Qt::WA_WState_Created not set (frame geometry
7182// unknown), that is, crect has a position including the frame.
7183// If we can determine the frame strut, fix that and clear the flag.
7184// FIXME: This does not play well with window states other than
7185// Qt::WindowNoState, as we depend on calling setGeometry() on the
7186// platform window after fixing up the position so that the new
7187// geometry is reflected in the platform window, but when the frame
7188// comes in after the window has been shown (e.g. maximized), we're
7189// not in a position to do that kind of fixup.
7190void QWidgetPrivate::fixPosIncludesFrame()
7191{
7192 Q_Q(QWidget);
7193 if (QTLWExtra *te = maybeTopData()) {
7194 if (te->posIncludesFrame) {
7195 // For Qt::WA_DontShowOnScreen, assume a frame of 0 (for
7196 // example, in QGraphicsProxyWidget).
7197 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
7198 te->posIncludesFrame = 0;
7199 } else {
7200 if (q->windowHandle() && q->windowHandle()->handle()) {
7201 updateFrameStrut();
7202 if (!q->data->fstrut_dirty) {
7203 data.crect.translate(te->frameStrut.x(), te->frameStrut.y());
7204 te->posIncludesFrame = 0;
7205 }
7206 } // windowHandle()
7207 } // !WA_DontShowOnScreen
7208 } // posIncludesFrame
7209 } // QTLWExtra
7210}
7211
7212/*! \fn void QWidget::resize(int w, int h)
7213 \overload
7214
7215 This corresponds to resize(QSize(\a w, \a h)).
7216*/
7217
7218void QWidget::resize(const QSize &s)
7219{
7220 Q_D(QWidget);
7221 setAttribute(Qt::WA_Resized);
7222 if (testAttribute(Qt::WA_WState_Created)) {
7223 d->fixPosIncludesFrame();
7224 d->setGeometry_sys(geometry().x(), geometry().y(), s.width(), s.height(), false);
7225 d->setDirtyOpaqueRegion();
7226 } else {
7227 const auto oldRect = data->crect;
7228 data->crect.setSize(s.boundedTo(maximumSize()).expandedTo(minimumSize()));
7229 if (oldRect != data->crect)
7230 setAttribute(Qt::WA_PendingResizeEvent);
7231 }
7232}
7233
7234void QWidget::setGeometry(const QRect &r)
7235{
7236 Q_D(QWidget);
7237 setAttribute(Qt::WA_Resized);
7238 setAttribute(Qt::WA_Moved);
7239 if (isWindow())
7240 d->topData()->posIncludesFrame = 0;
7241 if (testAttribute(Qt::WA_WState_Created)) {
7242 d->setGeometry_sys(r.x(), r.y(), r.width(), r.height(), true);
7243 d->setDirtyOpaqueRegion();
7244 } else {
7245 const auto oldRect = data->crect;
7246 data->crect.setTopLeft(r.topLeft());
7247 data->crect.setSize(r.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
7248 if (oldRect != data->crect) {
7249 setAttribute(Qt::WA_PendingMoveEvent);
7250 setAttribute(Qt::WA_PendingResizeEvent);
7251 }
7252 }
7253
7254 if (d->extra && d->extra->hasWindowContainer)
7255 QWindowContainer::parentWasMoved(this);
7256}
7257
7258void QWidgetPrivate::setGeometry_sys(int x, int y, int w, int h, bool isMove)
7259{
7260 Q_Q(QWidget);
7261 if (extra) { // any size restrictions?
7262 w = qMin(w,extra->maxw);
7263 h = qMin(h,extra->maxh);
7264 w = qMax(w,extra->minw);
7265 h = qMax(h,extra->minh);
7266 }
7267
7268 if (q->isWindow() && q->windowHandle()) {
7269 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
7270 if (!integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) {
7271 x = 0;
7272 y = 0;
7273 w = q->windowHandle()->width();
7274 h = q->windowHandle()->height();
7275 }
7276 }
7277
7278 QPoint oldp = q->geometry().topLeft();
7279 QSize olds = q->size();
7280 QRect r(x, y, w, h);
7281
7282 bool isResize = olds != r.size();
7283 if (!isMove)
7284 isMove = oldp != r.topLeft();
7285
7286
7287 // We only care about stuff that changes the geometry, or may
7288 // cause the window manager to change its state
7289 if (r.size() == olds && oldp == r.topLeft())
7290 return;
7291
7292 if (!data.in_set_window_state) {
7293 q->data->window_state &= ~Qt::WindowMaximized;
7294 q->data->window_state &= ~Qt::WindowFullScreen;
7295 if (q->isWindow())
7296 topData()->normalGeometry = QRect(0, 0, -1, -1);
7297 }
7298
7299 QPoint oldPos = q->pos();
7300 data.crect = r;
7301
7302 bool needsShow = false;
7303
7304 if (q->isWindow() || q->windowHandle()) {
7305 if (!(data.window_state & Qt::WindowFullScreen) && (w == 0 || h == 0)) {
7306 q->setAttribute(Qt::WA_OutsideWSRange, true);
7307 if (q->isVisible())
7308 hide_sys();
7309 data.crect = QRect(x, y, w, h);
7310 } else if (q->testAttribute(Qt::WA_OutsideWSRange)) {
7311 q->setAttribute(Qt::WA_OutsideWSRange, false);
7312 needsShow = true;
7313 }
7314 }
7315
7316 const bool hasOnScreenWindow = !q->testAttribute(Qt::WA_DontShowOnScreen)
7317 && !q->testAttribute(Qt::WA_OutsideWSRange);
7318
7319 if (hasOnScreenWindow) {
7320 // Unconditionally reflect geometry to QWindow,
7321 // even when the widget is hidden, to mirror that
7322 // we reflect geometry back from the platform in
7323 // QWidgetWindow's resize/move event handlers.
7324 if (QWindow *win = q->windowHandle()) {
7325 if (q->isWindow()) {
7326 if (isResize && !isMove)
7327 win->resize(w, h);
7328 else if (isMove && !isResize)
7329 win->setPosition(x, y);
7330 else
7331 win->setGeometry(q->geometry());
7332 } else {
7333 QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());
7334 win->setGeometry(QRect(posInNativeParent,r.size()));
7335 }
7336 }
7337 }
7338
7339 if (q->isVisible()) {
7340 if (hasOnScreenWindow) {
7341 if (needsShow && q->windowHandle())
7342 show_sys();
7343
7344 if (!q->isWindow()) {
7345 if (renderToTexture) {
7346 QRegion updateRegion(q->geometry());
7347 updateRegion += QRect(oldPos, olds);
7348 q->parentWidget()->d_func()->invalidateBackingStore(updateRegion);
7349 } else if (isMove && !isResize) {
7350 moveRect(QRect(oldPos, olds), x - oldPos.x(), y - oldPos.y());
7351 } else {
7352 invalidateBackingStore_resizeHelper(oldPos, olds);
7353 }
7354 }
7355 }
7356
7357 if (isMove) {
7358 QMoveEvent e(q->pos(), oldPos);
7359 QCoreApplication::sendEvent(q, &e);
7360 }
7361 if (isResize) {
7362 QResizeEvent e(r.size(), olds);
7363 QCoreApplication::sendEvent(q, &e);
7364 if (q->windowHandle())
7365 q->update();
7366 }
7367 } else { // not visible
7368 if (isMove && q->pos() != oldPos)
7369 q->setAttribute(Qt::WA_PendingMoveEvent, true);
7370 if (isResize)
7371 q->setAttribute(Qt::WA_PendingResizeEvent, true);
7372 }
7373
7374#if QT_CONFIG(accessibility)
7375 if (QAccessible::isActive() && q->isVisible()) {
7376 QAccessibleEvent event(q, QAccessible::LocationChanged);
7377 QAccessible::updateAccessibility(&event);
7378 }
7379#endif
7380}
7381
7382/*!
7383 \since 4.2
7384 Saves the current geometry and state for top-level widgets.
7385
7386 To save the geometry when the window closes, you can
7387 implement a close event like this:
7388
7389 \snippet code/src_gui_kernel_qwidget.cpp 11
7390
7391 See the \l{Window Geometry} documentation for an overview of geometry
7392 issues with windows.
7393
7394 Use QMainWindow::saveState() to save the geometry and the state of
7395 toolbars and dock widgets.
7396
7397 \sa restoreGeometry(), QMainWindow::saveState(), QMainWindow::restoreState()
7398*/
7399QByteArray QWidget::saveGeometry() const
7400{
7401 QByteArray array;
7402 QDataStream stream(&array, QIODevice::WriteOnly);
7403 stream.setVersion(QDataStream::Qt_4_0);
7404 const quint32 magicNumber = 0x1D9D0CB;
7405 // Version history:
7406 // - Qt 4.2 - 4.8.6, 5.0 - 5.3 : Version 1.0
7407 // - Qt 4.8.6 - today, 5.4 - today: Version 2.0, save screen width in addition to check for high DPI scaling.
7408 // - Qt 5.12 - today : Version 3.0, save QWidget::geometry()
7409 quint16 majorVersion = 3;
7410 quint16 minorVersion = 0;
7411 const int screenNumber = QGuiApplication::screens().indexOf(screen());
7412 stream << magicNumber
7413 << majorVersion
7414 << minorVersion
7415 << frameGeometry()
7416 << normalGeometry()
7417 << qint32(screenNumber)
7418 << quint8(windowState() & Qt::WindowMaximized)
7419 << quint8(windowState() & Qt::WindowFullScreen)
7420 << qint32(screen()->geometry().width()) // added in 2.0
7421 << geometry(); // added in 3.0
7422 return array;
7423}
7424
7425/*!
7426 \internal
7427
7428 Check a if \a restoredGeometry fits into \a availableGeometry
7429 This method is used to verify that a widget is restored to a geometry, which
7430 fits into the target screen.
7431
7432 \param frameHeight represents the height of the widget's title bar, which is expected
7433 to be on its top.
7434
7435 If the size of \a restoredGeometry exceeds \a availableGeometry, its height and width
7436 will be resized to fit into \a availableGeometry, with the height reduced by
7437 \a frameHeight.
7438
7439 If at least one edge of \a restoredGeometry is outside \a availableGeometry,
7440 \a restoredGeometry will be moved
7441 \list
7442 \li down if its top is off screen
7443 \li up if its bottom is off screen
7444 \li right if its left edge is off screen
7445 \li left if its right edge is off screen
7446 \endlist
7447 */
7448void QWidgetPrivate::checkRestoredGeometry(const QRect &availableGeometry, QRect *restoredGeometry,
7449 int frameHeight)
7450{
7451 // compare with restored geometry's height increased by frameHeight
7452 const int height = restoredGeometry->height() + frameHeight;
7453
7454 // Step 1: Resize if necessary, corrected for frame height
7455 if (availableGeometry.height() <= height)
7456 restoredGeometry->setHeight(availableGeometry.height() - frameHeight);
7457 if (availableGeometry.width() <= restoredGeometry->width())
7458 restoredGeometry->setWidth(availableGeometry.width());
7459
7460 // Step 2: Move if necessary:
7461 // Construct a rectangle from restored Geometry adjusted by frameHeight
7462 const QRect restored = restoredGeometry->adjusted(0, -frameHeight, 0, 0);
7463
7464 // Return if restoredGeometry (including frame) fits into screen
7465 if (availableGeometry.contains(restored))
7466 return;
7467
7468 // (size is correct, but at least one edge is off screen)
7469
7470 // Top out of bounds => move down
7471 if (restored.top() < availableGeometry.top()) {
7472 restoredGeometry->moveTop(availableGeometry.top() + frameHeight);
7473 } else if (restored.bottom() > availableGeometry.bottom()) {
7474 // Bottom out of bounds => move up
7475 restoredGeometry->moveBottom(availableGeometry.bottom());
7476 }
7477
7478 // Left edge out of bounds => move right
7479 if (restored.left() < availableGeometry.left()) {
7480 restoredGeometry->moveLeft(availableGeometry.left());
7481 } else if (restored.right() > availableGeometry.right()) {
7482 // Right edge out of bounds => move left
7483 restoredGeometry->moveRight(availableGeometry.right());
7484 }
7485}
7486
7487/*!
7488 \since 4.2
7489
7490 Restores the geometry and state of top-level widgets stored in the
7491 byte array \a geometry. Returns \c true on success; otherwise
7492 returns \c false.
7493
7494 If the restored geometry is off-screen, it will be modified to be
7495 inside the available screen geometry.
7496
7497 To restore geometry saved using QSettings, you can use code like
7498 this:
7499
7500 \snippet code/src_gui_kernel_qwidget.cpp 12
7501
7502 See the \l{Window Geometry} documentation for an overview of geometry
7503 issues with windows.
7504
7505 Use QMainWindow::restoreState() to restore the geometry and the
7506 state of toolbars and dock widgets.
7507
7508 \sa saveGeometry(), QSettings, QMainWindow::saveState(), QMainWindow::restoreState()
7509*/
7510bool QWidget::restoreGeometry(const QByteArray &geometry)
7511{
7512 if (geometry.size() < 4)
7513 return false;
7514 QDataStream stream(geometry);
7515 stream.setVersion(QDataStream::Qt_4_0);
7516
7517 const quint32 magicNumber = 0x1D9D0CB;
7518 quint32 storedMagicNumber;
7519 stream >> storedMagicNumber;
7520 if (storedMagicNumber != magicNumber)
7521 return false;
7522
7523 const quint16 currentMajorVersion = 3;
7524 quint16 majorVersion = 0;
7525 quint16 minorVersion = 0;
7526
7527 stream >> majorVersion >> minorVersion;
7528
7529 if (majorVersion > currentMajorVersion)
7530 return false;
7531 // (Allow all minor versions.)
7532
7533 QRect restoredFrameGeometry;
7534 QRect restoredGeometry;
7535 QRect restoredNormalGeometry;
7536 qint32 restoredScreenNumber;
7537 quint8 maximized;
7538 quint8 fullScreen;
7539 qint32 restoredScreenWidth = 0;
7540
7541 stream >> restoredFrameGeometry // Only used for sanity checks in version 0
7542 >> restoredNormalGeometry
7543 >> restoredScreenNumber
7544 >> maximized
7545 >> fullScreen;
7546
7547 if (majorVersion > 1)
7548 stream >> restoredScreenWidth;
7549 if (majorVersion > 2)
7550 stream >> restoredGeometry;
7551
7552 // ### Qt 6 - Perhaps it makes sense to dumb down the restoreGeometry() logic, see QTBUG-69104
7553
7554 if (restoredScreenNumber >= qMax(QGuiApplication::screens().size(), 1))
7555 restoredScreenNumber = 0;
7556 const QScreen *restoredScreen = QGuiApplication::screens().value(restoredScreenNumber, nullptr);
7557 const qreal screenWidthF = restoredScreen ? qreal(restoredScreen->geometry().width()) : 0;
7558 // Sanity check bailing out when large variations of screen sizes occur due to
7559 // high DPI scaling or different levels of DPI awareness.
7560 if (restoredScreenWidth) {
7561 const qreal factor = qreal(restoredScreenWidth) / screenWidthF;
7562 if (factor < 0.8 || factor > 1.25)
7563 return false;
7564 } else {
7565 // Saved by Qt 5.3 and earlier, try to prevent too large windows
7566 // unless the size will be adapted by maximized or fullscreen.
7567 if (!maximized && !fullScreen && qreal(restoredFrameGeometry.width()) / screenWidthF > 1.5)
7568 return false;
7569 }
7570
7571 const int frameHeight = QApplication::style()
7572 ? QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight, nullptr, this)
7573 : 20;
7574
7575 if (!restoredNormalGeometry.isValid())
7576 restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
7577 if (!restoredNormalGeometry.isValid()) {
7578 // use the widget's adjustedSize if the sizeHint() doesn't help
7579 restoredNormalGeometry.setSize(restoredNormalGeometry
7580 .size()
7581 .expandedTo(d_func()->adjustedSize()));
7582 }
7583
7584 const QRect availableGeometry = restoredScreen ? restoredScreen->availableGeometry()
7585 : QRect();
7586
7587 // Modify the restored geometry if we are about to restore to coordinates
7588 // that would make the window "lost". This happens if:
7589 // - The restored geometry is completely or partly oustside the available geometry
7590 // - The title bar is outside the available geometry.
7591
7592 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredGeometry, frameHeight);
7593 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredNormalGeometry, frameHeight);
7594
7595 if (maximized || fullScreen) {
7596 // set geometry before setting the window state to make
7597 // sure the window is maximized to the right screen.
7598 Qt::WindowStates ws = windowState();
7599#ifndef Q_OS_WIN
7600 setGeometry(restoredNormalGeometry);
7601#else
7602 if (ws & Qt::WindowFullScreen) {
7603 // Full screen is not a real window state on Windows.
7604 move(availableGeometry.topLeft());
7605 } else if (ws & Qt::WindowMaximized) {
7606 // Setting a geometry on an already maximized window causes this to be
7607 // restored into a broken, half-maximized state, non-resizable state (QTBUG-4397).
7608 // Move the window in normal state if needed.
7609 if (restoredScreen != screen()) {
7610 setWindowState(Qt::WindowNoState);
7611 setGeometry(restoredNormalGeometry);
7612 }
7613 } else {
7614 setGeometry(restoredNormalGeometry);
7615 }
7616#endif // Q_OS_WIN
7617 if (maximized)
7618 ws |= Qt::WindowMaximized;
7619 if (fullScreen)
7620 ws |= Qt::WindowFullScreen;
7621 setWindowState(ws);
7622 d_func()->topData()->normalGeometry = restoredNormalGeometry;
7623 } else {
7624 setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
7625
7626 // FIXME: Why fall back to restoredNormalGeometry if majorVersion <= 2?
7627 if (majorVersion > 2)
7628 setGeometry(restoredGeometry);
7629 else
7630 setGeometry(restoredNormalGeometry);
7631 }
7632 return true;
7633}
7634
7635/*!\fn void QWidget::setGeometry(int x, int y, int w, int h)
7636 \overload
7637
7638 This corresponds to setGeometry(QRect(\a x, \a y, \a w, \a h)).
7639*/
7640
7641/*!
7642 Sets the margins around the contents of the widget to have the sizes
7643 \a left, \a top, \a right, and \a bottom. The margins are used by
7644 the layout system, and may be used by subclasses to specify the area
7645 to draw in (e.g. excluding the frame).
7646
7647 Changing the margins will trigger a resizeEvent().
7648
7649 \sa contentsRect(), contentsMargins()
7650*/
7651void QWidget::setContentsMargins(int left, int top, int right, int bottom)
7652{
7653 Q_D(QWidget);
7654 if (left == d->leftmargin && top == d->topmargin
7655 && right == d->rightmargin && bottom == d->bottommargin)
7656 return;
7657 d->leftmargin = left;
7658 d->topmargin = top;
7659 d->rightmargin = right;
7660 d->bottommargin = bottom;
7661
7662 d->updateContentsRect();
7663}
7664
7665/*!
7666 \overload
7667 \since 4.6
7668
7669 \brief The setContentsMargins function sets the margins around the
7670 widget's contents.
7671
7672 Sets the margins around the contents of the widget to have the
7673 sizes determined by \a margins. The margins are
7674 used by the layout system, and may be used by subclasses to
7675 specify the area to draw in (e.g. excluding the frame).
7676
7677 Changing the margins will trigger a resizeEvent().
7678
7679 \sa contentsRect(), contentsMargins()
7680*/
7681void QWidget::setContentsMargins(const QMargins &margins)
7682{
7683 setContentsMargins(margins.left(), margins.top(),
7684 margins.right(), margins.bottom());
7685}
7686
7687void QWidgetPrivate::updateContentsRect()
7688{
7689 Q_Q(QWidget);
7690
7691 if (layout)
7692 layout->update(); //force activate; will do updateGeometry
7693 else
7694 q->updateGeometry();
7695
7696 if (q->isVisible()) {
7697 q->update();
7698 QResizeEvent e(q->data->crect.size(), q->data->crect.size());
7699 QCoreApplication::sendEvent(q, &e);
7700 } else {
7701 q->setAttribute(Qt::WA_PendingResizeEvent, true);
7702 }
7703
7704 QEvent e(QEvent::ContentsRectChange);
7705 QCoreApplication::sendEvent(q, &e);
7706}
7707
7708/*!
7709 \since 4.6
7710
7711 \brief The contentsMargins function returns the widget's contents margins.
7712
7713 \sa setContentsMargins(), contentsRect()
7714 */
7715QMargins QWidget::contentsMargins() const
7716{
7717 Q_D(const QWidget);
7718 QMargins userMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
7719 return testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea) ?
7720 userMargins | d->safeAreaMargins() : userMargins;
7721}
7722
7723/*!
7724 Returns the area inside the widget's margins.
7725
7726 \sa setContentsMargins(), contentsMargins()
7727*/
7728QRect QWidget::contentsRect() const
7729{
7730 return rect() - contentsMargins();
7731}
7732
7733QMargins QWidgetPrivate::safeAreaMargins() const
7734{
7735 Q_Q(const QWidget);
7736
7737#if QT_CONFIG(graphicsview)
7738 // Don't report margins for proxied widgets, as the logic
7739 // below doesn't handle that case (yet).
7740 if (nearestGraphicsProxyWidget(q))
7741 return QMargins();
7742#endif
7743
7744 QWidget *nativeWidget = q->window();
7745 if (!nativeWidget->windowHandle())
7746 return QMargins();
7747
7748 QMargins safeAreaMargins = nativeWidget->windowHandle()->safeAreaMargins();
7749
7750 if (!q->isWindow()) {
7751 // In theory the native parent widget already has a contents rect reflecting
7752 // the safe area of that widget, but we can't be sure that the widget or child
7753 // widgets of that widget have respected the contents rect when setting their
7754 // geometry, so we need to manually compute the safe area.
7755
7756 // Unless the native widget doesn't have any margins, in which case there's
7757 // nothing for us to compute.
7758 if (safeAreaMargins.isNull())
7759 return QMargins();
7760
7761 // Or, if one of our ancestors are in a layout that does not have WA_LayoutOnEntireRect
7762 // set, and the widget respects the safe area, then we know that the layout has already
7763 // taken care of placing us inside the safe area, by taking the contents rect of its
7764 // parent widget into account.
7765 const QWidget *assumedSafeWidget = nullptr;
7766 for (const QWidget *w = q; w != nativeWidget; w = w->parentWidget()) {
7767 QWidget *parentWidget = w->parentWidget();
7768 if (!parentWidget->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
7769 continue; // Layout can't help us
7770
7771 if (parentWidget->testAttribute(Qt::WA_LayoutOnEntireRect))
7772 continue; // Layout not going to help us
7773
7774 QLayout *layout = parentWidget->layout();
7775 if (!layout)
7776 continue;
7777
7778 if (layout->geometry().isNull())
7779 continue; // Layout hasn't been activated yet
7780
7781 if (layout->indexOf(w) < 0)
7782 continue; // Widget is not in layout
7783
7784 assumedSafeWidget = w;
7785 break;
7786 }
7787
7788#if !defined(QT_DEBUG)
7789 if (assumedSafeWidget) {
7790 // We found a layout that we assume will take care of keeping us within the safe area
7791 // For debug builds we still map the safe area using the fallback logic, so that we
7792 // can detect any misbehaving layouts.
7793 return QMargins();
7794 }
7795#endif
7796
7797 // In all other cases we need to map the safe area of the native parent to the widget.
7798 // This depends on the widget being positioned and sized already, which means the initial
7799 // layout will be wrong, but the layout will then adjust itself.
7800 QPoint topLeftMargins = q->mapFrom(nativeWidget, QPoint(safeAreaMargins.left(), safeAreaMargins.top()));
7801 QRect widgetRect = q->isVisible() ? q->visibleRegion().boundingRect() : q->rect();
7802 QPoint bottomRightMargins = widgetRect.bottomRight() - q->mapFrom(nativeWidget,
7803 nativeWidget->rect().bottomRight() - QPoint(safeAreaMargins.right(), safeAreaMargins.bottom()));
7804
7805 // Margins should never be negative
7806 safeAreaMargins = QMargins(qMax(0, topLeftMargins.x()), qMax(0, topLeftMargins.y()),
7807 qMax(0, bottomRightMargins.x()), qMax(0, bottomRightMargins.y()));
7808
7809 if (!safeAreaMargins.isNull() && assumedSafeWidget) {
7810 QLayout *layout = assumedSafeWidget->parentWidget()->layout();
7811 qWarning() << layout << "is laying out" << assumedSafeWidget
7812 << "outside of the contents rect of" << layout->parentWidget();
7813 return QMargins(); // Return empty margin to visually highlight the error
7814 }
7815 }
7816
7817 return safeAreaMargins;
7818}
7819
7820/*!
7821 \fn void QWidget::customContextMenuRequested(const QPoint &pos)
7822
7823 This signal is emitted when the widget's \l contextMenuPolicy is
7824 Qt::CustomContextMenu, and the user has requested a context menu on
7825 the widget. The position \a pos is the position of the context menu
7826 event that the widget receives. Normally this is in widget
7827 coordinates. The exception to this rule is QAbstractScrollArea and
7828 its subclasses that map the context menu event to coordinates of the
7829 \l{QAbstractScrollArea::viewport()}{viewport()}.
7830
7831
7832 \sa mapToGlobal(), QMenu, contextMenuPolicy
7833*/
7834
7835
7836/*!
7837 \property QWidget::contextMenuPolicy
7838 \brief how the widget shows a context menu
7839
7840 The default value of this property is Qt::DefaultContextMenu,
7841 which means the contextMenuEvent() handler is called. Other values
7842 are Qt::NoContextMenu, Qt::PreventContextMenu,
7843 Qt::ActionsContextMenu, and Qt::CustomContextMenu. With
7844 Qt::CustomContextMenu, the signal customContextMenuRequested() is
7845 emitted.
7846
7847 \sa contextMenuEvent(), customContextMenuRequested(), actions()
7848*/
7849
7850Qt::ContextMenuPolicy QWidget::contextMenuPolicy() const
7851{
7852 return (Qt::ContextMenuPolicy)data->context_menu_policy;
7853}
7854
7855void QWidget::setContextMenuPolicy(Qt::ContextMenuPolicy policy)
7856{
7857 data->context_menu_policy = (uint) policy;
7858}
7859
7860/*!
7861 \property QWidget::focusPolicy
7862 \brief the way the widget accepts keyboard focus
7863
7864 The policy is Qt::TabFocus if the widget accepts keyboard
7865 focus by tabbing, Qt::ClickFocus if the widget accepts
7866 focus by clicking, Qt::StrongFocus if it accepts both, and
7867 Qt::NoFocus (the default) if it does not accept focus at
7868 all.
7869
7870 You must enable keyboard focus for a widget if it processes
7871 keyboard events. This is normally done from the widget's
7872 constructor. For instance, the QLineEdit constructor calls
7873 setFocusPolicy(Qt::StrongFocus).
7874
7875 If the widget has a focus proxy, then the focus policy will
7876 be propagated to it.
7877
7878 \sa focusInEvent(), focusOutEvent(), keyPressEvent(), keyReleaseEvent(), enabled
7879*/
7880
7881
7882Qt::FocusPolicy QWidget::focusPolicy() const
7883{
7884 return (Qt::FocusPolicy)data->focus_policy;
7885}
7886
7887void QWidget::setFocusPolicy(Qt::FocusPolicy policy)
7888{
7889 data->focus_policy = (uint) policy;
7890 Q_D(QWidget);
7891 if (d->extra && d->extra->focus_proxy)
7892 d->extra->focus_proxy->setFocusPolicy(policy);
7893}
7894
7895/*!
7896 \property QWidget::updatesEnabled
7897 \brief whether updates are enabled
7898
7899 An updates enabled widget receives paint events and has a system
7900 background; a disabled widget does not. This also implies that
7901 calling update() and repaint() has no effect if updates are
7902 disabled.
7903
7904 By default, this property is \c true.
7905
7906 setUpdatesEnabled() is normally used to disable updates for a
7907 short period of time, for instance to avoid screen flicker during
7908 large changes. In Qt, widgets normally do not generate screen
7909 flicker, but on X11 the server might erase regions on the screen
7910 when widgets get hidden before they can be replaced by other
7911 widgets. Disabling updates solves this.
7912
7913 Example:
7914 \snippet code/src_gui_kernel_qwidget.cpp 13
7915
7916 Disabling a widget implicitly disables all its children. Enabling a widget
7917 enables all child widgets \e except top-level widgets or those that
7918 have been explicitly disabled. Re-enabling updates implicitly calls
7919 update() on the widget.
7920
7921 \sa paintEvent()
7922*/
7923void QWidget::setUpdatesEnabled(bool enable)
7924{
7925 Q_D(QWidget);
7926 setAttribute(Qt::WA_ForceUpdatesDisabled, !enable);
7927 d->setUpdatesEnabled_helper(enable);
7928}
7929
7930/*!
7931 Shows the widget and its child widgets.
7932
7933 For child windows, this is equivalent to calling setVisible(true).
7934 Otherwise, it is equivalent to calling showFullScreen(), showMaximized(),
7935 or setVisible(true), depending on the platform's default behavior for the window flags.
7936
7937 \sa raise(), showEvent(), hide(), setVisible(), showMinimized(), showMaximized(),
7938 showNormal(), isVisible(), windowFlags()
7939*/
7940void QWidget::show()
7941{
7942 // Note: We don't call showNormal() as not to clobber Qt::Window(Max/Min)imized
7943
7944 if (!isWindow()) {
7945 setVisible(true);
7946 } else {
7947 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
7948 Qt::WindowState defaultState = platformIntegration->defaultWindowState(data->window_flags);
7949 if (defaultState == Qt::WindowFullScreen)
7950 showFullScreen();
7951 else if (defaultState == Qt::WindowMaximized)
7952 showMaximized();
7953 else
7954 setVisible(true);
7955 }
7956}
7957
7958/*! \internal
7959
7960 Makes the widget visible in the isVisible() meaning of the word.
7961 It is only called for toplevels or widgets with visible parents.
7962 */
7963void QWidgetPrivate::show_recursive()
7964{
7965 Q_Q(QWidget);
7966 // polish if necessary
7967
7968 if (!q->testAttribute(Qt::WA_WState_Created))
7969 createRecursively();
7970 q->ensurePolished();
7971
7972 if (!q->isWindow() && q->parentWidget()->d_func()->layout && !q->parentWidget()->data->in_show)
7973 q->parentWidget()->d_func()->layout->activate();
7974 // activate our layout before we and our children become visible
7975 if (layout)
7976 layout->activate();
7977
7978 show_helper();
7979}
7980
7981void QWidgetPrivate::sendPendingMoveAndResizeEvents(bool recursive, bool disableUpdates)
7982{
7983 Q_Q(QWidget);
7984
7985 disableUpdates = disableUpdates && q->updatesEnabled();
7986 if (disableUpdates)
7987 q->setAttribute(Qt::WA_UpdatesDisabled);
7988
7989 if (q->testAttribute(Qt::WA_PendingMoveEvent)) {
7990 QMoveEvent e(data.crect.topLeft(), data.crect.topLeft());
7991 QCoreApplication::sendEvent(q, &e);
7992 q->setAttribute(Qt::WA_PendingMoveEvent, false);
7993 }
7994
7995 if (q->testAttribute(Qt::WA_PendingResizeEvent)) {
7996 QResizeEvent e(data.crect.size(), QSize());
7997 QCoreApplication::sendEvent(q, &e);
7998 q->setAttribute(Qt::WA_PendingResizeEvent, false);
7999 }
8000
8001 if (disableUpdates)
8002 q->setAttribute(Qt::WA_UpdatesDisabled, false);
8003
8004 if (!recursive)
8005 return;
8006
8007 for (int i = 0; i < children.size(); ++i) {
8008 if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
8009 child->d_func()->sendPendingMoveAndResizeEvents(recursive, disableUpdates);
8010 }
8011}
8012
8013void QWidgetPrivate::activateChildLayoutsRecursively()
8014{
8015 sendPendingMoveAndResizeEvents(false, true);
8016
8017 for (int i = 0; i < children.size(); ++i) {
8018 QWidget *child = qobject_cast<QWidget *>(children.at(i));
8019 if (!child || child->isHidden() || child->isWindow())
8020 continue;
8021
8022 child->ensurePolished();
8023
8024 // Activate child's layout
8025 QWidgetPrivate *childPrivate = child->d_func();
8026 if (childPrivate->layout)
8027 childPrivate->layout->activate();
8028
8029 // Pretend we're visible.
8030 const bool wasVisible = child->isVisible();
8031 if (!wasVisible)
8032 child->setAttribute(Qt::WA_WState_Visible);
8033
8034 // Do the same for all my children.
8035 childPrivate->activateChildLayoutsRecursively();
8036
8037 // We're not cheating anymore.
8038 if (!wasVisible)
8039 child->setAttribute(Qt::WA_WState_Visible, false);
8040 }
8041}
8042
8043void QWidgetPrivate::show_helper()
8044{
8045 Q_Q(QWidget);
8046 data.in_show = true; // qws optimization
8047 // make sure we receive pending move and resize events
8048 sendPendingMoveAndResizeEvents();
8049
8050 // become visible before showing all children
8051 q->setAttribute(Qt::WA_WState_Visible);
8052
8053 // finally show all children recursively
8054 showChildren(false);
8055
8056
8057
8058 const bool isWindow = q->isWindow();
8059#if QT_CONFIG(graphicsview)
8060 bool isEmbedded = isWindow && q->graphicsProxyWidget() != nullptr;
8061#else
8062 bool isEmbedded = false;
8063#endif
8064
8065 // popup handling: new popups and tools need to be raised, and
8066 // existing popups must be closed. Also propagate the current
8067 // windows's KeyboardFocusChange status.
8068 if (isWindow && !isEmbedded) {
8069 if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
8070 q->raise();
8071 if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
8072 q->setAttribute(Qt::WA_KeyboardFocusChange);
8073 } else {
8074 while (QApplication::activePopupWidget()) {
8075 if (!QApplication::activePopupWidget()->close())
8076 break;
8077 }
8078 }
8079 }
8080
8081 // Automatic embedding of child windows of widgets already embedded into
8082 // QGraphicsProxyWidget when they are shown the first time.
8083#if QT_CONFIG(graphicsview)
8084 if (isWindow) {
8085 if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
8086 QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
8087 if (ancestorProxy) {
8088 isEmbedded = true;
8089 ancestorProxy->d_func()->embedSubWindow(q);
8090 }
8091 }
8092 }
8093#else
8094 Q_UNUSED(isEmbedded);
8095#endif
8096
8097 // send the show event before showing the window
8098 QShowEvent showEvent;
8099 QCoreApplication::sendEvent(q, &showEvent);
8100
8101 show_sys();
8102
8103 if (!isEmbedded && q->windowType() == Qt::Popup)
8104 qApp->d_func()->openPopup(q);
8105
8106#if QT_CONFIG(accessibility)
8107 if (q->windowType() != Qt::ToolTip) { // Tooltips are read aloud twice in MS narrator.
8108 QAccessibleEvent event(q, QAccessible::ObjectShow);
8109 QAccessible::updateAccessibility(&event);
8110 }
8111#endif
8112
8113 if (QApplicationPrivate::hidden_focus_widget == q) {
8114 QApplicationPrivate::hidden_focus_widget = nullptr;
8115 q->setFocus(Qt::OtherFocusReason);
8116 }
8117
8118 // Process events when showing a Qt::SplashScreen widget before the event loop
8119 // is spinnning; otherwise it might not show up on particular platforms.
8120 // This makes QSplashScreen behave the same on all platforms.
8121 if (!qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
8122 QCoreApplication::processEvents();
8123
8124 data.in_show = false; // reset qws optimization
8125}
8126
8127void QWidgetPrivate::show_sys()
8128{
8129 Q_Q(QWidget);
8130
8131 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8132
8133 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8134 invalidateBackingStore(q->rect());
8135 q->setAttribute(Qt::WA_Mapped);
8136 // add our window the modal window list (native dialogs)
8137 if (window && q->isWindow()
8138#if QT_CONFIG(graphicsview)
8139 && (!extra || !extra->proxyWidget)
8140#endif
8141 && q->windowModality() != Qt::NonModal) {
8142 QGuiApplicationPrivate::showModalWindow(window);
8143 }
8144 return;
8145 }
8146
8147 if (renderToTexture && !q->isWindow())
8148 QCoreApplication::postEvent(q->parentWidget(), new QUpdateLaterEvent(q->geometry()));
8149 else
8150 QCoreApplication::postEvent(q, new QUpdateLaterEvent(q->rect()));
8151
8152 if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow))
8153 || q->testAttribute(Qt::WA_OutsideWSRange)) {
8154 return;
8155 }
8156
8157 if (window) {
8158 if (q->isWindow())
8159 fixPosIncludesFrame();
8160 QRect geomRect = q->geometry();
8161 if (!q->isWindow()) {
8162 QPoint topLeftOfWindow = q->mapTo(q->nativeParentWidget(),QPoint());
8163 geomRect.moveTopLeft(topLeftOfWindow);
8164 }
8165 const QRect windowRect = window->geometry();
8166 if (windowRect != geomRect) {
8167 if (q->testAttribute(Qt::WA_Moved)
8168 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
8169 window->setGeometry(geomRect);
8170 else
8171 window->resize(geomRect.size());
8172 }
8173
8174#ifndef QT_NO_CURSOR
8175 qt_qpa_set_cursor(q, false); // Needed in case cursor was set before show
8176#endif
8177 invalidateBackingStore(q->rect());
8178 window->setNativeWindowVisibility(true);
8179 // Was the window moved by the Window system or QPlatformWindow::initialGeometry() ?
8180 if (window->isTopLevel()) {
8181 const QPoint crectTopLeft = q->data->crect.topLeft();
8182 const QPoint windowTopLeft = window->geometry().topLeft();
8183 if (crectTopLeft == QPoint(0, 0) && windowTopLeft != crectTopLeft)
8184 q->data->crect.moveTopLeft(windowTopLeft);
8185 }
8186 }
8187}
8188
8189/*!
8190 Hides the widget. This function is equivalent to
8191 setVisible(false).
8192
8193
8194 \note If you are working with QDialog or its subclasses and you invoke
8195 the show() function after this function, the dialog will be displayed in
8196 its original position.
8197
8198 \sa hideEvent(), isHidden(), show(), setVisible(), isVisible(), close()
8199*/
8200void QWidget::hide()
8201{
8202 setVisible(false);
8203}
8204
8205/*!\internal
8206 */
8207void QWidgetPrivate::hide_helper()
8208{
8209 Q_Q(QWidget);
8210
8211 bool isEmbedded = false;
8212#if QT_CONFIG(graphicsview)
8213 isEmbedded = q->isWindow() && !bypassGraphicsProxyWidget(q) && nearestGraphicsProxyWidget(q->parentWidget()) != nullptr;
8214#else
8215 Q_UNUSED(isEmbedded);
8216#endif
8217
8218 if (!isEmbedded && (q->windowType() == Qt::Popup))
8219 qApp->d_func()->closePopup(q);
8220
8221 q->setAttribute(Qt::WA_Mapped, false);
8222 hide_sys();
8223
8224 bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
8225
8226 if (wasVisible) {
8227 q->setAttribute(Qt::WA_WState_Visible, false);
8228
8229 }
8230
8231 QHideEvent hideEvent;
8232 QCoreApplication::sendEvent(q, &hideEvent);
8233 hideChildren(false);
8234
8235 // next bit tries to move the focus if the focus widget is now
8236 // hidden.
8237 if (wasVisible) {
8238 qApp->d_func()->sendSyntheticEnterLeave(q);
8239 QWidget *fw = QApplication::focusWidget();
8240 while (fw && !fw->isWindow()) {
8241 if (fw == q) {
8242 q->focusNextPrevChild(true);
8243 break;
8244 }
8245 fw = fw->parentWidget();
8246 }
8247 }
8248
8249 if (QWidgetRepaintManager *repaintManager = maybeRepaintManager())
8250 repaintManager->removeDirtyWidget(q);
8251
8252#if QT_CONFIG(accessibility)
8253 if (wasVisible) {
8254 QAccessibleEvent event(q, QAccessible::ObjectHide);
8255 QAccessible::updateAccessibility(&event);
8256 }
8257#endif
8258}
8259
8260void QWidgetPrivate::hide_sys()
8261{
8262 Q_Q(QWidget);
8263
8264 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8265
8266 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8267 q->setAttribute(Qt::WA_Mapped, false);
8268 // remove our window from the modal window list (native dialogs)
8269 if (window && q->isWindow()
8270#if QT_CONFIG(graphicsview)
8271 && (!extra || !extra->proxyWidget)
8272#endif
8273 && q->windowModality() != Qt::NonModal) {
8274 QGuiApplicationPrivate::hideModalWindow(window);
8275 }
8276 // do not return here, if window non-zero, we must hide it
8277 }
8278
8279 deactivateWidgetCleanup();
8280
8281 if (!q->isWindow()) {
8282 QWidget *p = q->parentWidget();
8283 if (p &&p->isVisible()) {
8284 if (renderToTexture)
8285 p->d_func()->invalidateBackingStore(q->geometry());
8286 else
8287 invalidateBackingStore(q->rect());
8288 }
8289 } else {
8290 invalidateBackingStore(q->rect());
8291 }
8292
8293 if (window)
8294 window->setNativeWindowVisibility(false);
8295}
8296
8297/*!
8298 \fn bool QWidget::isHidden() const
8299
8300 Returns \c true if the widget is hidden, otherwise returns \c false.
8301
8302 A hidden widget will only become visible when show() is called on
8303 it. It will not be automatically shown when the parent is shown.
8304
8305 To check visibility, use !isVisible() instead (notice the exclamation mark).
8306
8307 isHidden() implies !isVisible(), but a widget can be not visible
8308 and not hidden at the same time. This is the case for widgets that are children of
8309 widgets that are not visible.
8310
8311
8312 Widgets are hidden if:
8313 \list
8314 \li they were created as independent windows,
8315 \li they were created as children of visible widgets,
8316 \li hide() or setVisible(false) was called.
8317 \endlist
8318*/
8319
8320void QWidget::setVisible(bool visible)
8321{
8322 Q_D(QWidget);
8323 qCDebug(lcWidgetShowHide) << "Setting visibility of" << this
8324 << "with attributes" << WidgetAttributes{this}
8325 << "to" << visible << "via QWidget";
8326
8327 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) == !visible)
8328 return;
8329
8330 if (d->dontSetExplicitShowHide) {
8331 d->dontSetExplicitShowHide = false;
8332 } else {
8333 // Remember that setVisible was called explicitly
8334 setAttribute(Qt::WA_WState_ExplicitShowHide);
8335 }
8336
8337 d->setVisible(visible);
8338}
8339
8340// This method is called from QWidgetWindow in response to QWindow::setVisible,
8341// and should match the semantics of QWindow::setVisible. QWidget::setVisible on
8342// the other hand keeps track of WA_WState_ExplicitShowHide in addition.
8343void QWidgetPrivate::setVisible(bool visible)
8344{
8345 Q_Q(QWidget);
8346 qCDebug(lcWidgetShowHide) << "Setting visibility of" << q
8347 << "with attributes" << WidgetAttributes{q}
8348 << "to" << visible << "via QWidgetPrivate";
8349
8350 if (visible) { // show
8351 // Designer uses a trick to make grabWidget work without showing
8352 if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
8353 && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
8354 q->parentWidget()->window()->d_func()->createRecursively();
8355
8356 //create toplevels but not children of non-visible parents
8357 QWidget *pw = q->parentWidget();
8358 if (!q->testAttribute(Qt::WA_WState_Created)
8359 && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
8360 q->create();
8361 }
8362
8363 bool wasResized = q->testAttribute(Qt::WA_Resized);
8364 Qt::WindowStates initialWindowState = q->windowState();
8365
8366 // polish if necessary
8367 q->ensurePolished();
8368
8369 // whether we need to inform the parent widget immediately
8370 bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
8371 // we are no longer hidden
8372 q->setAttribute(Qt::WA_WState_Hidden, false);
8373
8374 if (needUpdateGeometry)
8375 updateGeometry_helper(true);
8376
8377 // activate our layout before we and our children become visible
8378 if (layout)
8379 layout->activate();
8380
8381 if (!q->isWindow()) {
8382 QWidget *parent = q->parentWidget();
8383 while (parent && parent->isVisible() && parent->d_func()->layout && !parent->data->in_show) {
8384 parent->d_func()->layout->activate();
8385 if (parent->isWindow())
8386 break;
8387 parent = parent->parentWidget();
8388 }
8389 if (parent)
8390 parent->d_func()->setDirtyOpaqueRegion();
8391 }
8392
8393 // adjust size if necessary
8394 if (!wasResized
8395 && (q->isWindow() || !q->parentWidget()->d_func()->layout)) {
8396 if (q->isWindow()) {
8397 q->adjustSize();
8398 if (q->windowState() != initialWindowState)
8399 q->setWindowState(initialWindowState);
8400 } else {
8401 q->adjustSize();
8402 }
8403 q->setAttribute(Qt::WA_Resized, false);
8404 }
8405
8406 q->setAttribute(Qt::WA_KeyboardFocusChange, false);
8407
8408 if (q->isWindow() || q->parentWidget()->isVisible()) {
8409 show_helper();
8410
8411 qApp->d_func()->sendSyntheticEnterLeave(q);
8412 }
8413
8414 QEvent showToParentEvent(QEvent::ShowToParent);
8415 QCoreApplication::sendEvent(q, &showToParentEvent);
8416 } else { // hide
8417 if (QApplicationPrivate::hidden_focus_widget == q)
8418 QApplicationPrivate::hidden_focus_widget = nullptr;
8419
8420 // hw: The test on getOpaqueRegion() needs to be more intelligent
8421 // currently it doesn't work if the widget is hidden (the region will
8422 // be clipped). The real check should be testing the cached region
8423 // (and dirty flag) directly.
8424 if (!q->isWindow() && q->parentWidget()) // && !d->getOpaqueRegion().isEmpty())
8425 q->parentWidget()->d_func()->setDirtyOpaqueRegion();
8426
8427 if (!q->testAttribute(Qt::WA_WState_Hidden)) {
8428 q->setAttribute(Qt::WA_WState_Hidden);
8429 hide_helper();
8430 }
8431
8432 // invalidate layout similar to updateGeometry()
8433 if (!q->isWindow() && q->parentWidget()) {
8434 if (q->parentWidget()->d_func()->layout)
8435 q->parentWidget()->d_func()->layout->invalidate();
8436 else if (q->parentWidget()->isVisible())
8437 QCoreApplication::postEvent(q->parentWidget(), new QEvent(QEvent::LayoutRequest));
8438 }
8439
8440 QEvent hideToParentEvent(QEvent::HideToParent);
8441 QCoreApplication::sendEvent(q, &hideToParentEvent);
8442 }
8443}
8444
8445/*!
8446 Convenience function, equivalent to setVisible(!\a hidden).
8447*/
8448void QWidget::setHidden(bool hidden)
8449{
8450 setVisible(!hidden);
8451}
8452
8453bool QWidgetPrivate::isExplicitlyHidden() const
8454{
8455 Q_Q(const QWidget);
8456 return q->isHidden() && q->testAttribute(Qt::WA_WState_ExplicitShowHide);
8457}
8458
8459void QWidgetPrivate::_q_showIfNotHidden()
8460{
8461 Q_Q(QWidget);
8462 if (!isExplicitlyHidden())
8463 q->setVisible(true);
8464}
8465
8466void QWidgetPrivate::showChildren(bool spontaneous)
8467{
8468 Q_Q(QWidget);
8469 qCDebug(lcWidgetShowHide) << "Showing children of" << q
8470 << "spontaneously" << spontaneous;
8471
8472 QList<QObject*> childList = children;
8473 for (int i = 0; i < childList.size(); ++i) {
8474 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8475 if (!widget)
8476 continue;
8477 qCDebug(lcWidgetShowHide) << "Considering" << widget
8478 << "with attributes" << WidgetAttributes{widget};
8479 if (widget->windowHandle() && !widget->testAttribute(Qt::WA_WState_ExplicitShowHide))
8480 widget->setAttribute(Qt::WA_WState_Hidden, false);
8481 if (widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden))
8482 continue;
8483 if (spontaneous) {
8484 widget->setAttribute(Qt::WA_Mapped);
8485 widget->d_func()->showChildren(true);
8486 QShowEvent e;
8487 QApplication::sendSpontaneousEvent(widget, &e);
8488 } else {
8489 if (widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) {
8490 widget->d_func()->show_recursive();
8491 } else {
8492 // Call QWidget::setVisible() here, so that subclasses
8493 // that (wrongly) override setVisible to do initialization
8494 // will still be notified that they are made visible, but
8495 // do so without triggering ExplicitShowHide.
8496 widget->d_func()->dontSetExplicitShowHide = true;
8497 widget->setVisible(true);
8498 widget->d_func()->dontSetExplicitShowHide = false;
8499 }
8500 }
8501 }
8502}
8503
8504void QWidgetPrivate::hideChildren(bool spontaneous)
8505{
8506 Q_Q(QWidget);
8507 qCDebug(lcWidgetShowHide) << "Hiding children of" << q
8508 << "spontaneously" << spontaneous;
8509
8510 QList<QObject*> childList = children;
8511 for (int i = 0; i < childList.size(); ++i) {
8512 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8513 if (!widget)
8514 continue;
8515 qCDebug(lcWidgetShowHide) << "Considering" << widget
8516 << "with attributes" << WidgetAttributes{widget};
8517 if (widget->isWindow() || !widget->isVisible())
8518 continue;
8519
8520 if (spontaneous)
8521 widget->setAttribute(Qt::WA_Mapped, false);
8522 else
8523 widget->setAttribute(Qt::WA_WState_Visible, false);
8524 widget->d_func()->hideChildren(spontaneous);
8525 QHideEvent e;
8526 if (spontaneous) {
8527 QApplication::sendSpontaneousEvent(widget, &e);
8528 } else {
8529 QCoreApplication::sendEvent(widget, &e);
8530 if (widget->internalWinId()
8531 && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) {
8532 // hide_sys() on an ancestor won't have any affect on this
8533 // widget, so it needs an explicit hide_sys() of its own
8534 widget->d_func()->hide_sys();
8535 }
8536 }
8537 qApp->d_func()->sendSyntheticEnterLeave(widget);
8538#if QT_CONFIG(accessibility)
8539 if (!spontaneous) {
8540 QAccessibleEvent event(widget, QAccessible::ObjectHide);
8541 QAccessible::updateAccessibility(&event);
8542 }
8543#endif
8544 }
8545
8546 // If the window of this widget is not closed, then the leave event
8547 // will eventually handle the widget under mouse use case.
8548 // Otherwise, we need to explicitly handle it here.
8549 if (QWidget* widgetWindow = q->window();
8550 widgetWindow && widgetWindow->data->is_closing) {
8551 q->setAttribute(Qt::WA_UnderMouse, false);
8552 }
8553}
8554
8555/*!
8556 \internal
8557
8558 For windows, this is called from the QWidgetWindow::handleCloseEvent implementation,
8559 which QWidget::close indirectly calls by closing the QWindow. \a mode will be
8560 CloseWithEvent if QWidgetWindow::handleCloseEvent is called indirectly by
8561 QWindow::close, and CloseWithSpontaneousEvent if the close event originates from the
8562 system (i.e. the user clicked the close button in the title bar).
8563
8564 QDialog calls this method directly in its hide() implementation, which might be
8565 called from the QDialog::closeEvent override. \a mode will be set to CloseNoEvent
8566 to prevent recursion.
8567
8568 For non-windows, this is called directly by QWidget::close, and \a mode will be
8569 CloseWithEvent.
8570
8571 The function is also called by the QWidget destructor, with \a mode set to CloseNoEvent.
8572*/
8573bool QWidgetPrivate::handleClose(CloseMode mode)
8574{
8575 Q_Q(QWidget);
8576 qCDebug(lcWidgetShowHide) << "Handling close event for" << q;
8577
8578 if (data.is_closing)
8579 return true;
8580
8581 // We might not have initiated the close, so update the state now that we know
8582 data.is_closing = true;
8583
8584 QPointer<QWidget> that = q;
8585
8586 if (data.in_destructor)
8587 mode = CloseNoEvent;
8588
8589 if (mode != CloseNoEvent) {
8590 QCloseEvent e;
8591 if (mode == CloseWithSpontaneousEvent)
8592 QApplication::sendSpontaneousEvent(q, &e);
8593 else
8594 QCoreApplication::sendEvent(q, &e);
8595 if (!that.isNull() && !e.isAccepted()) {
8596 data.is_closing = false;
8597 return false;
8598 }
8599 }
8600
8601 // even for windows, make sure we deliver a hide event and that all children get hidden
8602 if (!that.isNull() && !q->isHidden())
8603 q->hide();
8604
8605 if (!that.isNull()) {
8606 data.is_closing = false;
8607 if (q->testAttribute(Qt::WA_DeleteOnClose)) {
8608 q->setAttribute(Qt::WA_DeleteOnClose, false);
8609 q->deleteLater();
8610 }
8611 }
8612 return true;
8613}
8614
8615
8616/*!
8617 Closes this widget. Returns \c true if the widget was closed;
8618 otherwise returns \c false.
8619
8620 First it sends the widget a QCloseEvent. The widget is
8621 \l{hide()}{hidden} if it \l{QEvent::accept()}{accepts}
8622 the close event. If it \l{QEvent::ignore()}{ignores}
8623 the event, nothing happens. The default
8624 implementation of QWidget::closeEvent() accepts the close event.
8625
8626 If the widget has the Qt::WA_DeleteOnClose flag, the widget
8627 is also deleted. A close events is delivered to the widget no
8628 matter if the widget is visible or not.
8629
8630 The \l QGuiApplication::lastWindowClosed() signal is emitted when the
8631 last visible primary window (i.e. window with no parent) with the
8632 Qt::WA_QuitOnClose attribute set is closed. By default this
8633 attribute is set for all widgets except transient windows such as
8634 splash screens, tool windows, and popup menus.
8635
8636*/
8637
8638bool QWidget::close()
8639{
8640 return d_func()->close();
8641}
8642
8643bool QWidgetPrivate::close()
8644{
8645 // FIXME: We're not setting is_closing here, even though that would
8646 // make sense, as the code below will not end up in handleClose to
8647 // reset is_closing when there's a QWindow, but no QPlatformWindow,
8648 // and we can't assume close is synchronous so we can't reset it here.
8649
8650 // Close native widgets via QWindow::close() in order to run QWindow
8651 // close code. The QWidget-specific close code in handleClose() will
8652 // in this case be called from the Close event handler in QWidgetWindow.
8653 if (QWindow *widgetWindow = windowHandle()) {
8654 if (widgetWindow->isTopLevel())
8655 return widgetWindow->close();
8656 }
8657
8658 return handleClose(QWidgetPrivate::CloseWithEvent);
8659}
8660
8661/*!
8662 \property QWidget::visible
8663 \brief whether the widget is visible
8664
8665 Calling setVisible(true) or show() sets the widget to visible
8666 status if all its parent widgets up to the window are visible. If
8667 an ancestor is not visible, the widget won't become visible until
8668 all its ancestors are shown. If its size or position has changed,
8669 Qt guarantees that a widget gets move and resize events just
8670 before it is shown. If the widget has not been resized yet, Qt
8671 will adjust the widget's size to a useful default using
8672 adjustSize().
8673
8674 Calling setVisible(false) or hide() hides a widget explicitly. An
8675 explicitly hidden widget will never become visible, even if all
8676 its ancestors become visible, unless you show it.
8677
8678 A widget receives show and hide events when its visibility status
8679 changes. Between a hide and a show event, there is no need to
8680 waste CPU cycles preparing or displaying information to the user.
8681 A video application, for example, might simply stop generating new
8682 frames.
8683
8684 A widget that happens to be obscured by other windows on the
8685 screen is considered to be visible. The same applies to iconified
8686 windows and windows that exist on another virtual
8687 desktop (on platforms that support this concept). A widget
8688 receives spontaneous show and hide events when its mapping status
8689 is changed by the window system, e.g. a spontaneous hide event
8690 when the user minimizes the window, and a spontaneous show event
8691 when the window is restored again.
8692
8693 You seldom have to reimplement the setVisible() function. If
8694 you need to change some settings before a widget is shown, use
8695 showEvent() instead. If you need to do some delayed initialization
8696 use the Polish event delivered to the event() function.
8697
8698 \sa show(), hide(), isHidden(), isVisibleTo(), isMinimized(),
8699 showEvent(), hideEvent()
8700*/
8701
8702
8703/*!
8704 Returns \c true if this widget would become visible if \a ancestor is
8705 shown; otherwise returns \c false.
8706
8707 The true case occurs if neither the widget itself nor any parent
8708 up to but excluding \a ancestor has been explicitly hidden.
8709
8710 This function will still return true if the widget is obscured by
8711 other windows on the screen, but could be physically visible if it
8712 or they were to be moved.
8713
8714 isVisibleTo(0) is identical to isVisible().
8715
8716 \sa show(), hide(), isVisible()
8717*/
8718
8719bool QWidget::isVisibleTo(const QWidget *ancestor) const
8720{
8721 if (!ancestor)
8722 return isVisible();
8723 const QWidget * w = this;
8724 while (!w->isHidden()
8725 && !w->isWindow()
8726 && w->parentWidget()
8727 && w->parentWidget() != ancestor)
8728 w = w->parentWidget();
8729 return !w->isHidden();
8730}
8731
8732
8733/*!
8734 Returns the unobscured region where paint events can occur.
8735
8736 For visible widgets, this is an approximation of the area not
8737 covered by other widgets; otherwise, this is an empty region.
8738
8739 The repaint() function calls this function if necessary, so in
8740 general you do not need to call it.
8741
8742*/
8743QRegion QWidget::visibleRegion() const
8744{
8745 Q_D(const QWidget);
8746
8747 QRect clipRect = d->clipRect();
8748 if (clipRect.isEmpty())
8749 return QRegion();
8750 QRegion r(clipRect);
8751 d->subtractOpaqueChildren(r, clipRect);
8752 d->subtractOpaqueSiblings(r);
8753 return r;
8754}
8755
8756
8757QSize QWidgetPrivate::adjustedSize() const
8758{
8759 Q_Q(const QWidget);
8760
8761 QSize s = q->sizeHint();
8762
8763 if (q->isWindow()) {
8764 Qt::Orientations exp;
8765 if (layout) {
8766 if (layout->hasHeightForWidth())
8767 s.setHeight(layout->totalHeightForWidth(s.width()));
8768 exp = layout->expandingDirections();
8769 } else
8770 {
8771 if (q->sizePolicy().hasHeightForWidth())
8772 s.setHeight(q->heightForWidth(s.width()));
8773 exp = q->sizePolicy().expandingDirections();
8774 }
8775 if (exp & Qt::Horizontal)
8776 s.setWidth(qMax(s.width(), 200));
8777 if (exp & Qt::Vertical)
8778 s.setHeight(qMax(s.height(), 100));
8779
8780 QRect screen;
8781 if (const QScreen *screenAtPoint = QGuiApplication::screenAt(q->pos()))
8782 screen = screenAtPoint->geometry();
8783 else
8784 screen = QGuiApplication::primaryScreen()->geometry();
8785
8786 s.setWidth(qMin(s.width(), screen.width()*2/3));
8787 s.setHeight(qMin(s.height(), screen.height()*2/3));
8788
8789 if (QTLWExtra *extra = maybeTopData())
8790 extra->sizeAdjusted = true;
8791 }
8792
8793 if (!s.isValid()) {
8794 QRect r = q->childrenRect(); // get children rectangle
8795 if (r.isNull())
8796 return s;
8797 s = r.size() + QSize(2 * r.x(), 2 * r.y());
8798 }
8799
8800 return s;
8801}
8802
8803/*!
8804 Adjusts the size of the widget to fit its contents.
8805
8806 This function uses sizeHint() if it is valid, i.e., the size hint's width
8807 and height are >= 0. Otherwise, it sets the size to the children
8808 rectangle that covers all child widgets (the union of all child widget
8809 rectangles).
8810
8811 For windows, the screen size is also taken into account. If the sizeHint()
8812 is less than (200, 100) and the size policy is \l{QSizePolicy::Expanding}
8813 {expanding}, the window will be at least (200, 100). The maximum size of
8814 a window is 2/3 of the screen's width and height.
8815
8816 \sa sizeHint(), childrenRect()
8817*/
8818
8819void QWidget::adjustSize()
8820{
8821 Q_D(QWidget);
8822 ensurePolished();
8823 QSize s = d->adjustedSize();
8824
8825 if (d->layout)
8826 d->layout->activate();
8827
8828 if (s.isValid())
8829 resize(s);
8830}
8831
8832
8833/*!
8834 \property QWidget::sizeHint
8835 \brief the recommended size for the widget
8836
8837 If the value of this property is an invalid size, no size is
8838 recommended.
8839
8840 The default implementation of sizeHint() returns an invalid size
8841 if there is no layout for this widget, and returns the layout's
8842 preferred size otherwise.
8843
8844 \sa QSize::isValid(), minimumSizeHint(), sizePolicy(),
8845 setMinimumSize(), updateGeometry()
8846*/
8847
8848QSize QWidget::sizeHint() const
8849{
8850 Q_D(const QWidget);
8851 if (d->layout)
8852 return d->layout->totalSizeHint();
8853 return QSize(-1, -1);
8854}
8855
8856/*!
8857 \property QWidget::minimumSizeHint
8858 \brief the recommended minimum size for the widget
8859
8860 If the value of this property is an invalid size, no minimum size
8861 is recommended.
8862
8863 The default implementation of minimumSizeHint() returns an invalid
8864 size if there is no layout for this widget, and returns the
8865 layout's minimum size otherwise. Most built-in widgets reimplement
8866 minimumSizeHint().
8867
8868 \l QLayout will never resize a widget to a size smaller than the
8869 minimum size hint unless minimumSize() is set or the size policy is
8870 set to QSizePolicy::Ignore. If minimumSize() is set, the minimum
8871 size hint will be ignored.
8872
8873 \sa QSize::isValid(), resize(), setMinimumSize(), sizePolicy()
8874*/
8875QSize QWidget::minimumSizeHint() const
8876{
8877 Q_D(const QWidget);
8878 if (d->layout)
8879 return d->layout->totalMinimumSize();
8880 return QSize(-1, -1);
8881}
8882
8883
8884/*!
8885 \fn QWidget *QWidget::parentWidget() const
8886
8887 Returns the parent of this widget, or \nullptr if it does not have any
8888 parent widget.
8889*/
8890
8891
8892/*!
8893 Returns \c true if this widget is a parent, (or grandparent and so on
8894 to any level), of the given \a child, and both widgets are within
8895 the same window; otherwise returns \c false.
8896*/
8897
8898bool QWidget::isAncestorOf(const QWidget *child) const
8899{
8900 while (child) {
8901 if (child == this)
8902 return true;
8903 if (child->isWindow())
8904 return false;
8905 child = child->parentWidget();
8906 }
8907 return false;
8908}
8909
8910/*****************************************************************************
8911 QWidget event handling
8912 *****************************************************************************/
8913
8914/*!
8915 This is the main event handler; it handles event \a event. You can
8916 reimplement this function in a subclass, but we recommend using
8917 one of the specialized event handlers instead.
8918
8919 Key press and release events are treated differently from other
8920 events. event() checks for Tab and Shift+Tab and tries to move the
8921 focus appropriately. If there is no widget to move the focus to
8922 (or the key press is not Tab or Shift+Tab), event() calls
8923 keyPressEvent().
8924
8925 Mouse and tablet event handling is also slightly special: only
8926 when the widget is \l enabled, event() will call the specialized
8927 handlers such as mousePressEvent(); otherwise it will discard the
8928 event.
8929
8930 This function returns \c true if the event was recognized, otherwise
8931 it returns \c false. If the recognized event was accepted (see \l
8932 QEvent::accepted), any further processing such as event
8933 propagation to the parent widget stops.
8934
8935 \sa closeEvent(), focusInEvent(), focusOutEvent(), enterEvent(),
8936 keyPressEvent(), keyReleaseEvent(), leaveEvent(),
8937 mouseDoubleClickEvent(), mouseMoveEvent(), mousePressEvent(),
8938 mouseReleaseEvent(), moveEvent(), paintEvent(), resizeEvent(),
8939 QObject::event(), QObject::timerEvent()
8940*/
8941
8942bool QWidget::event(QEvent *event)
8943{
8944 Q_D(QWidget);
8945
8946 // ignore mouse and key events when disabled
8947 if (!isEnabled()) {
8948 switch(event->type()) {
8949 case QEvent::TabletPress:
8950 case QEvent::TabletRelease:
8951 case QEvent::TabletMove:
8952 case QEvent::MouseButtonPress:
8953 case QEvent::MouseButtonRelease:
8954 case QEvent::MouseButtonDblClick:
8955 case QEvent::MouseMove:
8956 case QEvent::TouchBegin:
8957 case QEvent::TouchUpdate:
8958 case QEvent::TouchEnd:
8959 case QEvent::TouchCancel:
8960 case QEvent::ContextMenu:
8961 case QEvent::KeyPress:
8962 case QEvent::KeyRelease:
8963#if QT_CONFIG(wheelevent)
8964 case QEvent::Wheel:
8965#endif
8966 return false;
8967 default:
8968 break;
8969 }
8970 }
8971 switch (event->type()) {
8972 case QEvent::PlatformSurface: {
8973 // Sync up QWidget's view of whether or not the widget has been created
8974 switch (static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
8975 case QPlatformSurfaceEvent::SurfaceCreated:
8976 if (!testAttribute(Qt::WA_WState_Created))
8977 create();
8978 break;
8979 case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
8980 if (testAttribute(Qt::WA_WState_Created)) {
8981 // Child windows have already been destroyed by QWindow,
8982 // so we skip them here.
8983 destroy(false, false);
8984 }
8985 break;
8986 }
8987 break;
8988 }
8989 case QEvent::MouseMove:
8990 mouseMoveEvent((QMouseEvent*)event);
8991 break;
8992
8993 case QEvent::MouseButtonPress:
8994 mousePressEvent((QMouseEvent*)event);
8995 break;
8996
8997 case QEvent::MouseButtonRelease:
8998 mouseReleaseEvent((QMouseEvent*)event);
8999 break;
9000
9001 case QEvent::MouseButtonDblClick:
9002 mouseDoubleClickEvent((QMouseEvent*)event);
9003 break;
9004#if QT_CONFIG(wheelevent)
9005 case QEvent::Wheel:
9006 wheelEvent((QWheelEvent*)event);
9007 break;
9008#endif
9009#if QT_CONFIG(tabletevent)
9010 case QEvent::TabletMove:
9011 if (static_cast<QTabletEvent *>(event)->buttons() == Qt::NoButton && !testAttribute(Qt::WA_TabletTracking))
9012 break;
9013 Q_FALLTHROUGH();
9014 case QEvent::TabletPress:
9015 case QEvent::TabletRelease:
9016 tabletEvent((QTabletEvent*)event);
9017 break;
9018#endif
9019 case QEvent::KeyPress: {
9020 QKeyEvent *k = static_cast<QKeyEvent *>(event);
9021 bool res = false;
9022 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier?
9023 if (k->key() == Qt::Key_Backtab
9024 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
9025 res = focusNextPrevChild(false);
9026 else if (k->key() == Qt::Key_Tab)
9027 res = focusNextPrevChild(true);
9028 if (res)
9029 break;
9030 }
9031 keyPressEvent(k);
9032#if QT_CONFIG(whatsthis)
9033 if (!k->isAccepted()
9034 && k->modifiers() & Qt::ShiftModifier && k->key() == Qt::Key_F1
9035 && d->whatsThis.size()) {
9036 QWhatsThis::showText(mapToGlobal(inputMethodQuery(Qt::ImCursorRectangle).toRect().center()), d->whatsThis, this);
9037 k->accept();
9038 }
9039#endif
9040 }
9041 break;
9042
9043 case QEvent::KeyRelease:
9044 keyReleaseEvent((QKeyEvent*)event);
9045 Q_FALLTHROUGH();
9046 case QEvent::ShortcutOverride:
9047 break;
9048
9049 case QEvent::InputMethod:
9050 inputMethodEvent((QInputMethodEvent *) event);
9051 break;
9052
9053 case QEvent::InputMethodQuery: {
9054 QInputMethodQueryEvent *query = static_cast<QInputMethodQueryEvent *>(event);
9055 Qt::InputMethodQueries queries = query->queries();
9056 for (uint i = 0; i < 32; ++i) {
9057 Qt::InputMethodQuery q = (Qt::InputMethodQuery)(int)(queries & (1<<i));
9058 if (q) {
9059 QVariant v = inputMethodQuery(q);
9060 if (q == Qt::ImEnabled && !v.isValid() && isEnabled()) {
9061 // Qt:ImEnabled was added in Qt 5.3. So not all widgets support it, even
9062 // if they implement IM otherwise (by overriding inputMethodQuery()). Instead
9063 // they set the widget attribute Qt::WA_InputMethodEnabled. But this attribute
9064 // will only be set if the widget supports IM _and_ is not read-only. So for
9065 // read-only widgets, not all IM features will be supported when ImEnabled is
9066 // not implemented explicitly (e.g selection handles for read-only widgets on iOS).
9067 v = QVariant(testAttribute(Qt::WA_InputMethodEnabled));
9068 }
9069 query->setValue(q, v);
9070 }
9071 }
9072 query->accept();
9073 }
9074 break;
9075
9076 case QEvent::PolishRequest:
9077 ensurePolished();
9078 break;
9079
9080 case QEvent::Polish: {
9081 style()->polish(this);
9082 setAttribute(Qt::WA_WState_Polished);
9083 if (!QApplication::font(this).isCopyOf(QApplication::font()))
9084 d->resolveFont();
9085 if (!QApplication::palette(this).isCopyOf(QGuiApplication::palette()))
9086 d->resolvePalette();
9087 }
9088 break;
9089
9090 case QEvent::ApplicationWindowIconChange:
9091 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
9092 d->setWindowIcon_sys();
9093 d->setWindowIcon_helper();
9094 }
9095 break;
9096 case QEvent::FocusIn:
9097 focusInEvent((QFocusEvent*)event);
9098 d->updateWidgetTransform(event);
9099 break;
9100
9101 case QEvent::FocusOut:
9102 focusOutEvent((QFocusEvent*)event);
9103 break;
9104
9105 case QEvent::Enter:
9106#if QT_CONFIG(statustip)
9107 if (d->statusTip.size()) {
9108 QStatusTipEvent tip(d->statusTip);
9109 QCoreApplication::sendEvent(const_cast<QWidget *>(this), &tip);
9110 }
9111#endif
9112 enterEvent(static_cast<QEnterEvent*>(event));
9113 break;
9114
9115 case QEvent::Leave:
9116#if QT_CONFIG(statustip)
9117 if (d->statusTip.size()) {
9118 QString empty;
9119 QStatusTipEvent tip(empty);
9120 QCoreApplication::sendEvent(const_cast<QWidget *>(this), &tip);
9121 }
9122#endif
9123 leaveEvent(event);
9124 break;
9125
9126 case QEvent::HoverEnter:
9127 case QEvent::HoverLeave:
9128 update();
9129 break;
9130
9131 case QEvent::Paint:
9132 // At this point the event has to be delivered, regardless
9133 // whether the widget isVisible() or not because it
9134 // already went through the filters
9135 paintEvent((QPaintEvent*)event);
9136 break;
9137
9138 case QEvent::Move:
9139 moveEvent((QMoveEvent*)event);
9140 d->updateWidgetTransform(event);
9141 break;
9142
9143 case QEvent::Resize:
9144 resizeEvent((QResizeEvent*)event);
9145 d->updateWidgetTransform(event);
9146 break;
9147
9148 case QEvent::Close:
9149 closeEvent((QCloseEvent *)event);
9150 break;
9151
9152#ifndef QT_NO_CONTEXTMENU
9153 case QEvent::ContextMenu:
9154 switch (data->context_menu_policy) {
9155 case Qt::PreventContextMenu:
9156 break;
9157 case Qt::DefaultContextMenu:
9158 contextMenuEvent(static_cast<QContextMenuEvent *>(event));
9159 break;
9160 case Qt::CustomContextMenu:
9161 emit customContextMenuRequested(static_cast<QContextMenuEvent *>(event)->pos());
9162 break;
9163#if QT_CONFIG(menu)
9164 case Qt::ActionsContextMenu:
9165 if (d->actions.size()) {
9166 QMenu::exec(d->actions, static_cast<QContextMenuEvent *>(event)->globalPos(),
9167 nullptr, this);
9168 break;
9169 }
9170 Q_FALLTHROUGH();
9171#endif
9172 default:
9173 event->ignore();
9174 break;
9175 }
9176 break;
9177#endif // QT_NO_CONTEXTMENU
9178
9179#if QT_CONFIG(draganddrop)
9180 case QEvent::Drop:
9181 dropEvent((QDropEvent*) event);
9182 break;
9183
9184 case QEvent::DragEnter:
9185 dragEnterEvent((QDragEnterEvent*) event);
9186 break;
9187
9188 case QEvent::DragMove:
9189 dragMoveEvent((QDragMoveEvent*) event);
9190 break;
9191
9192 case QEvent::DragLeave:
9193 dragLeaveEvent((QDragLeaveEvent*) event);
9194 break;
9195#endif
9196
9197 case QEvent::Show:
9198 showEvent((QShowEvent*) event);
9199 break;
9200
9201 case QEvent::Hide:
9202 hideEvent((QHideEvent*) event);
9203 break;
9204
9205 case QEvent::ShowWindowRequest:
9206 if (!isHidden())
9207 d->show_sys();
9208 break;
9209
9210 case QEvent::ApplicationFontChange:
9211 d->resolveFont();
9212 break;
9213 case QEvent::ApplicationPaletteChange:
9214 d->resolvePalette();
9215 break;
9216
9217 case QEvent::ToolBarChange:
9218 case QEvent::ActivationChange:
9219 case QEvent::EnabledChange:
9220 case QEvent::FontChange:
9221 case QEvent::StyleChange:
9222 case QEvent::PaletteChange:
9223 case QEvent::WindowTitleChange:
9224 case QEvent::IconTextChange:
9225 case QEvent::ModifiedChange:
9226 case QEvent::MouseTrackingChange:
9227 case QEvent::TabletTrackingChange:
9228 case QEvent::ParentChange:
9229 case QEvent::LocaleChange:
9230 case QEvent::MacSizeChange:
9231 case QEvent::ContentsRectChange:
9232 case QEvent::ThemeChange:
9233 case QEvent::ReadOnlyChange:
9234 changeEvent(event);
9235 break;
9236
9237 case QEvent::WindowStateChange: {
9238 const bool wasMinimized = static_cast<const QWindowStateChangeEvent *>(event)->oldState() & Qt::WindowMinimized;
9239 if (wasMinimized != isMinimized()) {
9240 QWidget *widget = const_cast<QWidget *>(this);
9241 if (wasMinimized) {
9242 // Always send the spontaneous events here, otherwise it can break the application!
9243 if (!d->childrenShownByExpose) {
9244 // Show widgets only when they are not yet shown by the expose event
9245 d->showChildren(true);
9246 QShowEvent showEvent;
9247 QCoreApplication::sendSpontaneousEvent(widget, &showEvent);
9248 }
9249 d->childrenHiddenByWState = false; // Set it always to "false" when window is restored
9250 } else {
9251 QHideEvent hideEvent;
9252 QCoreApplication::sendSpontaneousEvent(widget, &hideEvent);
9253 d->hideChildren(true);
9254 d->childrenHiddenByWState = true;
9255 }
9256 d->childrenShownByExpose = false; // Set it always to "false" when window state changes
9257 }
9258 changeEvent(event);
9259 }
9260 break;
9261
9262 case QEvent::WindowActivate:
9263 case QEvent::WindowDeactivate: {
9264 if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
9265 update();
9266 QList<QObject*> childList = d->children;
9267 for (int i = 0; i < childList.size(); ++i) {
9268 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9269 if (w && w->isVisible() && !w->isWindow())
9270 QCoreApplication::sendEvent(w, event);
9271 }
9272 break; }
9273
9274 case QEvent::LanguageChange:
9275 changeEvent(event);
9276 {
9277 QList<QObject*> childList = d->children;
9278 for (int i = 0; i < childList.size(); ++i) {
9279 QObject *o = childList.at(i);
9280 if (o)
9281 QCoreApplication::sendEvent(o, event);
9282 }
9283 }
9284 update();
9285 break;
9286
9287 case QEvent::ApplicationLayoutDirectionChange:
9288 d->resolveLayoutDirection();
9289 break;
9290
9291 case QEvent::LayoutDirectionChange:
9292 if (d->layout)
9293 d->layout->invalidate();
9294 update();
9295 changeEvent(event);
9296 break;
9297 case QEvent::UpdateRequest:
9298 d->syncBackingStore();
9299 break;
9300 case QEvent::UpdateLater:
9301 update(static_cast<QUpdateLaterEvent*>(event)->region());
9302 break;
9303 case QEvent::StyleAnimationUpdate:
9304 if (isVisible() && !window()->isMinimized()) {
9305 event->accept();
9306 update();
9307 }
9308 break;
9309
9310 case QEvent::WindowBlocked:
9311 case QEvent::WindowUnblocked:
9312 if (!d->children.isEmpty()) {
9313 QWidget *modalWidget = QApplication::activeModalWidget();
9314 for (int i = 0; i < d->children.size(); ++i) {
9315 QObject *o = d->children.at(i);
9316 if (o && o != modalWidget && o->isWidgetType()) {
9317 QWidget *w = static_cast<QWidget *>(o);
9318 // do not forward the event to child windows; QApplication does this for us
9319 if (!w->isWindow())
9320 QCoreApplication::sendEvent(w, event);
9321 }
9322 }
9323 }
9324 break;
9325#if QT_CONFIG(tooltip)
9326 case QEvent::ToolTip:
9327 if (!d->toolTip.isEmpty())
9328 QToolTip::showText(static_cast<QHelpEvent*>(event)->globalPos(), d->toolTip, this, QRect(), d->toolTipDuration);
9329 else
9330 event->ignore();
9331 break;
9332#endif
9333#if QT_CONFIG(whatsthis)
9334 case QEvent::WhatsThis:
9335 if (d->whatsThis.size())
9336 QWhatsThis::showText(static_cast<QHelpEvent *>(event)->globalPos(), d->whatsThis, this);
9337 else
9338 event->ignore();
9339 break;
9340 case QEvent::QueryWhatsThis:
9341 if (d->whatsThis.isEmpty())
9342 event->ignore();
9343 break;
9344#endif
9345 case QEvent::EmbeddingControl:
9346 d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
9347 data->fstrut_dirty = false;
9348 break;
9349#if QT_CONFIG(action)
9350 case QEvent::ActionAdded:
9351 case QEvent::ActionRemoved:
9352 case QEvent::ActionChanged:
9353 actionEvent((QActionEvent*)event);
9354 break;
9355#endif
9356
9357 case QEvent::KeyboardLayoutChange:
9358 {
9359 changeEvent(event);
9360
9361 // inform children of the change
9362 QList<QObject*> childList = d->children;
9363 for (int i = 0; i < childList.size(); ++i) {
9364 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9365 if (w && w->isVisible() && !w->isWindow())
9366 QCoreApplication::sendEvent(w, event);
9367 }
9368 break;
9369 }
9370 case QEvent::TouchBegin:
9371 case QEvent::TouchUpdate:
9372 case QEvent::TouchEnd:
9373 case QEvent::TouchCancel:
9374 {
9375 event->ignore();
9376 break;
9377 }
9378#ifndef QT_NO_GESTURES
9379 case QEvent::Gesture:
9380 event->ignore();
9381 break;
9382#endif
9383 case QEvent::ScreenChangeInternal:
9384 if (const QTLWExtra *te = d->maybeTopData()) {
9385 const QWindow *win = te->window;
9386 d->setWinId((win && win->handle()) ? win->handle()->winId() : 0);
9387 }
9388 break;
9389 case QEvent::DevicePixelRatioChange:
9390 if (d->data.fnt.d->dpi != logicalDpiY())
9391 d->updateFont(d->data.fnt);
9392 d->renderToTextureReallyDirty = 1;
9393 break;
9394 case QEvent::DynamicPropertyChange: {
9395 const QByteArray &propName = static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
9396 if (propName.size() == 13 && !qstrncmp(propName, "_q_customDpi", 12)) {
9397 uint value = property(propName.constData()).toUInt();
9398 if (!d->extra)
9399 d->createExtra();
9400 const char axis = propName.at(12);
9401 if (axis == 'X')
9402 d->extra->customDpiX = value;
9403 else if (axis == 'Y')
9404 d->extra->customDpiY = value;
9405 d->updateFont(d->data.fnt);
9406 }
9407 if (windowHandle() && !qstrncmp(propName, "_q_platform_", 12))
9408 windowHandle()->setProperty(propName, property(propName));
9409 Q_FALLTHROUGH();
9410 }
9411 default:
9412 return QObject::event(event);
9413 }
9414 return true;
9415}
9416
9417/*!
9418 This event handler can be reimplemented to handle state changes.
9419
9420 The state being changed in this event can be retrieved through the \a event
9421 supplied.
9422
9423 Change events include: QEvent::ToolBarChange,
9424 QEvent::ActivationChange, QEvent::EnabledChange, QEvent::FontChange,
9425 QEvent::StyleChange, QEvent::PaletteChange,
9426 QEvent::WindowTitleChange, QEvent::IconTextChange,
9427 QEvent::ModifiedChange, QEvent::MouseTrackingChange,
9428 QEvent::ParentChange, QEvent::WindowStateChange,
9429 QEvent::LanguageChange, QEvent::LocaleChange,
9430 QEvent::LayoutDirectionChange, QEvent::ReadOnlyChange.
9431
9432*/
9433void QWidget::changeEvent(QEvent * event)
9434{
9435 switch(event->type()) {
9436 case QEvent::EnabledChange: {
9437 update();
9438#if QT_CONFIG(accessibility)
9439 QAccessible::State s;
9440 s.disabled = true;
9441 QAccessibleStateChangeEvent event(this, s);
9442 QAccessible::updateAccessibility(&event);
9443#endif
9444 break;
9445 }
9446
9447 case QEvent::FontChange:
9448 case QEvent::StyleChange: {
9449 Q_D(QWidget);
9450 update();
9451 updateGeometry();
9452 if (d->layout)
9453 d->layout->invalidate();
9454 break;
9455 }
9456
9457 case QEvent::PaletteChange:
9458 update();
9459 break;
9460
9461 case QEvent::ThemeChange:
9462 if (QGuiApplication::desktopSettingsAware()
9463 && qApp && !QCoreApplication::closingDown()) {
9464 if (testAttribute(Qt::WA_WState_Polished))
9465 QApplication::style()->unpolish(this);
9466 if (testAttribute(Qt::WA_WState_Polished))
9467 QApplication::style()->polish(this);
9468 QEvent styleChangedEvent(QEvent::StyleChange);
9469 QCoreApplication::sendEvent(this, &styleChangedEvent);
9470 if (isVisible())
9471 update();
9472 }
9473 break;
9474
9475#ifdef Q_OS_MACOS
9476 case QEvent::MacSizeChange:
9477 updateGeometry();
9478 break;
9479#endif
9480
9481 default:
9482 break;
9483 }
9484}
9485
9486/*!
9487 This event handler, for event \a event, can be reimplemented in a
9488 subclass to receive mouse move events for the widget.
9489
9490 If mouse tracking is switched off, mouse move events only occur if
9491 a mouse button is pressed while the mouse is being moved. If mouse
9492 tracking is switched on, mouse move events occur even if no mouse
9493 button is pressed.
9494
9495 QMouseEvent::position() reports the position of the mouse cursor,
9496 relative to this widget. For press and release events, the
9497 position is usually the same as the position of the last mouse
9498 move event, but it might be different if the user's hand shakes.
9499 This is a feature of the underlying window system, not Qt.
9500
9501 If you want to show a tooltip immediately, while the mouse is
9502 moving (e.g., to get the mouse coordinates with QMouseEvent::position()
9503 and show them as a tooltip), you must first enable mouse tracking
9504 as described above. Then, to ensure that the tooltip is updated
9505 immediately, you must call QToolTip::showText() instead of
9506 setToolTip() in your implementation of mouseMoveEvent().
9507
9508 \sa setMouseTracking(), mousePressEvent(), mouseReleaseEvent(),
9509 mouseDoubleClickEvent(), event(), QMouseEvent, {Scribble Example}
9510*/
9511
9512void QWidget::mouseMoveEvent(QMouseEvent *event)
9513{
9514 event->ignore();
9515}
9516
9517/*!
9518 This event handler, for event \a event, can be reimplemented in a
9519 subclass to receive mouse press events for the widget.
9520
9521 If you create new widgets in the mousePressEvent() the
9522 mouseReleaseEvent() may not end up where you expect, depending on
9523 the underlying window system (or X11 window manager), the widgets'
9524 location and maybe more.
9525
9526 The default implementation implements the closing of popup widgets
9527 when you click outside the window. For other widget types it does
9528 nothing.
9529
9530 \sa mouseReleaseEvent(), mouseDoubleClickEvent(),
9531 mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}
9532*/
9533
9534void QWidget::mousePressEvent(QMouseEvent *event)
9535{
9536 event->ignore();
9537 if ((windowType() == Qt::Popup)) {
9538 event->accept();
9539 QWidget* w;
9540 while ((w = QApplication::activePopupWidget()) && w != this){
9541 w->close();
9542 if (QApplication::activePopupWidget() == w) // widget does not want to disappear
9543 w->hide(); // hide at least
9544 }
9545 if (!rect().contains(event->position().toPoint())){
9546 close();
9547 }
9548 }
9549}
9550
9551/*!
9552 This event handler, for event \a event, can be reimplemented in a
9553 subclass to receive mouse release events for the widget.
9554
9555 \sa mousePressEvent(), mouseDoubleClickEvent(),
9556 mouseMoveEvent(), event(), QMouseEvent, {Scribble Example}
9557*/
9558
9559void QWidget::mouseReleaseEvent(QMouseEvent *event)
9560{
9561 event->ignore();
9562}
9563
9564/*!
9565 This event handler, for event \a event, can be reimplemented in a
9566 subclass to receive mouse double click events for the widget.
9567
9568 The double click event replaces the second mouse press event of a
9569 double click. The widget receives a mouse press event, a mouse
9570 release event, the double click event, and a second mouse release
9571 event.
9572
9573 The default implementation calls mousePressEvent(), so a widget that
9574 doesn't reimplement this function handles the second click as an
9575 ordinary press. If you reimplement this function and also need to
9576 react to every press, either call the base implementation or handle
9577 the event as a press yourself.
9578
9579 \note If another widget that overlaps this widget disappears in
9580 response to press or release events, then this widget will only
9581 receive the double click event. It is up to the developer to ensure
9582 that the application interprets these events correctly.
9583
9584 \sa mousePressEvent(), mouseReleaseEvent(), mouseMoveEvent(),
9585 event(), QMouseEvent
9586*/
9587
9588void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
9589{
9590 mousePressEvent(event);
9591}
9592
9593#if QT_CONFIG(wheelevent)
9594/*!
9595 This event handler, for event \a event, can be reimplemented in a
9596 subclass to receive wheel events for the widget.
9597
9598 If you reimplement this handler, it is very important that you
9599 \l{QEvent}{ignore()} the event if you do not handle
9600 it, so that the widget's parent can interpret it.
9601
9602 The default implementation ignores the event.
9603
9604 \sa QEvent::ignore(), QEvent::accept(), event(),
9605 QWheelEvent
9606*/
9607
9608void QWidget::wheelEvent(QWheelEvent *event)
9609{
9610 event->ignore();
9611}
9612#endif // QT_CONFIG(wheelevent)
9613
9614#if QT_CONFIG(tabletevent)
9615/*!
9616 This event handler, for event \a event, can be reimplemented in a
9617 subclass to receive tablet events for the widget.
9618
9619 If you reimplement this handler, it is very important that you
9620 \l{QEvent}{ignore()} the event if you do not handle
9621 it, so that the widget's parent can interpret it.
9622
9623 The default implementation ignores the event.
9624
9625 If tablet tracking is switched off, tablet move events only occur if the
9626 stylus is in contact with the tablet, or at least one stylus button is
9627 pressed, while the stylus is being moved. If tablet tracking is switched on,
9628 tablet move events occur even while the stylus is hovering in proximity of
9629 the tablet, with no buttons pressed.
9630
9631 \sa QEvent::ignore(), QEvent::accept(), event(), setTabletTracking(),
9632 QTabletEvent
9633*/
9634
9635void QWidget::tabletEvent(QTabletEvent *event)
9636{
9637 event->ignore();
9638}
9639#endif // QT_CONFIG(tabletevent)
9640
9641/*!
9642 This event handler, for event \a event, can be reimplemented in a
9643 subclass to receive key press events for the widget.
9644
9645 A widget must call setFocusPolicy() to accept focus initially and
9646 have focus in order to receive a key press event.
9647
9648 If you reimplement this handler, it is very important that you
9649 call the base class implementation if you do not act upon the key.
9650
9651 The default implementation closes popup widgets if the user
9652 presses the key sequence for QKeySequence::Cancel (typically the
9653 Escape key). Otherwise the event is ignored, so that the widget's
9654 parent can interpret it.
9655
9656 Note that QKeyEvent starts with isAccepted() == true, so you do not
9657 need to call QKeyEvent::accept() - just do not call the base class
9658 implementation if you act upon the key.
9659
9660 \sa keyReleaseEvent(), setFocusPolicy(),
9661 focusInEvent(), focusOutEvent(), event(), QKeyEvent
9662*/
9663
9664void QWidget::keyPressEvent(QKeyEvent *event)
9665{
9666#ifndef QT_NO_SHORTCUT
9667 if ((windowType() == Qt::Popup) && event->matches(QKeySequence::Cancel)) {
9668 event->accept();
9669 close();
9670 } else
9671#endif
9672 {
9673 event->ignore();
9674 }
9675}
9676
9677/*!
9678 This event handler, for event \a event, can be reimplemented in a
9679 subclass to receive key release events for the widget.
9680
9681 A widget must \l{setFocusPolicy()}{accept focus}
9682 initially and \l{hasFocus()}{have focus} in order to
9683 receive a key release event.
9684
9685 If you reimplement this handler, it is very important that you
9686 call the base class implementation if you do not act upon the key.
9687
9688 The default implementation ignores the event, so that the widget's
9689 parent can interpret it.
9690
9691 Note that QKeyEvent starts with isAccepted() == true, so you do not
9692 need to call QKeyEvent::accept() - just do not call the base class
9693 implementation if you act upon the key.
9694
9695 \sa keyPressEvent(), QEvent::ignore(), setFocusPolicy(),
9696 focusInEvent(), focusOutEvent(), event(), QKeyEvent
9697*/
9698
9699void QWidget::keyReleaseEvent(QKeyEvent *event)
9700{
9701 event->ignore();
9702}
9703
9704/*!
9705 \fn void QWidget::focusInEvent(QFocusEvent *event)
9706
9707 This event handler can be reimplemented in a subclass to receive
9708 keyboard focus events (focus received) for the widget. The event
9709 is passed in the \a event parameter
9710
9711 A widget normally must setFocusPolicy() to something other than
9712 Qt::NoFocus to receive focus events. (Note that the
9713 application programmer can call setFocus() on any widget, even
9714 those that do not normally accept focus.)
9715
9716 The default implementation updates the widget (except for windows
9717 that do not specify a focusPolicy()).
9718
9719 \sa focusOutEvent(), setFocusPolicy(), keyPressEvent(),
9720 keyReleaseEvent(), event(), QFocusEvent
9721*/
9722
9723void QWidget::focusInEvent(QFocusEvent *)
9724{
9725 if (focusPolicy() != Qt::NoFocus || !isWindow()) {
9726 update();
9727 }
9728}
9729
9730/*!
9731 \fn void QWidget::focusOutEvent(QFocusEvent *event)
9732
9733 This event handler can be reimplemented in a subclass to receive
9734 keyboard focus events (focus lost) for the widget. The event is
9735 passed in the \a event parameter.
9736
9737 A widget normally must setFocusPolicy() to something other than
9738 Qt::NoFocus to receive focus events. (Note that the
9739 application programmer can call setFocus() on any widget, even
9740 those that do not normally accept focus.)
9741
9742 The default implementation updates the widget (except for windows
9743 that do not specify a focusPolicy()).
9744
9745 \sa focusInEvent(), setFocusPolicy(), keyPressEvent(),
9746 keyReleaseEvent(), event(), QFocusEvent
9747*/
9748
9749void QWidget::focusOutEvent(QFocusEvent *)
9750{
9751 if (focusPolicy() != Qt::NoFocus || !isWindow())
9752 update();
9753
9754#if !defined(QT_PLATFORM_UIKIT)
9755 // FIXME: revisit autoSIP logic, QTBUG-42906
9756 if (qApp->autoSipEnabled() && testAttribute(Qt::WA_InputMethodEnabled))
9757 QGuiApplication::inputMethod()->hide();
9758#endif
9759}
9760
9761/*!
9762 \fn void QWidget::enterEvent(QEnterEvent *event)
9763
9764 This event handler can be reimplemented in a subclass to receive
9765 widget enter events which are passed in the \a event parameter.
9766
9767 An event is sent to the widget when the mouse cursor enters the
9768 widget.
9769
9770 \sa leaveEvent(), mouseMoveEvent(), event()
9771*/
9772
9773void QWidget::enterEvent(QEnterEvent *)
9774{
9775}
9776
9777/*!
9778 \fn void QWidget::leaveEvent(QEvent *event)
9779
9780 This event handler can be reimplemented in a subclass to receive
9781 widget leave events which are passed in the \a event parameter.
9782
9783 A leave event is sent to the widget when the mouse cursor leaves
9784 the widget.
9785
9786 \sa enterEvent(), mouseMoveEvent(), event()
9787*/
9788
9789void QWidget::leaveEvent(QEvent *)
9790{
9791}
9792
9793/*!
9794 \fn void QWidget::paintEvent(QPaintEvent *event)
9795
9796 This event handler can be reimplemented in a subclass to receive paint
9797 events passed in \a event.
9798
9799 A paint event is a request to repaint all or part of a widget. It can
9800 happen for one of the following reasons:
9801
9802 \list
9803 \li repaint() or update() was invoked,
9804 \li the widget was obscured and has now been uncovered, or
9805 \li many other reasons.
9806 \endlist
9807
9808 Many widgets can simply repaint their entire surface when asked to, but
9809 some slow widgets need to optimize by painting only the requested region:
9810 QPaintEvent::region(). This speed optimization does not change the result,
9811 as painting is clipped to that region during event processing. QListView
9812 and QTableView do this, for example.
9813
9814 Qt also tries to speed up painting by merging multiple paint events into
9815 one. When update() is called several times or the window system sends
9816 several paint events, Qt merges these events into one event with a larger
9817 region (see QRegion::united()). The repaint() function does not permit this
9818 optimization, so we suggest using update() whenever possible.
9819
9820 When the paint event occurs, the update region has normally been erased, so
9821 you are painting on the widget's background.
9822
9823 The background can be set using setBackgroundRole() and setPalette().
9824
9825 Since Qt 4.0, QWidget automatically double-buffers its painting, so there
9826 is no need to write double-buffering code in paintEvent() to avoid flicker.
9827
9828 \note Generally, you should refrain from calling update() or repaint()
9829 \b{inside} a paintEvent(). For example, calling update() or repaint() on
9830 children inside a paintEvent() results in undefined behavior; the child may
9831 or may not get a paint event.
9832
9833 \warning If you are using a custom paint engine without Qt's backingstore,
9834 Qt::WA_PaintOnScreen must be set. Otherwise, QWidget::paintEngine() will
9835 never be called; the backingstore will be used instead.
9836
9837 \sa event(), repaint(), update(), QPainter, QPixmap, QPaintEvent,
9838 {Analog Clock}
9839*/
9840
9841void QWidget::paintEvent(QPaintEvent *)
9842{
9843}
9844
9845
9846/*!
9847 \fn void QWidget::moveEvent(QMoveEvent *event)
9848
9849 This event handler can be reimplemented in a subclass to receive
9850 widget move events which are passed in the \a event parameter.
9851 When the widget receives this event, it is already at the new
9852 position.
9853
9854 The old position is accessible through QMoveEvent::oldPos().
9855
9856 \sa resizeEvent(), event(), move(), QMoveEvent
9857*/
9858
9859void QWidget::moveEvent(QMoveEvent *)
9860{
9861}
9862
9863
9864/*!
9865 This event handler can be reimplemented in a subclass to receive
9866 widget resize events which are passed in the \a event parameter.
9867 When resizeEvent() is called, the widget already has its new
9868 geometry. The old size is accessible through
9869 QResizeEvent::oldSize().
9870
9871 The widget will be erased and receive a paint event immediately
9872 after processing the resize event. No drawing need be (or should
9873 be) done inside this handler.
9874
9875
9876 \sa moveEvent(), event(), resize(), QResizeEvent, paintEvent(),
9877 {Scribble Example}
9878*/
9879
9880void QWidget::resizeEvent(QResizeEvent * /* event */)
9881{
9882}
9883
9884#if QT_CONFIG(action)
9885/*!
9886 \fn void QWidget::actionEvent(QActionEvent *event)
9887
9888 This event handler is called with the given \a event whenever the
9889 widget's actions are changed.
9890
9891 \sa addAction(), insertAction(), removeAction(), actions(), QActionEvent
9892*/
9893void QWidget::actionEvent(QActionEvent *)
9894{
9895
9896}
9897#endif
9898
9899/*!
9900 This event handler is called with the given \a event when Qt receives a window
9901 close request for a top-level widget from the window system.
9902
9903 By default, the event is accepted and the widget is closed. You can reimplement
9904 this function to change the way the widget responds to window close requests.
9905 For example, you can prevent the window from closing by calling \l{QEvent::}{ignore()}
9906 on all events.
9907
9908 Main window applications typically use reimplementations of this function to check
9909 whether the user's work has been saved and ask for permission before closing.
9910
9911 \sa event(), hide(), close(), QCloseEvent
9912*/
9913
9914void QWidget::closeEvent(QCloseEvent *event)
9915{
9916 event->accept();
9917}
9918
9919#ifndef QT_NO_CONTEXTMENU
9920/*!
9921 This event handler, for event \a event, can be reimplemented in a
9922 subclass to receive widget context menu events.
9923
9924 The handler is called when the widget's \l contextMenuPolicy is
9925 Qt::DefaultContextMenu.
9926
9927 The default implementation ignores the context event.
9928 See the \l QContextMenuEvent documentation for more details.
9929
9930 \sa event(), QContextMenuEvent, customContextMenuRequested()
9931*/
9932
9933void QWidget::contextMenuEvent(QContextMenuEvent *event)
9934{
9935 event->ignore();
9936}
9937#endif // QT_NO_CONTEXTMENU
9938
9939
9940/*!
9941 This event handler, for event \a event, can be reimplemented in a
9942 subclass to receive Input Method composition events. This handler
9943 is called when the state of the input method changes.
9944
9945 Note that when creating custom text editing widgets, the
9946 Qt::WA_InputMethodEnabled window attribute must be set explicitly
9947 (using the setAttribute() function) in order to receive input
9948 method events.
9949
9950 The default implementation calls event->ignore(), which rejects the
9951 Input Method event. See the \l QInputMethodEvent documentation for more
9952 details.
9953
9954 \sa event(), QInputMethodEvent
9955*/
9956void QWidget::inputMethodEvent(QInputMethodEvent *event)
9957{
9958 event->ignore();
9959}
9960
9961/*!
9962 This method is only relevant for input widgets. It is used by the
9963 input method to query a set of properties of the widget to be
9964 able to support complex input method operations as support for
9965 surrounding text and reconversions.
9966
9967 \a query specifies which property is queried.
9968
9969 \sa inputMethodEvent(), QInputMethodEvent, QInputMethodQueryEvent, inputMethodHints
9970*/
9971QVariant QWidget::inputMethodQuery(Qt::InputMethodQuery query) const
9972{
9973 switch(query) {
9974 case Qt::ImCursorRectangle:
9975 return QRect(width()/2, 0, 1, height());
9976 case Qt::ImFont:
9977 return font();
9978 case Qt::ImAnchorPosition:
9979 // Fallback.
9980 return inputMethodQuery(Qt::ImCursorPosition);
9981 case Qt::ImHints:
9982 return (int)inputMethodHints();
9983 case Qt::ImInputItemClipRectangle:
9984 return d_func()->clipRect();
9985 default:
9986 return QVariant();
9987 }
9988}
9989
9990/*!
9991 \property QWidget::inputMethodHints
9992 \brief What input method specific hints the widget has.
9993
9994 This is only relevant for input widgets. It is used by
9995 the input method to retrieve hints as to how the input method
9996 should operate. For example, if the Qt::ImhFormattedNumbersOnly flag
9997 is set, the input method may change its visual components to reflect
9998 that only numbers can be entered.
9999
10000 \warning Some widgets require certain flags to work as
10001 intended. To set a flag, do \c{w->setInputMethodHints(w->inputMethodHints()|f)}
10002 instead of \c{w->setInputMethodHints(f)}.
10003
10004 \note The flags are only hints, so the particular input method
10005 implementation is free to ignore them. If you want to be
10006 sure that a certain type of characters are entered,
10007 you should also set a QValidator on the widget.
10008
10009 The default value is Qt::ImhNone.
10010
10011 \since 4.6
10012
10013 \sa inputMethodQuery()
10014*/
10015Qt::InputMethodHints QWidget::inputMethodHints() const
10016{
10017#if QT_CONFIG(im)
10018 const QWidgetPrivate *priv = d_func();
10019 while (priv->inheritsInputMethodHints) {
10020 priv = priv->q_func()->parentWidget()->d_func();
10021 Q_ASSERT(priv);
10022 }
10023 return priv->imHints;
10024#else
10025 return Qt::ImhNone;
10026#endif
10027}
10028
10029void QWidget::setInputMethodHints(Qt::InputMethodHints hints)
10030{
10031#ifndef QT_NO_IM
10032 Q_D(QWidget);
10033 if (d->imHints == hints)
10034 return;
10035 d->imHints = hints;
10036 if (this == QGuiApplication::focusObject())
10037 QGuiApplication::inputMethod()->update(Qt::ImHints);
10038#else
10039 Q_UNUSED(hints);
10040#endif //QT_NO_IM
10041}
10042
10043
10044#if QT_CONFIG(draganddrop)
10045
10046/*!
10047 \fn void QWidget::dragEnterEvent(QDragEnterEvent *event)
10048
10049 This event handler is called when a drag is in progress and the
10050 mouse enters this widget. The event is passed in the \a event parameter.
10051
10052 If the event is ignored, the widget won't receive any \l{dragMoveEvent()}{drag
10053 move events}.
10054
10055 See the \l{dnd.html}{Drag-and-drop documentation} for an
10056 overview of how to provide drag-and-drop in your application.
10057
10058 \sa QDrag, QDragEnterEvent
10059*/
10060void QWidget::dragEnterEvent(QDragEnterEvent *)
10061{
10062}
10063
10064/*!
10065 \fn void QWidget::dragMoveEvent(QDragMoveEvent *event)
10066
10067 This event handler is called if a drag is in progress, and when
10068 any of the following conditions occur: the cursor enters this widget,
10069 the cursor moves within this widget, or a modifier key is pressed on
10070 the keyboard while this widget has the focus. The event is passed
10071 in the \a event parameter.
10072
10073 See the \l{dnd.html}{Drag-and-drop documentation} for an
10074 overview of how to provide drag-and-drop in your application.
10075
10076 \sa QDrag, QDragMoveEvent
10077*/
10078void QWidget::dragMoveEvent(QDragMoveEvent *)
10079{
10080}
10081
10082/*!
10083 \fn void QWidget::dragLeaveEvent(QDragLeaveEvent *event)
10084
10085 This event handler is called when a drag is in progress and the
10086 mouse leaves this widget. The event is passed in the \a event
10087 parameter.
10088
10089 See the \l{dnd.html}{Drag-and-drop documentation} for an
10090 overview of how to provide drag-and-drop in your application.
10091
10092 \sa QDrag, QDragLeaveEvent
10093*/
10094void QWidget::dragLeaveEvent(QDragLeaveEvent *)
10095{
10096}
10097
10098/*!
10099 \fn void QWidget::dropEvent(QDropEvent *event)
10100
10101 This event handler is called when the drag is dropped on this
10102 widget. The event is passed in the \a event parameter.
10103
10104 See the \l{dnd.html}{Drag-and-drop documentation} for an
10105 overview of how to provide drag-and-drop in your application.
10106
10107 \sa QDrag, QDropEvent
10108*/
10109void QWidget::dropEvent(QDropEvent *)
10110{
10111}
10112
10113#endif // QT_CONFIG(draganddrop)
10114
10115/*!
10116 \fn void QWidget::showEvent(QShowEvent *event)
10117
10118 This event handler can be reimplemented in a subclass to receive
10119 widget show events which are passed in the \a event parameter.
10120
10121 Non-spontaneous show events are sent to widgets immediately
10122 before they are shown. The spontaneous show events of windows are
10123 delivered afterwards.
10124
10125 Note: A widget receives spontaneous show and hide events when its
10126 mapping status is changed by the window system, e.g. a spontaneous
10127 hide event when the user minimizes the window, and a spontaneous
10128 show event when the window is restored again. After receiving a
10129 spontaneous hide event, a widget is still considered visible in
10130 the sense of isVisible().
10131
10132 \sa visible, event(), QShowEvent
10133*/
10134void QWidget::showEvent(QShowEvent *)
10135{
10136}
10137
10138/*!
10139 \fn void QWidget::hideEvent(QHideEvent *event)
10140
10141 This event handler can be reimplemented in a subclass to receive
10142 widget hide events. The event is passed in the \a event parameter.
10143
10144 Hide events are sent to widgets immediately after they have been
10145 hidden.
10146
10147 Note: A widget receives spontaneous show and hide events when its
10148 mapping status is changed by the window system, e.g. a spontaneous
10149 hide event when the user minimizes the window, and a spontaneous
10150 show event when the window is restored again. After receiving a
10151 spontaneous hide event, a widget is still considered visible in
10152 the sense of isVisible().
10153
10154 \sa visible, event(), QHideEvent
10155*/
10156void QWidget::hideEvent(QHideEvent *)
10157{
10158}
10159
10160/*!
10161 This special event handler can be reimplemented in a subclass to
10162 receive native platform events identified by \a eventType
10163 which are passed in the \a message parameter.
10164
10165 In your reimplementation of this function, if you want to stop the
10166 event being handled by Qt, return true and set \a result. The \a result
10167 parameter has meaning only on Windows. If you return false, this native
10168 event is passed back to Qt, which translates the event into a Qt event
10169 and sends it to the widget.
10170
10171 \note Events are only delivered to this event handler if the widget
10172 has a native window handle.
10173
10174 \note This function superseedes the event filter functions
10175 x11Event(), winEvent() and macEvent() of Qt 4.
10176
10177 \sa QAbstractNativeEventFilter
10178
10179 \table
10180 \header \li Platform \li Event Type Identifier \li Message Type \li Result Type
10181 \row \li Windows \li "windows_generic_MSG" \li MSG * \li LRESULT
10182 \row \li macOS \li "NSEvent" \li NSEvent * \li
10183 \row \li XCB \li "xcb_generic_event_t" \li xcb_generic_event_t * \li
10184 \endtable
10185*/
10186
10187bool QWidget::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
10188{
10189 Q_UNUSED(eventType);
10190 Q_UNUSED(message);
10191 Q_UNUSED(result);
10192 return false;
10193}
10194
10195/*!
10196 Ensures that the widget and its children have been polished by
10197 QStyle (i.e., have a proper font and palette).
10198
10199 QWidget calls this function after it has been fully constructed
10200 but before it is shown the very first time. You can call this
10201 function if you want to ensure that the widget is polished before
10202 doing an operation, e.g., the correct font size might be needed in
10203 the widget's sizeHint() reimplementation. Note that this function
10204 \e is called from the default implementation of sizeHint().
10205
10206 Polishing is useful for final initialization that must happen after
10207 all constructors (from base classes as well as from subclasses)
10208 have been called.
10209
10210 If you need to change some settings when a widget is polished,
10211 reimplement event() and handle the QEvent::Polish event type.
10212
10213 \b{Note:} The function is declared const so that it can be called from
10214 other const functions (e.g., sizeHint()).
10215
10216 \sa event()
10217*/
10218void QWidget::ensurePolished() const
10219{
10220 Q_D(const QWidget);
10221
10222 const QMetaObject *m = metaObject();
10223 if (m == d->polished)
10224 return;
10225 d->polished = m;
10226
10227 QEvent e(QEvent::Polish);
10228 QCoreApplication::sendEvent(const_cast<QWidget *>(this), &e);
10229
10230 // polish children after 'this'
10231 QList<QObject*> children = d->children;
10232 for (int i = 0; i < children.size(); ++i) {
10233 QObject *o = children.at(i);
10234 if (!o->isWidgetType())
10235 continue;
10236 if (QWidget *w = qobject_cast<QWidget *>(o))
10237 w->ensurePolished();
10238 }
10239
10240 if (d->parent && d->sendChildEvents) {
10241 QChildEvent e(QEvent::ChildPolished, const_cast<QWidget *>(this));
10242 QCoreApplication::sendEvent(d->parent, &e);
10243 }
10244}
10245
10246/*!
10247 Returns the mask currently set on a widget. If no mask is set the
10248 return value will be an empty region.
10249
10250 \sa setMask(), clearMask(), QRegion::isEmpty()
10251*/
10252QRegion QWidget::mask() const
10253{
10254 Q_D(const QWidget);
10255 return d->extra ? d->extra->mask : QRegion();
10256}
10257
10258/*!
10259 Returns the layout manager that is installed on this widget, or \nullptr
10260 if no layout manager is installed.
10261
10262 The layout manager sets the geometry of the widget's children
10263 that have been added to the layout.
10264
10265 \sa setLayout(), sizePolicy(), {Layout Management}
10266*/
10267QLayout *QWidget::layout() const
10268{
10269 return d_func()->layout;
10270}
10271
10272
10273/*!
10274 \fn void QWidget::setLayout(QLayout *layout)
10275
10276 Sets the layout manager for this widget to \a layout.
10277
10278 If there already is a layout manager installed on this widget,
10279 QWidget won't let you install another. You must first delete the
10280 existing layout manager (returned by layout()) before you can
10281 call setLayout() with the new layout.
10282
10283 If \a layout is the layout manager on a different widget, setLayout()
10284 will reparent the layout and make it the layout manager for this widget.
10285
10286 Example:
10287
10288 \snippet layouts/layouts.cpp 24
10289
10290 An alternative to calling this function is to pass this widget to
10291 the layout's constructor.
10292
10293 The QWidget will take ownership of \a layout.
10294
10295 \sa layout(), {Layout Management}
10296*/
10297
10298void QWidget::setLayout(QLayout *l)
10299{
10300 if (Q_UNLIKELY(!l)) {
10301 qWarning("QWidget::setLayout: Cannot set layout to 0");
10302 return;
10303 }
10304 if (layout()) {
10305 if (Q_UNLIKELY(layout() != l))
10306 qWarning("QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
10307 " layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
10308 objectName().toLocal8Bit().data());
10309 return;
10310 }
10311
10312 QObject *oldParent = l->parent();
10313 if (oldParent && oldParent != this) {
10314 if (oldParent->isWidgetType()) {
10315 // Steal the layout off a widget parent. Takes effect when
10316 // morphing laid-out container widgets in Designer.
10317 QWidget *oldParentWidget = static_cast<QWidget *>(oldParent);
10318 oldParentWidget->takeLayout();
10319 } else {
10320 qWarning("QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
10321 l->objectName().toLocal8Bit().data(), metaObject()->className(),
10322 objectName().toLocal8Bit().data());
10323 return;
10324 }
10325 }
10326
10327 Q_D(QWidget);
10328 l->d_func()->topLevel = true;
10329 d->layout = l;
10330 if (oldParent != this) {
10331 l->setParent(this);
10332 l->d_func()->reparentChildWidgets(this);
10333 l->invalidate();
10334 }
10335
10336 if (isWindow() && d->maybeTopData())
10337 d->topData()->sizeAdjusted = false;
10338}
10339
10340/*!
10341 \fn QLayout *QWidget::takeLayout()
10342
10343 Remove the layout from the widget.
10344 \since 4.5
10345*/
10346
10347QLayout *QWidget::takeLayout()
10348{
10349 Q_D(QWidget);
10350 QLayout *l = layout();
10351 if (!l)
10352 return nullptr;
10353 d->layout = nullptr;
10354 l->setParent(nullptr);
10355 return l;
10356}
10357
10358/*!
10359 \property QWidget::sizePolicy
10360 \brief the default layout behavior of the widget
10361
10362 The layout that contains this widget uses this size policy, even
10363 if the widget has its own layout that manages its children. That
10364 layout affects the widget in two ways: it provides the default
10365 sizeHint() and minimumSizeHint(), and if the size policy allows the
10366 widget to grow in a direction in which the layout expands, the
10367 widget is also treated as expanding in that direction.
10368
10369 The default policy is Preferred/Preferred, which means that the
10370 widget can be freely resized, but prefers to be the size
10371 sizeHint() returns. Button-like widgets set the size policy to
10372 specify that they may stretch horizontally, but are fixed
10373 vertically. The same applies to lineedit controls (such as
10374 QLineEdit, QSpinBox or an editable QComboBox) and other
10375 horizontally orientated widgets (such as QProgressBar).
10376 QToolButton is fixed in both directions. Widgets that support
10377 different directions (such as QSlider or QScrollBar) specify
10378 stretching in the respective direction only. Widgets that can
10379 provide scroll bars (usually subclasses of QAbstractScrollArea)
10380 tend to specify that they can use additional space, and that they
10381 can make do with less than sizeHint().
10382
10383 \sa sizeHint(), QLayout, QSizePolicy, updateGeometry()
10384*/
10385QSizePolicy QWidget::sizePolicy() const
10386{
10387 Q_D(const QWidget);
10388 return d->size_policy;
10389}
10390
10391void QWidget::setSizePolicy(QSizePolicy policy)
10392{
10393 Q_D(QWidget);
10394 setAttribute(Qt::WA_WState_OwnSizePolicy);
10395 if (policy == d->size_policy)
10396 return;
10397
10398 if (d->size_policy.retainSizeWhenHidden() != policy.retainSizeWhenHidden())
10399 d->retainSizeWhenHiddenChanged = 1;
10400
10401 d->size_policy = policy;
10402
10403#if QT_CONFIG(graphicsview)
10404 if (const auto &extra = d->extra) {
10405 if (extra->proxyWidget)
10406 extra->proxyWidget->setSizePolicy(policy);
10407 }
10408#endif
10409
10410 updateGeometry();
10411 d->retainSizeWhenHiddenChanged = 0;
10412
10413 if (isWindow() && d->maybeTopData())
10414 d->topData()->sizeAdjusted = false;
10415}
10416
10417/*!
10418 \fn void QWidget::setSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
10419 \overload
10420
10421 Sets the size policy of the widget to \a horizontal and \a
10422 vertical, with standard stretch and no height-for-width.
10423
10424 \sa QSizePolicy::QSizePolicy()
10425*/
10426
10427/*!
10428 Returns the preferred height for this widget, given the width \a w.
10429
10430 If this widget has a layout, the default implementation returns
10431 the layout's preferred height. if there is no layout, the default
10432 implementation returns -1 indicating that the preferred height
10433 does not depend on the width.
10434*/
10435
10436int QWidget::heightForWidth(int w) const
10437{
10438 if (layout() && layout()->hasHeightForWidth())
10439 return layout()->totalHeightForWidth(w);
10440 return -1;
10441}
10442
10443
10444/*!
10445 \since 5.0
10446
10447 Returns \c true if the widget's preferred height depends on its width; otherwise returns \c false.
10448*/
10449bool QWidget::hasHeightForWidth() const
10450{
10451 Q_D(const QWidget);
10452 return d->layout ? d->layout->hasHeightForWidth() : d->size_policy.hasHeightForWidth();
10453}
10454
10455/*!
10456 \fn QWidget *QWidget::childAt(int x, int y) const
10457
10458 Returns the visible child widget at the position (\a{x}, \a{y})
10459 in the widget's coordinate system. If there is no visible child
10460 widget at the specified position, the function returns \nullptr.
10461*/
10462
10463/*!
10464 \overload
10465
10466 Returns the visible child widget at point \a p in the widget's own
10467 coordinate system.
10468*/
10469
10470QWidget *QWidget::childAt(const QPoint &p) const
10471{
10472 return d_func()->childAt_helper(QPointF(p), false);
10473}
10474
10475/*!
10476 \overload
10477 \since 6.8
10478
10479 Returns the visible child widget at point \a p in the widget's own
10480 coordinate system.
10481*/
10482
10483QWidget *QWidget::childAt(const QPointF &p) const
10484{
10485 return d_func()->childAt_helper(p, false);
10486}
10487
10488QWidget *QWidgetPrivate::childAt_helper(const QPointF &p, bool ignoreChildrenInDestructor) const
10489{
10490 if (children.isEmpty())
10491 return nullptr;
10492
10493 if (!pointInsideRectAndMask(p))
10494 return nullptr;
10495 return childAtRecursiveHelper(p, ignoreChildrenInDestructor);
10496}
10497
10498QWidget *QWidgetPrivate::childAtRecursiveHelper(const QPointF &p, bool ignoreChildrenInDestructor) const
10499{
10500 for (int i = children.size() - 1; i >= 0; --i) {
10501 QWidget *child = qobject_cast<QWidget *>(children.at(i));
10502 if (!child || child->isWindow() || child->isHidden() || child->testAttribute(Qt::WA_TransparentForMouseEvents)
10503 || (ignoreChildrenInDestructor && child->data->in_destructor)) {
10504 continue;
10505 }
10506
10507 // Map the point 'p' from parent coordinates to child coordinates.
10508 QPointF childPoint = p;
10509 childPoint -= child->data->crect.topLeft();
10510
10511 // Check if the point hits the child.
10512 if (!child->d_func()->pointInsideRectAndMask(childPoint))
10513 continue;
10514
10515 // Do the same for the child's descendants.
10516 if (QWidget *w = child->d_func()->childAtRecursiveHelper(childPoint, ignoreChildrenInDestructor))
10517 return w;
10518
10519 // We have found our target; namely the child at position 'p'.
10520 return child;
10521 }
10522 return nullptr;
10523}
10524
10525void QWidgetPrivate::updateGeometry_helper(bool forceUpdate)
10526{
10527 Q_Q(QWidget);
10528 if (widgetItem)
10529 widgetItem->invalidateSizeCache();
10530 QWidget *parent;
10531 if (forceUpdate || !extra || extra->minw != extra->maxw || extra->minh != extra->maxh) {
10532 const int isHidden = q->isHidden() && !size_policy.retainSizeWhenHidden() && !retainSizeWhenHiddenChanged;
10533
10534 if (!q->isWindow() && !isHidden && (parent = q->parentWidget())) {
10535 if (parent->d_func()->layout)
10536 parent->d_func()->layout->invalidate();
10537 else if (parent->isVisible())
10538 QCoreApplication::postEvent(parent, new QEvent(QEvent::LayoutRequest));
10539 }
10540 }
10541}
10542
10543/*!
10544 Notifies the layout system that this widget has changed and may
10545 need to change geometry.
10546
10547 Call this function if the sizeHint() or sizePolicy() have changed.
10548
10549 For explicitly hidden widgets, updateGeometry() is a no-op. The
10550 layout system will be notified as soon as the widget is shown.
10551*/
10552
10553void QWidget::updateGeometry()
10554{
10555 Q_D(QWidget);
10556 d->updateGeometry_helper(false);
10557}
10558
10559/*! \property QWidget::windowFlags
10560
10561 Window flags are a combination of a type (e.g. Qt::Dialog) and
10562 zero or more hints to the window system (e.g.
10563 Qt::FramelessWindowHint).
10564
10565 If the widget had type Qt::Widget or Qt::SubWindow and becomes a
10566 window (Qt::Window, Qt::Dialog, etc.), it is put at position (0,
10567 0) on the desktop. If the widget is a window and becomes a
10568 Qt::Widget or Qt::SubWindow, it is put at position (0, 0)
10569 relative to its parent widget.
10570
10571 \note This function calls setParent() when changing the flags for
10572 a window, causing the widget to be hidden. You must call show() to make
10573 the widget visible again..
10574
10575 \sa windowType(), setWindowFlag(), {Window Flags Example}
10576*/
10577void QWidget::setWindowFlags(Qt::WindowFlags flags)
10578{
10579 Q_D(QWidget);
10580 d->setWindowFlags(flags);
10581}
10582
10583/*!
10584 \since 5.9
10585
10586 Sets the window flag \a flag on this widget if \a on is true;
10587 otherwise clears the flag.
10588
10589 \note This function calls setParent() when changing the flags for
10590 a window, causing the widget to be hidden. You must call show() to make
10591 the widget visible again.
10592
10593 \sa setWindowFlags(), windowFlags(), windowType()
10594*/
10595void QWidget::setWindowFlag(Qt::WindowType flag, bool on)
10596{
10597 Q_D(QWidget);
10598 if (on)
10599 d->setWindowFlags(data->window_flags | flag);
10600 else
10601 d->setWindowFlags(data->window_flags & ~flag);
10602}
10603
10604/*! \internal
10605
10606 Implemented in QWidgetPrivate so that QMdiSubWindowPrivate can reimplement it.
10607*/
10608void QWidgetPrivate::setWindowFlags(Qt::WindowFlags flags)
10609{
10610 Q_Q(QWidget);
10611#if QT_DEPRECATED_SINCE(6, 11)
10612 QT_IGNORE_DEPRECATIONS(
10613 if ((flags & Qt::WindowType_Mask) == Qt::WindowType::Desktop) {
10614 qWarning() << "Qt::WindowType::Desktop has been deprecated in Qt 6. Ignoring.";
10615 flags.setFlag(Qt::WindowType::Desktop, false);
10616 }
10617 )
10618#endif
10619
10620 if (q->data->window_flags == flags)
10621 return;
10622
10623 if ((q->data->window_flags | flags) & Qt::Window) {
10624 // the old type was a window and/or the new type is a window
10625 QPoint oldPos = q->pos();
10626 bool visible = q->isVisible();
10627 const bool windowFlagChanged = (q->data->window_flags ^ flags) & Qt::Window;
10628 q->setParent(q->parentWidget(), flags);
10629
10630 // if both types are windows or neither of them are, we restore
10631 // the old position
10632 if (!windowFlagChanged && (visible || q->testAttribute(Qt::WA_Moved)))
10633 q->move(oldPos);
10634 // for backward-compatibility we change Qt::WA_QuitOnClose attribute value only when the window was recreated.
10635 adjustQuitOnCloseAttribute();
10636 } else {
10637 q->data->window_flags = flags;
10638 }
10639}
10640
10641/*!
10642 Sets the window flags for the widget to \a flags,
10643 \e without telling the window system.
10644
10645 \warning Do not call this function unless you really know what
10646 you're doing.
10647
10648 \sa setWindowFlags()
10649*/
10650void QWidget::overrideWindowFlags(Qt::WindowFlags flags)
10651{
10652 data->window_flags = flags;
10653}
10654
10655/*!
10656 \fn Qt::WindowType QWidget::windowType() const
10657
10658 Returns the window type of this widget. This is identical to
10659 windowFlags() & Qt::WindowType_Mask.
10660
10661 \sa windowFlags
10662*/
10663
10664/*!
10665 Sets the parent of the widget to \a parent, and resets the window
10666 flags. The widget is moved to position (0, 0) in its new parent.
10667
10668 If the new parent widget is in a different window, the
10669 reparented widget and its children are appended to the end of the
10670 \l{setFocusPolicy()}{tab chain} of the new parent
10671 widget, in the same internal order as before. If one of the moved
10672 widgets had keyboard focus, setParent() calls clearFocus() for that
10673 widget.
10674
10675 If the new parent widget is in the same window as the
10676 old parent, setting the parent doesn't change the tab order or
10677 keyboard focus.
10678
10679 If the "new" parent widget is the old parent widget, this function
10680 does nothing.
10681
10682 \note The widget becomes invisible as part of changing its parent,
10683 even if it was previously visible. You must call show() to make the
10684 widget visible again.
10685
10686 \warning It is very unlikely that you will ever need this
10687 function. If you have a widget that changes its content
10688 dynamically, it is far easier to use \l QStackedWidget.
10689
10690 \sa setWindowFlags()
10691*/
10692void QWidget::setParent(QWidget *parent)
10693{
10694 if (parent == parentWidget())
10695 return;
10696 setParent((QWidget*)parent, windowFlags() & ~Qt::WindowType_Mask);
10697}
10698
10699void qSendWindowChangeToTextureChildrenRecursively(QWidget *widget, QEvent::Type eventType)
10700{
10701 QWidgetPrivate *d = QWidgetPrivate::get(widget);
10702 if (d->renderToTexture) {
10703 QEvent e(eventType);
10704 QCoreApplication::sendEvent(widget, &e);
10705 }
10706
10707 for (int i = 0; i < d->children.size(); ++i) {
10708 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
10709 if (w && !w->isWindow())
10710 qSendWindowChangeToTextureChildrenRecursively(w, eventType);
10711 }
10712
10713 // Notify QWidgetWindow after we've notified all child QWidgets
10714 if (auto *window = d->windowHandle(QWidgetPrivate::WindowHandleMode::Direct)) {
10715 QEvent e(eventType);
10716 QCoreApplication::sendEvent(window, &e);
10717 }
10718}
10719
10720/*!
10721 \overload
10722
10723 This function also takes widget flags, \a f as an argument.
10724*/
10725
10726void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
10727{
10728 Q_D(QWidget);
10729 Q_ASSERT_X(this != parent, Q_FUNC_INFO, "Cannot parent a QWidget to itself");
10730#ifdef QT_DEBUG
10731 const auto checkForParentChildLoops = qScopeGuard([&](){
10732 int depth = 0;
10733 auto p = parentWidget();
10734 while (p) {
10735 if (++depth == QObjectPrivate::CheckForParentChildLoopsWarnDepth) {
10736 qWarning("QWidget %p (class: '%s', object name: '%s') may have a loop in its parent-child chain; "
10737 "this is undefined behavior",
10738 this, metaObject()->className(), qPrintable(objectName()));
10739 }
10740 p = p->parentWidget();
10741 }
10742 });
10743#endif
10744
10745 const bool resized = testAttribute(Qt::WA_Resized);
10746 const bool wasCreated = testAttribute(Qt::WA_WState_Created);
10747 QWidget *oldtlw = window();
10748 Q_ASSERT(oldtlw);
10749 QWidget *oldParentWithWindow = d->closestParentWidgetWithWindowHandle();
10750
10751 if (f & Qt::Window) // Frame geometry likely changes, refresh.
10752 d->data.fstrut_dirty = true;
10753
10754 bool newParent = (parent != parentWidget());
10755
10756 if (newParent && parent) {
10757 if (testAttribute(Qt::WA_NativeWindow) && !QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings))
10758 parent->d_func()->enforceNativeChildren();
10759 else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen))
10760 setAttribute(Qt::WA_NativeWindow);
10761 }
10762
10763 if (wasCreated) {
10764 if (!testAttribute(Qt::WA_WState_Hidden)) {
10765 // Hiding the widget will set WA_WState_Hidden as well, which would
10766 // normally require the widget to be explicitly shown again to become
10767 // visible, even as a child widget. But we refine this value later in
10768 // setParent_sys(), applying WA_WState_Hidden based on whether the
10769 // widget is a top level or not.
10770 hide();
10771
10772 // We reset WA_WState_ExplicitShowHide here, likely as a remnant of
10773 // when we only had QWidget::setVisible(), which is treated as an
10774 // explicit show/hide. Nowadays we have QWidgetPrivate::setVisible(),
10775 // that allows us to hide a widget without affecting ExplicitShowHide.
10776 // Though it can be argued that ExplicitShowHide should reflect the
10777 // last update of the widget's state, so if we hide the widget as a
10778 // side effect of changing parent, perhaps we _should_ reset it?
10779 setAttribute(Qt::WA_WState_ExplicitShowHide, false);
10780 }
10781 if (newParent) {
10782 QEvent e(QEvent::ParentAboutToChange);
10783 QCoreApplication::sendEvent(this, &e);
10784 }
10785 }
10786
10787 // texture-based widgets need a pre-notification when their associated top-level window changes
10788 // This is not under the wasCreated/newParent conditions above in order to also play nice with QDockWidget.
10789 const bool oldWidgetUsesRhiFlush = oldParentWithWindow ? oldParentWithWindow->d_func()->usesRhiFlush
10790 : oldtlw->d_func()->usesRhiFlush;
10791 if (oldWidgetUsesRhiFlush && ((!parent && parentWidget()) || (parent && parent->window() != oldtlw)))
10792 qSendWindowChangeToTextureChildrenRecursively(this, QEvent::WindowAboutToChangeInternal);
10793
10794 // If we get parented into another window, children will be folded
10795 // into the new parent's focus chain, so clear focus now.
10796 if (newParent && isAncestorOf(focusWidget()) && !(f & Qt::Window))
10797 focusWidget()->clearFocus();
10798
10799 d->setParent_sys(parent, f);
10800
10801 if (d->textureChildSeen && parent) {
10802 // set the textureChildSeen flag up the whole parent chain
10803 QWidgetPrivate::get(parent)->setTextureChildSeen();
10804 }
10805
10806 if (QWidgetRepaintManager *oldPaintManager = oldtlw->d_func()->maybeRepaintManager()) {
10807 if (newParent)
10808 oldPaintManager->removeDirtyWidget(this);
10809 // Move the widget and all its static children from
10810 // the old backing store to the new one.
10811 oldPaintManager->moveStaticWidgets(this);
10812 }
10813
10814 d->reparentFocusWidgets(oldtlw);
10815 setAttribute(Qt::WA_Resized, resized);
10816
10817 const bool useStyleSheetPropagationInWidgetStyles =
10818 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
10819
10820 if (!useStyleSheetPropagationInWidgetStyles && !testAttribute(Qt::WA_StyleSheet)
10821 && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
10822 // if the parent has a font set or inherited, then propagate the mask to the new child
10823 if (parent) {
10824 const auto pd = parent->d_func();
10825 d->inheritedFontResolveMask = pd->directFontResolveMask | pd->inheritedFontResolveMask;
10826 d->inheritedPaletteResolveMask = pd->directPaletteResolveMask | pd->inheritedPaletteResolveMask;
10827 }
10828 d->resolveFont();
10829 d->resolvePalette();
10830 }
10831 d->resolveLayoutDirection();
10832 d->resolveLocale();
10833
10834 // Note: GL widgets under WGL or EGL will always need a ParentChange
10835 // event to handle recreation/rebinding of the GL context, hence the
10836 // (f & Qt::MSWindowsOwnDC) clause (which is set on QGLWidgets on all
10837 // platforms).
10838 if (newParent || !wasCreated
10839#if QT_CONFIG(opengles2)
10840 || (f & Qt::MSWindowsOwnDC)
10841#endif
10842 ) {
10843 // propagate enabled updates enabled state to non-windows
10844 if (!isWindow()) {
10845 if (!testAttribute(Qt::WA_ForceDisabled))
10846 d->setEnabled_helper(parent ? parent->isEnabled() : true);
10847 if (!testAttribute(Qt::WA_ForceUpdatesDisabled))
10848 d->setUpdatesEnabled_helper(parent ? parent->updatesEnabled() : true);
10849 }
10850 d->inheritStyle();
10851
10852 // send and post remaining QObject events
10853 if (parent && d->sendChildEvents) {
10854 QChildEvent e(QEvent::ChildAdded, this);
10855 QCoreApplication::sendEvent(parent, &e);
10856 }
10857
10858 if (parent && d->sendChildEvents && d->polished) {
10859 QChildEvent e(QEvent::ChildPolished, this);
10860 QCoreApplication::sendEvent(parent, &e);
10861 }
10862
10863 QEvent e(QEvent::ParentChange);
10864 QCoreApplication::sendEvent(this, &e);
10865 }
10866
10867 // texture-based widgets need another event when their top-level window
10868 // changes (more precisely, has already changed at this point)
10869 if (oldWidgetUsesRhiFlush && oldtlw != window())
10870 qSendWindowChangeToTextureChildrenRecursively(this, QEvent::WindowChangeInternal);
10871
10872 if (!wasCreated) {
10873 if (isWindow() || parentWidget()->isVisible())
10874 setAttribute(Qt::WA_WState_Hidden, true);
10875 else if (!testAttribute(Qt::WA_WState_ExplicitShowHide))
10876 setAttribute(Qt::WA_WState_Hidden, false);
10877 }
10878
10879 d->updateIsOpaque();
10880
10881#if QT_CONFIG(graphicsview)
10882 // Embed the widget into a proxy if the parent is embedded.
10883 // ### Doesn't handle reparenting out of an embedded widget.
10884 if (oldtlw->graphicsProxyWidget()) {
10885 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
10886 ancestorProxy->d_func()->unembedSubWindow(this);
10887 }
10888 if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(this)) {
10889 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
10890 ancestorProxy->d_func()->embedSubWindow(this);
10891 }
10892#endif
10893
10894 if (d->extra && d->extra->hasWindowContainer)
10895 QWindowContainer::parentWasChanged(this);
10896
10897 QWidget *newParentWithWindow = d->closestParentWidgetWithWindowHandle();
10898 if (newParentWithWindow && newParentWithWindow != oldParentWithWindow) {
10899 // Check if the native parent now needs to switch to RHI
10900 qCDebug(lcWidgetPainting) << "Evaluating whether reparenting of" << this
10901 << "into" << parent << "requires RHI enablement for" << newParentWithWindow;
10902
10903 QPlatformBackingStoreRhiConfig rhiConfig;
10904 QSurface::SurfaceType surfaceType = QSurface::RasterSurface;
10905
10906 // First evaluate whether the reparented widget uses RHI.
10907 // We do this as a separate step because the performance
10908 // implications of always checking the native parent are
10909 // problematic when it comes to large widget trees.
10910 if (q_evaluateRhiConfig(this, &rhiConfig, &surfaceType)) {
10911 // Then check whether the native parent requires RHI
10912 // as a result. It may not, if this widget is a native
10913 // window, and can handle its own RHI flushing.
10914 if (q_evaluateRhiConfig(newParentWithWindow, nullptr, nullptr)) {
10915 // Finally, check whether we need to recreate the
10916 // native parent to enable RHI flushing.
10917 auto *existingWindow = newParentWithWindow->windowHandle();
10918 auto existingSurfaceType = existingWindow->surfaceType();
10919 if (existingSurfaceType != surfaceType) {
10920 qCDebug(lcWidgetPainting)
10921 << "Recreating" << existingWindow
10922 << "with current type" << existingSurfaceType
10923 << "to support" << surfaceType;
10924 const auto windowStateBeforeDestroy = newParentWithWindow->windowState();
10925 const auto visibilityBeforeDestroy = newParentWithWindow->isVisible();
10926 const auto positionBeforeDestroy = newParentWithWindow->pos();
10927 newParentWithWindow->d_func()->recreate();
10928 Q_ASSERT(newParentWithWindow->windowHandle());
10929 newParentWithWindow->windowHandle()->setWindowStates(windowStateBeforeDestroy);
10930 newParentWithWindow->move(positionBeforeDestroy);
10931 QWidgetPrivate::get(newParentWithWindow)->setVisible(visibilityBeforeDestroy);
10932 } else if (auto *backingStore = newParentWithWindow->backingStore()) {
10933 // If we don't recreate we still need to make sure the native parent
10934 // widget has a RHI config that the reparented widget can use.
10935 backingStore->handle()->createRhi(existingWindow, rhiConfig);
10936 // And that it knows it's now flushing with RHI
10937 QWidgetPrivate::get(newParentWithWindow)->usesRhiFlush = true;
10938 }
10939 }
10940 }
10941 }
10942
10943#if QT_CONFIG(accessibility)
10944 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing) {
10945 QAccessibleEvent qaEvent(this, QAccessible::ParentChanged);
10946 QAccessible::updateAccessibility(&qaEvent);
10947 }
10948#endif
10949
10950}
10951
10952void QWidgetPrivate::setParent_sys(QWidget *newparent, Qt::WindowFlags f)
10953{
10954 Q_Q(QWidget);
10955
10956 Qt::WindowFlags oldFlags = data.window_flags;
10957 bool wasCreated = q->testAttribute(Qt::WA_WState_Created);
10958
10959 QScreen *targetScreen = nullptr;
10960 setWinId(0);
10961
10962 if (!newparent) {
10963 f |= Qt::Window;
10964 if (parent)
10965 targetScreen = q->parentWidget()->window()->screen();
10966 }
10967
10968 const bool destroyWindow = (
10969 // Reparenting top level to child
10970 (oldFlags & Qt::Window) && !(f & Qt::Window)
10971 // And we can dispose of the window
10972 && wasCreated && !q->testAttribute(Qt::WA_NativeWindow)
10973 );
10974
10975 if (parent != newparent) {
10976 // Update object parent now, so we can resolve new parent window below
10977 QObjectPrivate::setParent_helper(newparent);
10978
10979 if (q->windowHandle())
10980 q->windowHandle()->setFlags(f);
10981
10982 // If the widget itself or any of its children have been created,
10983 // we need to reparent their QWindows as well.
10984 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
10985 // But if the widget is about to be destroyed we must skip the
10986 // widget itself, and only reparent children.
10987 if (destroyWindow) {
10988 reparentWidgetWindowChildren(parentWithWindow);
10989 } else {
10990 // During reparentWidgetWindows() we need to know whether the reparented
10991 // QWindow should be a top level (with a transient parent) or not. This
10992 // widget has not updated its window flags yet, so we can't ask the widget
10993 // directly at that point. Nor can we use the QWindow flags, as unlike QWidgets
10994 // the QWindow flags always reflect Qt::Window, even for child windows. And
10995 // we can't use QWindow::isTopLevel() either, as that depends on the parent,
10996 // which we are in the process of updating. So we propagate the
10997 // new flags of the reparented window here.
10998 reparentWidgetWindows(parentWithWindow, f);
10999 }
11000 }
11001
11002 bool explicitlyHidden = isExplicitlyHidden();
11003
11004 if (destroyWindow) {
11005 if (extra && extra->hasWindowContainer)
11006 QWindowContainer::toplevelAboutToBeDestroyed(q);
11007
11008 // There shouldn't be any QWindow children left, but if there
11009 // are, re-parent them now, before we destroy.
11010 if (!q->windowHandle()->children().isEmpty()) {
11011 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11012 QWindow *newParentWindow = parentWithWindow ? parentWithWindow->windowHandle() : nullptr;
11013 for (QObject *child : q->windowHandle()->children()) {
11014 if (QWindow *childWindow = qobject_cast<QWindow *>(child)) {
11015 qCWarning(lcWidgetWindow) << "Reparenting" << childWindow
11016 << "before destroying" << this;
11017 childWindow->setParent(newParentWindow);
11018 }
11019 }
11020 }
11021
11022 // We have reparented any child windows of the widget we are
11023 // about to destroy to the new parent window handle, so we can
11024 // safely destroy this widget without destroying sub windows.
11025 q->destroy(true, false);
11026 }
11027
11028 adjustFlags(f, q);
11029 data.window_flags = f;
11030
11031 // A top level respects the safe area by default (see QWidget::create()). A child is
11032 // placed by its parent's layout, which takes care of that, so a former top level drops
11033 // the default again, unless it was set explicitly. Otherwise, e.g., a dock widget that
11034 // has been floating would map its window's safe area into its own geometry (far off
11035 // when it's behind a tab), and the dock area layout would grow its minimum size by that.
11036 if ((oldFlags & Qt::Window) && !(f & Qt::Window)) {
11037 const QTLWExtra *topExtra = maybeTopData();
11038 if (!topExtra || !topExtra->explicitContentsMarginsRespectsSafeArea)
11039 setAttribute_internal(Qt::WA_ContentsMarginsRespectsSafeArea, false, &data, this);
11040 }
11041
11042 q->setAttribute(Qt::WA_WState_Created, false);
11043 q->setAttribute(Qt::WA_WState_Visible, false);
11044 q->setAttribute(Qt::WA_WState_Hidden, false);
11045
11046 if (newparent && wasCreated && (q->testAttribute(Qt::WA_NativeWindow) || (f & Qt::Window)))
11047 q->createWinId();
11048
11049 if (q->isWindow() || (!newparent || newparent->isVisible()) || explicitlyHidden)
11050 q->setAttribute(Qt::WA_WState_Hidden);
11051 q->setAttribute(Qt::WA_WState_ExplicitShowHide, explicitlyHidden);
11052
11053 // move the window to the selected screen
11054 if (!newparent && targetScreen) {
11055 // only if it is already created
11056 if (q->testAttribute(Qt::WA_WState_Created))
11057 q->windowHandle()->setScreen(targetScreen);
11058 else
11059 topData()->initialScreen = targetScreen;
11060 }
11061}
11062
11063void QWidgetPrivate::reparentWidgetWindows(QWidget *parentWithWindow, Qt::WindowFlags windowFlags)
11064{
11065 if (QWindow *window = windowHandle()) {
11066 // Reparent this QWindow, and all QWindow children will follow
11067 if (parentWithWindow) {
11068 if (windowFlags & Qt::Window) {
11069 // Top level windows can only have transient parents,
11070 // and the transient parent must be another top level.
11071 QWidget *topLevel = parentWithWindow->window();
11072 auto *transientParent = topLevel->windowHandle();
11073 Q_ASSERT(transientParent);
11074 qCDebug(lcWidgetWindow) << "Setting" << window << "transient parent to" << transientParent;
11075 window->setTransientParent(transientParent);
11076 window->setParent(nullptr);
11077 } else {
11078 auto *parentWindow = parentWithWindow->windowHandle();
11079 qCDebug(lcWidgetWindow) << "Reparenting" << window << "into" << parentWindow;
11080 window->setTransientParent(nullptr);
11081 window->setParent(parentWindow);
11082 }
11083 } else {
11084 qCDebug(lcWidgetWindow) << "Making" << window << "top level window";
11085 window->setTransientParent(nullptr);
11086 window->setParent(nullptr);
11087 }
11088 } else {
11089 reparentWidgetWindowChildren(parentWithWindow);
11090 }
11091}
11092
11093void QWidgetPrivate::reparentWidgetWindowChildren(QWidget *parentWithWindow)
11094{
11095 for (auto *child : std::as_const(children)) {
11096 if (auto *childWidget = qobject_cast<QWidget*>(child)) {
11097 auto *childPrivate = QWidgetPrivate::get(childWidget);
11098 // Child widgets with QWindows should always continue to be child
11099 // windows, so we pass on the child's current window flags here.
11100 childPrivate->reparentWidgetWindows(parentWithWindow, childWidget->windowFlags());
11101 }
11102 }
11103}
11104
11105/*!
11106 Scrolls the widget including its children \a dx pixels to the
11107 right and \a dy downward. Both \a dx and \a dy may be negative.
11108
11109 After scrolling, the widgets will receive paint events for
11110 the areas that need to be repainted. For widgets that Qt knows to
11111 be opaque, this is only the newly exposed parts.
11112 For example, if an opaque widget is scrolled 8 pixels to the left,
11113 only an 8-pixel wide stripe at the right edge needs updating.
11114
11115 Since widgets propagate the contents of their parents by default,
11116 you need to set the \l autoFillBackground property, or use
11117 setAttribute() to set the Qt::WA_OpaquePaintEvent attribute, to make
11118 a widget opaque.
11119
11120 For widgets that use contents propagation, a scroll will cause an
11121 update of the entire scroll area.
11122
11123 \sa {Transparency and Double Buffering}
11124*/
11125
11126void QWidget::scroll(int dx, int dy)
11127{
11128 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11129 return;
11130 if (dx == 0 && dy == 0)
11131 return;
11132 Q_D(QWidget);
11133#if QT_CONFIG(graphicsview)
11134 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(this)) {
11135 // Graphics View maintains its own dirty region as a list of rects;
11136 // until we can connect item updates directly to the view, we must
11137 // separately add a translated dirty region.
11138 for (const QRect &rect : d->dirty)
11139 proxy->update(rect.translated(dx, dy));
11140 proxy->scroll(dx, dy, proxy->subWidgetRect(this));
11141 d->scrollChildren(dx, dy); // QTBUG-138381: scroll item view cell widgets
11142 return;
11143 }
11144#endif
11145 d->setDirtyOpaqueRegion();
11146 d->scroll_sys(dx, dy);
11147}
11148
11149void QWidgetPrivate::scroll_sys(int dx, int dy)
11150{
11151 Q_Q(QWidget);
11152 scrollChildren(dx, dy);
11153 scrollRect(q->rect(), dx, dy);
11154}
11155
11156/*!
11157 \overload
11158
11159 This version only scrolls \a r and does not move the children of
11160 the widget.
11161
11162 If \a r is empty or invalid, the result is undefined.
11163
11164 \sa QScrollArea
11165*/
11166void QWidget::scroll(int dx, int dy, const QRect &r)
11167{
11168
11169 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11170 return;
11171 if (dx == 0 && dy == 0)
11172 return;
11173 Q_D(QWidget);
11174#if QT_CONFIG(graphicsview)
11175 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(this)) {
11176 // Graphics View maintains its own dirty region as a list of rects;
11177 // until we can connect item updates directly to the view, we must
11178 // separately add a translated dirty region.
11179 if (!d->dirty.isEmpty()) {
11180 for (const QRect &rect : d->dirty.translated(dx, dy) & r)
11181 proxy->update(rect);
11182 }
11183 proxy->scroll(dx, dy, r.translated(proxy->subWidgetRect(this).topLeft().toPoint()));
11184 return;
11185 }
11186#endif
11187 d->scroll_sys(dx, dy, r);
11188}
11189
11190void QWidgetPrivate::scroll_sys(int dx, int dy, const QRect &r)
11191{
11192 scrollRect(r, dx, dy);
11193}
11194
11195/*!
11196 Repaints the widget directly by calling paintEvent() immediately,
11197 unless updates are disabled or the widget is hidden.
11198
11199 We suggest only using repaint() if you need an immediate repaint,
11200 for example during animation. In most circumstances update()
11201 is better, as it permits Qt to optimize for speed and minimize
11202 flicker.
11203
11204 \warning If you call repaint() in a function which may itself be
11205 called from paintEvent(), you may get infinite recursion. The
11206 update() function never causes recursion.
11207
11208 \sa update(), paintEvent(), setUpdatesEnabled()
11209*/
11210
11211void QWidget::repaint()
11212{
11213 repaint(rect());
11214}
11215
11216/*! \overload
11217
11218 This version repaints a rectangle (\a x, \a y, \a w, \a h) inside
11219 the widget.
11220
11221 If \a w is negative, it is replaced with \c{width() - x}, and if
11222 \a h is negative, it is replaced width \c{height() - y}.
11223*/
11224void QWidget::repaint(int x, int y, int w, int h)
11225{
11226 if (x > data->crect.width() || y > data->crect.height())
11227 return;
11228
11229 if (w < 0)
11230 w = data->crect.width() - x;
11231 if (h < 0)
11232 h = data->crect.height() - y;
11233
11234 repaint(QRect(x, y, w, h));
11235}
11236
11237/*! \overload
11238
11239 This version repaints a rectangle \a rect inside the widget.
11240*/
11241void QWidget::repaint(const QRect &rect)
11242{
11243 Q_D(QWidget);
11244 d->repaint(rect);
11245}
11246
11247/*!
11248 \overload
11249
11250 This version repaints a region \a rgn inside the widget.
11251*/
11252void QWidget::repaint(const QRegion &rgn)
11253{
11254 Q_D(QWidget);
11255 d->repaint(rgn);
11256}
11257
11258template <typename T>
11259void QWidgetPrivate::repaint(T r)
11260{
11261 Q_Q(QWidget);
11262
11263 if (!q->isVisible() || !q->updatesEnabled() || r.isEmpty())
11264 return;
11265
11266 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11267 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11268 tlwExtra->repaintManager->markDirty(r, q, QWidgetRepaintManager::UpdateNow);
11269}
11270
11271/*!
11272 Updates the widget unless updates are disabled or the widget is
11273 hidden.
11274
11275 This function does not cause an immediate repaint; instead it
11276 schedules a paint event for processing when Qt returns to the main
11277 event loop. This permits Qt to optimize for more speed and less
11278 flicker than a call to repaint() does.
11279
11280 Calling update() several times normally results in just one
11281 paintEvent() call.
11282
11283 Qt normally erases the widget's area before the paintEvent() call.
11284 If the Qt::WA_OpaquePaintEvent widget attribute is set, the widget is
11285 responsible for painting all its pixels with an opaque color.
11286
11287 \sa repaint(), paintEvent(), setUpdatesEnabled(), {Analog Clock}
11288*/
11289void QWidget::update()
11290{
11291 update(rect());
11292}
11293
11294/*! \fn void QWidget::update(int x, int y, int w, int h)
11295 \overload
11296
11297 This version updates a rectangle (\a x, \a y, \a w, \a h) inside
11298 the widget.
11299*/
11300
11301/*!
11302 \overload
11303
11304 This version updates a rectangle \a rect inside the widget.
11305*/
11306void QWidget::update(const QRect &rect)
11307{
11308 Q_D(QWidget);
11309 d->update(rect);
11310}
11311
11312/*!
11313 \overload
11314
11315 This version repaints a region \a rgn inside the widget.
11316*/
11317void QWidget::update(const QRegion &rgn)
11318{
11319 Q_D(QWidget);
11320 d->update(rgn);
11321}
11322
11323template <typename T>
11324void QWidgetPrivate::update(T r)
11325{
11326 Q_Q(QWidget);
11327
11328 if (renderToTexture && !q->isVisible()) {
11329 renderToTextureReallyDirty = 1;
11330 return;
11331 }
11332
11333 if (!q->isVisible() || !q->updatesEnabled())
11334 return;
11335
11336 T clipped = r & q->rect();
11337
11338 if (clipped.isEmpty())
11339 return;
11340
11341 if (q->testAttribute(Qt::WA_WState_InPaintEvent)) {
11342 QCoreApplication::postEvent(q, new QUpdateLaterEvent(clipped));
11343 return;
11344 }
11345
11346 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11347 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11348 tlwExtra->repaintManager->markDirty(clipped, q);
11349}
11350
11351 /*!
11352 \internal
11353
11354 This just sets the corresponding attribute bit to 1 or 0
11355 */
11356static void setAttribute_internal(Qt::WidgetAttribute attribute, bool on, QWidgetData *data,
11357 QWidgetPrivate *d)
11358{
11359 if (attribute < int(8*sizeof(uint))) {
11360 if (on)
11361 data->widget_attributes |= (1<<attribute);
11362 else
11363 data->widget_attributes &= ~(1<<attribute);
11364 } else {
11365 const int x = attribute - 8*sizeof(uint);
11366 const int int_off = x / (8*sizeof(uint));
11367 if (on)
11368 d->high_attributes[int_off] |= (1<<(x-(int_off*8*sizeof(uint))));
11369 else
11370 d->high_attributes[int_off] &= ~(1<<(x-(int_off*8*sizeof(uint))));
11371 }
11372}
11373
11374#ifdef Q_OS_MACOS
11375void QWidgetPrivate::macUpdateSizeAttribute()
11376{
11377 Q_Q(QWidget);
11378 QEvent event(QEvent::MacSizeChange);
11379 QCoreApplication::sendEvent(q, &event);
11380 for (int i = 0; i < children.size(); ++i) {
11381 QWidget *w = qobject_cast<QWidget *>(children.at(i));
11382 if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
11383 && !w->testAttribute(Qt::WA_MacMiniSize) // no attribute set? inherit from parent
11384 && !w->testAttribute(Qt::WA_MacSmallSize)
11385 && !w->testAttribute(Qt::WA_MacNormalSize))
11386 w->d_func()->macUpdateSizeAttribute();
11387 }
11388 resolveFont();
11389}
11390#endif
11391
11392/*!
11393 Sets the attribute \a attribute on this widget if \a on is true;
11394 otherwise clears the attribute.
11395
11396 \sa testAttribute()
11397*/
11398void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on)
11399{
11400 Q_D(QWidget);
11401
11402 if (attribute == Qt::WA_ContentsMarginsRespectsSafeArea) {
11403 if (isWindow()) {
11404 auto *topExtra = d->topData();
11405 topExtra->explicitContentsMarginsRespectsSafeArea = true;
11406 }
11407 }
11408
11409 if (testAttribute(attribute) == on)
11410 return;
11411
11412 static_assert(sizeof(d->high_attributes)*8 >= (Qt::WA_AttributeCount - sizeof(uint)*8),
11413 "QWidget::setAttribute(WidgetAttribute, bool): "
11414 "QWidgetPrivate::high_attributes[] too small to contain all attributes in WidgetAttribute");
11415#ifdef Q_OS_WIN
11416 // ### Don't use PaintOnScreen+paintEngine() to do native painting in some future release
11417 if (attribute == Qt::WA_PaintOnScreen && on && !inherits("QGLWidget")) {
11418 // see ::paintEngine for details
11419 paintEngine();
11420 if (d->noPaintOnScreen)
11421 return;
11422 }
11423#endif
11424
11425 // Don't set WA_NativeWindow on platforms that don't support it -- except for QGLWidget, which depends on it
11426 if (attribute == Qt::WA_NativeWindow && !d->mustHaveWindowHandle) {
11427 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
11428 if (!platformIntegration->hasCapability(QPlatformIntegration::NativeWidgets))
11429 return;
11430 }
11431
11432 setAttribute_internal(attribute, on, data, d);
11433
11434 switch (attribute) {
11435
11436#if QT_CONFIG(draganddrop)
11437 case Qt::WA_AcceptDrops: {
11438 if (on && !testAttribute(Qt::WA_DropSiteRegistered))
11439 setAttribute(Qt::WA_DropSiteRegistered, true);
11440 else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)))
11441 setAttribute(Qt::WA_DropSiteRegistered, false);
11442 QEvent e(QEvent::AcceptDropsChange);
11443 QCoreApplication::sendEvent(this, &e);
11444 break;
11445 }
11446 case Qt::WA_DropSiteRegistered: {
11447 for (int i = 0; i < d->children.size(); ++i) {
11448 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
11449 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_AcceptDrops) && w->testAttribute(Qt::WA_DropSiteRegistered) != on)
11450 w->setAttribute(Qt::WA_DropSiteRegistered, on);
11451 }
11452 break;
11453 }
11454#endif
11455
11456 case Qt::WA_NoChildEventsForParent:
11457 d->sendChildEvents = !on;
11458 break;
11459 case Qt::WA_NoChildEventsFromChildren:
11460 d->receiveChildEvents = !on;
11461 break;
11462 case Qt::WA_MacNormalSize:
11463 case Qt::WA_MacSmallSize:
11464 case Qt::WA_MacMiniSize:
11465#ifdef Q_OS_MACOS
11466 {
11467 // We can only have one of these set at a time
11468 const Qt::WidgetAttribute MacSizes[] = { Qt::WA_MacNormalSize, Qt::WA_MacSmallSize,
11469 Qt::WA_MacMiniSize };
11470 for (int i = 0; i < 3; ++i) {
11471 if (MacSizes[i] != attribute)
11472 setAttribute_internal(MacSizes[i], false, data, d);
11473 }
11474 d->macUpdateSizeAttribute();
11475 }
11476#endif
11477 break;
11478 case Qt::WA_ShowModal:
11479 if (!on) {
11480 // reset modality type to NonModal when clearing WA_ShowModal
11481 data->window_modality = Qt::NonModal;
11482 } else if (data->window_modality == Qt::NonModal) {
11483 // If modality hasn't been set prior to setting WA_ShowModal, use
11484 // ApplicationModal.
11485 data->window_modality = Qt::ApplicationModal;
11486 // Some window managers do not allow us to enter modality after the
11487 // window is visible.The window must be hidden before changing the
11488 // windowModality property and then reshown.
11489 }
11490 if (testAttribute(Qt::WA_WState_Created)) {
11491 // don't call setModal_sys() before create()
11492 d->setModal_sys();
11493 }
11494 break;
11495 case Qt::WA_MouseTracking: {
11496 QEvent e(QEvent::MouseTrackingChange);
11497 QCoreApplication::sendEvent(this, &e);
11498 break; }
11499 case Qt::WA_TabletTracking: {
11500 QEvent e(QEvent::TabletTrackingChange);
11501 QCoreApplication::sendEvent(this, &e);
11502 break; }
11503 case Qt::WA_NativeWindow: {
11504 d->createTLExtra();
11505 if (on)
11506 d->createTLSysExtra();
11507#ifndef QT_NO_IM
11508 QWidget *focusWidget = d->effectiveFocusWidget();
11509 if (on && !internalWinId() && this == QGuiApplication::focusObject()
11510 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11511 QGuiApplication::inputMethod()->commit();
11512 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11513 }
11514 if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget())
11515 parentWidget()->d_func()->enforceNativeChildren();
11516 if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created))
11517 d->createWinId();
11518 if (isEnabled() && focusWidget->isEnabled() && this == QGuiApplication::focusObject()
11519 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11520 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11521 }
11522#endif //QT_NO_IM
11523 break;
11524 }
11525 case Qt::WA_PaintOnScreen:
11526 d->updateIsOpaque();
11527 Q_FALLTHROUGH();
11528 case Qt::WA_OpaquePaintEvent:
11529 d->updateIsOpaque();
11530 break;
11531 case Qt::WA_NoSystemBackground:
11532 d->updateIsOpaque();
11533 Q_FALLTHROUGH();
11534 case Qt::WA_UpdatesDisabled:
11535 d->updateSystemBackground();
11536 break;
11537 case Qt::WA_TransparentForMouseEvents:
11538 break;
11539 case Qt::WA_InputMethodEnabled: {
11540#ifndef QT_NO_IM
11541 if (QGuiApplication::focusObject() == this) {
11542 if (!on)
11543 QGuiApplication::inputMethod()->commit();
11544 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11545 }
11546#endif //QT_NO_IM
11547 break;
11548 }
11549 case Qt::WA_WindowPropagation:
11550 d->resolvePalette();
11551 d->resolveFont();
11552 d->resolveLocale();
11553 break;
11554 case Qt::WA_DontShowOnScreen: {
11555 if (on && isVisible()) {
11556 // Make sure we keep the current state and only hide the widget
11557 // from the desktop. show_sys will only update platform specific
11558 // attributes at this point.
11559 d->hide_sys();
11560 d->show_sys();
11561 }
11562 break;
11563 }
11564
11565 case Qt::WA_X11NetWmWindowTypeDesktop:
11566 case Qt::WA_X11NetWmWindowTypeDock:
11567 case Qt::WA_X11NetWmWindowTypeToolBar:
11568 case Qt::WA_X11NetWmWindowTypeMenu:
11569 case Qt::WA_X11NetWmWindowTypeUtility:
11570 case Qt::WA_X11NetWmWindowTypeSplash:
11571 case Qt::WA_X11NetWmWindowTypeDialog:
11572 case Qt::WA_X11NetWmWindowTypeDropDownMenu:
11573 case Qt::WA_X11NetWmWindowTypePopupMenu:
11574 case Qt::WA_X11NetWmWindowTypeToolTip:
11575 case Qt::WA_X11NetWmWindowTypeNotification:
11576 case Qt::WA_X11NetWmWindowTypeCombo:
11577 case Qt::WA_X11NetWmWindowTypeDND:
11578 d->setNetWmWindowTypes();
11579 break;
11580
11581 case Qt::WA_StaticContents:
11582 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
11583 if (on)
11584 repaintManager->addStaticWidget(this);
11585 else
11586 repaintManager->removeStaticWidget(this);
11587 }
11588 break;
11589 case Qt::WA_TranslucentBackground:
11590 if (on)
11591 setAttribute(Qt::WA_NoSystemBackground);
11592 d->updateIsTranslucent();
11593
11594 break;
11595 case Qt::WA_AcceptTouchEvents:
11596 break;
11597 default:
11598 break;
11599 }
11600}
11601
11602/*! \fn bool QWidget::testAttribute(Qt::WidgetAttribute attribute) const
11603
11604 Returns \c true if attribute \a attribute is set on this widget;
11605 otherwise returns \c false.
11606
11607 \sa setAttribute()
11608 */
11609bool QWidget::testAttribute_helper(Qt::WidgetAttribute attribute) const
11610{
11611 Q_D(const QWidget);
11612 const int x = attribute - 8*sizeof(uint);
11613 const int int_off = x / (8*sizeof(uint));
11614 return (d->high_attributes[int_off] & (1<<(x-(int_off*8*sizeof(uint)))));
11615}
11616
11617/*!
11618 \property QWidget::windowOpacity
11619
11620 \brief The level of opacity for the window.
11621
11622 The valid range of opacity is from 1.0 (completely opaque) to
11623 0.0 (completely transparent).
11624
11625 By default the value of this property is 1.0.
11626
11627 This feature is available on Embedded Linux, \macos, Windows,
11628 and X11 platforms that support the Composite extension.
11629
11630 \note On X11 you need to have a composite manager running,
11631 and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be
11632 supported by the window manager you are using.
11633
11634 \warning Changing this property from opaque to transparent might issue a
11635 paint event that needs to be processed before the window is displayed
11636 correctly. This affects mainly the use of QScreen::grabWindow(). Also note
11637 that semi-transparent windows update and resize significantly slower than
11638 opaque windows.
11639
11640 \sa setMask()
11641*/
11642qreal QWidget::windowOpacity() const
11643{
11644 Q_D(const QWidget);
11645 return (isWindow() && d->maybeTopData()) ? d->maybeTopData()->opacity / 255. : 1.0;
11646}
11647
11648void QWidget::setWindowOpacity(qreal opacity)
11649{
11650 Q_D(QWidget);
11651 if (!isWindow())
11652 return;
11653
11654 opacity = qBound(qreal(0.0), opacity, qreal(1.0));
11655 QTLWExtra *extra = d->topData();
11656 extra->opacity = uint(opacity * 255);
11657 setAttribute(Qt::WA_WState_WindowOpacitySet);
11658 d->setWindowOpacity_sys(opacity);
11659
11660 if (!testAttribute(Qt::WA_WState_Created))
11661 return;
11662
11663#if QT_CONFIG(graphicsview)
11664 if (QGraphicsProxyWidget *proxy = graphicsProxyWidget()) {
11665 // Avoid invalidating the cache if set.
11666 if (proxy->cacheMode() == QGraphicsItem::NoCache)
11667 proxy->update();
11668 else if (QGraphicsScene *scene = proxy->scene())
11669 scene->update(proxy->sceneBoundingRect());
11670 return;
11671 }
11672#endif
11673}
11674
11675void QWidgetPrivate::setWindowOpacity_sys(qreal level)
11676{
11677 Q_Q(QWidget);
11678 if (q->windowHandle())
11679 q->windowHandle()->setOpacity(level);
11680}
11681
11682/*!
11683 \property QWidget::windowModified
11684 \brief whether the document shown in the window has unsaved changes
11685
11686 A modified window is a window whose content has changed but has
11687 not been saved to disk. This flag will have different effects
11688 varied by the platform. On \macos the close button will have a
11689 modified look; on other platforms, the window title will have an
11690 '*' (asterisk).
11691
11692 The window title must contain a "[*]" placeholder, which
11693 indicates where the '*' should appear. Normally, it should appear
11694 right after the file name (e.g., "document1.txt[*] - Text
11695 Editor"). If the window isn't modified, the placeholder is simply
11696 removed.
11697
11698 Note that if a widget is set as modified, all its ancestors will
11699 also be set as modified. However, if you call \c
11700 {setWindowModified(false)} on a widget, this will not propagate to
11701 its parent because other children of the parent might have been
11702 modified.
11703
11704 \sa windowTitle
11705*/
11706bool QWidget::isWindowModified() const
11707{
11708 return testAttribute(Qt::WA_WindowModified);
11709}
11710
11711void QWidget::setWindowModified(bool mod)
11712{
11713 Q_D(QWidget);
11714 setAttribute(Qt::WA_WindowModified, mod);
11715
11716 d->setWindowModified_helper();
11717
11718 QEvent e(QEvent::ModifiedChange);
11719 QCoreApplication::sendEvent(this, &e);
11720}
11721
11722void QWidgetPrivate::setWindowModified_helper()
11723{
11724 Q_Q(QWidget);
11725 QWindow *window = q->windowHandle();
11726 if (!window)
11727 return;
11728 QPlatformWindow *platformWindow = window->handle();
11729 if (!platformWindow)
11730 return;
11731 bool on = q->testAttribute(Qt::WA_WindowModified);
11732 if (!platformWindow->setWindowModified(on)) {
11733 if (Q_UNLIKELY(on && !q->windowTitle().contains("[*]"_L1)))
11734 qWarning("QWidget::setWindowModified: The window title does not contain a '[*]' placeholder");
11735 setWindowTitle_helper(q->windowTitle());
11736 setWindowIconText_helper(q->windowIconText());
11737 }
11738}
11739
11740#if QT_CONFIG(tooltip)
11741/*!
11742 \property QWidget::toolTip
11743
11744 \brief the widget's tooltip
11745
11746 Note that by default tooltips are only shown for widgets that are
11747 children of the active window. You can change this behavior by
11748 setting the attribute Qt::WA_AlwaysShowToolTips on the \e window,
11749 not on the widget with the tooltip.
11750
11751 If you want to control a tooltip's behavior, you can intercept the
11752 event() function and catch the QEvent::ToolTip event (e.g., if you
11753 want to customize the area for which the tooltip should be shown).
11754
11755 By default, this property contains an empty string.
11756
11757 \sa QToolTip, statusTip, whatsThis
11758*/
11759void QWidget::setToolTip(const QString &s)
11760{
11761 Q_D(QWidget);
11762 d->toolTip = s;
11763
11764 QEvent event(QEvent::ToolTipChange);
11765 QCoreApplication::sendEvent(this, &event);
11766}
11767
11768QString QWidget::toolTip() const
11769{
11770 Q_D(const QWidget);
11771 return d->toolTip;
11772}
11773
11774/*!
11775 \property QWidget::toolTipDuration
11776 \brief the widget's tooltip duration
11777 \since 5.2
11778
11779 Specifies how long time the tooltip will be displayed, in milliseconds.
11780 If the value is -1 (default) the duration is calculated depending on the length of the tooltip.
11781
11782 \sa toolTip
11783*/
11784
11785void QWidget::setToolTipDuration(int msec)
11786{
11787 Q_D(QWidget);
11788 d->toolTipDuration = std::chrono::milliseconds(msec);
11789}
11790
11791int QWidget::toolTipDuration() const
11792{
11793 Q_D(const QWidget);
11794 return d->toolTipDuration.count();
11795}
11796
11797/*!
11798 \since 6.13
11799 \overload
11800
11801 Sets the tooltip duration to \a msec.
11802 A value of -1ms (default) calculates the duration depending on the length of the tooltip.
11803
11804 \sa toolTipDurationAsDuration()
11805*/
11806void QWidget::setToolTipDuration(std::chrono::milliseconds msec)
11807{
11808 Q_D(QWidget);
11809 d->toolTipDuration = msec;
11810}
11811
11812/*!
11813 \since 6.13
11814
11815 Returns how long the tooltip will be displayed.
11816 If the value is -1 (default) the duration is calculated depending on the length of the tooltip.
11817*/
11818std::chrono::milliseconds QWidget::toolTipDurationAsDuration() const
11819{
11820 Q_D(const QWidget);
11821 return d->toolTipDuration;
11822}
11823
11824#endif // QT_CONFIG(tooltip)
11825
11826
11827#if QT_CONFIG(statustip)
11828/*!
11829 \property QWidget::statusTip
11830 \brief the widget's status tip
11831
11832 By default, this property contains an empty string.
11833
11834 \sa toolTip, whatsThis
11835*/
11836void QWidget::setStatusTip(const QString &s)
11837{
11838 Q_D(QWidget);
11839 d->statusTip = s;
11840}
11841
11842QString QWidget::statusTip() const
11843{
11844 Q_D(const QWidget);
11845 return d->statusTip;
11846}
11847#endif // QT_CONFIG(statustip)
11848
11849#if QT_CONFIG(whatsthis)
11850/*!
11851 \property QWidget::whatsThis
11852
11853 \brief the widget's What's This help text.
11854
11855 By default, this property contains an empty string.
11856
11857 \sa QWhatsThis, QWidget::toolTip, QWidget::statusTip
11858*/
11859void QWidget::setWhatsThis(const QString &s)
11860{
11861 Q_D(QWidget);
11862 d->whatsThis = s;
11863}
11864
11865QString QWidget::whatsThis() const
11866{
11867 Q_D(const QWidget);
11868 return d->whatsThis;
11869}
11870#endif // QT_CONFIG(whatsthis)
11871
11872#if QT_CONFIG(accessibility)
11873/*!
11874 \property QWidget::accessibleName
11875
11876 \brief the widget's name as seen by assistive technologies
11877
11878 This is the primary name by which assistive technology such as screen readers
11879 announce this widget. For most widgets setting this property is not required.
11880 For example for QPushButton the button's text will be used.
11881
11882 It is important to set this property when the widget does not provide any
11883 text. For example a button that only contains an icon needs to set this
11884 property to work with screen readers.
11885 The name should be short and equivalent to the visual information conveyed
11886 by the widget.
11887
11888 This property has to be \l{Internationalization with Qt}{localized}.
11889
11890 By default, this property contains an empty string.
11891
11892 \sa QWidget::accessibleDescription, QAccessibleInterface::text()
11893*/
11894void QWidget::setAccessibleName(const QString &name)
11895{
11896 Q_D(QWidget);
11897 if (d->accessibleName == name)
11898 return;
11899
11900 d->accessibleName = name;
11901 QAccessibleEvent event(this, QAccessible::NameChanged);
11902 QAccessible::updateAccessibility(&event);
11903}
11904
11905QString QWidget::accessibleName() const
11906{
11907 Q_D(const QWidget);
11908 return d->accessibleName;
11909}
11910
11911/*!
11912 \property QWidget::accessibleDescription
11913
11914 \brief the widget's description as seen by assistive technologies
11915
11916 The accessible description of a widget should convey what a widget does.
11917 While the \l accessibleName should be a short and concise string (e.g. \gui{Save}),
11918 the description should give more context, such as \gui{Saves the current document}.
11919
11920 This property has to be \l{Internationalization with Qt}{localized}.
11921
11922 By default, this property contains an empty string and Qt falls back
11923 to using the tool tip to provide this information.
11924
11925 \sa QWidget::accessibleName, QAccessibleInterface::text()
11926*/
11927void QWidget::setAccessibleDescription(const QString &description)
11928{
11929 Q_D(QWidget);
11930 if (d->accessibleDescription == description)
11931 return;
11932
11933 d->accessibleDescription = description;
11934 QAccessibleEvent event(this, QAccessible::DescriptionChanged);
11935 QAccessible::updateAccessibility(&event);
11936}
11937
11938QString QWidget::accessibleDescription() const
11939{
11940 Q_D(const QWidget);
11941 return d->accessibleDescription;
11942}
11943
11944/*!
11945 \property QWidget::accessibleIdentifier
11946
11947 \brief the widget's identifier as seen by assistive technologies
11948
11949 If set, the accessible identifier of a widget can be used by assistive
11950 technologies in order to identify a specific widget, e.g. in automated
11951 tests.
11952
11953 \since 6.9
11954*/
11955void QWidget::setAccessibleIdentifier(const QString &identifier)
11956{
11957 Q_D(QWidget);
11958 if (d->accessibleIdentifier == identifier)
11959 return;
11960
11961 d->accessibleIdentifier = identifier;
11962 QAccessibleEvent event(this, QAccessible::IdentifierChanged);
11963 QAccessible::updateAccessibility(&event);
11964}
11965
11966QString QWidget::accessibleIdentifier() const
11967{
11968 Q_D(const QWidget);
11969 return d->accessibleIdentifier;
11970}
11971
11972#endif // QT_CONFIG(accessibility)
11973
11974#ifndef QT_NO_SHORTCUT
11975/*!
11976 Adds a shortcut to Qt's shortcut system that watches for the given
11977 \a key sequence in the given \a context. If the \a context is
11978 Qt::ApplicationShortcut, the shortcut applies to the application as a
11979 whole. Otherwise, it is either local to this widget, Qt::WidgetShortcut,
11980 or to the window itself, Qt::WindowShortcut.
11981
11982 If the same \a key sequence has been grabbed by several widgets,
11983 when the \a key sequence occurs a QEvent::Shortcut event is sent
11984 to all the widgets to which it applies in a non-deterministic
11985 order, but with the ``ambiguous'' flag set to true.
11986
11987 \warning You should not normally need to use this function;
11988 instead create \l{QAction}s with the shortcut key sequences you
11989 require (if you also want equivalent menu options and toolbar
11990 buttons), or create \l{QShortcut}s if you just need key sequences.
11991 Both QAction and QShortcut handle all the event filtering for you,
11992 and provide signals which are triggered when the user triggers the
11993 key sequence, so are much easier to use than this low-level
11994 function.
11995
11996 \sa releaseShortcut(), setShortcutEnabled()
11997*/
11998int QWidget::grabShortcut(const QKeySequence &key, Qt::ShortcutContext context)
11999{
12000 Q_ASSERT(qApp);
12001 if (key.isEmpty())
12002 return 0;
12003 setAttribute(Qt::WA_GrabbedShortcut);
12004 return QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(this, key, context, qWidgetShortcutContextMatcher);
12005}
12006
12007/*!
12008 Removes the shortcut with the given \a id from Qt's shortcut
12009 system. The widget will no longer receive QEvent::Shortcut events
12010 for the shortcut's key sequence (unless it has other shortcuts
12011 with the same key sequence).
12012
12013 \warning You should not normally need to use this function since
12014 Qt's shortcut system removes shortcuts automatically when their
12015 parent widget is destroyed. It is best to use QAction or
12016 QShortcut to handle shortcuts, since they are easier to use than
12017 this low-level function. Note also that this is an expensive
12018 operation.
12019
12020 \sa grabShortcut(), setShortcutEnabled()
12021*/
12022void QWidget::releaseShortcut(int id)
12023{
12024 Q_ASSERT(qApp);
12025 if (id)
12026 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id, this, 0);
12027}
12028
12029/*!
12030 If \a enable is true, the shortcut with the given \a id is
12031 enabled; otherwise the shortcut is disabled.
12032
12033 \warning You should not normally need to use this function since
12034 Qt's shortcut system enables/disables shortcuts automatically as
12035 widgets become hidden/visible and gain or lose focus. It is best
12036 to use QAction or QShortcut to handle shortcuts, since they are
12037 easier to use than this low-level function.
12038
12039 \sa grabShortcut(), releaseShortcut()
12040*/
12041void QWidget::setShortcutEnabled(int id, bool enable)
12042{
12043 Q_ASSERT(qApp);
12044 if (id)
12045 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id, this, 0);
12046}
12047
12048/*!
12049 \since 4.2
12050
12051 If \a enable is true, auto repeat of the shortcut with the
12052 given \a id is enabled; otherwise it is disabled.
12053
12054 \sa grabShortcut(), releaseShortcut()
12055*/
12056void QWidget::setShortcutAutoRepeat(int id, bool enable)
12057{
12058 Q_ASSERT(qApp);
12059 if (id)
12060 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(enable, id, this, 0);
12061}
12062#endif // QT_NO_SHORTCUT
12063
12064/*!
12065 Updates the widget's micro focus and informs input methods
12066 that the state specified by \a query has changed.
12067*/
12068void QWidget::updateMicroFocus(Qt::InputMethodQuery query)
12069{
12070 if (this == QGuiApplication::focusObject())
12071 QGuiApplication::inputMethod()->update(query);
12072}
12073
12074/*!
12075 Raises this widget to the top of the parent widget's stack.
12076
12077 After this call the widget will be visually in front of any
12078 overlapping sibling widgets.
12079
12080 \note When using activateWindow(), you can call this function to
12081 ensure that the window is stacked on top.
12082
12083 \sa lower(), stackUnder()
12084*/
12085
12086void QWidget::raise()
12087{
12088 Q_D(QWidget);
12089 if (!isWindow()) {
12090 QWidget *p = parentWidget();
12091 const int parentChildCount = p->d_func()->children.size();
12092 if (parentChildCount < 2)
12093 return;
12094 const int from = p->d_func()->children.indexOf(this);
12095 Q_ASSERT(from >= 0);
12096 // Do nothing if the widget is already in correct stacking order _and_ created.
12097 if (from != parentChildCount -1)
12098 p->d_func()->children.move(from, parentChildCount - 1);
12099 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12100 create();
12101 else if (from == parentChildCount - 1)
12102 return;
12103
12104 QRegion region(rect());
12105 d->subtractOpaqueSiblings(region);
12106 d->invalidateBackingStore(region);
12107 }
12108 if (testAttribute(Qt::WA_WState_Created))
12109 d->raise_sys();
12110
12111 if (d->extra && d->extra->hasWindowContainer)
12112 QWindowContainer::parentWasRaised(this);
12113
12114 QEvent e(QEvent::ZOrderChange);
12115 QCoreApplication::sendEvent(this, &e);
12116}
12117
12118void QWidgetPrivate::raise_sys()
12119{
12120 Q_Q(QWidget);
12121 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12122 q->windowHandle()->raise();
12123 } else if (renderToTexture) {
12124 if (QWidget *p = q->parentWidget()) {
12125 setDirtyOpaqueRegion();
12126 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12127 }
12128 }
12129}
12130
12131/*!
12132 Lowers the widget to the bottom of the parent widget's stack.
12133
12134 After this call the widget will be visually behind (and therefore
12135 obscured by) any overlapping sibling widgets.
12136
12137 \note On HarmonyOS, top-level windows cannot be lowered. The reason
12138 is that the platform window API has no call to move a window below
12139 other windows.
12140
12141 \sa raise(), stackUnder()
12142*/
12143
12144void QWidget::lower()
12145{
12146 Q_D(QWidget);
12147 if (!isWindow()) {
12148 QWidget *p = parentWidget();
12149 const int parentChildCount = p->d_func()->children.size();
12150 if (parentChildCount < 2)
12151 return;
12152 const int from = p->d_func()->children.indexOf(this);
12153 Q_ASSERT(from >= 0);
12154 // Do nothing if the widget is already in correct stacking order _and_ created.
12155 if (from != 0)
12156 p->d_func()->children.move(from, 0);
12157 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12158 create();
12159 else if (from == 0)
12160 return;
12161 }
12162 if (testAttribute(Qt::WA_WState_Created))
12163 d->lower_sys();
12164
12165 if (d->extra && d->extra->hasWindowContainer)
12166 QWindowContainer::parentWasLowered(this);
12167
12168 QEvent e(QEvent::ZOrderChange);
12169 QCoreApplication::sendEvent(this, &e);
12170}
12171
12172void QWidgetPrivate::lower_sys()
12173{
12174 Q_Q(QWidget);
12175 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12176 Q_ASSERT(q->testAttribute(Qt::WA_WState_Created));
12177 q->windowHandle()->lower();
12178 } else if (QWidget *p = q->parentWidget()) {
12179 setDirtyOpaqueRegion();
12180 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12181 }
12182}
12183
12184/*!
12185 Places the widget under \a w in the parent widget's stack.
12186
12187 To make this work, the widget itself and \a w must be siblings.
12188
12189 \sa raise(), lower()
12190*/
12191void QWidget::stackUnder(QWidget* w)
12192{
12193 Q_D(QWidget);
12194 QWidget *p = parentWidget();
12195 if (!w || isWindow() || p != w->parentWidget() || this == w)
12196 return;
12197 if (p) {
12198 int from = p->d_func()->children.indexOf(this);
12199 int to = p->d_func()->children.indexOf(w);
12200 Q_ASSERT(from >= 0);
12201 Q_ASSERT(to >= 0);
12202 if (from < to)
12203 --to;
12204 // Do nothing if the widget is already in correct stacking order _and_ created.
12205 if (from != to)
12206 p->d_func()->children.move(from, to);
12207 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12208 create();
12209 else if (from == to)
12210 return;
12211 }
12212 if (testAttribute(Qt::WA_WState_Created))
12213 d->stackUnder_sys(w);
12214
12215 QEvent e(QEvent::ZOrderChange);
12216 QCoreApplication::sendEvent(this, &e);
12217}
12218
12219void QWidgetPrivate::stackUnder_sys(QWidget*)
12220{
12221 Q_Q(QWidget);
12222 if (QWidget *p = q->parentWidget()) {
12223 setDirtyOpaqueRegion();
12224 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12225 }
12226}
12227
12228/*!
12229 \fn bool QWidget::isTopLevel() const
12230 \deprecated
12231
12232 Use isWindow() instead.
12233*/
12234
12235/*!
12236 \fn bool QWidget::isRightToLeft() const
12237 \internal
12238*/
12239
12240/*!
12241 \fn bool QWidget::isLeftToRight() const
12242 \internal
12243*/
12244
12245/*!
12246 \macro QWIDGETSIZE_MAX
12247 \relates QWidget
12248
12249 Defines the maximum size for a QWidget object.
12250
12251 The largest allowed size for a widget is QSize(QWIDGETSIZE_MAX,
12252 QWIDGETSIZE_MAX), i.e. QSize (16777215,16777215).
12253
12254 \sa QWidget::setMaximumSize()
12255*/
12256
12257/*!
12258 \fn QWidget::setupUi(QWidget *widget)
12259
12260 Sets up the user interface for the specified \a widget.
12261
12262 \note This function is available with widgets that derive from user
12263 interface descriptions created using \l{uic}.
12264
12265 \sa {Using a Qt Widgets Designer UI File in Your Application}
12266*/
12267
12268QRect QWidgetPrivate::frameStrut() const
12269{
12270 Q_Q(const QWidget);
12271 if (!q->isWindow() || q->testAttribute(Qt::WA_DontShowOnScreen)) {
12272 // x2 = x1 + w - 1, so w/h = 1
12273 return QRect(0, 0, 1, 1);
12274 }
12275
12276 if (data.fstrut_dirty
12277 // ### Fix properly for 4.3
12278 && q->isVisible()
12279 && q->testAttribute(Qt::WA_WState_Created))
12280 const_cast<QWidgetPrivate *>(this)->updateFrameStrut();
12281
12282 return maybeTopData() ? maybeTopData()->frameStrut : QRect();
12283}
12284
12285void QWidgetPrivate::updateFrameStrut()
12286{
12287 Q_Q(QWidget);
12288 if (q->data->fstrut_dirty) {
12289 if (QTLWExtra *te = maybeTopData()) {
12290 if (te->window && te->window->handle()) {
12291 const QMargins margins = te->window->frameMargins();
12292 if (!margins.isNull()) {
12293 te->frameStrut.setCoords(margins.left(), margins.top(), margins.right(), margins.bottom());
12294 q->data->fstrut_dirty = false;
12295 }
12296 }
12297 }
12298 }
12299}
12300
12301/*!
12302 \since 5.0
12303 \internal
12304
12305 Sets the backing store to be the \a store specified.
12306 The QWidget will take ownership of the \a store.
12307*/
12308void QWidget::setBackingStore(QBackingStore *store)
12309{
12310 // ### createWinId() ??
12311
12312 if (!isWindow())
12313 return;
12314
12315 Q_D(QWidget);
12316
12317 QTLWExtra *topData = d->topData();
12318 if (topData->backingStore == store)
12319 return;
12320
12321 QBackingStore *oldStore = topData->backingStore;
12322 delete topData->backingStore;
12323 topData->backingStore = store;
12324
12325 QWidgetRepaintManager *repaintManager = d->maybeRepaintManager();
12326 if (!repaintManager)
12327 return;
12328
12329 if (isWindow()) {
12330 if (repaintManager->backingStore() != oldStore && repaintManager->backingStore() != store)
12331 delete repaintManager->backingStore();
12332 repaintManager->setBackingStore(store);
12333 }
12334}
12335
12336/*!
12337 \since 5.0
12338
12339 Returns the QBackingStore this widget will be drawn into.
12340*/
12341QBackingStore *QWidget::backingStore() const
12342{
12343 Q_D(const QWidget);
12344 QTLWExtra *extra = d->maybeTopData();
12345 if (extra && extra->backingStore)
12346 return extra->backingStore;
12347
12348 if (!isWindow())
12349 return window()->backingStore();
12350
12351 return nullptr;
12352}
12353
12354void QWidgetPrivate::getLayoutItemMargins(int *left, int *top, int *right, int *bottom) const
12355{
12356 if (left)
12357 *left = (int)leftLayoutItemMargin;
12358 if (top)
12359 *top = (int)topLayoutItemMargin;
12360 if (right)
12361 *right = (int)rightLayoutItemMargin;
12362 if (bottom)
12363 *bottom = (int)bottomLayoutItemMargin;
12364}
12365
12366void QWidgetPrivate::setLayoutItemMargins(int left, int top, int right, int bottom)
12367{
12368 if (leftLayoutItemMargin == left
12369 && topLayoutItemMargin == top
12370 && rightLayoutItemMargin == right
12371 && bottomLayoutItemMargin == bottom)
12372 return;
12373
12374 Q_Q(QWidget);
12375 leftLayoutItemMargin = (signed char)left;
12376 topLayoutItemMargin = (signed char)top;
12377 rightLayoutItemMargin = (signed char)right;
12378 bottomLayoutItemMargin = (signed char)bottom;
12379 q->updateGeometry();
12380}
12381
12382void QWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element, const QStyleOption *opt)
12383{
12384 Q_Q(QWidget);
12385 QStyleOption myOpt;
12386 if (!opt) {
12387 myOpt.initFrom(q);
12388 myOpt.rect.setRect(0, 0, 32768, 32768); // arbitrary
12389 opt = &myOpt;
12390 }
12391
12392 QRect liRect = q->style()->subElementRect(element, opt, q);
12393 if (liRect.isValid()) {
12394 leftLayoutItemMargin = (signed char)(opt->rect.left() - liRect.left());
12395 topLayoutItemMargin = (signed char)(opt->rect.top() - liRect.top());
12396 rightLayoutItemMargin = (signed char)(liRect.right() - opt->rect.right());
12397 bottomLayoutItemMargin = (signed char)(liRect.bottom() - opt->rect.bottom());
12398 } else {
12399 leftLayoutItemMargin = 0;
12400 topLayoutItemMargin = 0;
12401 rightLayoutItemMargin = 0;
12402 bottomLayoutItemMargin = 0;
12403 }
12404}
12405// resets the Qt::WA_QuitOnClose attribute to the default value for transient widgets.
12406void QWidgetPrivate::adjustQuitOnCloseAttribute()
12407{
12408 Q_Q(QWidget);
12409
12410 if (!q->parentWidget()) {
12411 Qt::WindowType type = q->windowType();
12412 if (type == Qt::Widget || type == Qt::SubWindow)
12413 type = Qt::Window;
12414 if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
12415 q->setAttribute(Qt::WA_QuitOnClose, false);
12416 }
12417}
12418
12419void QWidgetPrivate::sendComposeStatus(QWidget *w, bool end)
12420{
12421 QWidgetPrivate *wd = QWidgetPrivate::get(w);
12422 if (!wd->textureChildSeen)
12423 return;
12424 if (end)
12425 wd->endCompose();
12426 else
12427 wd->beginCompose();
12428 for (int i = 0; i < wd->children.size(); ++i) {
12429 w = qobject_cast<QWidget *>(wd->children.at(i));
12430 if (w && !w->isWindow() && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen)
12431 sendComposeStatus(w, end);
12432 }
12433}
12434
12435Q_WIDGETS_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget)
12436{
12437 return widget->data;
12438}
12439
12440Q_WIDGETS_EXPORT QWidgetPrivate *qt_widget_private(QWidget *widget)
12441{
12442 return widget->d_func();
12443}
12444
12445
12446#if QT_CONFIG(graphicsview)
12447/*!
12448 \since 4.5
12449
12450 Returns the proxy widget for the corresponding embedded widget in a graphics
12451 view; otherwise returns \nullptr.
12452
12453 \sa QGraphicsProxyWidget::createProxyForChildWidget(),
12454 QGraphicsScene::addWidget()
12455 */
12456QGraphicsProxyWidget *QWidget::graphicsProxyWidget() const
12457{
12458 Q_D(const QWidget);
12459 if (d->extra) {
12460 return d->extra->proxyWidget;
12461 }
12462 return nullptr;
12463}
12464#endif
12465
12466#ifndef QT_NO_GESTURES
12467/*!
12468 Subscribes the widget to a given \a gesture with specific \a flags.
12469
12470 \sa ungrabGesture(), QGestureEvent
12471 \since 4.6
12472*/
12473void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
12474{
12475 Q_D(QWidget);
12476 d->gestureContext.insert(gesture, flags);
12477 (void)QGestureManager::instance(); // create a gesture manager
12478}
12479
12480/*!
12481 Unsubscribes the widget from a given \a gesture type
12482
12483 \sa grabGesture(), QGestureEvent
12484 \since 4.6
12485*/
12486void QWidget::ungrabGesture(Qt::GestureType gesture)
12487{
12488 // if you modify this function, check the inlined version in ~QWidget, too
12489 Q_D(QWidget);
12490 if (d->gestureContext.remove(gesture)) {
12491 if (QGestureManager *manager = QGestureManager::instance())
12492 manager->cleanupCachedGestures(this, gesture);
12493 }
12494}
12495#endif // QT_NO_GESTURES
12496
12497/*!
12498 \fn void QWidget::destroy(bool destroyWindow, bool destroySubWindows)
12499
12500 Frees up window system resources. Destroys the widget window if \a
12501 destroyWindow is true.
12502
12503 destroy() calls itself recursively for all the child widgets,
12504 passing \a destroySubWindows for the \a destroyWindow parameter.
12505 To have more control over destruction of subwidgets, destroy
12506 subwidgets selectively first.
12507
12508 This function is usually called from the QWidget destructor.
12509*/
12510void QWidget::destroy(bool destroyWindow, bool destroySubWindows)
12511{
12512 Q_D(QWidget);
12513
12514 d->aboutToDestroy();
12515 if (!isWindow() && parentWidget())
12516 parentWidget()->d_func()->invalidateBackingStore(d->effectiveRectFor(geometry()));
12517 d->deactivateWidgetCleanup();
12518
12519 if ((windowType() == Qt::Popup) && qApp)
12520 qApp->d_func()->closePopup(this);
12521
12522 if (this == qApp->activeWindow())
12523 QApplicationPrivate::setActiveWindow(nullptr);
12524 if (QWidget::mouseGrabber() == this)
12525 releaseMouse();
12526 if (QWidget::keyboardGrabber() == this)
12527 releaseKeyboard();
12528
12529 setAttribute(Qt::WA_WState_Created, false);
12530
12531 if (destroySubWindows) {
12532 QObjectList childList(children());
12533 for (int i = 0; i < childList.size(); i++) {
12534 QWidget *widget = qobject_cast<QWidget *>(childList.at(i));
12535 if (widget && widget->testAttribute(Qt::WA_NativeWindow)) {
12536 if (widget->windowHandle()) {
12537 widget->destroy();
12538 }
12539 }
12540 }
12541 }
12542 if (destroyWindow) {
12543 d->deleteTLSysExtra();
12544 } else {
12545 if (parentWidget() && parentWidget()->testAttribute(Qt::WA_WState_Created)) {
12546 d->hide_sys();
12547 }
12548 }
12549
12550 d->setWinId(0);
12551}
12552
12553/*!
12554 \fn QPaintEngine *QWidget::paintEngine() const
12555
12556 Returns the widget's paint engine.
12557
12558 Note that this function should not be called explicitly by the
12559 user, since it's meant for reimplementation purposes only. The
12560 function is called by Qt internally, and the default
12561 implementation may not always return a valid pointer.
12562*/
12563QPaintEngine *QWidget::paintEngine() const
12564{
12565 qWarning("QWidget::paintEngine: Should no longer be called");
12566
12567#ifdef Q_OS_WIN
12568 // We set this bit which is checked in setAttribute for
12569 // Qt::WA_PaintOnScreen. We do this to allow these two scenarios:
12570 //
12571 // 1. Users accidentally set Qt::WA_PaintOnScreen on X and port to
12572 // Windows which would mean suddenly their widgets stop working.
12573 //
12574 // 2. Users set paint on screen and subclass paintEngine() to
12575 // return 0, in which case we have a "hole" in the backingstore
12576 // allowing use of GDI or DirectX directly.
12577 //
12578 // 1 is WRONG, but to minimize silent failures, we have set this
12579 // bit to ignore the setAttribute call. 2. needs to be
12580 // supported because its our only means of embedding native
12581 // graphics stuff.
12582 const_cast<QWidgetPrivate *>(d_func())->noPaintOnScreen = 1;
12583#endif
12584
12585 return nullptr; //##### @@@
12586}
12587
12588// Do not call QWindow::mapToGlobal() until QPlatformWindow is properly showing.
12589static inline bool canMapPosition(QWindow *window)
12590{
12591 return window->handle() && !qt_window_private(window)->resizeEventPending;
12592}
12593
12594#if QT_CONFIG(graphicsview)
12595static inline QGraphicsProxyWidget *graphicsProxyWidget(const QWidget *w)
12596{
12597 QGraphicsProxyWidget *result = nullptr;
12598 const QWidgetPrivate *d = qt_widget_private(const_cast<QWidget *>(w));
12599 if (d->extra)
12600 result = d->extra->proxyWidget;
12601 return result;
12602}
12603#endif // QT_CONFIG(graphicsview)
12604
12606 QTransform transform;
12607 QWindow *window;
12608};
12609
12611{
12613 result.window = nullptr;
12614 for ( ; w ; w = w->parentWidget()) {
12615#if QT_CONFIG(graphicsview)
12616 if (QGraphicsProxyWidget *qgpw = graphicsProxyWidget(w)) {
12617 if (const QGraphicsScene *scene = qgpw->scene()) {
12618 const QList <QGraphicsView *> views = scene->views();
12619 if (!views.isEmpty()) {
12620 auto *viewP = static_cast<QGraphicsViewPrivate *>(qt_widget_private(views.constFirst()));
12621 result.transform *= viewP->mapToViewTransform(qgpw);
12622 w = views.first()->viewport();
12623 }
12624 }
12625 }
12626#endif // QT_CONFIG(graphicsview)
12627 QWindow *window = w->windowHandle();
12628 if (window && canMapPosition(window)) {
12629 result.window = window;
12630 break;
12631 }
12632
12633 const auto &geometry = w->geometry();
12634 result.transform *= QTransform::fromTranslate(geometry.x(), geometry.y());
12635 if (w->isWindow())
12636 break;
12637 }
12638 return result;
12639}
12640
12641/*!
12642 \fn QPointF QWidget::mapToGlobal(const QPointF &pos) const
12643
12644 Translates the widget coordinate \a pos to global screen
12645 coordinates. For example, \c{mapToGlobal(QPointF(0,0))} would give
12646 the global coordinates of the top-left pixel of the widget.
12647
12648 \sa mapFromGlobal(), mapTo(), mapToParent()
12649 \since 6.0
12650*/
12651QPointF QWidget::mapToGlobal(const QPointF &pos) const
12652{
12653 const MapToGlobalTransformResult t = mapToGlobalTransform(this);
12654 const QPointF g = t.transform.map(pos);
12655 return t.window ? t.window->mapToGlobal(g) : g;
12656}
12657
12658/*!
12659 \overload
12660*/
12661QPoint QWidget::mapToGlobal(const QPoint &pos) const
12662{
12663 return mapToGlobal(QPointF(pos)).toPoint();
12664}
12665
12666/*!
12667 \fn QPointF QWidget::mapFromGlobal(const QPointF &pos) const
12668
12669 Translates the global screen coordinate \a pos to widget
12670 coordinates.
12671
12672 \sa mapToGlobal(), mapFrom(), mapFromParent()
12673 \since 6.0
12674*/
12675QPointF QWidget::mapFromGlobal(const QPointF &pos) const
12676{
12677 const MapToGlobalTransformResult t = mapToGlobalTransform(this);
12678 const QPointF windowLocal = t.window ? t.window->mapFromGlobal(pos) : pos;
12679 return t.transform.inverted().map(windowLocal);
12680}
12681
12682/*!
12683 \overload
12684*/
12685QPoint QWidget::mapFromGlobal(const QPoint &pos) const
12686{
12687 return mapFromGlobal(QPointF(pos)).toPoint();
12688}
12689
12691static bool mouseGrabWithCursor = false;
12692static QWidget *keyboardGrb = nullptr;
12693
12694static inline QWindow *grabberWindow(const QWidget *w)
12695{
12696 QWindow *window = w->windowHandle();
12697 if (!window)
12698 if (const QWidget *nativeParent = w->nativeParentWidget())
12699 window = nativeParent->windowHandle();
12700 return window;
12701}
12702
12703#ifndef QT_NO_CURSOR
12704static void grabMouseForWidget(QWidget *widget, const QCursor *cursor = nullptr)
12705#else
12706static void grabMouseForWidget(QWidget *widget)
12707#endif
12708{
12709 if (qt_mouseGrb)
12710 qt_mouseGrb->releaseMouse();
12711
12712 mouseGrabWithCursor = false;
12713 if (QWindow *window = grabberWindow(widget)) {
12714#ifndef QT_NO_CURSOR
12715 if (cursor) {
12716 mouseGrabWithCursor = true;
12717 QGuiApplication::setOverrideCursor(*cursor);
12718 }
12719#endif // !QT_NO_CURSOR
12720 window->setMouseGrabEnabled(true);
12721 }
12722
12723 qt_mouseGrb = widget;
12724}
12725
12727{
12728 if (qt_mouseGrb == widget) {
12729 if (QWindow *window = grabberWindow(widget)) {
12730#ifndef QT_NO_CURSOR
12731 if (mouseGrabWithCursor) {
12732 QGuiApplication::restoreOverrideCursor();
12733 mouseGrabWithCursor = false;
12734 }
12735#endif // !QT_NO_CURSOR
12736 window->setMouseGrabEnabled(false);
12737 }
12738 }
12739 qt_mouseGrb = nullptr;
12740}
12741
12742/*!
12743 \fn void QWidget::grabMouse()
12744
12745 Grabs the mouse input.
12746
12747 This widget receives all mouse events until releaseMouse() is
12748 called; other widgets get no mouse events at all. Keyboard
12749 events are not affected. Use grabKeyboard() if you want to grab
12750 that.
12751
12752 \warning Bugs in mouse-grabbing applications very often lock the
12753 terminal. Use this function with extreme caution, and consider
12754 using the \c -nograb command line option while debugging.
12755
12756 It is seldom necessary to grab the mouse when using Qt, as
12757 Qt grabs and releases it sensibly. In particular, Qt grabs the
12758 mouse when a mouse button is pressed and keeps it until the last
12759 button is released.
12760
12761 \note Only visible widgets can grab mouse input. If isVisible()
12762 returns \c false for a widget, that widget cannot call grabMouse().
12763
12764 \note On Windows, grabMouse() only works when the mouse is inside a window
12765 owned by the process.
12766 On \macos, grabMouse() only works when the mouse is inside the frame of that widget.
12767
12768 \sa releaseMouse(), grabKeyboard(), releaseKeyboard()
12769*/
12770void QWidget::grabMouse()
12771{
12772 grabMouseForWidget(this);
12773}
12774
12775/*!
12776 \fn void QWidget::grabMouse(const QCursor &cursor)
12777 \overload grabMouse()
12778
12779 Grabs the mouse input and changes the cursor shape.
12780
12781 The cursor will assume shape \a cursor (for as long as the mouse
12782 focus is grabbed) and this widget will be the only one to receive
12783 mouse events until releaseMouse() is called().
12784
12785 \warning Grabbing the mouse might lock the terminal.
12786
12787 \note See the note in QWidget::grabMouse().
12788
12789 \sa releaseMouse(), grabKeyboard(), releaseKeyboard(), setCursor()
12790*/
12791#ifndef QT_NO_CURSOR
12792void QWidget::grabMouse(const QCursor &cursor)
12793{
12794 grabMouseForWidget(this, &cursor);
12795}
12796#endif
12797
12798bool QWidgetPrivate::stealMouseGrab(bool grab)
12799{
12800 // This is like a combination of grab/releaseMouse() but with error checking
12801 // and it has no effect on the result of mouseGrabber().
12802 Q_Q(QWidget);
12803 QWindow *window = grabberWindow(q);
12804 return window ? window->setMouseGrabEnabled(grab) : false;
12805}
12806
12807/*!
12808 \fn void QWidget::releaseMouse()
12809
12810 Releases the mouse grab.
12811
12812 \sa grabMouse(), grabKeyboard(), releaseKeyboard()
12813*/
12814void QWidget::releaseMouse()
12815{
12816 releaseMouseGrabOfWidget(this);
12817}
12818
12819/*!
12820 \fn void QWidget::grabKeyboard()
12821
12822 Grabs the keyboard input.
12823
12824 This widget receives all keyboard events until releaseKeyboard()
12825 is called; other widgets get no keyboard events at all. Mouse
12826 events are not affected. Use grabMouse() if you want to grab that.
12827
12828 The focus widget is not affected, except that it doesn't receive
12829 any keyboard events. setFocus() moves the focus as usual, but the
12830 new focus widget receives keyboard events only after
12831 releaseKeyboard() is called.
12832
12833 If a different widget is currently grabbing keyboard input, that
12834 widget's grab is released first.
12835
12836 \sa releaseKeyboard(), grabMouse(), releaseMouse(), focusWidget()
12837*/
12838void QWidget::grabKeyboard()
12839{
12840 if (keyboardGrb)
12841 keyboardGrb->releaseKeyboard();
12842 if (QWindow *window = grabberWindow(this))
12843 window->setKeyboardGrabEnabled(true);
12844 keyboardGrb = this;
12845}
12846
12847bool QWidgetPrivate::stealKeyboardGrab(bool grab)
12848{
12849 // This is like a combination of grab/releaseKeyboard() but with error
12850 // checking and it has no effect on the result of keyboardGrabber().
12851 Q_Q(QWidget);
12852 QWindow *window = grabberWindow(q);
12853 return window ? window->setKeyboardGrabEnabled(grab) : false;
12854}
12855
12856/*!
12857 \fn void QWidget::releaseKeyboard()
12858
12859 Releases the keyboard grab.
12860
12861 \sa grabKeyboard(), grabMouse(), releaseMouse()
12862*/
12863void QWidget::releaseKeyboard()
12864{
12865 if (keyboardGrb == this) {
12866 if (QWindow *window = grabberWindow(this))
12867 window->setKeyboardGrabEnabled(false);
12868 keyboardGrb = nullptr;
12869 }
12870}
12871
12872/*!
12873 \fn QWidget *QWidget::mouseGrabber()
12874
12875 Returns the widget that is currently grabbing the mouse input.
12876
12877 If no widget in this application is currently grabbing the mouse,
12878 \nullptr is returned.
12879
12880 \sa grabMouse(), keyboardGrabber()
12881*/
12882QWidget *QWidget::mouseGrabber()
12883{
12884 return qt_mouseGrb;
12885}
12886
12887/*!
12888 \fn QWidget *QWidget::keyboardGrabber()
12889
12890 Returns the widget that is currently grabbing the keyboard input.
12891
12892 If no widget in this application is currently grabbing the
12893 keyboard, \nullptr is returned.
12894
12895 \sa grabMouse(), mouseGrabber()
12896*/
12897QWidget *QWidget::keyboardGrabber()
12898{
12899 return keyboardGrb;
12900}
12901
12902/*!
12903 \fn void QWidget::activateWindow()
12904
12905 Sets the top-level widget containing this widget to be the active
12906 window.
12907
12908 An active window is a visible top-level window that has the
12909 keyboard input focus.
12910
12911 This function performs the same operation as clicking the mouse on
12912 the title bar of a top-level window. On X11, the result depends on
12913 the Window Manager. If you want to ensure that the window is
12914 stacked on top as well you should also call raise(). Note that the
12915 window must be visible, otherwise activateWindow() has no effect.
12916
12917 On Windows, if you are calling this when the application is not
12918 currently the active one then it will not make it the active
12919 window. It will change the color of the taskbar entry to indicate
12920 that the window has changed in some way. This is because Microsoft
12921 does not allow an application to interrupt what the user is currently
12922 doing in another application.
12923
12924 \sa isActiveWindow(), window(), show()
12925*/
12926void QWidget::activateWindow()
12927{
12928 QWindow *const wnd = window()->windowHandle();
12929
12930 if (wnd)
12931 wnd->requestActivate();
12932}
12933
12934/*!
12935
12936 Internal implementation of the virtual QPaintDevice::metric()
12937 function.
12938
12939 \a m is the metric to get.
12940*/
12941int QWidget::metric(PaintDeviceMetric m) const
12942{
12943 QScreen *screen = this->screen();
12944
12945 if (!screen) {
12946 if (m == PdmDpiX || m == PdmDpiY)
12947 return 72;
12948 return QPaintDevice::metric(m);
12949 }
12950
12951 auto resolveDevicePixelRatio = [this, screen]() -> qreal {
12952
12953 // Note: keep in sync with QBackingStorePrivate::backingStoreDevicePixelRatio()!
12954 static bool downscale = qEnvironmentVariableIntValue("QT_WIDGETS_HIGHDPI_DOWNSCALE") > 0;
12955 QWindow *window = this->window()->windowHandle();
12956 if (window)
12957 return downscale ? std::ceil(window->devicePixelRatio()) : window->devicePixelRatio();
12958 return screen->devicePixelRatio();
12959 };
12960
12961 switch (m) {
12962 case PdmWidth:
12963 return data->crect.width();
12964 case PdmWidthMM:
12965 return data->crect.width() * screen->physicalSize().width() / screen->geometry().width();
12966 case PdmHeight:
12967 return data->crect.height();
12968 case PdmHeightMM:
12969 return data->crect.height() * screen->physicalSize().height() / screen->geometry().height();
12970 case PdmDepth:
12971 return screen->depth();
12972 case PdmDpiX:
12973 for (const QWidget *p = this; p; p = p->parentWidget()) {
12974 if (p->d_func()->extra && p->d_func()->extra->customDpiX)
12975 return p->d_func()->extra->customDpiX;
12976 }
12977 return qRound(screen->logicalDotsPerInchX());
12978 case PdmDpiY:
12979 for (const QWidget *p = this; p; p = p->parentWidget()) {
12980 if (p->d_func()->extra && p->d_func()->extra->customDpiY)
12981 return p->d_func()->extra->customDpiY;
12982 }
12983 return qRound(screen->logicalDotsPerInchY());
12984 case PdmPhysicalDpiX:
12985 return qRound(screen->physicalDotsPerInchX());
12986 case PdmPhysicalDpiY:
12987 return qRound(screen->physicalDotsPerInchY());
12988 case PdmDevicePixelRatio:
12989 return resolveDevicePixelRatio();
12990 case PdmDevicePixelRatioScaled:
12991 return QPaintDevice::devicePixelRatioFScale() * resolveDevicePixelRatio();
12992 case PdmDevicePixelRatioF_EncodedA:
12993 Q_FALLTHROUGH();
12994 case PdmDevicePixelRatioF_EncodedB:
12995 return QPaintDevice::encodeMetricF(m, resolveDevicePixelRatio());
12996 default:
12997 break;
12998 }
12999 return QPaintDevice::metric(m);
13000}
13001
13002/*!
13003 Initializes the \a painter pen, background and font to the same as
13004 the given widget's. This function is called automatically when the
13005 painter is opened on a QWidget.
13006*/
13007void QWidget::initPainter(QPainter *painter) const
13008{
13009 const QPalette &pal = palette();
13010 QPainterPrivate *painterPrivate = QPainterPrivate::get(painter);
13011
13012 painterPrivate->state->pen = QPen(pal.brush(foregroundRole()), 1);
13013 painterPrivate->state->bgBrush = pal.brush(backgroundRole());
13014 QFont f(font(), this);
13015 painterPrivate->state->deviceFont = f;
13016 painterPrivate->state->font = f;
13017
13018 painterPrivate->setEngineDirtyFlags({
13019 QPaintEngine::DirtyPen,
13020 QPaintEngine::DirtyBrush,
13021 QPaintEngine::DirtyFont,
13022 });
13023
13024 if (painterPrivate->extended)
13025 painterPrivate->extended->penChanged();
13026}
13027
13028/*!
13029 \internal
13030
13031 Do PaintDevice rendering with the specified \a offset.
13032*/
13033QPaintDevice *QWidget::redirected(QPoint *offset) const
13034{
13035 return d_func()->redirected(offset);
13036}
13037
13038/*!
13039 \internal
13040
13041 A painter that is shared among other instances of QPainter.
13042*/
13043QPainter *QWidget::sharedPainter() const
13044{
13045 // Someone sent a paint event directly to the widget
13046 if (!d_func()->redirectDev)
13047 return nullptr;
13048
13049 QPainter *sp = d_func()->sharedPainter();
13050 if (!sp || !sp->isActive())
13051 return nullptr;
13052
13053 if (sp->paintEngine()->paintDevice() != d_func()->redirectDev)
13054 return nullptr;
13055
13056 return sp;
13057}
13058
13059/*!
13060 \fn void QWidget::setMask(const QRegion &region)
13061 \overload
13062
13063 Causes only the parts of the widget which overlap \a region to be
13064 visible. If the region includes pixels outside the rect() of the
13065 widget, window system controls in that area may or may not be
13066 visible, depending on the platform.
13067
13068 Since QRegion allows arbitrarily complex regions to be created, widget
13069 masks can be made to suit the most unconventionally-shaped windows, and
13070 even allow widgets to be displayed with holes in them. Note that this
13071 effect can be slow if the region is particularly complex.
13072
13073 Widget masks are used to hint to the window system that the application
13074 does not want mouse events for areas outside the mask. On most systems,
13075 they also result in coarse visual clipping. To get smooth window edges, use
13076 translucent background and anti-aliased painting instead, as shown in the
13077 \l{Translucent Background} example.
13078
13079 \sa windowOpacity
13080*/
13081void QWidget::setMask(const QRegion &newMask)
13082{
13083 Q_D(QWidget);
13084
13085 d->createExtra();
13086 if (newMask == d->extra->mask)
13087 return;
13088
13089#ifndef QT_NO_BACKINGSTORE
13090 const QRegion oldMask(d->extra->mask);
13091#endif
13092
13093 d->extra->mask = newMask;
13094 d->extra->hasMask = !newMask.isEmpty();
13095
13096 if (!testAttribute(Qt::WA_WState_Created))
13097 return;
13098
13099 d->setMask_sys(newMask);
13100
13101#ifndef QT_NO_BACKINGSTORE
13102 if (!isVisible())
13103 return;
13104
13105 if (!d->extra->hasMask) {
13106 // Mask was cleared; update newly exposed area.
13107 QRegion expose(rect());
13108 expose -= oldMask;
13109 if (!expose.isEmpty()) {
13110 d->setDirtyOpaqueRegion();
13111 update(expose);
13112 }
13113 return;
13114 }
13115
13116 if (!isWindow()) {
13117 // Update newly exposed area on the parent widget.
13118 QRegion parentExpose(rect());
13119 parentExpose -= newMask;
13120 if (!parentExpose.isEmpty()) {
13121 d->setDirtyOpaqueRegion();
13122 parentExpose.translate(data->crect.topLeft());
13123 parentWidget()->update(parentExpose);
13124 }
13125
13126 // Update newly exposed area on this widget
13127 if (!oldMask.isEmpty())
13128 update(newMask - oldMask);
13129 }
13130#endif
13131}
13132
13133void QWidgetPrivate::setMask_sys(const QRegion &region)
13134{
13135 Q_Q(QWidget);
13136 if (QWindow *window = q->windowHandle())
13137 window->setMask(region);
13138}
13139
13140/*!
13141 \fn void QWidget::setMask(const QBitmap &bitmap)
13142
13143 Causes only the pixels of the widget for which \a bitmap has a
13144 corresponding 1 bit to be visible. If the region includes pixels
13145 outside the rect() of the widget, window system controls in that
13146 area may or may not be visible, depending on the platform.
13147
13148 Note that this effect can be slow if the region is particularly
13149 complex.
13150
13151 The following code shows how an image with an alpha channel can be
13152 used to generate a mask for a widget:
13153
13154 \snippet widget-mask/main.cpp 0
13155
13156 The label shown by this code is masked using the image it contains,
13157 giving the appearance that an irregularly-shaped image is being drawn
13158 directly onto the screen.
13159
13160 Masked widgets receive mouse events only on their visible
13161 portions.
13162
13163 \sa clearMask(), windowOpacity()
13164*/
13165void QWidget::setMask(const QBitmap &bitmap)
13166{
13167 setMask(QRegion(bitmap));
13168}
13169
13170/*!
13171 \fn void QWidget::clearMask()
13172
13173 Removes any mask set by setMask().
13174
13175 \sa setMask()
13176*/
13177void QWidget::clearMask()
13178{
13179 Q_D(QWidget);
13180 if (!d->extra || !d->extra->hasMask)
13181 return;
13182 setMask(QRegion());
13183}
13184
13185void QWidgetPrivate::setWidgetParentHelper(QObject *widgetAsObject, QObject *newParent)
13186{
13187 Q_ASSERT(widgetAsObject->isWidgetType());
13188 Q_ASSERT(!newParent || newParent->isWidgetType());
13189 QWidget *widget = static_cast<QWidget*>(widgetAsObject);
13190 widget->setParent(static_cast<QWidget*>(newParent));
13191}
13192
13193std::string QWidgetPrivate::flagsForDumping() const
13194{
13195 Q_Q(const QWidget);
13196 std::string flags = QObjectPrivate::flagsForDumping();
13197 if (QApplication::focusWidget() == q)
13198 flags += 'F';
13199 if (q->isVisible()) {
13200 std::stringstream s;
13201 s << '<'
13202 << q->width() << 'x' << q->height()
13203 << std::showpos << q->x() << q->y()
13204 << '>';
13205 flags += s.str();
13206 } else {
13207 flags += 'I';
13208 }
13209 return flags;
13210}
13211
13212void QWidgetPrivate::setNetWmWindowTypes(bool skipIfMissing)
13213{
13214#if QT_CONFIG(xcb)
13215 Q_Q(QWidget);
13216
13217 if (!q->windowHandle())
13218 return;
13219
13220 QXcbWindow::WindowTypes wmWindowType = QXcbWindow::None;
13221 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDesktop))
13222 wmWindowType |= QXcbWindow::Desktop;
13223 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDock))
13224 wmWindowType |= QXcbWindow::Dock;
13225 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolBar))
13226 wmWindowType |= QXcbWindow::Toolbar;
13227 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeMenu))
13228 wmWindowType |= QXcbWindow::Menu;
13229 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeUtility))
13230 wmWindowType |= QXcbWindow::Utility;
13231 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeSplash))
13232 wmWindowType |= QXcbWindow::Splash;
13233 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDialog))
13234 wmWindowType |= QXcbWindow::Dialog;
13235 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu))
13236 wmWindowType |= QXcbWindow::DropDownMenu;
13237 if (q->testAttribute(Qt::WA_X11NetWmWindowTypePopupMenu))
13238 wmWindowType |= QXcbWindow::PopupMenu;
13239 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip))
13240 wmWindowType |= QXcbWindow::Tooltip;
13241 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeNotification))
13242 wmWindowType |= QXcbWindow::Notification;
13243 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeCombo))
13244 wmWindowType |= QXcbWindow::Combo;
13245 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDND))
13246 wmWindowType |= QXcbWindow::Dnd;
13247
13248 if (wmWindowType == QXcbWindow::None && skipIfMissing)
13249 return;
13250
13251 if (auto *xcbWindow = dynamic_cast<QXcbWindow*>(q->windowHandle()->handle()))
13252 xcbWindow->setWindowType(wmWindowType);
13253#else
13254 Q_UNUSED(skipIfMissing);
13255#endif
13256}
13257
13258/*!
13259 \internal
13260 \return \c true, if a child with \param policy exists and isn't a child of \param excludeChildrenOf.
13261 Return false otherwise.
13262 */
13263bool QWidgetPrivate::hasChildWithFocusPolicy(Qt::FocusPolicy policy, const QWidget *excludeChildrenOf) const
13264{
13265 Q_Q(const QWidget);
13266 const QWidgetList &children = q->findChildren<QWidget *>(Qt::FindChildrenRecursively);
13267 for (const auto *child : children) {
13268 if (child->focusPolicy() == policy && child->isEnabled()
13269 && (!excludeChildrenOf || !excludeChildrenOf->isAncestorOf(child))) {
13270 return true;
13271 }
13272 }
13273 return false;
13274}
13275
13276#ifndef QT_NO_DEBUG_STREAM
13277
13278namespace {
13279QDebug operator<<(QDebug debug, const WidgetAttributes &attributes)
13280{
13281 const QDebugStateSaver saver(debug);
13282 debug.nospace();
13283 debug << '[';
13284 if (const QWidget *widget = attributes.widget) {
13285 const QMetaObject *qtMo = qt_getEnumMetaObject(Qt::WA_AttributeCount);
13286 const QMetaEnum me = qtMo->enumerator(qtMo->indexOfEnumerator("WidgetAttribute"));
13287 int count = 0;
13288 for (int a = 0; a < Qt::WA_AttributeCount; ++a) {
13289 if (widget->testAttribute(static_cast<Qt::WidgetAttribute>(a))) {
13290 if (count++)
13291 debug << ',';
13292 debug << me.valueToKey(a);
13293 }
13294 }
13295 }
13296 debug << ']';
13297 return debug;
13298}
13299}
13300
13301QDebug operator<<(QDebug debug, const QWidget *widget)
13302{
13303 const QDebugStateSaver saver(debug);
13304 debug.nospace();
13305 if (widget) {
13306 debug << widget->metaObject()->className() << '(' << (const void *)widget;
13307 if (!widget->objectName().isEmpty())
13308 debug << ", name=" << widget->objectName();
13309 if (debug.verbosity() > 2) {
13310 const QRect geometry = widget->geometry();
13311 const QRect frameGeometry = widget->frameGeometry();
13312 if (widget->isVisible())
13313 debug << ", visible";
13314 if (!widget->isEnabled())
13315 debug << ", disabled";
13316 debug << ", states=" << widget->windowState()
13317 << ", type=" << widget->windowType() << ", flags=" << widget->windowFlags();
13318 debug << ", attributes=" << WidgetAttributes{widget};
13319 if (widget->isWindow())
13320 debug << ", window";
13321 debug << ", " << geometry.width() << 'x' << geometry.height()
13322 << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
13323 if (frameGeometry != geometry) {
13324 const QMargins margins(geometry.x() - frameGeometry.x(),
13325 geometry.y() - frameGeometry.y(),
13326 frameGeometry.right() - geometry.right(),
13327 frameGeometry.bottom() - geometry.bottom());
13328 debug << ", margins=" << margins;
13329 }
13330 debug << ", devicePixelRatio=" << widget->devicePixelRatio();
13331 if (const WId wid = widget->internalWinId())
13332 debug << ", winId=0x" << Qt::hex << wid << Qt::dec;
13333 }
13334 debug << ')';
13335 } else {
13336 debug << "QWidget(0x0)";
13337 }
13338 return debug;
13339}
13340#endif // !QT_NO_DEBUG_STREAM
13341
13342
13343// *************************** Focus abstraction ************************************
13344
13345#define FOCUS_NEXT(w) w->d_func()->focus_next
13346#define FOCUS_PREV(w) w->d_func()->focus_prev
13347
13348/*!
13349 \internal
13350 \return next or previous element in the focus chain, depending on
13351 \param direction, irrespective of focus proxies or widgets with Qt::NoFocus.
13352 */
13353QWidget *QWidgetPrivate::nextPrevElementInFocusChain(FocusDirection direction) const
13354{
13355 Q_Q(const QWidget);
13356 return direction == FocusDirection::Next ? FOCUS_NEXT(q) : FOCUS_PREV(q);
13357}
13358
13359/*!
13360 \internal
13361 Removes a widget from the focus chain, respecting the flags set in \param rules.
13362 \list
13363 \li EnsureFocusOut: If the widget has input focus, transfer focus to the next or previous widget
13364 in the focus chain, depending on \param direction.
13365 \li RemoveInconsistent: Remove the widget, even if its focus chain is inconsistent.
13366 \li AssertConsistency: qFatal, if the focus chain is inconsistent. This is used in the QWidget destructor.
13367 \endlist
13368 \return \c true if the widget has been removed, otherwise \c false.
13369 */
13370bool QWidgetPrivate::removeFromFocusChain(FocusChainRemovalRules rules, FocusDirection direction)
13371{
13372 Q_Q(QWidget);
13373 if (!isFocusChainConsistent()) {
13374#ifdef QT_DEBUG
13375 if (rules.testFlag(FocusChainRemovalRule::AssertConsistency))
13376 qFatal() << q << "has inconsistent focus chain.";
13377#endif
13378 qCDebug(lcWidgetFocus) << q << "wasn't removed, because of inconsistent focus chain.";
13379 return false;
13380 }
13381
13382 if (!isInFocusChain()) {
13383 qCDebug(lcWidgetFocus) << q << "wasn't removed, because it is not part of a focus chain.";
13384 return false;
13385 }
13386
13387 if (rules.testFlag(FocusChainRemovalRule::EnsureFocusOut))
13388 q->focusNextPrevChild(direction == FocusDirection::Next);
13389
13392 initFocusChain();
13393 qCDebug(lcWidgetFocus) << q << "removed from focus chain.";
13394 return true;
13395}
13396
13397/*!
13398 \internal
13399 Initialises the focus chain by making the widget point to itself.
13400 */
13401void QWidgetPrivate::initFocusChain()
13402{
13403 Q_Q(QWidget);
13404 qCDebug(lcWidgetFocus) << "Initializing focus chain of" << q;
13405 FOCUS_PREV(q) = q;
13406 FOCUS_NEXT(q) = q;
13407}
13408
13409/*!
13410 \internal
13411 Reads QWidget children, which are not part of a focus chain yet.
13412 Inserts them into the focus chain before or after the widget,
13413 depending on \param direction and in the order of their creation.
13414 This is used, when QWidget::setParent() causes a widget to change toplevel windows.
13415 */
13416void QWidgetPrivate::reparentFocusChildren(FocusDirection direction)
13417{
13418 Q_Q(QWidget);
13419
13420 // separate the focus chain into new (children of myself) and old (the rest)
13421 QWidget *firstOld = nullptr;
13422 QWidget *lastOld = nullptr; // last in the old list
13423 QWidget *lastNew = q; // last in the new list
13424 bool prevWasNew = true;
13425 QWidget *widget = nextPrevElementInFocusChain(direction);
13426
13427 // For efficiency, do not maintain the list invariant inside the loop.
13428 // Append items to the relevant list, and we optimize by not changing pointers,
13429 // when subsequent items are going into the same list.
13430 while (widget != q) {
13431 bool currentIsNew = q->isAncestorOf(widget);
13432 if (currentIsNew) {
13433 if (!prevWasNew) {
13434 // previous was old => append to new list
13435 FOCUS_NEXT(lastNew) = widget;
13436 FOCUS_PREV(widget) = lastNew;
13437 }
13438 lastNew = widget;
13439 } else {
13440 if (prevWasNew) {
13441 // prev was new => append to old list, if it exists
13442 if (lastOld) {
13443 FOCUS_NEXT(lastOld) = widget;
13444 FOCUS_PREV(widget) = lastOld;
13445 } else {
13446 // start the old list
13447 firstOld = widget;
13448 }
13449 }
13450 lastOld = widget;
13451 }
13452 widget = widget->d_func()->nextPrevElementInFocusChain(direction);
13453 prevWasNew = currentIsNew;
13454 }
13455
13456 // repair old list:
13457 if (firstOld) {
13458 FOCUS_NEXT(lastOld) = firstOld;
13459 FOCUS_PREV(firstOld) = lastOld;
13460 }
13461
13462 if (!q->isWindow()) {
13463 QWidget *topLevel = q->window();
13464 // insert new chain into toplevel's chain
13465 QWidget *prev = FOCUS_PREV(topLevel);
13466 FOCUS_PREV(topLevel) = lastNew;
13467 FOCUS_NEXT(prev) = q;
13468 FOCUS_PREV(q) = prev;
13469 FOCUS_NEXT(lastNew) = topLevel;
13470 } else {
13471 // repair new list
13472 FOCUS_NEXT(lastNew) = q;
13473 FOCUS_PREV(q) = lastNew;
13474 }
13475}
13476
13477/*!
13478 \internal
13479 Inserts a widget into the focus chain before or after \param position, depending on
13480 \param direction.
13481 \return \c true, if the insertion has changed the focus chain, otherwise \c false.
13482 */
13483bool QWidgetPrivate::insertIntoFocusChain(FocusDirection direction, QWidget *position)
13484{
13485 Q_Q(QWidget);
13486 Q_ASSERT(position);
13487 QWidget *next = FOCUS_NEXT(q);
13488 QWidget *previous = FOCUS_PREV(q);
13489
13490 switch (direction) {
13491 case FocusDirection::Next:
13492 if (previous == position) {
13493 qCDebug(lcWidgetFocus) << "No-op insertion." << q << "is already before" << position;
13494 return false;
13495 }
13496
13497 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13498
13499 FOCUS_NEXT(q) = FOCUS_NEXT(position);
13500 FOCUS_PREV(FOCUS_NEXT(position)) = q;
13501 FOCUS_NEXT(position) = q;
13502 FOCUS_PREV(q) = position;
13503 qCDebug(lcWidgetFocus) << q << "inserted after" << position;
13504 break;
13505
13506 case FocusDirection::Previous:
13507 if (next == position) {
13508 qCDebug(lcWidgetFocus) << "No-op insertion." << q << "is already after" << position;
13509 return false;
13510 }
13511
13512 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13513
13514 FOCUS_PREV(q) = FOCUS_PREV(position);
13515 FOCUS_NEXT(FOCUS_PREV(position)) = q;
13516 FOCUS_PREV(position) = q;
13517 FOCUS_NEXT(q) = position;
13518 qCDebug(lcWidgetFocus) << q << "inserted before" << position;
13519 break;
13520 }
13521
13522 Q_ASSERT(isFocusChainConsistent());
13523 return true;
13524}
13525
13526/*!
13527 \internal
13528 Convenience override to insert a QWidgetList \param toBeInserted into the focus chain
13529 before or after \param position, depending on \param direction.
13530 \return \c true, if the insertion has changed the focus chain, otherwise \c false.
13531 \note
13532 \param toBeInserted must be a consistent focus chain.
13533 */
13534bool QWidgetPrivate::insertIntoFocusChain(const QWidgetList &toBeInserted,
13535 FocusDirection direction, QWidget *position)
13536{
13537 if (toBeInserted.isEmpty()) {
13538 qCDebug(lcWidgetFocus) << "No-op insertion of an empty list";
13539 return false;
13540 }
13541
13542 Q_ASSERT_X(!toBeInserted.contains(position),
13543 Q_FUNC_INFO,
13544 "Coding error: toBeInserted contains position");
13545
13546 QWidget *first = toBeInserted.constFirst();
13547 QWidget *last = toBeInserted.constLast();
13548
13549 // Call QWidget override to log accordingly
13550 if (toBeInserted.count() == 1)
13551 return first->d_func()->insertIntoFocusChain(direction, position);
13552
13553 Q_ASSERT(first != last);
13554 switch (direction) {
13555 case FocusDirection::Previous:
13556 if (FOCUS_PREV(position) == last) {
13557 qCDebug(lcWidgetFocus) << "No-op insertion." << toBeInserted << "is already before"
13558 << position;
13559 return false;
13560 }
13561 FOCUS_NEXT(FOCUS_PREV(position)) = first;
13562 FOCUS_PREV(first) = FOCUS_PREV(position);
13563 FOCUS_NEXT(last) = position;
13564 FOCUS_PREV(position) = last;
13565 qCDebug(lcWidgetFocus) << toBeInserted << "inserted before" << position;
13566 break;
13567 case FocusDirection::Next:
13568 if (FOCUS_PREV(position) == last) {
13569 qCDebug(lcWidgetFocus) << "No-op insertion." << toBeInserted << "is already after"
13570 << position;
13571 return false;
13572 }
13573 FOCUS_PREV(FOCUS_NEXT(position)) = last;
13574 FOCUS_NEXT(last) = FOCUS_NEXT(position);
13575 FOCUS_PREV(first) = position;
13576 FOCUS_NEXT(position) = first;
13577 qCDebug(lcWidgetFocus) << toBeInserted << "inserted after" << position;
13578 break;
13579 }
13580
13581 Q_ASSERT(position->d_func()->isFocusChainConsistent());
13582 return true;
13583}
13584
13585/*!
13586 \internal
13587 \return a QWidgetList, representing the part of the focus chain,
13588 starting with \param from and ending with \param to, in \param direction.
13589 */
13590QWidgetList focusPath(QWidget *from, QWidget *to, QWidgetPrivate::FocusDirection direction)
13591{
13592 QWidgetList path({from});
13593 if (from == to)
13594 return path;
13595
13596 QWidget *current = from;
13597 do {
13598 switch (direction) {
13599 case QWidgetPrivate::FocusDirection::Previous:
13600 current = current->previousInFocusChain();
13601 break;
13602 case QWidgetPrivate::FocusDirection::Next:
13603 current = current->nextInFocusChain();
13604 break;
13605 }
13606 if (path.contains(current))
13607 return QWidgetList();
13608 path << current;
13609 } while (current != to);
13610
13611 return path;
13612}
13613
13614/*!
13615 \internal
13616 Removes the part from the focus chain starting with \param from and ending with \param to,
13617 in \param direction.
13618 \return removed part as a QWidgetList.
13619 */
13620QWidgetList QWidgetPrivate::takeFromFocusChain(QWidget *from,
13621 QWidget *to,
13622 FocusDirection direction)
13623{
13624 // Check if there is a path from->to in direction
13625 const QWidgetList path = focusPath(from, to , direction);
13626 if (path.isEmpty()) {
13627 qCDebug(lcWidgetFocus) << "No-op removal. Focus chain from" << from << "doesn't lead to " << to;
13628 return QWidgetList();
13629 }
13630
13631 QWidget *first = path.constFirst();
13632 QWidget *last = path.constLast();
13633 if (first == last) {
13634 first->d_func()->removeFromFocusChain();
13635 return QWidgetList({first});
13636 }
13637
13638 FOCUS_NEXT(FOCUS_PREV(first)) = FOCUS_NEXT(last);
13639 FOCUS_PREV(FOCUS_NEXT(last)) = FOCUS_PREV(first);
13640 FOCUS_PREV(first) = last;
13641 FOCUS_NEXT(last) = first;
13642 qCDebug(lcWidgetFocus) << path << "removed from focus chain";
13643 return path;
13644}
13645
13646/*!
13647 \internal
13648 \return The last focus child of the widget, traversing the focus chain no further than
13649 \param noFurtherThan.
13650 */
13651QWidget *QWidgetPrivate::determineLastFocusChild(QWidget *noFurtherThan)
13652{
13653 Q_Q(QWidget);
13654 // Since we need to repeat the same logic for both 'first' and 'second', we add a function
13655 // that determines the last focus child for a widget, taking proxies and compound widgets into
13656 // account. If the target is not a compound widget (it doesn't have a focus proxy that points
13657 // to a child), 'lastFocusChild' will be set to the target itself.
13658 QWidget *lastFocusChild = q;
13659
13660 QWidget *focusProxy = deepestFocusProxy();
13661 if (!focusProxy) {
13662 // QTBUG-81097: Another case is possible here. We can have a child
13663 // widget, that sets its focusProxy() to the parent (target).
13664 // An example of such widget is a QLineEdit, nested into
13665 // a QAbstractSpinBox. In this case such widget should be considered
13666 // the last focus child.
13667 for (auto *object : std::as_const(q->children())) {
13668 QWidget *w = qobject_cast<QWidget *>(object);
13669 if (w && w->focusProxy() == q) {
13670 lastFocusChild = w;
13671 break;
13672 }
13673 }
13674 } else if (q->isAncestorOf(focusProxy)) {
13675 lastFocusChild = focusProxy;
13676 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
13677 focusNext != focusProxy && q->isAncestorOf(focusNext)
13678 && focusNext->window() == focusProxy->window();
13679 focusNext = focusNext->nextInFocusChain()) {
13680 if (focusNext == noFurtherThan)
13681 break;
13682 if (focusNext->focusPolicy() != Qt::NoFocus)
13683 lastFocusChild = focusNext;
13684 }
13685 }
13686 return lastFocusChild;
13687};
13688
13689/*!
13690 \internal
13691 \return \c true, if the widget is part of a focus chain and \c false otherwise.
13692 A widget is considered to be part of a focus chain, neither FOCUS_NEXT, nor FOCUS_PREV
13693 are pointing to the widget itself.
13694
13695 \note
13696 This method doesn't check the consistency of the focus chain.
13697 If multiple widgets have been removed from the focus chain by takeFromFocusChain(),
13698 isInFocusChain() will return \c true for all of those widgets, even if they represent
13699 an inconsistent focus chain.
13700 */
13701bool QWidgetPrivate::isInFocusChain() const
13702{
13703 Q_Q(const QWidget);
13704 return !(FOCUS_NEXT(q) == q && FOCUS_PREV(q) == q);
13705}
13706
13707/*!
13708 \internal
13709 A focus chain is consistent, when it is circular: Following the chain in either direction
13710 has to return to the beginning. This is why a newly constructed widget points to itself,
13711 when the focus chain has been initialized. A newly constructed widget is considered to have
13712 a consistent focus chain, while not being part of a focus chain.
13713
13714 The method always returns \c true, when the logging category "qt.widgets.focus" is disabled.
13715 When it is enabled, the method returns \c true early, if a widget is pointing to itself.
13716 It returns \c false, if one of the following is detected:
13717 \list
13718 \li nullptr found in a previous/next pointer.
13719 \li broken chain: widget A is B's previous, but B isn't A's next.
13720 \li chain isn't closed: starting at A doesn't lead back to A.
13721 \endlist
13722 It return \c true, if none of the above is observed.
13723
13724 \note
13725 The focus chain is checked only in forward direction.
13726 This is sufficient, because the check for a broken chain asserts consistent paths
13727 in both directions.
13728 */
13729bool QWidgetPrivate::isFocusChainConsistent() const
13730{
13731 Q_Q(const QWidget);
13732 const bool skip = !lcWidgetFocus().isDebugEnabled();
13733 if (skip)
13734 return true;
13735
13736 if (!isInFocusChain())
13737 return true;
13738
13739 const QWidget *position = q;
13740
13741 for (int i = 0; i < QApplication::allWidgets().count(); ++i) {
13742 if (!FOCUS_PREV(position) || !FOCUS_NEXT(position)) {
13743 qCDebug(lcWidgetFocus) << "Nullptr found at:" << position
13744 << "Previous pointing to" << FOCUS_PREV(position)
13745 << "Next pointing to" << FOCUS_NEXT(position);
13746 return false;
13747 }
13748 if (!(FOCUS_PREV(FOCUS_NEXT(position)) == position
13749 && FOCUS_NEXT(FOCUS_PREV(position)) == position)) {
13750 qCDebug(lcWidgetFocus) << "Inconsistent focus chain at:" << position
13751 << "Previous pointing to" << FOCUS_PREV(FOCUS_NEXT(position))
13752 << "Next pointing to" << FOCUS_NEXT(FOCUS_PREV(position));
13753 return false;
13754 }
13755 position = FOCUS_NEXT(position);
13756 if (position == q)
13757 return true;
13758
13759 }
13760
13761 qCDebug(lcWidgetFocus) << "Focus chain leading from" << q << "to" << position << "is not closed.";
13762 return false;
13763}
13764
13765#undef FOCUS_NEXT
13766#undef FOCUS_PREV
13767
13768
13769QT_END_NAMESPACE
13770
13771#include "moc_qwidget.cpp"
13772#include "moc_qwidget_p.cpp"
friend class QWidgetPrivate
friend class QWidget
Definition qpainter.h:432
\inmodule QtWidgets
Definition qwidget_p.h:183
void qt_qpa_set_cursor(QWidget *w, bool force)
Definition qwidget.cpp:5038
#define qApp
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static bool canMapPosition(QWindow *window)
static void unsetCursor(QWidget *w)
Definition qwidget.cpp:5032
void q_createNativeChildrenAndSetParent(const QWidget *parentWidget)
Definition qwidget.cpp:1200
QString qt_setWindowTitle_helperHelper(const QString &title, const QWidget *widget)
Returns a modified window title with the [*] place holder replaced according to the rules described i...
Definition qwidget.cpp:6016
static void sendResizeEvents(QWidget *target)
Definition qwidget.cpp:5212
static void applyCursor(QWidget *w, const QCursor &c)
Definition qwidget.cpp:5026
#define FOCUS_PREV(w)
static bool isEmbedded(const QWindow *w)
Definition qwidget.cpp:6660
static void releaseMouseGrabOfWidget(QWidget *widget)
#define FOCUS_NEXT(w)
static void grabMouseForWidget(QWidget *widget, const QCursor *cursor=nullptr)
QWidget * qt_button_down
Definition qwidget.cpp:2304
static MapToGlobalTransformResult mapToGlobalTransform(const QWidget *w)
static void setAttribute_internal(Qt::WidgetAttribute attribute, bool on, QWidgetData *data, QWidgetPrivate *d)
void qSendWindowChangeToTextureChildrenRecursively(QWidget *widget, QEvent::Type eventType)
static bool qRectIntersects(const QRect &r1, const QRect &r2)
Definition qwidget.cpp:95
bool qt_sendSpontaneousEvent(QObject *, QEvent *)
QWidgetList focusPath(QWidget *from, QWidget *to, QWidgetPrivate::FocusDirection direction)
bool q_evaluateRhiConfig(const QWidget *w, QPlatformBackingStoreRhiConfig *outConfig, QSurface::SurfaceType *outType)
Definition qwidget.cpp:1069
static QWidget * keyboardGrb
QWidget * qt_mouseGrb
static bool mouseGrabWithCursor
static QWindow * grabberWindow(const QWidget *w)
static void fillRegion(QPainter *painter, const QRegion &rgn, const QBrush &brush)
Definition qwidget.cpp:2223
static bool q_evaluateRhiConfigRecursive(const QWidget *w, QPlatformBackingStoreRhiConfig *outConfig, QSurface::SurfaceType *outType)
Definition qwidget.cpp:1043
#define QWIDGETSIZE_MAX
Definition qwidget.h:919
static void cleanup(QWidget *that, QWidgetPrivate *d)
Definition qwidget.cpp:754