870void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
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.");
881 f.setFlag(Qt::WindowType::Desktop,
false);
885 Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
887 if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
888 qFatal(
"QWidget: Cannot create a QWidget without QApplication");
890 Q_ASSERT(allWidgets);
892 allWidgets->insert(q);
896 Q_ASSERT_X(QThread::isMainThread(),
"QWidget",
897 "Widgets must be created in the GUI thread.");
901 data.fstrut_dirty =
true;
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;
911 data.sizehint_forced = 0;
912 data.is_closing =
false;
914 data.in_set_window_state = 0;
915 data.in_destructor =
false;
918 if (f & Qt::MSWindowsOwnDC) {
919 mustHaveWindowHandle = 1;
920 q->setAttribute(Qt::WA_NativeWindow);
923 q->setAttribute(Qt::WA_QuitOnClose);
924 adjustQuitOnCloseAttribute();
926 q->setAttribute(Qt::WA_WState_Hidden);
929 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
933 q->setParent(parentWidget, data.window_flags);
935 adjustFlags(data.window_flags, q);
936 resolveLayoutDirection();
938 const QBrush &background = q->palette().brush(QPalette::Window);
939 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
941 data.fnt = QFont(data.fnt, q);
943 q->setAttribute(Qt::WA_PendingMoveEvent);
944 q->setAttribute(Qt::WA_PendingResizeEvent);
946 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
947 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
949 QEvent e(QEvent::Create);
950 QCoreApplication::sendEvent(q, &e);
951 QCoreApplication::postEvent(q,
new QEvent(QEvent::PolishRequest));
953 extraPaintEngine =
nullptr;
1103void QWidget::create(WId window,
bool initializeWindow,
bool destroyOldWindow)
1105 Q_UNUSED(initializeWindow);
1106 Q_UNUSED(destroyOldWindow);
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())
1114 if (d->data.in_destructor)
1117 Qt::WindowType type = windowType();
1118 Qt::WindowFlags &flags = data->window_flags;
1120 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1122 flags |= Qt::Window;
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)) {
1136 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1137 Q_ASSERT(internalWinId());
1143 static const bool paintOnScreenEnv = qEnvironmentVariableIntValue(
"QT_ONSCREEN_PAINT") > 0;
1144 if (paintOnScreenEnv)
1145 setAttribute(Qt::WA_PaintOnScreen);
1147 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1148 setAttribute(Qt::WA_NativeWindow);
1151#if QT_CONFIG(graphicsview)
1152 && !graphicsProxyWidget()
1156 auto *topExtra = d->maybeTopData();
1157 if (!topExtra || !topExtra->explicitContentsMarginsRespectsSafeArea) {
1158 setAttribute_internal(Qt::WA_ContentsMarginsRespectsSafeArea,
1163 d->updateIsOpaque();
1165 setAttribute(Qt::WA_WState_Created);
1170 d->topData()->repaintManager.reset(
new QWidgetRepaintManager(
this));
1174 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1175 setAttribute(Qt::WA_DropSiteRegistered,
true);
1178 if (testAttribute(Qt::WA_SetWindowIcon))
1179 d->setWindowIcon_sys();
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();
1189 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1190 d->setWindowIcon_sys();
1197 d->updateFrameStrut();
1226void QWidgetPrivate::create()
1230 if (!q->testAttribute(Qt::WA_NativeWindow) && !q->isWindow())
1233 QWidgetWindow *win = topData()->window;
1238 Q_ASSERT(topData()->window);
1239 win = topData()->window;
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));
1248 Qt::WindowFlags &flags = data.window_flags;
1250#if defined(QT_PLATFORM_UIKIT)
1251 if (q->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
1252 flags |= Qt::ExpandedClientAreaHint;
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());
1265 win->resize(q->size());
1266 if (win->isTopLevel()) {
1267 QScreen *targetScreen = topData()->initialScreen;
1268 topData()->initialScreen =
nullptr;
1270 targetScreen = q->screen();
1271 win->setScreen(targetScreen);
1274 QSurfaceFormat format = win->requestedFormat();
1275 if ((flags & Qt::Window) && win->surfaceType() != QSurface::OpenGLSurface
1276 && q->testAttribute(Qt::WA_TranslucentBackground)) {
1277 format.setAlphaBufferSize(8);
1279 win->setFormat(format);
1281 if (QWidget *nativeParent = q->nativeParentWidget()) {
1282 if (nativeParent->windowHandle()) {
1283 if (flags & Qt::Window) {
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;
1302 QWindow *transientParent = nativeParent->window()->windowHandle();
1303 if (!isPopupBased(winType)) {
1305 while (transientParent) {
1306 const Qt::WindowType tp = transientParent->type();
1307 if (!isPopupBased(tp))
1309 transientParent = transientParent->transientParent();
1312 win->setTransientParent(transientParent);
1313 win->setParent(
nullptr);
1315 win->setTransientParent(
nullptr);
1316 win->setParent(nativeParent->windowHandle());
1321 qt_window_private(win)->positionPolicy = topData()->posIncludesFrame ?
1322 QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
1326 if (QPlatformWindow *platformWindow = win->handle())
1327 platformWindow->setFrameStrutEventsEnabled(
true);
1329 data.window_flags = win->flags();
1332 if (!topData()->role.isNull()) {
1333 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(win->handle()))
1334 xcbWindow->setWindowRole(topData()->role);
1337#if QT_CONFIG(wayland)
1338 if (!topData()->role.isNull()) {
1339 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(win->handle()))
1340 waylandWindow->setSessionRestoreId(topData()->role);
1344 QBackingStore *store = q->backingStore();
1345 usesRhiFlush =
false;
1347 if (!store && q->isWindow())
1348 q->setBackingStore(
new QBackingStore(win));
1350 QPlatformBackingStoreRhiConfig rhiConfig;
1351 usesRhiFlush = q_evaluateRhiConfig(q, &rhiConfig,
nullptr);
1352 if (usesRhiFlush && q->backingStore()) {
1355 q->backingStore()->handle()->createRhi(win, rhiConfig);
1358 setWindowModified_helper();
1360 if (win->handle()) {
1361 WId id = win->winId();
1363 Q_ASSERT(id != WId(0));
1366 setNetWmWindowTypes(
true);
1369 q_createNativeChildrenAndSetParent(q);
1371 if (extra && !extra->mask.isEmpty())
1372 setMask_sys(extra->mask);
1374 if (data.crect.width() == 0 || data.crect.height() == 0) {
1375 q->setAttribute(Qt::WA_OutsideWSRange,
true);
1377 q->setAttribute(Qt::WA_OutsideWSRange,
false);
1378 if (q->isVisible()) {
1380 win->setNativeWindowVisibility(
true);
1431 d->data.in_destructor =
true;
1433#if QT_CONFIG(accessibility)
1434 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing && QAccessible::isActive())
1435 QAccessibleCache::instance()->sendObjectDestroyedEvent(
this);
1438#if defined (QT_CHECK_STATE)
1439 if (Q_UNLIKELY(paintingActive()))
1440 qWarning(
"QWidget: %s (%s) deleted while being painted", className(), name());
1443#ifndef QT_NO_GESTURES
1444 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1446 for (
auto it = d->gestureContext.keyBegin(), end = d->gestureContext.keyEnd(); it != end; ++it)
1447 manager->cleanupCachedGestures(
this, *it);
1449 d->gestureContext.clear();
1452#if QT_CONFIG(action)
1454 for (
auto action : std::as_const(d->actions)) {
1455 QActionPrivate *apriv = action->d_func();
1456 apriv->associatedObjects.removeAll(
this);
1461#ifndef QT_NO_SHORTCUT
1464 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1465 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0,
this, QKeySequence());
1470 d->layout =
nullptr;
1473 d->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency);
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))
1489 d->setDirtyOpaqueRegion();
1491 if (isWindow() && isVisible() && internalWinId()) {
1502 }
else if (isVisible()) {
1503 qApp->d_func()->sendSyntheticEnterLeave(
this);
1506 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
1507 repaintManager->removeDirtyWidget(
this);
1508 if (testAttribute(Qt::WA_StaticContents))
1509 repaintManager->removeStaticWidget(
this);
1512 delete d->needsFlush;
1513 d->needsFlush =
nullptr;
1517 bool blocked = d->blockSig;
1520 if (d->isSignalConnected(0)) {
1522 emit destroyed(
this);
1526 qWarning(
"Detected an unexpected exception in ~QWidget while emitting destroyed().");
1531 if (d->declarativeData) {
1532 d->wasDeleted =
true;
1533 if (QAbstractDeclarativeData::destroyed)
1534 QAbstractDeclarativeData::destroyed(d->declarativeData,
this);
1535 d->declarativeData =
nullptr;
1536 d->wasDeleted =
false;
1539 d->blockSig = blocked;
1541 if (!d->children.isEmpty())
1542 d->deleteChildren();
1544 QCoreApplication::removePostedEvents(
this);
1551 --QWidgetPrivate::instanceCounter;
1553 if (QWidgetPrivate::allWidgets)
1554 QWidgetPrivate::allWidgets->remove(
this);
1557 QEvent e(QEvent::Destroy);
1558 QCoreApplication::sendEvent(
this, &e);
1559 } QT_CATCH(
const std::exception&) {
1563#if QT_CONFIG(graphicseffect)
1564 delete d->graphicsEffect;
1568 d->isWidget =
false;
2018void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion,
bool *hasDirtySiblingsAbove,
2019 bool alsoNonOpaque)
const
2022 static int disableSubtractOpaqueSiblings = qEnvironmentVariableIntValue(
"QT_NO_SUBTRACTOPAQUESIBLINGS");
2023 if (disableSubtractOpaqueSiblings || q->isWindow())
2026 QRect clipBoundingRect;
2027 bool dirtyClipBoundingRect =
true;
2030 bool dirtyParentClip =
true;
2032 QPoint parentOffset = data.crect.topLeft();
2034 const QWidget *w = q;
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())
2047 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2048 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2051 if (dirtyClipBoundingRect) {
2052 clipBoundingRect = sourceRegion.boundingRect();
2053 dirtyClipBoundingRect =
false;
2056 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2059 if (dirtyParentClip) {
2060 parentClip = sourceRegion.translated(parentOffset);
2061 dirtyParentClip =
false;
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;
2071 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2072 if (siblingDirty.isEmpty())
2075 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2077 siblingDirty.translate(-parentOffset);
2078 sourceRegion -= siblingDirty;
2080 sourceRegion -= siblingGeometry.translated(-parentOffset);
2083 if (hasDirtySiblingsAbove)
2084 *hasDirtySiblingsAbove =
true;
2085 if (sibling->d_func()->children.isEmpty())
2087 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2088 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2089 sourceRegion -= opaqueSiblingChildren;
2091 if (sourceRegion.isEmpty())
2094 dirtyClipBoundingRect =
true;
2095 dirtyParentClip =
true;
2098 w = w->parentWidget();
2099 parentOffset += pd->data.crect.topLeft();
2100 dirtyParentClip =
true;
4801void QWidgetPrivate::updateFont(
const QFont &font)
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);
4811 data.fnt = QFont(font, q);
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();
4820 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
4821 inheritedFontResolveMask = 0;
4823 uint newMask = data.fnt.resolveMask() | inheritedFontResolveMask;
4827 directFontResolveMask = data.fnt.resolveMask();
4828 data.fnt.setResolveMask(newMask);
4830 for (
int i = 0; i < children.size(); ++i) {
4831 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4834#if QT_CONFIG(style_stylesheet)
4835 }
else if (!useStyleSheetPropagationInWidgetStyles && w->testAttribute(Qt::WA_StyleSheet)) {
4838 cssStyle->updateStyleSheetFont(w);
4840 }
else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
4842 QWidgetPrivate *wd = w->d_func();
4843 wd->inheritedFontResolveMask = newMask;
4849#if QT_CONFIG(style_stylesheet)
4850 if (!useStyleSheetPropagationInWidgetStyles && cssStyle) {
4851 cssStyle->updateStyleSheetFont(q);
4855 QEvent e(QEvent::FontChange);
4856 QCoreApplication::sendEvent(q, &e);
5136void QWidget::render(QPainter *painter,
const QPoint &targetOffset,
5137 const QRegion &sourceRegion, RenderFlags renderFlags)
5139 if (Q_UNLIKELY(!painter)) {
5140 qWarning(
"QWidget::render: Null pointer to painter");
5144 if (Q_UNLIKELY(!painter->isActive())) {
5145 qWarning(
"QWidget::render: Cannot render with an inactive painter");
5149 const qreal opacity = painter->opacity();
5150 if (qFuzzyIsNull(opacity))
5154 const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
5155 const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
5157 if (toBePainted.isEmpty())
5162 d->extra->inRenderWithPainter =
true;
5164 QPaintEngine *engine = painter->paintEngine();
5166 QPaintEnginePrivate *enginePriv = engine->d_func();
5167 Q_ASSERT(enginePriv);
5168 QPaintDevice *target = engine->paintDevice();
5172 if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
5173 d->render_helper(painter, targetOffset, toBePainted, renderFlags);
5174 d->extra->inRenderWithPainter = inRenderWithPainter;
5179 QPainter *oldPainter = d->sharedPainter();
5180 d->setSharedPainter(painter);
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();
5190 if (painter->hasClipping()) {
5191 const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
5192 enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
5194 enginePriv->setSystemViewport(oldSystemClip);
5196 painter->setLayoutDirection(layoutDirection());
5198 d->render(target, targetOffset, toBePainted, renderFlags);
5201 enginePriv->baseSystemClip = oldBaseClip;
5202 enginePriv->setSystemTransformAndViewport(oldTransform, oldSystemViewport);
5203 enginePriv->systemStateChanged();
5204 painter->setLayoutDirection(oldLayoutDirection);
5207 d->setSharedPainter(oldPainter);
5209 d->extra->inRenderWithPainter = inRenderWithPainter;
5350QRegion QWidgetPrivate::prepareToRender(
const QRegion ®ion, QWidget::RenderFlags renderFlags)
5353 const bool isVisible = q->isVisible();
5356 if (!isVisible && !isAboutToShow()) {
5357 QWidget *topLevel = q->window();
5358 (
void)topLevel->d_func()->topData();
5359 topLevel->ensurePolished();
5363 QWidget *widget = q;
5364 QWidgetList hiddenWidgets;
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);
5372 widget = widget->parentWidget();
5376 if (topLevel->d_func()->layout)
5377 topLevel->d_func()->layout->activate();
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);
5388 topLevel->d_func()->activateChildLayoutsRecursively();
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();
5397 }
else if (isVisible) {
5398 q->window()->d_func()->sendPendingMoveAndResizeEvents(
true,
true);
5402 QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
5403 if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
5404 toBePainted &= extra->mask;
5408void QWidgetPrivate::render_helper(QPainter *painter,
const QPoint &targetOffset,
const QRegion &toBePainted,
5409 QWidget::RenderFlags renderFlags)
5412 Q_ASSERT(!toBePainted.isEmpty());
5415 const QTransform originalTransform = painter->worldTransform();
5416 const bool useDeviceCoordinates = originalTransform.isScaling();
5417 if (!useDeviceCoordinates) {
5419 const QRect rect = toBePainted.boundingRect();
5420 const QSize size = rect.size();
5424 const qreal pixmapDevicePixelRatio = painter->device()->devicePixelRatio();
5425 QPixmap pixmap(size * pixmapDevicePixelRatio);
5426 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio);
5428 if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
5429 pixmap.fill(Qt::transparent);
5430 q->render(&pixmap, QPoint(), toBePainted, renderFlags);
5432 const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
5433 painter->setRenderHints(QPainter::SmoothPixmapTransform,
true);
5435 painter->drawPixmap(targetOffset, pixmap);
5438 painter->setRenderHints(QPainter::SmoothPixmapTransform,
false);
5442 QTransform transform = originalTransform;
5443 transform.translate(targetOffset.x(), targetOffset.y());
5445 QPaintDevice *device = painter->device();
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());
5453 QPixmap pixmap(deviceRect.size());
5454 pixmap.fill(Qt::transparent);
5457 QPainter pixmapPainter(&pixmap);
5458 pixmapPainter.setRenderHints(painter->renderHints());
5459 transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
5460 pixmapPainter.setTransform(transform);
5462 q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
5463 pixmapPainter.end();
5466 painter->setTransform(QTransform());
5467 painter->drawPixmap(deviceRect.topLeft(), pixmap);
5468 painter->setTransform(originalTransform);
5472void QWidgetPrivate::drawWidget(QPaintDevice *pdev,
const QRegion &rgn,
const QPoint &offset, DrawWidgetFlags flags,
5473 QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5480 qCInfo(lcWidgetPainting) <<
"Drawing" << rgn <<
"of" << q <<
"at" << offset
5481 <<
"into paint device" << pdev <<
"with" << flags;
5483 const bool asRoot = flags & DrawAsRoot;
5484 bool onScreen = shouldPaintOnScreen();
5486#if QT_CONFIG(graphicseffect)
5487 if (graphicsEffect && graphicsEffect->isEnabled()) {
5488 QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
5489 QWidgetEffectSourcePrivate *sourced =
static_cast<QWidgetEffectSourcePrivate *>
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));
5498 p.translate(offset);
5499 context.painter = &p;
5500 graphicsEffect->draw(&p);
5501 setSystemClip(pdev->paintEngine(), 1, QRegion());
5503 context.painter = sharedPainter;
5504 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
5505 sourced->invalidateCache();
5506 sourced->lastEffectTransform = sharedPainter->worldTransform();
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();
5515 sourced->context =
nullptr;
5518 repaintManager->markNeedsFlush(q, effectRgn, offset);
5524 flags = flags & ~UseEffectRegionBounds;
5526 const bool alsoOnScreen = flags & DrawPaintOnScreen;
5527 const bool recursive = flags & DrawRecursive;
5528 const bool alsoInvisible = flags & DrawInvisible;
5530 Q_ASSERT(sharedPainter ? sharedPainter->isActive() :
true);
5532 QRegion toBePainted(rgn);
5533 if (asRoot && !alsoInvisible)
5534 toBePainted &= clipRect();
5535 if (!(flags & DontSubtractOpaqueChildren))
5536 subtractOpaqueChildren(toBePainted, q->rect());
5538 if (!toBePainted.isEmpty()) {
5539 if (!onScreen || alsoOnScreen) {
5541 if (Q_UNLIKELY(q->testAttribute(Qt::WA_WState_InPaintEvent)))
5542 qWarning(
"QWidget::repaint: Recursive repaint detected");
5543 q->setAttribute(Qt::WA_WState_InPaintEvent);
5546 QPaintEngine *paintEngine = pdev->paintEngine();
5548 setRedirected(pdev, -offset);
5551 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted);
5553 paintEngine->d_func()->systemRect = q->data->crect;
5556 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
5557 && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
5558 beginBackingStorePainting();
5560 p.setRenderHint(QPainter::SmoothPixmapTransform);
5561 paintBackground(&p, toBePainted, (asRoot || onScreen) ? (flags | DrawAsRoot) : DrawWidgetFlags());
5562 endBackingStorePainting();
5566 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted.translated(offset));
5568 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
5569 beginBackingStorePainting();
5571 QColor tint = q->palette().window().color();
5572 tint.setAlphaF(.6f);
5573 p.fillRect(toBePainted.boundingRect(), tint);
5574 endBackingStorePainting();
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());
5584 bool skipPaintEvent =
false;
5585 if (renderToTexture) {
5588 beginBackingStorePainting();
5589 if (!q->testAttribute(Qt::WA_AlwaysStackOnTop) && repaintManager) {
5591 p.setCompositionMode(QPainter::CompositionMode_Source);
5592 p.fillRect(q->rect(), Qt::transparent);
5593 }
else if (!repaintManager) {
5595 QImage img = grabFramebuffer();
5598 if (img.format() == QImage::Format_RGB32)
5599 img.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied);
5601 p.drawImage(q->rect(), img);
5602 skipPaintEvent =
true;
5604 endBackingStorePainting();
5605 if (renderToTextureReallyDirty)
5606 renderToTextureReallyDirty = 0;
5608 skipPaintEvent =
true;
5611 if (!skipPaintEvent) {
5613 sendPaintEvent(toBePainted);
5617 repaintManager->markNeedsFlush(q, toBePainted, offset);
5621 restoreRedirected();
5623 paintEngine->d_func()->systemRect = QRect();
5625 paintEngine->d_func()->currentClipDevice =
nullptr;
5627 setSystemClip(pdev->paintEngine(), 1, QRegion());
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");
5633 if (paintEngine && paintEngine->autoDestruct()) {
5636 }
else if (q->isWindow()) {
5637 QPaintEngine *engine = pdev->paintEngine();
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());
5645 p.fillRect(q->rect(), bg);
5647 if (engine->autoDestruct())
5653 if (recursive && !children.isEmpty()) {
5654 paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot,
5655 sharedPainter, repaintManager);
5669void QWidgetPrivate::render(QPaintDevice *target,
const QPoint &targetOffset,
5670 const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
5672 if (Q_UNLIKELY(!target)) {
5673 qWarning(
"QWidget::render: null pointer to paint device");
5677 const bool inRenderWithPainter = extra && extra->inRenderWithPainter;
5678 QRegion paintRegion = !inRenderWithPainter
5679 ? prepareToRender(sourceRegion, renderFlags)
5681 if (paintRegion.isEmpty())
5684 QPainter *oldSharedPainter = inRenderWithPainter ? sharedPainter() :
nullptr;
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);
5700 QPoint offset = targetOffset;
5701 offset -= paintRegion.boundingRect().topLeft();
5702 QPoint redirectionOffset;
5703 QPaintDevice *redirected =
nullptr;
5705 if (target->devType() == QInternal::Widget)
5706 redirected =
static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
5709 target = redirected;
5710 offset -= redirectionOffset;
5713 if (!inRenderWithPainter) {
5714 if (QPaintEngine *targetEngine = target->paintEngine()) {
5715 const QRegion targetSystemClip = targetEngine->systemClip();
5716 if (!targetSystemClip.isEmpty())
5717 paintRegion &= targetSystemClip.translated(-offset);
5722 DrawWidgetFlags flags = DrawPaintOnScreen | DrawInvisible;
5723 if (renderFlags & QWidget::DrawWindowBackground)
5724 flags |= DrawAsRoot;
5726 if (renderFlags & QWidget::DrawChildren)
5727 flags |= DrawRecursive;
5729 flags |= DontSubtractOpaqueChildren;
5731 flags |= DontSetCompositionMode;
5734 drawWidget(target, paintRegion, offset, flags, sharedPainter());
5737 if (oldSharedPainter)
5738 setSharedPainter(oldSharedPainter);
5741void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev,
const QObjectList& siblings,
int index,
const QRegion &rgn,
5742 const QPoint &offset, DrawWidgetFlags flags
5743 , QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5745 QWidget *w =
nullptr;
5747 bool dirtyBoundingRect =
true;
5748 const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
5749 const bool excludeNativeChildren = (flags & DontDrawNativeChildren);
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;
5760 if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
5766 }
while (index >= 0);
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;
5777 wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
5778 paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags,
5779 sharedPainter, repaintManager);
5782 if (w->updatesEnabled()
5783#if QT_CONFIG(graphicsview)
5784 && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
5787 QRegion wRegion(rgn);
5788 wRegion &= wd->effectiveRectFor(w->data->crect);
5789 wRegion.translate(-widgetPos);
5791 wRegion &= wd->extra->mask;
5792 wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, repaintManager);
6544void QWidget::setFocus(Qt::FocusReason reason)
6549 QWidget *f = d_func()->deepestFocusProxy();
6553 if (QApplication::focusWidget() == f)
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)
6569#if QT_CONFIG(graphicsview)
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;
6581 if (f->isActiveWindow()) {
6582 QWidget *prev = QApplicationPrivate::focus_widget;
6584 if (reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
6585 && prev->testAttribute(Qt::WA_InputMethodEnabled)) {
6586 QGuiApplication::inputMethod()->commit();
6589 if (reason != Qt::NoFocusReason) {
6590 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason);
6591 QCoreApplication::sendEvent(prev, &focusAboutToChange);
6595 f->d_func()->updateFocusChild();
6597 QApplicationPrivate::setFocusWidget(f, reason);
6598#if QT_CONFIG(accessibility)
6600 if (!(f->inherits(
"QMenuBar") || f->inherits(
"QMenu") || f->inherits(
"QMenuItem")))
6602 QAccessibleEvent event(f, QAccessible::Focus);
6603 QAccessible::updateAccessibility(&event);
6606#if QT_CONFIG(graphicsview)
6607 if (
const auto &topData = window()->d_func()->extra) {
6608 if (topData->proxyWidget) {
6609 if (previousProxyFocus && previousProxyFocus != f) {
6611 QFocusEvent event(QEvent::FocusOut, reason);
6612 QPointer<QWidget> that = previousProxyFocus;
6613 QCoreApplication::sendEvent(previousProxyFocus, &event);
6615 QCoreApplication::sendEvent(that->style(), &event);
6618#if QT_CONFIG(graphicsview)
6620 if (
const auto &topData = window()->d_func()->extra)
6621 if (topData->proxyWidget && topData->proxyWidget->hasFocus())
6622 topData->proxyWidget->d_func()->updateProxyInputMethodAcceptanceFromWidget();
6625 QFocusEvent event(QEvent::FocusIn, reason);
6626 QPointer<QWidget> that = f;
6627 QCoreApplication::sendEvent(f, &event);
6629 QCoreApplication::sendEvent(that->style(), &event);
6635 f->d_func()->updateFocusChild();
7029void QWidget::setTabOrder(QWidget* first, QWidget *second)
7031 if (!first || !second || first == second
7032 || first->focusPolicy() == Qt::NoFocus
7033 || second->focusPolicy() == Qt::NoFocus)
7036 if (Q_UNLIKELY(first->window() != second->window())) {
7037 qWarning(
"QWidget::setTabOrder: 'first' and 'second' must be in the same window");
7041 const auto determineLastFocusChild = [](QWidget *target, QWidget *noFurtherThan)
7047 QWidget *lastFocusChild = target;
7049 QWidget *focusProxy = target->d_func()->deepestFocusProxy();
7056 for (
auto *object : target->children()) {
7057 QWidget *w = qobject_cast<QWidget*>(object);
7058 if (w && w->focusProxy() == target) {
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)
7070 if (focusNext->focusPolicy() != Qt::NoFocus)
7071 lastFocusChild = focusNext;
7074 return lastFocusChild;
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;
7085 QWidget *oldPrev = second->previousInFocusChain();
7086 QWidget *prevWithFocus = oldPrev;
7087 while (prevWithFocus->focusPolicy() == Qt::NoFocus)
7088 prevWithFocus = prevWithFocus->previousInFocusChain();
7089 if (prevWithFocus == first)
7091 const QWidgetList chain = QWidgetPrivate::takeFromFocusChain(second, lastFocusChildOfSecond);
7092 QWidgetPrivate::insertIntoFocusChain(chain, QWidgetPrivate::FocusDirection::Next, lastFocusChildOfFirst);
7258void QWidgetPrivate::setGeometry_sys(
int x,
int y,
int w,
int h,
bool isMove)
7262 w = qMin(w,extra->maxw);
7263 h = qMin(h,extra->maxh);
7264 w = qMax(w,extra->minw);
7265 h = qMax(h,extra->minh);
7268 if (q->isWindow() && q->windowHandle()) {
7269 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
7270 if (!integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) {
7273 w = q->windowHandle()->width();
7274 h = q->windowHandle()->height();
7278 QPoint oldp = q->geometry().topLeft();
7279 QSize olds = q->size();
7280 QRect r(x, y, w, h);
7282 bool isResize = olds != r.size();
7284 isMove = oldp != r.topLeft();
7289 if (r.size() == olds && oldp == r.topLeft())
7292 if (!data.in_set_window_state) {
7293 q->data->window_state &= ~Qt::WindowMaximized;
7294 q->data->window_state &= ~Qt::WindowFullScreen;
7296 topData()->normalGeometry = QRect(0, 0, -1, -1);
7299 QPoint oldPos = q->pos();
7302 bool needsShow =
false;
7304 if (q->isWindow() || q->windowHandle()) {
7305 if (!(data.window_state & Qt::WindowFullScreen) && (w == 0 || h == 0)) {
7306 q->setAttribute(Qt::WA_OutsideWSRange,
true);
7309 data.crect = QRect(x, y, w, h);
7310 }
else if (q->testAttribute(Qt::WA_OutsideWSRange)) {
7311 q->setAttribute(Qt::WA_OutsideWSRange,
false);
7316 const bool hasOnScreenWindow = !q->testAttribute(Qt::WA_DontShowOnScreen)
7317 && !q->testAttribute(Qt::WA_OutsideWSRange);
7319 if (hasOnScreenWindow) {
7324 if (QWindow *win = q->windowHandle()) {
7325 if (q->isWindow()) {
7326 if (isResize && !isMove)
7328 else if (isMove && !isResize)
7329 win->setPosition(x, y);
7331 win->setGeometry(q->geometry());
7333 QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());
7334 win->setGeometry(QRect(posInNativeParent,r.size()));
7339 if (q->isVisible()) {
7340 if (hasOnScreenWindow) {
7341 if (needsShow && q->windowHandle())
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());
7352 invalidateBackingStore_resizeHelper(oldPos, olds);
7358 QMoveEvent e(q->pos(), oldPos);
7359 QCoreApplication::sendEvent(q, &e);
7362 QResizeEvent e(r.size(), olds);
7363 QCoreApplication::sendEvent(q, &e);
7364 if (q->windowHandle())
7368 if (isMove && q->pos() != oldPos)
7369 q->setAttribute(Qt::WA_PendingMoveEvent,
true);
7371 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7374#if QT_CONFIG(accessibility)
7375 if (QAccessible::isActive() && q->isVisible()) {
7376 QAccessibleEvent event(q, QAccessible::LocationChanged);
7377 QAccessible::updateAccessibility(&event);
7510bool QWidget::restoreGeometry(
const QByteArray &geometry)
7512 if (geometry.size() < 4)
7514 QDataStream stream(geometry);
7515 stream.setVersion(QDataStream::Qt_4_0);
7517 const quint32 magicNumber = 0x1D9D0CB;
7518 quint32 storedMagicNumber;
7519 stream >> storedMagicNumber;
7520 if (storedMagicNumber != magicNumber)
7523 const quint16 currentMajorVersion = 3;
7524 quint16 majorVersion = 0;
7525 quint16 minorVersion = 0;
7527 stream >> majorVersion >> minorVersion;
7529 if (majorVersion > currentMajorVersion)
7533 QRect restoredFrameGeometry;
7534 QRect restoredGeometry;
7535 QRect restoredNormalGeometry;
7536 qint32 restoredScreenNumber;
7539 qint32 restoredScreenWidth = 0;
7541 stream >> restoredFrameGeometry
7542 >> restoredNormalGeometry
7543 >> restoredScreenNumber
7547 if (majorVersion > 1)
7548 stream >> restoredScreenWidth;
7549 if (majorVersion > 2)
7550 stream >> restoredGeometry;
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;
7560 if (restoredScreenWidth) {
7561 const qreal factor = qreal(restoredScreenWidth) / screenWidthF;
7562 if (factor < 0.8 || factor > 1.25)
7567 if (!maximized && !fullScreen && qreal(restoredFrameGeometry.width()) / screenWidthF > 1.5)
7571 const int frameHeight = QApplication::style()
7572 ? QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight,
nullptr,
this)
7575 if (!restoredNormalGeometry.isValid())
7576 restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
7577 if (!restoredNormalGeometry.isValid()) {
7579 restoredNormalGeometry.setSize(restoredNormalGeometry
7581 .expandedTo(d_func()->adjustedSize()));
7584 const QRect availableGeometry = restoredScreen ? restoredScreen->availableGeometry()
7592 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredGeometry, frameHeight);
7593 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredNormalGeometry, frameHeight);
7595 if (maximized || fullScreen) {
7598 Qt::WindowStates ws = windowState();
7600 setGeometry(restoredNormalGeometry);
7602 if (ws & Qt::WindowFullScreen) {
7604 move(availableGeometry.topLeft());
7605 }
else if (ws & Qt::WindowMaximized) {
7609 if (restoredScreen != screen()) {
7610 setWindowState(Qt::WindowNoState);
7611 setGeometry(restoredNormalGeometry);
7614 setGeometry(restoredNormalGeometry);
7618 ws |= Qt::WindowMaximized;
7620 ws |= Qt::WindowFullScreen;
7622 d_func()->topData()->normalGeometry = restoredNormalGeometry;
7624 setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
7627 if (majorVersion > 2)
7628 setGeometry(restoredGeometry);
7630 setGeometry(restoredNormalGeometry);
7733QMargins QWidgetPrivate::safeAreaMargins()
const
7737#if QT_CONFIG(graphicsview)
7740 if (nearestGraphicsProxyWidget(q))
7744 QWidget *nativeWidget = q->window();
7745 if (!nativeWidget->windowHandle())
7748 QMargins safeAreaMargins = nativeWidget->windowHandle()->safeAreaMargins();
7750 if (!q->isWindow()) {
7758 if (safeAreaMargins.isNull())
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))
7771 if (parentWidget->testAttribute(Qt::WA_LayoutOnEntireRect))
7774 QLayout *layout = parentWidget->layout();
7778 if (layout->geometry().isNull())
7781 if (layout->indexOf(w) < 0)
7784 assumedSafeWidget = w;
7788#if !defined(QT_DEBUG)
7789 if (assumedSafeWidget) {
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()));
7806 safeAreaMargins = QMargins(qMax(0, topLeftMargins.x()), qMax(0, topLeftMargins.y()),
7807 qMax(0, bottomRightMargins.x()), qMax(0, bottomRightMargins.y()));
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();
7817 return safeAreaMargins;
8043void QWidgetPrivate::show_helper()
8046 data.in_show =
true;
8048 sendPendingMoveAndResizeEvents();
8051 q->setAttribute(Qt::WA_WState_Visible);
8054 showChildren(
false);
8058 const bool isWindow = q->isWindow();
8059#if QT_CONFIG(graphicsview)
8060 bool isEmbedded = isWindow && q->graphicsProxyWidget() !=
nullptr;
8062 bool isEmbedded =
false;
8068 if (isWindow && !isEmbedded) {
8069 if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
8071 if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
8072 q->setAttribute(Qt::WA_KeyboardFocusChange);
8074 while (QApplication::activePopupWidget()) {
8075 if (!QApplication::activePopupWidget()->close())
8083#if QT_CONFIG(graphicsview)
8085 if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
8086 QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
8087 if (ancestorProxy) {
8089 ancestorProxy->d_func()->embedSubWindow(q);
8094 Q_UNUSED(isEmbedded);
8098 QShowEvent showEvent;
8099 QCoreApplication::sendEvent(q, &showEvent);
8103 if (!isEmbedded && q->windowType() == Qt::Popup)
8104 qApp->d_func()->openPopup(q);
8106#if QT_CONFIG(accessibility)
8107 if (q->windowType() != Qt::ToolTip) {
8108 QAccessibleEvent event(q, QAccessible::ObjectShow);
8109 QAccessible::updateAccessibility(&event);
8113 if (QApplicationPrivate::hidden_focus_widget == q) {
8114 QApplicationPrivate::hidden_focus_widget =
nullptr;
8115 q->setFocus(Qt::OtherFocusReason);
8121 if (!
qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
8122 QCoreApplication::processEvents();
8124 data.in_show =
false;
8127void QWidgetPrivate::show_sys()
8131 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8133 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8134 invalidateBackingStore(q->rect());
8135 q->setAttribute(Qt::WA_Mapped);
8137 if (window && q->isWindow()
8138#if QT_CONFIG(graphicsview)
8139 && (!extra || !extra->proxyWidget)
8141 && q->windowModality() != Qt::NonModal) {
8142 QGuiApplicationPrivate::showModalWindow(window);
8147 if (renderToTexture && !q->isWindow())
8148 QCoreApplication::postEvent(q->parentWidget(),
new QUpdateLaterEvent(q->geometry()));
8150 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(q->rect()));
8152 if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow))
8153 || q->testAttribute(Qt::WA_OutsideWSRange)) {
8159 fixPosIncludesFrame();
8160 QRect geomRect = q->geometry();
8161 if (!q->isWindow()) {
8162 QPoint topLeftOfWindow = q->mapTo(q->nativeParentWidget(),QPoint());
8163 geomRect.moveTopLeft(topLeftOfWindow);
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);
8171 window->resize(geomRect.size());
8175 qt_qpa_set_cursor(q,
false);
8177 invalidateBackingStore(q->rect());
8178 window->setNativeWindowVisibility(
true);
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);
8343void QWidgetPrivate::setVisible(
bool visible)
8346 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" << q
8347 <<
"with attributes" << WidgetAttributes{q}
8348 <<
"to" << visible <<
"via QWidgetPrivate";
8352 if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
8353 && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
8354 q->parentWidget()->window()->d_func()->createRecursively();
8357 QWidget *pw = q->parentWidget();
8358 if (!q->testAttribute(Qt::WA_WState_Created)
8359 && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
8363 bool wasResized = q->testAttribute(Qt::WA_Resized);
8364 Qt::WindowStates initialWindowState = q->windowState();
8367 q->ensurePolished();
8370 bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
8372 q->setAttribute(Qt::WA_WState_Hidden,
false);
8374 if (needUpdateGeometry)
8375 updateGeometry_helper(
true);
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())
8387 parent = parent->parentWidget();
8390 parent->d_func()->setDirtyOpaqueRegion();
8395 && (q->isWindow() || !q->parentWidget()->d_func()->layout)) {
8396 if (q->isWindow()) {
8398 if (q->windowState() != initialWindowState)
8399 q->setWindowState(initialWindowState);
8403 q->setAttribute(Qt::WA_Resized,
false);
8406 q->setAttribute(Qt::WA_KeyboardFocusChange,
false);
8408 if (q->isWindow() || q->parentWidget()->isVisible()) {
8411 qApp->d_func()->sendSyntheticEnterLeave(q);
8414 QEvent showToParentEvent(QEvent::ShowToParent);
8415 QCoreApplication::sendEvent(q, &showToParentEvent);
8417 if (QApplicationPrivate::hidden_focus_widget == q)
8418 QApplicationPrivate::hidden_focus_widget =
nullptr;
8424 if (!q->isWindow() && q->parentWidget())
8425 q->parentWidget()->d_func()->setDirtyOpaqueRegion();
8427 if (!q->testAttribute(Qt::WA_WState_Hidden)) {
8428 q->setAttribute(Qt::WA_WState_Hidden);
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));
8440 QEvent hideToParentEvent(QEvent::HideToParent);
8441 QCoreApplication::sendEvent(q, &hideToParentEvent);
8942bool QWidget::event(QEvent *event)
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)
8971 switch (event->type()) {
8972 case QEvent::PlatformSurface: {
8974 switch (
static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
8975 case QPlatformSurfaceEvent::SurfaceCreated:
8976 if (!testAttribute(Qt::WA_WState_Created))
8979 case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
8980 if (testAttribute(Qt::WA_WState_Created)) {
8983 destroy(
false,
false);
8989 case QEvent::MouseMove:
8990 mouseMoveEvent((QMouseEvent*)event);
8993 case QEvent::MouseButtonPress:
8994 mousePressEvent((QMouseEvent*)event);
8997 case QEvent::MouseButtonRelease:
8998 mouseReleaseEvent((QMouseEvent*)event);
9001 case QEvent::MouseButtonDblClick:
9002 mouseDoubleClickEvent((QMouseEvent*)event);
9004#if QT_CONFIG(wheelevent)
9006 wheelEvent((QWheelEvent*)event);
9009#if QT_CONFIG(tabletevent)
9010 case QEvent::TabletMove:
9011 if (
static_cast<QTabletEvent *>(event)->buttons() == Qt::NoButton && !testAttribute(Qt::WA_TabletTracking))
9014 case QEvent::TabletPress:
9015 case QEvent::TabletRelease:
9016 tabletEvent((QTabletEvent*)event);
9019 case QEvent::KeyPress: {
9020 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
9022 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
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);
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);
9043 case QEvent::KeyRelease:
9044 keyReleaseEvent((QKeyEvent*)event);
9046 case QEvent::ShortcutOverride:
9049 case QEvent::InputMethod:
9050 inputMethodEvent((QInputMethodEvent *) event);
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));
9059 QVariant v = inputMethodQuery(q);
9060 if (q == Qt::ImEnabled && !v.isValid() && isEnabled()) {
9067 v = QVariant(testAttribute(Qt::WA_InputMethodEnabled));
9069 query->setValue(q, v);
9076 case QEvent::PolishRequest:
9080 case QEvent::Polish: {
9081 style()->polish(
this);
9082 setAttribute(Qt::WA_WState_Polished);
9083 if (!QApplication::font(
this).isCopyOf(QApplication::font()))
9085 if (!QApplication::palette(
this).isCopyOf(QGuiApplication::palette()))
9086 d->resolvePalette();
9090 case QEvent::ApplicationWindowIconChange:
9091 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
9092 d->setWindowIcon_sys();
9093 d->setWindowIcon_helper();
9096 case QEvent::FocusIn:
9097 focusInEvent((QFocusEvent*)event);
9098 d->updateWidgetTransform(event);
9101 case QEvent::FocusOut:
9102 focusOutEvent((QFocusEvent*)event);
9106#if QT_CONFIG(statustip)
9107 if (d->statusTip.size()) {
9108 QStatusTipEvent tip(d->statusTip);
9109 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9112 enterEvent(
static_cast<QEnterEvent*>(event));
9116#if QT_CONFIG(statustip)
9117 if (d->statusTip.size()) {
9119 QStatusTipEvent tip(empty);
9120 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9126 case QEvent::HoverEnter:
9127 case QEvent::HoverLeave:
9135 paintEvent((QPaintEvent*)event);
9139 moveEvent((QMoveEvent*)event);
9140 d->updateWidgetTransform(event);
9143 case QEvent::Resize:
9144 resizeEvent((QResizeEvent*)event);
9145 d->updateWidgetTransform(event);
9149 closeEvent((QCloseEvent *)event);
9152#ifndef QT_NO_CONTEXTMENU
9153 case QEvent::ContextMenu:
9154 switch (data->context_menu_policy) {
9155 case Qt::PreventContextMenu:
9157 case Qt::DefaultContextMenu:
9158 contextMenuEvent(
static_cast<QContextMenuEvent *>(event));
9160 case Qt::CustomContextMenu:
9161 emit customContextMenuRequested(
static_cast<QContextMenuEvent *>(event)->pos());
9164 case Qt::ActionsContextMenu:
9165 if (d->actions.size()) {
9166 QMenu::exec(d->actions,
static_cast<QContextMenuEvent *>(event)->globalPos(),
9179#if QT_CONFIG(draganddrop)
9181 dropEvent((QDropEvent*) event);
9184 case QEvent::DragEnter:
9185 dragEnterEvent((QDragEnterEvent*) event);
9188 case QEvent::DragMove:
9189 dragMoveEvent((QDragMoveEvent*) event);
9192 case QEvent::DragLeave:
9193 dragLeaveEvent((QDragLeaveEvent*) event);
9198 showEvent((QShowEvent*) event);
9202 hideEvent((QHideEvent*) event);
9205 case QEvent::ShowWindowRequest:
9210 case QEvent::ApplicationFontChange:
9213 case QEvent::ApplicationPaletteChange:
9214 d->resolvePalette();
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:
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);
9243 if (!d->childrenShownByExpose) {
9245 d->showChildren(
true);
9246 QShowEvent showEvent;
9247 QCoreApplication::sendSpontaneousEvent(widget, &showEvent);
9249 d->childrenHiddenByWState =
false;
9251 QHideEvent hideEvent;
9252 QCoreApplication::sendSpontaneousEvent(widget, &hideEvent);
9253 d->hideChildren(
true);
9254 d->childrenHiddenByWState =
true;
9256 d->childrenShownByExpose =
false;
9262 case QEvent::WindowActivate:
9263 case QEvent::WindowDeactivate: {
9264 if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
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);
9274 case QEvent::LanguageChange:
9277 QList<QObject*> childList = d->children;
9278 for (
int i = 0; i < childList.size(); ++i) {
9279 QObject *o = childList.at(i);
9281 QCoreApplication::sendEvent(o, event);
9287 case QEvent::ApplicationLayoutDirectionChange:
9288 d->resolveLayoutDirection();
9291 case QEvent::LayoutDirectionChange:
9293 d->layout->invalidate();
9297 case QEvent::UpdateRequest:
9298 d->syncBackingStore();
9300 case QEvent::UpdateLater:
9301 update(
static_cast<QUpdateLaterEvent*>(event)->region());
9303 case QEvent::StyleAnimationUpdate:
9304 if (isVisible() && !window()->isMinimized()) {
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);
9320 QCoreApplication::sendEvent(w, event);
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);
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);
9340 case QEvent::QueryWhatsThis:
9341 if (d->whatsThis.isEmpty())
9345 case QEvent::EmbeddingControl:
9346 d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
9347 data->fstrut_dirty =
false;
9349#if QT_CONFIG(action)
9350 case QEvent::ActionAdded:
9351 case QEvent::ActionRemoved:
9352 case QEvent::ActionChanged:
9353 actionEvent((QActionEvent*)event);
9357 case QEvent::KeyboardLayoutChange:
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);
9370 case QEvent::TouchBegin:
9371 case QEvent::TouchUpdate:
9372 case QEvent::TouchEnd:
9373 case QEvent::TouchCancel:
9378#ifndef QT_NO_GESTURES
9379 case QEvent::Gesture:
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);
9389 case QEvent::DevicePixelRatioChange:
9390 if (d->data.fnt.d->dpi != logicalDpiY())
9391 d->updateFont(d->data.fnt);
9392 d->renderToTextureReallyDirty = 1;
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();
9400 const char axis = propName.at(12);
9402 d->extra->customDpiX = value;
9403 else if (axis ==
'Y')
9404 d->extra->customDpiY = value;
9405 d->updateFont(d->data.fnt);
9407 if (windowHandle() && !qstrncmp(propName,
"_q_platform_", 12))
9408 windowHandle()->setProperty(propName, property(propName));
9412 return QObject::event(event);
10726void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
10729 Q_ASSERT_X(
this != parent, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
10731 const auto checkForParentChildLoops = qScopeGuard([&](){
10733 auto p = parentWidget();
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()));
10740 p = p->parentWidget();
10745 const bool resized = testAttribute(Qt::WA_Resized);
10746 const bool wasCreated = testAttribute(Qt::WA_WState_Created);
10747 QWidget *oldtlw = window();
10749 QWidget *oldParentWithWindow = d->closestParentWidgetWithWindowHandle();
10751 if (f & Qt::Window)
10752 d->data.fstrut_dirty =
true;
10754 bool newParent = (parent != parentWidget());
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);
10764 if (!testAttribute(Qt::WA_WState_Hidden)) {
10779 setAttribute(Qt::WA_WState_ExplicitShowHide,
false);
10782 QEvent e(QEvent::ParentAboutToChange);
10783 QCoreApplication::sendEvent(
this, &e);
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);
10796 if (newParent && isAncestorOf(focusWidget()) && !(f & Qt::Window))
10797 focusWidget()->clearFocus();
10799 d->setParent_sys(parent, f);
10801 if (d->textureChildSeen && parent) {
10803 QWidgetPrivate::get(parent)->setTextureChildSeen();
10806 if (QWidgetRepaintManager *oldPaintManager = oldtlw->d_func()->maybeRepaintManager()) {
10808 oldPaintManager->removeDirtyWidget(
this);
10811 oldPaintManager->moveStaticWidgets(
this);
10814 d->reparentFocusWidgets(oldtlw);
10815 setAttribute(Qt::WA_Resized, resized);
10817 const bool useStyleSheetPropagationInWidgetStyles =
10818 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
10820 if (!useStyleSheetPropagationInWidgetStyles && !testAttribute(Qt::WA_StyleSheet)
10821 && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
10824 const auto pd = parent->d_func();
10825 d->inheritedFontResolveMask = pd->directFontResolveMask | pd->inheritedFontResolveMask;
10826 d->inheritedPaletteResolveMask = pd->directPaletteResolveMask | pd->inheritedPaletteResolveMask;
10829 d->resolvePalette();
10831 d->resolveLayoutDirection();
10832 d->resolveLocale();
10838 if (newParent || !wasCreated
10839#if QT_CONFIG(opengles2)
10840 || (f & Qt::MSWindowsOwnDC)
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);
10853 if (parent && d->sendChildEvents) {
10854 QChildEvent e(QEvent::ChildAdded,
this);
10855 QCoreApplication::sendEvent(parent, &e);
10858 if (parent && d->sendChildEvents && d->polished) {
10859 QChildEvent e(QEvent::ChildPolished,
this);
10860 QCoreApplication::sendEvent(parent, &e);
10863 QEvent e(QEvent::ParentChange);
10864 QCoreApplication::sendEvent(
this, &e);
10869 if (oldWidgetUsesRhiFlush && oldtlw != window())
10870 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowChangeInternal);
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);
10879 d->updateIsOpaque();
10881#if QT_CONFIG(graphicsview)
10884 if (oldtlw->graphicsProxyWidget()) {
10885 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
10886 ancestorProxy->d_func()->unembedSubWindow(
this);
10888 if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(
this)) {
10889 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
10890 ancestorProxy->d_func()->embedSubWindow(
this);
10894 if (d->extra && d->extra->hasWindowContainer)
10895 QWindowContainer::parentWasChanged(
this);
10897 QWidget *newParentWithWindow = d->closestParentWidgetWithWindowHandle();
10898 if (newParentWithWindow && newParentWithWindow != oldParentWithWindow) {
10900 qCDebug(lcWidgetPainting) <<
"Evaluating whether reparenting of" <<
this
10901 <<
"into" << parent <<
"requires RHI enablement for" << newParentWithWindow;
10903 QPlatformBackingStoreRhiConfig rhiConfig;
10904 QSurface::SurfaceType surfaceType = QSurface::RasterSurface;
10910 if (q_evaluateRhiConfig(
this, &rhiConfig, &surfaceType)) {
10914 if (q_evaluateRhiConfig(newParentWithWindow,
nullptr,
nullptr)) {
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()) {
10935 backingStore->handle()->createRhi(existingWindow, rhiConfig);
10937 QWidgetPrivate::get(newParentWithWindow)->usesRhiFlush =
true;
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);
10952void QWidgetPrivate::setParent_sys(QWidget *newparent, Qt::WindowFlags f)
10956 Qt::WindowFlags oldFlags = data.window_flags;
10957 bool wasCreated = q->testAttribute(Qt::WA_WState_Created);
10959 QScreen *targetScreen =
nullptr;
10965 targetScreen = q->parentWidget()->window()->screen();
10968 const bool destroyWindow = (
10970 (oldFlags & Qt::Window) && !(f & Qt::Window)
10972 && wasCreated && !q->testAttribute(Qt::WA_NativeWindow)
10975 if (parent != newparent) {
10977 QObjectPrivate::setParent_helper(newparent);
10979 if (q->windowHandle())
10980 q->windowHandle()->setFlags(f);
10984 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
10987 if (destroyWindow) {
10988 reparentWidgetWindowChildren(parentWithWindow);
10998 reparentWidgetWindows(parentWithWindow, f);
11002 bool explicitlyHidden = isExplicitlyHidden();
11004 if (destroyWindow) {
11005 if (extra && extra->hasWindowContainer)
11006 QWindowContainer::toplevelAboutToBeDestroyed(q);
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);
11025 q->destroy(
true,
false);
11029 data.window_flags = f;
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);
11042 q->setAttribute(Qt::WA_WState_Created,
false);
11043 q->setAttribute(Qt::WA_WState_Visible,
false);
11044 q->setAttribute(Qt::WA_WState_Hidden,
false);
11046 if (newparent && wasCreated && (q->testAttribute(Qt::WA_NativeWindow) || (f & Qt::Window)))
11049 if (q->isWindow() || (!newparent || newparent->isVisible()) || explicitlyHidden)
11050 q->setAttribute(Qt::WA_WState_Hidden);
11051 q->setAttribute(Qt::WA_WState_ExplicitShowHide, explicitlyHidden);
11054 if (!newparent && targetScreen) {
11056 if (q->testAttribute(Qt::WA_WState_Created))
11057 q->windowHandle()->setScreen(targetScreen);
11059 topData()->initialScreen = targetScreen;