569
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
795QWidgetMapper *QWidgetPrivate::mapper =
nullptr;
796QWidgetSet *QWidgetPrivate::allWidgets =
nullptr;
800
801
804
805
806
807
808
809
810
811
812
813
814
815
816
817
824#ifdef QT_NO_EXCEPTIONS
828 QWidgetPrivate::allWidgets->remove(that);
829 d->removeFromFocusChain();
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
860 : QWidget(*
new QWidgetPrivate, parent, f)
866
867QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
868 : QObject(dd,
nullptr), QPaintDevice()
875 QWidgetExceptionCleaner::cleanup(
this, d_func());
881
882
883int QWidget::devType()
const
885 return QInternal::Widget;
890void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
892 bool customize = (flags & (Qt::CustomizeWindowHint
893 | Qt::FramelessWindowHint
894 | Qt::WindowTitleHint
895 | Qt::WindowSystemMenuHint
896 | Qt::WindowMinimizeButtonHint
897 | Qt::WindowMaximizeButtonHint
898 | Qt::WindowCloseButtonHint
899 | Qt::WindowContextHelpButtonHint));
901 uint type = (flags & Qt::WindowType_Mask);
903 if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
908 if (flags & Qt::CustomizeWindowHint) {
913 if ((flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint))
915 && type != Qt::Dialog
918 flags |= Qt::WindowSystemMenuHint;
919 flags |= Qt::WindowTitleHint;
920 flags &= ~Qt::FramelessWindowHint;
922 }
else if (customize && !(flags & Qt::FramelessWindowHint)) {
926 flags |= Qt::WindowSystemMenuHint;
927 flags |= Qt::WindowTitleHint;
930 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
931 if (type != Qt::Dialog && type != Qt::Sheet && type != Qt::Tool)
932 flags |= Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowFullscreenButtonHint;
934 if (w->testAttribute(Qt::WA_TransparentForMouseEvents))
935 flags |= Qt::WindowTransparentForInput;
938void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
944#if QT_DEPRECATED_SINCE(6
, 11
)
945 QT_IGNORE_DEPRECATIONS(
946 Q_ASSERT_X(!f.testFlag(Qt::WindowType::Desktop), Q_FUNC_INFO,
"Qt::WindowType::Desktop is not allowed.");
947 if (f.testFlag(Qt::WindowType::Desktop))
948 f.setFlag(Qt::WindowType::Desktop,
false);
952 Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
954 if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
955 qFatal(
"QWidget: Cannot create a QWidget without QApplication");
957 Q_ASSERT(allWidgets);
959 allWidgets->insert(q);
963 Q_ASSERT_X(QThread::isMainThread(),
"QWidget",
964 "Widgets must be created in the GUI thread.");
968 data.fstrut_dirty =
true;
971 data.widget_attributes = 0;
972 data.window_flags = f;
973 data.window_state = 0;
974 data.focus_policy = 0;
975 data.context_menu_policy = Qt::DefaultContextMenu;
976 data.window_modality = Qt::NonModal;
978 data.sizehint_forced = 0;
979 data.is_closing =
false;
981 data.in_set_window_state = 0;
982 data.in_destructor =
false;
985 if (f & Qt::MSWindowsOwnDC) {
986 mustHaveWindowHandle = 1;
987 q->setAttribute(Qt::WA_NativeWindow);
990 q->setAttribute(Qt::WA_QuitOnClose);
991 adjustQuitOnCloseAttribute();
993 q->setAttribute(Qt::WA_WState_Hidden);
996 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
1000 q->setParent(parentWidget, data.window_flags);
1002 adjustFlags(data.window_flags, q);
1003 resolveLayoutDirection();
1005 const QBrush &background = q->palette().brush(QPalette::Window);
1006 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
1008 data.fnt = QFont(data.fnt, q);
1010 q->setAttribute(Qt::WA_PendingMoveEvent);
1011 q->setAttribute(Qt::WA_PendingResizeEvent);
1013 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
1014 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
1016 QEvent e(QEvent::Create);
1017 QCoreApplication::sendEvent(q, &e);
1018 QCoreApplication::postEvent(q,
new QEvent(QEvent::PolishRequest));
1020 extraPaintEngine =
nullptr;
1023void QWidgetPrivate::createRecursively()
1026 q->create(0,
true,
true);
1027 for (
int i = 0; i < children.size(); ++i) {
1028 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1029 if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
1030 child->d_func()->createRecursively();
1034QRhi *QWidgetPrivate::rhi()
const
1037 if (
auto *backingStore = q->backingStore()) {
1038 auto *window = windowHandle(WindowHandleMode::Closest);
1039 return backingStore->handle()->rhi(window);
1046
1047
1048
1049
1050
1051
1052
1053QWidget *QWidgetPrivate::closestParentWidgetWithWindowHandle()
const
1056 QWidget *parent = q->parentWidget();
1057 while (parent && !parent->windowHandle())
1058 parent = parent->parentWidget();
1062QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode)
const
1064 if (mode == WindowHandleMode::Direct || mode == WindowHandleMode::Closest) {
1065 if (QTLWExtra *x = maybeTopData()) {
1066 if (x->window !=
nullptr || mode == WindowHandleMode::Direct)
1070 if (mode == WindowHandleMode::Closest) {
1072 if (
auto nativeParent = q_func()->nativeParentWidget()) {
1073 if (
auto window = nativeParent->windowHandle())
1077 if (mode == WindowHandleMode::TopLevel || mode == WindowHandleMode::Closest) {
1078 if (
auto topLevel = q_func()->topLevelWidget()) {
1079 if (
auto window = topLevel ->windowHandle())
1087
1088
1089
1090
1091
1092QWindow *QWidgetPrivate::_q_closestWindowHandle()
const
1094 return windowHandle(QWidgetPrivate::WindowHandleMode::Closest);
1097QScreen *QWidgetPrivate::associatedScreen()
const
1099#if QT_CONFIG(graphicsview)
1101 if (nearestGraphicsProxyWidget(q_func()))
1104 if (
auto window = windowHandle(WindowHandleMode::Closest))
1105 return window->screen();
1112 QPlatformBackingStoreRhiConfig config = QWidgetPrivate::get(w)->rhiConfig();
1113 if (config.isEnabled()) {
1115 *outConfig = config;
1117 *outType = QBackingStoreRhiSupport::surfaceTypeForConfig(config);
1120 for (
const QObject *child : w->children()) {
1121 if (
const QWidget *childWidget = qobject_cast<
const QWidget *>(child)) {
1122 if (q_evaluateRhiConfigRecursive(childWidget, outConfig, outType)) {
1123 static bool optOut = qEnvironmentVariableIsSet(
"QT_WIDGETS_NO_CHILD_RHI");
1126 if (!optOut && childWidget->testAttribute(Qt::WA_NativeWindow))
1142 if (QBackingStoreRhiSupport::checkForceRhi(outConfig, outType)) {
1143 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluated to forced flushing with QRhi";
1149 if (q_evaluateRhiConfigRecursive(w, outConfig, outType)) {
1150 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluates to flushing with QRhi";
1160
1161
1162
1163
1164
1165
1166
1167
1168
1170void QWidget::create(WId window,
bool initializeWindow,
bool destroyOldWindow)
1172 Q_UNUSED(initializeWindow);
1173 Q_UNUSED(destroyOldWindow);
1176 if (Q_UNLIKELY(window))
1177 qWarning(
"QWidget::create(): Parameter 'window' does not have any effect.");
1178 if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
1181 if (d->data.in_destructor)
1184 Qt::WindowType type = windowType();
1185 Qt::WindowFlags &flags = data->window_flags;
1187 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1189 flags |= Qt::Window;
1192 if (QWidget *parent = parentWidget()) {
1193 if (type & Qt::Window) {
1194 if (!parent->testAttribute(Qt::WA_WState_Created))
1195 parent->createWinId();
1196 }
else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
1197 && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
1203 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1204 Q_ASSERT(internalWinId());
1210 static const bool paintOnScreenEnv = qEnvironmentVariableIntValue(
"QT_ONSCREEN_PAINT") > 0;
1211 if (paintOnScreenEnv)
1212 setAttribute(Qt::WA_PaintOnScreen);
1214 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1215 setAttribute(Qt::WA_NativeWindow);
1218#if QT_CONFIG(graphicsview)
1219 && !graphicsProxyWidget()
1223 auto *topExtra = d->maybeTopData();
1224 if (!topExtra || !topExtra->explicitContentsMarginsRespectsSafeArea) {
1225 setAttribute_internal(Qt::WA_ContentsMarginsRespectsSafeArea,
1230 d->updateIsOpaque();
1232 setAttribute(Qt::WA_WState_Created);
1237 d->topData()->repaintManager.reset(
new QWidgetRepaintManager(
this));
1241 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1242 setAttribute(Qt::WA_DropSiteRegistered,
true);
1245 if (testAttribute(Qt::WA_SetWindowIcon))
1246 d->setWindowIcon_sys();
1248 if (isWindow() && !d->topData()->iconText.isEmpty())
1249 d->setWindowIconText_helper(d->topData()->iconText);
1250 if (isWindow() && !d->topData()->caption.isEmpty())
1251 d->setWindowTitle_helper(d->topData()->caption);
1252 if (isWindow() && !d->topData()->filePath.isEmpty())
1253 d->setWindowFilePath_helper(d->topData()->filePath);
1254 d->updateSystemBackground();
1256 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1257 d->setWindowIcon_sys();
1264 d->updateFrameStrut();
1269 QObjectList children = parentWidget->children();
1270 for (
int i = 0; i < children.size(); i++) {
1271 if (children.at(i)->isWidgetType()) {
1272 const QWidget *childWidget = qobject_cast<
const QWidget *>(children.at(i));
1274 if (childWidget->testAttribute(Qt::WA_NativeWindow)) {
1275 if (!childWidget->internalWinId())
1276 childWidget->winId();
1277 if (childWidget->windowHandle()) {
1278 if (childWidget->isWindow()) {
1279 childWidget->windowHandle()->setTransientParent(parentWidget->window()->windowHandle());
1281 childWidget->windowHandle()->setParent(childWidget->nativeParentWidget()->windowHandle());
1293void QWidgetPrivate::create()
1297 if (!q->testAttribute(Qt::WA_NativeWindow) && !q->isWindow())
1300 QWidgetWindow *win = topData()->window;
1305 Q_ASSERT(topData()->window);
1306 win = topData()->window;
1309 const auto dynamicPropertyNames = q->dynamicPropertyNames();
1310 for (
const QByteArray &propertyName : dynamicPropertyNames) {
1311 if (!qstrncmp(propertyName,
"_q_platform_", 12))
1312 win->setProperty(propertyName, q->property(propertyName));
1315 Qt::WindowFlags &flags = data.window_flags;
1317#if defined(QT_PLATFORM_UIKIT)
1318 if (q->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
1319 flags |= Qt::ExpandedClientAreaHint;
1322 if (q->testAttribute(Qt::WA_ShowWithoutActivating))
1323 win->setProperty(
"_q_showWithoutActivating", QVariant(
true));
1324 if (q->testAttribute(Qt::WA_MacAlwaysShowToolWindow))
1325 win->setProperty(
"_q_macAlwaysShowToolWindow", QVariant(
true));
1326 win->setFlags(flags);
1327 fixPosIncludesFrame();
1328 if (q->testAttribute(Qt::WA_Moved)
1329 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
1330 win->setGeometry(q->geometry());
1332 win->resize(q->size());
1333 if (win->isTopLevel()) {
1334 QScreen *targetScreen = topData()->initialScreen;
1335 topData()->initialScreen =
nullptr;
1337 targetScreen = q->screen();
1338 win->setScreen(targetScreen);
1341 QSurfaceFormat format = win->requestedFormat();
1342 if ((flags & Qt::Window) && win->surfaceType() != QSurface::OpenGLSurface
1343 && q->testAttribute(Qt::WA_TranslucentBackground)) {
1344 format.setAlphaBufferSize(8);
1346 win->setFormat(format);
1348 if (QWidget *nativeParent = q->nativeParentWidget()) {
1349 if (nativeParent->windowHandle()) {
1350 if (flags & Qt::Window) {
1351 win->setTransientParent(nativeParent->window()->windowHandle());
1352 win->setParent(
nullptr);
1354 win->setTransientParent(
nullptr);
1355 win->setParent(nativeParent->windowHandle());
1360 qt_window_private(win)->positionPolicy = topData()->posIncludesFrame ?
1361 QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
1365 if (QPlatformWindow *platformWindow = win->handle())
1366 platformWindow->setFrameStrutEventsEnabled(
true);
1368 data.window_flags = win->flags();
1371 if (!topData()->role.isNull()) {
1372 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(win->handle()))
1373 xcbWindow->setWindowRole(topData()->role);
1376#if QT_CONFIG(wayland)
1377 if (!topData()->role.isNull()) {
1378 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(win->handle()))
1379 waylandWindow->setSessionRestoreId(topData()->role);
1383 QBackingStore *store = q->backingStore();
1384 usesRhiFlush =
false;
1386 if (!store && q->isWindow())
1387 q->setBackingStore(
new QBackingStore(win));
1389 QPlatformBackingStoreRhiConfig rhiConfig;
1390 usesRhiFlush = q_evaluateRhiConfig(q, &rhiConfig,
nullptr);
1391 if (usesRhiFlush && q->backingStore()) {
1394 q->backingStore()->handle()->createRhi(win, rhiConfig);
1397 setWindowModified_helper();
1399 if (win->handle()) {
1400 WId id = win->winId();
1402 Q_ASSERT(id != WId(0));
1405 setNetWmWindowTypes(
true);
1408 q_createNativeChildrenAndSetParent(q);
1410 if (extra && !extra->mask.isEmpty())
1411 setMask_sys(extra->mask);
1413 if (data.crect.width() == 0 || data.crect.height() == 0) {
1414 q->setAttribute(Qt::WA_OutsideWSRange,
true);
1416 q->setAttribute(Qt::WA_OutsideWSRange,
false);
1417 if (q->isVisible()) {
1419 win->setNativeWindowVisibility(
true);
1425static const char activeXNativeParentHandleProperty[] =
"_q_embedded_native_parent_handle";
1428void QWidgetPrivate::createTLSysExtra()
1431 if (!extra->topextra->window && (q->testAttribute(Qt::WA_NativeWindow) || q->isWindow())) {
1432 extra->topextra->window =
new QWidgetWindow(q);
1433 if (extra->minw || extra->minh)
1434 extra->topextra->window->setMinimumSize(QSize(extra->minw, extra->minh));
1436 extra->topextra->window->setMaximumSize(QSize(extra->maxw, extra->maxh));
1437 if (extra->topextra->opacity != 255 && q->isWindow())
1438 extra->topextra->window->setOpacity(qreal(extra->topextra->opacity) / qreal(255));
1440#if QT_CONFIG(tooltip)
1441 const bool isTipLabel = qobject_cast<
const QTipLabel *>(q) !=
nullptr;
1443 const bool isAlphaWidget = !isTipLabel && q->inherits(
"QAlphaWidget");
1446 const QVariant activeXNativeParentHandle = q->property(activeXNativeParentHandleProperty);
1447 if (activeXNativeParentHandle.isValid())
1448 extra->topextra->window->setProperty(activeXNativeParentHandleProperty, activeXNativeParentHandle);
1449 if (isTipLabel || isAlphaWidget)
1450 extra->topextra->window->setProperty(
"_q_windowsDropShadow", QVariant(
true));
1452 if (isTipLabel || isAlphaWidget || q->inherits(
"QRollEffect"))
1453 qt_window_private(extra->topextra->window)->setAutomaticPositionAndResizeEnabled(
false);
1455 updateIsTranslucent();
1461
1462
1463
1464
1465
1470 d->data.in_destructor =
true;
1472#if QT_CONFIG(accessibility)
1473 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing && QAccessible::isActive())
1474 QAccessibleCache::instance()->sendObjectDestroyedEvent(
this);
1477#if defined (QT_CHECK_STATE)
1478 if (Q_UNLIKELY(paintingActive()))
1479 qWarning(
"QWidget: %s (%s) deleted while being painted", className(), name());
1482#ifndef QT_NO_GESTURES
1483 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1485 for (
auto it = d->gestureContext.keyBegin(), end = d->gestureContext.keyEnd(); it != end; ++it)
1486 manager->cleanupCachedGestures(
this, *it);
1488 d->gestureContext.clear();
1493 for (
auto action : std::as_const(d->actions)) {
1494 QActionPrivate *apriv = action->d_func();
1495 apriv->associatedObjects.removeAll(
this);
1500#ifndef QT_NO_SHORTCUT
1503 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1504 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0,
this, QKeySequence());
1509 d->layout =
nullptr;
1512 d->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency);
1515#if QT_CONFIG(graphicsview)
1516 const QWidget* w =
this;
1517 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
1518 w = w->d_func()->extra->focus_proxy;
1519 QWidget *window = w->window();
1520 QWExtra *e = window ? window->d_func()->extra.get() :
nullptr ;
1521 if (!e || !e->proxyWidget || (w->parentWidget() && w->parentWidget()->d_func()->focus_child ==
this))
1528 d->setDirtyOpaqueRegion();
1530 if (isWindow() && isVisible() && internalWinId()) {
1541 }
else if (isVisible()) {
1542 qApp->d_func()->sendSyntheticEnterLeave(
this);
1545 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
1546 repaintManager->removeDirtyWidget(
this);
1547 if (testAttribute(Qt::WA_StaticContents))
1548 repaintManager->removeStaticWidget(
this);
1551 delete d->needsFlush;
1552 d->needsFlush =
nullptr;
1556 bool blocked = d->blockSig;
1559 if (d->isSignalConnected(0)) {
1561 emit destroyed(
this);
1565 qWarning(
"Detected an unexpected exception in ~QWidget while emitting destroyed().");
1570 if (d->declarativeData) {
1571 d->wasDeleted =
true;
1572 if (QAbstractDeclarativeData::destroyed)
1573 QAbstractDeclarativeData::destroyed(d->declarativeData,
this);
1574 d->declarativeData =
nullptr;
1575 d->wasDeleted =
false;
1578 d->blockSig = blocked;
1580 if (!d->children.isEmpty())
1581 d->deleteChildren();
1583 QCoreApplication::removePostedEvents(
this);
1590 --QWidgetPrivate::instanceCounter;
1592 if (QWidgetPrivate::allWidgets)
1593 QWidgetPrivate::allWidgets->remove(
this);
1596 QEvent e(QEvent::Destroy);
1597 QCoreApplication::sendEvent(
this, &e);
1598 } QT_CATCH(
const std::exception&) {
1602#if QT_CONFIG(graphicseffect)
1603 delete d->graphicsEffect;
1607 d->isWidget =
false;
1610int QWidgetPrivate::instanceCounter = 0;
1611int QWidgetPrivate::maxInstances = 0;
1613void QWidgetPrivate::setWinId(WId id)
1616 if (mapper && data.winid) {
1617 mapper->remove(data.winid);
1620 const WId oldWinId = data.winid;
1624 mapper->insert(data.winid, q);
1627 if (oldWinId != id) {
1628 QEvent e(QEvent::WinIdChange);
1629 QCoreApplication::sendEvent(q, &e);
1633void QWidgetPrivate::createTLExtra()
1637 if (!extra->topextra) {
1638 extra->topextra = std::make_unique<QTLWExtra>();
1639 QTLWExtra* x = extra->topextra.get();
1640 x->backingStore =
nullptr;
1641 x->sharedPainter =
nullptr;
1642 x->incw = x->inch = 0;
1643 x->basew = x->baseh = 0;
1644 x->frameStrut.setCoords(0, 0, 0, 0);
1645 x->normalGeometry = QRect(0,0,-1,-1);
1646 x->savedFlags = { };
1648 x->posIncludesFrame = 0;
1649 x->sizeAdjusted =
false;
1651 x->explicitContentsMarginsRespectsSafeArea = 0;
1652 x->window =
nullptr;
1653 x->initialScreen =
nullptr;
1655#ifdef QWIDGET_EXTRA_DEBUG
1656 static int count = 0;
1657 qDebug() <<
"tlextra" << ++count;
1663
1664
1665
1667void QWidgetPrivate::createExtra()
1670 extra = std::make_unique<QWExtra>();
1671 extra->glContext =
nullptr;
1672#if QT_CONFIG(graphicsview)
1673 extra->proxyWidget =
nullptr;
1679 extra->customDpiX = 0;
1680 extra->customDpiY = 0;
1681 extra->explicitMinSize = 0;
1682 extra->explicitMaxSize = 0;
1683 extra->autoFillBackground = 0;
1684 extra->nativeChildrenForced = 0;
1685 extra->inRenderWithPainter = 0;
1686 extra->hasWindowContainer =
false;
1689#ifdef QWIDGET_EXTRA_DEBUG
1690 static int count = 0;
1691 qDebug() <<
"extra" << ++count;
1696void QWidgetPrivate::createSysExtra()
1701
1702
1703
1705void QWidgetPrivate::deleteExtra()
1709#if QT_CONFIG(style_stylesheet)
1711 if (QStyleSheetStyle *proxy = qt_styleSheet(extra->style))
1714 if (extra->topextra)
1722void QWidgetPrivate::deleteSysExtra()
1726void QWidgetPrivate::deleteTLSysExtra()
1729 if (extra && extra->topextra) {
1730 if (extra->hasWindowContainer)
1731 QWindowContainer::toplevelAboutToBeDestroyed(q);
1733 delete extra->topextra->window;
1734 extra->topextra->window =
nullptr;
1739
1740
1741
1743QRegion QWidgetPrivate::overlappedRegion(
const QRect &rect,
bool breakAfterFirst)
const
1747 const QWidget *w = q;
1754 QWidgetPrivate *pd = w->parentWidget()->d_func();
1756 for (
int i = 0; i < pd->children.size(); ++i) {
1757 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
1758 if (!sibling || !sibling->isVisible() || sibling->isWindow())
1761 above = (sibling == w);
1765 const QRect siblingRect = sibling->d_func()->effectiveRectFor(sibling->data->crect);
1766 if (qRectIntersects(siblingRect, r)) {
1767 const auto &siblingExtra = sibling->d_func()->extra;
1768 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
1769 && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
1772 region += siblingRect.translated(-p);
1773 if (breakAfterFirst)
1777 w = w->parentWidget();
1778 r.translate(pd->data.crect.topLeft());
1779 p += pd->data.crect.topLeft();
1784void QWidgetPrivate::syncBackingStore()
1786 if (shouldPaintOnScreen()) {
1787 paintOnScreen(dirty);
1789 }
else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1790 repaintManager->sync();
1794void QWidgetPrivate::syncBackingStore(
const QRegion ®ion)
1796 if (shouldPaintOnScreen())
1797 paintOnScreen(region);
1798 else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1799 repaintManager->sync(q_func(), region);
1803void QWidgetPrivate::paintOnScreen(
const QRegion &rgn)
1805 if (data.in_destructor)
1808 if (shouldDiscardSyncRequest())
1812 if (q->testAttribute(Qt::WA_StaticContents)) {
1815 extra->staticContentsSize = data.crect.size();
1818 QPaintEngine *engine = q->paintEngine();
1823 const bool noPartialUpdateSupport = (engine && (engine->type() == QPaintEngine::OpenGL
1824 || engine->type() == QPaintEngine::OpenGL2))
1825 && (usesDoubleBufferedGLContext || q->autoFillBackground());
1826 QRegion toBePainted(noPartialUpdateSupport ? q->rect() : rgn);
1828 toBePainted &= clipRect();
1829 clipToEffectiveMask(toBePainted);
1830 if (toBePainted.isEmpty())
1833 drawWidget(q, toBePainted, QPoint(), QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen,
nullptr);
1835 if (Q_UNLIKELY(q->paintingActive()))
1836 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
1839void QWidgetPrivate::setUpdatesEnabled_helper(
bool enable)
1843 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
1846 if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
1849 q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
1853 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
1854 for (
int i = 0; i < children.size(); ++i) {
1855 QWidget *w = qobject_cast<QWidget *>(children.at(i));
1856 if (w && !w->isWindow() && !w->testAttribute(attribute))
1857 w->d_func()->setUpdatesEnabled_helper(enable);
1862
1863
1864
1865
1866
1867
1868void QWidgetPrivate::propagatePaletteChange()
1872#if QT_CONFIG(graphicsview)
1873 if (!q->parentWidget() && extra && extra->proxyWidget) {
1874 QGraphicsProxyWidget *p = extra->proxyWidget;
1875 inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolveMask();
1878 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
1879 inheritedPaletteResolveMask = 0;
1882 directPaletteResolveMask = data.pal.resolveMask();
1883 auto mask = directPaletteResolveMask | inheritedPaletteResolveMask;
1885 const bool useStyleSheetPropagationInWidgetStyles =
1886 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
1888 QEvent pc(QEvent::PaletteChange);
1889 QCoreApplication::sendEvent(q, &pc);
1890 for (
int i = 0; i < children.size(); ++i) {
1891 QWidget *w = qobject_cast<QWidget*>(children.at(i));
1892 if (w && (!w->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
1893 && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
1894 QWidgetPrivate *wd = w->d_func();
1895 wd->inheritedPaletteResolveMask = mask;
1896 wd->resolvePalette();
1902
1903
1904QRect QWidgetPrivate::clipRect()
const
1907 const QWidget * w = q;
1908 if (!w->isVisible())
1910 QRect r = effectiveRectFor(q->rect());
1916 && w->parentWidget()) {
1919 w = w->parentWidget();
1920 r &= QRect(ox, oy, w->width(), w->height());
1926
1927
1928QRegion QWidgetPrivate::clipRegion()
const
1931 if (!q->isVisible())
1933 QRegion r(q->rect());
1934 const QWidget * w = q;
1935 const QWidget *ignoreUpTo;
1941 && w->parentWidget()) {
1945 w = w->parentWidget();
1946 r &= QRegion(ox, oy, w->width(), w->height());
1949 while(w->d_func()->children.at(i++) !=
static_cast<
const QObject *>(ignoreUpTo))
1951 for ( ; i < w->d_func()->children.size(); ++i) {
1952 if (QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
1953 if (sibling->isVisible() && !sibling->isWindow()) {
1954 QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
1955 sibling->width(), sibling->height());
1956 if (qRectIntersects(siblingRect, q->rect()))
1957 r -= QRegion(siblingRect);
1965void QWidgetPrivate::setSystemClip(QPaintEngine *paintEngine, qreal devicePixelRatio,
const QRegion ®ion)
1968 QTransform scaleTransform;
1969 scaleTransform.scale(devicePixelRatio, devicePixelRatio);
1971 paintEngine->d_func()->baseSystemClip = region;
1972 paintEngine->d_func()->setSystemTransform(scaleTransform);
1976#if QT_CONFIG(graphicseffect)
1977void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
1982 if (w->graphicsEffect()) {
1983 QWidgetEffectSourcePrivate *sourced =
1984 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
1985 if (!sourced->updateDueToGraphicsEffect)
1986 w->graphicsEffect()->source()->d_func()->invalidateCache();
1988 w = w->parentWidget();
1993void QWidgetPrivate::setDirtyOpaqueRegion()
1997 dirtyOpaqueChildren =
true;
1999#if QT_CONFIG(graphicseffect)
2000 invalidateGraphicsEffectsRecursively();
2006 QWidget *parent = q->parentWidget();
2011 QWidgetPrivate *pd = parent->d_func();
2012 if (!pd->dirtyOpaqueChildren)
2013 pd->setDirtyOpaqueRegion();
2016const QRegion &QWidgetPrivate::getOpaqueChildren()
const
2018 if (!dirtyOpaqueChildren)
2019 return opaqueChildren;
2021 QWidgetPrivate *that =
const_cast<QWidgetPrivate*>(
this);
2022 that->opaqueChildren = QRegion();
2024 for (
int i = 0; i < children.size(); ++i) {
2025 QWidget *child = qobject_cast<QWidget *>(children.at(i));
2026 if (!child || !child->isVisible() || child->isWindow())
2029 const QPoint offset = child->geometry().topLeft();
2030 QWidgetPrivate *childd = child->d_func();
2031 QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
2032 if (childd->extra && childd->extra->hasMask)
2033 r &= childd->extra->mask;
2036 r.translate(offset);
2037 that->opaqueChildren += r;
2040 that->opaqueChildren &= q_func()->rect();
2041 that->dirtyOpaqueChildren =
false;
2043 return that->opaqueChildren;
2046void QWidgetPrivate::subtractOpaqueChildren(QRegion &source,
const QRect &clipRect)
const
2048 if (children.isEmpty() || clipRect.isEmpty())
2051 const QRegion &r = getOpaqueChildren();
2053 source -= (r & clipRect);
2057void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion,
bool *hasDirtySiblingsAbove,
2058 bool alsoNonOpaque)
const
2061 static int disableSubtractOpaqueSiblings = qEnvironmentVariableIntValue(
"QT_NO_SUBTRACTOPAQUESIBLINGS");
2062 if (disableSubtractOpaqueSiblings || q->isWindow())
2065 QRect clipBoundingRect;
2066 bool dirtyClipBoundingRect =
true;
2069 bool dirtyParentClip =
true;
2071 QPoint parentOffset = data.crect.topLeft();
2073 const QWidget *w = q;
2078 QWidgetPrivate *pd = w->parentWidget()->d_func();
2079 const int myIndex = pd->children.indexOf(
const_cast<QWidget *>(w));
2080 const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
2081 for (
int i = myIndex + 1; i < pd->children.size(); ++i) {
2082 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
2083 if (!sibling || !sibling->isVisible() || sibling->isWindow())
2086 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2087 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2090 if (dirtyClipBoundingRect) {
2091 clipBoundingRect = sourceRegion.boundingRect();
2092 dirtyClipBoundingRect =
false;
2095 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2098 if (dirtyParentClip) {
2099 parentClip = sourceRegion.translated(parentOffset);
2100 dirtyParentClip =
false;
2103 const QPoint siblingPos(sibling->data->crect.topLeft());
2104 const QRect siblingClipRect(sibling->d_func()->clipRect());
2105 QRegion siblingDirty(parentClip);
2106 siblingDirty &= (siblingClipRect.translated(siblingPos));
2107 const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
2108 && !sibling->d_func()->graphicsEffect;
2110 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2111 if (siblingDirty.isEmpty())
2114 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2116 siblingDirty.translate(-parentOffset);
2117 sourceRegion -= siblingDirty;
2119 sourceRegion -= siblingGeometry.translated(-parentOffset);
2122 if (hasDirtySiblingsAbove)
2123 *hasDirtySiblingsAbove =
true;
2124 if (sibling->d_func()->children.isEmpty())
2126 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2127 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2128 sourceRegion -= opaqueSiblingChildren;
2130 if (sourceRegion.isEmpty())
2133 dirtyClipBoundingRect =
true;
2134 dirtyParentClip =
true;
2137 w = w->parentWidget();
2138 parentOffset += pd->data.crect.topLeft();
2139 dirtyParentClip =
true;
2143void QWidgetPrivate::clipToEffectiveMask(QRegion ®ion)
const
2147 const QWidget *w = q;
2150#if QT_CONFIG(graphicseffect)
2151 if (graphicsEffect && !w->isWindow()) {
2152 w = q->parentWidget();
2153 offset -= data.crect.topLeft();
2158 const QWidgetPrivate *wd = w->d_func();
2159 if (wd->extra && wd->extra->hasMask)
2160 region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
2163 offset -= wd->data.crect.topLeft();
2164 w = w->parentWidget();
2168bool QWidgetPrivate::shouldPaintOnScreen()
const
2170#if defined(QT_NO_BACKINGSTORE)
2174 if (q->testAttribute(Qt::WA_PaintOnScreen)
2175 || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
2183void QWidgetPrivate::updateIsOpaque()
2186 setDirtyOpaqueRegion();
2188#if QT_CONFIG(graphicseffect)
2189 if (graphicsEffect) {
2197 if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
2202 const QPalette &pal = q->palette();
2204 if (q->autoFillBackground()) {
2205 const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
2206 if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
2212 if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
2213 const QBrush &windowBrush = q->palette().brush(QPalette::Window);
2214 if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
2222void QWidgetPrivate::setOpaque(
bool opaque)
2224 if (isOpaque != opaque) {
2226 updateIsTranslucent();
2230void QWidgetPrivate::updateIsTranslucent()
2233 if (QWindow *window = q->windowHandle()) {
2234 QSurfaceFormat format = window->format();
2235 const int oldAlpha = format.alphaBufferSize();
2236 const int newAlpha = q->testAttribute(Qt::WA_TranslucentBackground) ? 8 : -1;
2237 if (oldAlpha != newAlpha) {
2254 if (!window->handle()) {
2255 format.setAlphaBufferSize(newAlpha);
2256 window->setFormat(format);
2262static inline void fillRegion(QPainter *painter,
const QRegion &rgn,
const QBrush &brush)
2266 if (brush.style() == Qt::TexturePattern) {
2267 const QRect rect(rgn.boundingRect());
2268 painter->setClipRegion(rgn);
2269 painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
2270 }
else if (brush.gradient()
2271 && (brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode
2272 || brush.gradient()->coordinateMode() == QGradient::ObjectMode)) {
2274 painter->setClipRegion(rgn);
2275 painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
2278 for (
const QRect &rect : rgn)
2279 painter->fillRect(rect, brush);
2283bool QWidgetPrivate::updateBrushOrigin(QPainter *painter,
const QBrush &brush)
const
2285#if QT_CONFIG(scrollarea)
2288 if (brush.style() == Qt::NoBrush || brush.style() == Qt::SolidPattern)
2290 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
2291 if (scrollArea && scrollArea->viewport() == q) {
2292 QObjectData *scrollPrivate =
static_cast<QWidget *>(scrollArea)->d_ptr.data();
2293 QAbstractScrollAreaPrivate *priv =
static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
2294 painter->setBrushOrigin(-priv->contentsOffset());
2300void QWidgetPrivate::paintBackground(QPainter *painter,
const QRegion &rgn, DrawWidgetFlags flags)
const
2304 bool brushOriginSet =
false;
2305 const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
2307 if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
2308 const QBrush bg = q->palette().brush(QPalette::Window);
2309 if (!brushOriginSet)
2310 brushOriginSet = updateBrushOrigin(painter, bg);
2311 if (!(flags & DontSetCompositionMode)) {
2313 QPainter::CompositionMode oldMode = painter->compositionMode();
2314 painter->setCompositionMode(QPainter::CompositionMode_Source);
2315 fillRegion(painter, rgn, bg);
2316 painter->setCompositionMode(oldMode);
2318 fillRegion(painter, rgn, bg);
2322 if (q->autoFillBackground()) {
2323 if (!brushOriginSet)
2324 brushOriginSet = updateBrushOrigin(painter, autoFillBrush);
2325 fillRegion(painter, rgn, autoFillBrush);
2328 if (q->testAttribute(Qt::WA_StyledBackground)) {
2329 painter->setClipRegion(rgn);
2332 q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
2337
2338
2339
2340
2341
2345void QWidgetPrivate::deactivateWidgetCleanup()
2349 if (QApplication::activeWindow() == q)
2350 QApplicationPrivate::setActiveWindow(
nullptr);
2352 if (q == qt_button_down)
2353 qt_button_down =
nullptr;
2358
2359
2360
2361
2362
2363
2364
2366QWidget *QWidget::find(WId id)
2368 return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) :
nullptr;
2374
2375
2376
2377
2378
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396WId QWidget::winId()
const
2398 if (!data->in_destructor
2399 && (!testAttribute(Qt::WA_WState_Created) || !internalWinId()))
2401 QWidget *that =
const_cast<QWidget*>(
this);
2402 that->setAttribute(Qt::WA_NativeWindow);
2403 that->d_func()->createWinId();
2404 return that->data->winid;
2409void QWidgetPrivate::createWinId()
2413 const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
2414 if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
2415 if (!q->isWindow()) {
2416 QWidget *parent = q->parentWidget();
2417 QWidgetPrivate *pd = parent->d_func();
2418 if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
2419 parent->setAttribute(Qt::WA_NativeWindow);
2420 if (!parent->internalWinId()) {
2424 for (
int i = 0; i < pd->children.size(); ++i) {
2425 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
2426 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
2427 || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
2438
2439
2440
2441
2442
2444bool QWidgetPrivate::setScreenForPoint(
const QPoint &pos)
2450 return setScreen(QGuiApplication::screenAt(pos));
2454
2455
2456
2457
2459bool QWidgetPrivate::setScreen(QScreen *screen)
2462 if (!screen || !q->isWindow())
2464 const QScreen *currentScreen = windowHandle() ? windowHandle()->screen() :
nullptr;
2465 if (currentScreen != screen) {
2466 topData()->initialScreen = screen;
2468 windowHandle()->setScreen(screen);
2475
2476
2477
2478
2480void QWidget::createWinId()
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501WId QWidget::effectiveWinId()
const
2503 const WId id = internalWinId();
2504 if (id || !testAttribute(Qt::WA_WState_Created))
2506 if (
const QWidget *realParent = nativeParentWidget())
2507 return realParent->internalWinId();
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522QWindow *QWidget::windowHandle()
const
2525 return d->windowHandle();
2529
2530
2531
2532
2533
2534
2535QScreen *QWidget::screen()
const
2538 if (
auto associatedScreen = d->associatedScreen())
2539 return associatedScreen;
2540 if (
auto topLevel = window()) {
2541 if (
auto topData = qt_widget_private(topLevel)->topData()) {
2542 if (topData->initialScreen)
2543 return topData->initialScreen;
2545 if (
auto screenByPos = QGuiApplication::screenAt(topLevel->geometry().center()))
2548 return QGuiApplication::primaryScreen();
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563void QWidget::setScreen(QScreen *screen)
2566 d->setScreen(screen);
2569#if QT_CONFIG(style_stylesheet)
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586QString QWidget::styleSheet()
const
2591 return d->extra->styleSheet;
2594void QWidget::setStyleSheet(
const QString& styleSheet)
2597 if (data->in_destructor)
2601 QStyleSheetStyle *proxy = qt_styleSheet(d->extra->style);
2602 d->extra->styleSheet = styleSheet;
2603 if (styleSheet.isEmpty()) {
2612 bool repolish = d->polished;
2614 const auto childWidgets = findChildren<QWidget*>();
2615 for (
auto child : childWidgets) {
2616 repolish = child->d_func()->polished;
2622 proxy->repolish(
this);
2626 if (testAttribute(Qt::WA_SetStyle)) {
2627 d->setStyle_helper(
new QStyleSheetStyle(d->extra->style),
true);
2629 d->setStyle_helper(
new QStyleSheetStyle(
nullptr),
true);
2636
2637
2639QStyle *QWidget::style()
const
2643 if (d->extra && d->extra->style)
2644 return d->extra->style;
2645 return QApplication::style();
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2669void QWidget::setStyle(QStyle *style)
2672 setAttribute(Qt::WA_SetStyle, style !=
nullptr);
2674#if QT_CONFIG(style_stylesheet)
2675 if (QStyleSheetStyle *styleSheetStyle = qt_styleSheet(style)) {
2678 styleSheetStyle->ref();
2679 d->setStyle_helper(style,
false);
2680 }
else if (qt_styleSheet(d->extra->style) || !qApp->styleSheet().isEmpty()) {
2682 d->setStyle_helper(
new QStyleSheetStyle(style),
true);
2685 d->setStyle_helper(style,
false);
2688void QWidgetPrivate::setStyle_helper(QStyle *newStyle,
bool propagate)
2691 QStyle *oldStyle = q->style();
2695#if QT_CONFIG(style_stylesheet)
2696 QPointer<QStyle> origStyle = extra->style;
2698 extra->style = newStyle;
2702 oldStyle->unpolish(q);
2703 q->style()->polish(q);
2708 const QObjectList childrenList = children;
2709 for (
int i = 0; i < childrenList.size(); ++i) {
2710 QWidget *c = qobject_cast<QWidget*>(childrenList.at(i));
2712 c->d_func()->inheritStyle();
2716#if QT_CONFIG(style_stylesheet)
2717 if (!qt_styleSheet(newStyle)) {
2718 if (
const QStyleSheetStyle* cssStyle = qt_styleSheet(origStyle)) {
2719 cssStyle->clearWidgetFont(q);
2724 QEvent e(QEvent::StyleChange);
2725 QCoreApplication::sendEvent(q, &e);
2727#if QT_CONFIG(style_stylesheet)
2729 if (QStyleSheetStyle *proxy = qt_styleSheet(origStyle))
2735void QWidgetPrivate::inheritStyle()
2737#if QT_CONFIG(style_stylesheet)
2740 QStyle *extraStyle = extra ? (QStyle*)extra->style :
nullptr;
2742 QStyleSheetStyle *proxy = qt_styleSheet(extraStyle);
2744 if (!q->styleSheet().isEmpty()) {
2749 if (inheritStyleRecursionGuard)
2751 inheritStyleRecursionGuard =
true;
2752 const auto resetGuard = qScopeGuard([&]() {
2753 inheritStyleRecursionGuard =
false;
2756 QStyle *origStyle = proxy ? proxy->base : extraStyle;
2757 QWidget *parent = q->parentWidget();
2758 QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style :
nullptr;
2761 if (!qApp->styleSheet().isEmpty() || qt_styleSheet(parentStyle)) {
2762 QStyle *newStyle = parentStyle;
2763 if (q->testAttribute(Qt::WA_SetStyle) && qt_styleSheet(origStyle) ==
nullptr)
2764 newStyle =
new QStyleSheetStyle(origStyle);
2765 else if (
auto *styleSheetStyle = qt_styleSheet(origStyle))
2766 newStyle = styleSheetStyle;
2767 else if (QStyleSheetStyle *newProxy = qt_styleSheet(parentStyle))
2770 setStyle_helper(newStyle,
true);
2776 if (origStyle == extraStyle)
2782 if (!q->testAttribute(Qt::WA_SetStyle))
2783 origStyle =
nullptr;
2785 setStyle_helper(origStyle,
true);
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2841Qt::WindowModality QWidget::windowModality()
const
2843 return static_cast<Qt::WindowModality>(data->window_modality);
2846void QWidget::setWindowModality(Qt::WindowModality windowModality)
2848 data->window_modality = windowModality;
2850 setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
2851 setAttribute(Qt::WA_SetWindowModality,
true);
2854void QWidgetPrivate::setModal_sys()
2857 if (q->windowHandle())
2858 q->windowHandle()->setModality(q->windowModality());
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883bool QWidget::isMinimized()
const
2884{
return data->window_state & Qt::WindowMinimized; }
2887
2888
2889
2890
2891
2892
2893
2894void QWidget::showMinimized()
2896 bool isMin = isMinimized();
2897 if (isMin && isVisible())
2903 setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923bool QWidget::isMaximized()
const
2924{
return data->window_state & Qt::WindowMaximized; }
2929
2930
2931
2932
2933
2934
2935Qt::WindowStates QWidget::windowState()
const
2937 return Qt::WindowStates(data->window_state);
2941
2942
2943
2944
2945
2946
2947void QWidget::overrideWindowState(Qt::WindowStates newstate)
2949 QWindowStateChangeEvent e(Qt::WindowStates(data->window_state),
true);
2950 data->window_state = newstate;
2951 QCoreApplication::sendEvent(
this, &e);
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984void QWidget::setWindowState(Qt::WindowStates newstate)
2987 Qt::WindowStates oldstate = windowState();
2988 if (newstate.testFlag(Qt::WindowMinimized))
2989 newstate.setFlag(Qt::WindowActive,
false);
2990 if (oldstate == newstate)
2992 if (isWindow() && !testAttribute(Qt::WA_WState_Created))
2995 data->window_state = newstate;
2996 data->in_set_window_state = 1;
2999 if (!testAttribute(Qt::WA_Resized) && !isVisible())
3003 if (!(oldstate & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)))
3004 d->topData()->normalGeometry = geometry();
3006 Q_ASSERT(windowHandle());
3007 windowHandle()->setWindowStates(newstate & ~Qt::WindowActive);
3009 data->in_set_window_state = 0;
3011 if (newstate & Qt::WindowActive)
3014 QWindowStateChangeEvent e(oldstate);
3015 QCoreApplication::sendEvent(
this, &e);
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029bool QWidget::isFullScreen()
const
3030{
return data->window_state & Qt::WindowFullScreen; }
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068void QWidget::showFullScreen()
3072 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
3073 | Qt::WindowFullScreen);
3075#if !defined Q_OS_QNX
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091void QWidget::showMaximized()
3095 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
3096 | Qt::WindowMaximized);
3101
3102
3103
3104
3105
3106
3107void QWidget::showNormal()
3111 setWindowState(windowState() & ~(Qt::WindowMinimized
3112 | Qt::WindowMaximized
3113 | Qt::WindowFullScreen));
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3137bool QWidget::isEnabledTo(
const QWidget *ancestor)
const
3139 const QWidget * w =
this;
3140 while (!w->testAttribute(Qt::WA_ForceDisabled)
3142 && w->parentWidget()
3143 && w->parentWidget() != ancestor)
3144 w = w->parentWidget();
3145 return !w->testAttribute(Qt::WA_ForceDisabled);
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164void QWidget::addAction(QAction *action)
3166 insertAction(
nullptr, action);
3170
3171
3172
3173
3174void QWidget::addActions(
const QList<QAction *> &actions)
3176 for(
int i = 0; i < actions.size(); i++)
3177 insertAction(
nullptr, actions.at(i));
3181
3182
3183
3184
3185
3186
3187
3188
3189void QWidget::insertAction(QAction *before, QAction *action)
3191 if (Q_UNLIKELY(!action)) {
3192 qWarning(
"QWidget::insertAction: Attempt to insert null action");
3197 if (d->actions.contains(action))
3198 removeAction(action);
3200 int pos = d->actions.indexOf(before);
3203 pos = d->actions.size();
3205 d->actions.insert(pos, action);
3207 QActionPrivate *apriv = action->d_func();
3208 apriv->associatedObjects.append(
this);
3210 QActionEvent e(QEvent::ActionAdded, action, before);
3211 QCoreApplication::sendEvent(
this, &e);
3215
3216
3217
3218
3219
3220
3221
3222
3223void QWidget::insertActions(QAction *before,
const QList<QAction*> &actions)
3225 for(
int i = 0; i < actions.size(); ++i)
3226 insertAction(before, actions.at(i));
3230
3231
3232
3233void QWidget::removeAction(QAction *action)
3240 QActionPrivate *apriv = action->d_func();
3241 apriv->associatedObjects.removeAll(
this);
3243 if (d->actions.removeAll(action)) {
3244 QActionEvent e(QEvent::ActionRemoved, action);
3245 QCoreApplication::sendEvent(
this, &e);
3250
3251
3252
3253
3254QList<QAction*> QWidget::actions()
const
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276QAction *QWidget::addAction(
const QString &text)
3278 QAction *ret =
new QAction(text,
this);
3283QAction *QWidget::addAction(
const QIcon &icon,
const QString &text)
3285 QAction *ret =
new QAction(icon, text,
this);
3290#if QT_CONFIG(shortcut)
3291QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut)
3293 QAction *ret = addAction(text);
3294 ret->setShortcut(shortcut);
3298QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut)
3300 QAction *ret = addAction(icon, text);
3301 ret->setShortcut(shortcut);
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324QAction *QWidget::addAction(
const QString &text,
const QObject *receiver,
const char* member,
3325 Qt::ConnectionType type)
3327 QAction *action = addAction(text);
3328 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3332QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
3333 const QObject *receiver,
const char* member,
3334 Qt::ConnectionType type)
3336 QAction *action = addAction(icon, text);
3337 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3341#if QT_CONFIG(shortcut)
3342QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut,
3343 const QObject *receiver,
const char* member,
3344 Qt::ConnectionType type)
3346 QAction *action = addAction(text, receiver, member, type);
3347 action->setShortcut(shortcut);
3351QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut,
3352 const QObject *receiver,
const char* member,
3353 Qt::ConnectionType type)
3355 QAction *action = addAction(icon, text, receiver, member, type);
3356 action->setShortcut(shortcut);
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405void QWidget::setEnabled(
bool enable)
3409#if QT_CONFIG(accessibility)
3410 const bool wasEnabled = !testAttribute(Qt::WA_ForceDisabled);
3413 setAttribute(Qt::WA_ForceDisabled, !enable);
3414 d->setEnabled_helper(enable);
3416#if QT_CONFIG(accessibility)
3420 if (QAccessible::isActive() && wasEnabled != enable) {
3421 QAccessible::State states;
3422 states.disabled = 1;
3423 QAccessibleStateChangeEvent scEvent(
this, states);
3424 QAccessible::updateAccessibility(&scEvent);
3429void QWidgetPrivate::setEnabled_helper(
bool enable)
3433 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
3436 if (enable != q->testAttribute(Qt::WA_Disabled))
3439 q->setAttribute(Qt::WA_Disabled, !enable);
3440 updateSystemBackground();
3442 if (!enable && q->window()->focusWidget() == q) {
3443 bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
3444 if (!parentIsEnabled || !q->focusNextChild())
3448 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
3449 for (
int i = 0; i < children.size(); ++i) {
3450 QWidget *w = qobject_cast<QWidget *>(children.at(i));
3451 if (w && !w->testAttribute(attribute))
3452 w->d_func()->setEnabled_helper(enable);
3455 if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
3458 qt_qpa_set_cursor(q,
false);
3462 if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
3463 QWidget *focusWidget = effectiveFocusWidget();
3466 if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
3467 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3469 QGuiApplication::inputMethod()->commit();
3470 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3474 QEvent e(QEvent::EnabledChange);
3475 QCoreApplication::sendEvent(q, &e);
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491bool QWidget::acceptDrops()
const
3493 return testAttribute(Qt::WA_AcceptDrops);
3496void QWidget::setAcceptDrops(
bool on)
3498 setAttribute(Qt::WA_AcceptDrops, on);
3503
3504
3505
3506
3507
3508
3509
3510void QWidget::setDisabled(
bool disable)
3512 setEnabled(!disable);
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528QRect QWidget::frameGeometry()
const
3531 if (isWindow() && ! (windowType() == Qt::Popup)) {
3532 QRect fs = d->frameStrut();
3533 return QRect(data->crect.x() - fs.left(),
3534 data->crect.y() - fs.top(),
3535 data->crect.width() + fs.left() + fs.right(),
3536 data->crect.height() + fs.top() + fs.bottom());
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554int QWidget::x()
const
3557 if (isWindow() && ! (windowType() == Qt::Popup))
3558 return data->crect.x() - d->frameStrut().left();
3559 return data->crect.x();
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574int QWidget::y()
const
3577 if (isWindow() && ! (windowType() == Qt::Popup))
3578 return data->crect.y() - d->frameStrut().top();
3579 return data->crect.y();
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609QPoint QWidget::pos()
const
3612 QPoint result = data->crect.topLeft();
3613 if (isWindow() && ! (windowType() == Qt::Popup))
3614 if (!d->maybeTopData() || !d->maybeTopData()->posIncludesFrame)
3615 result -= d->frameStrut().topLeft();
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728QRect QWidget::normalGeometry()
const
3734 if (!isMaximized() && !isFullScreen())
3737 return d->topData()->normalGeometry;
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3753QRect QWidget::childrenRect()
const
3756 QRect r(0, 0, 0, 0);
3757 for (
int i = 0; i < d->children.size(); ++i) {
3758 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3759 if (w && !w->isWindow() && !w->isHidden())
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3777QRegion QWidget::childrenRegion()
const
3781 for (
int i = 0; i < d->children.size(); ++i) {
3782 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3783 if (w && !w->isWindow() && !w->isHidden()) {
3784 QRegion mask = w->mask();
3788 r |= mask.translated(w->pos());
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3812QSize QWidget::minimumSize()
const
3815 return d->extra ? QSize(d->extra->minw, d->extra->minh) : QSize(0, 0);
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3834QSize QWidget::maximumSize()
const
3837 return d->extra ? QSize(d->extra->maxw, d->extra->maxh)
3843
3844
3845
3846
3847
3848
3849
3850
3851
3854
3855
3856
3857
3858
3859
3860
3861
3862
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913QSize QWidget::sizeIncrement()
const
3916 return (d->extra && d->extra->topextra)
3917 ? QSize(d->extra->topextra->incw, d->extra->topextra->inch)
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3934QSize QWidget::baseSize()
const
3937 return (d->extra && d->extra->topextra)
3938 ? QSize(d->extra->topextra->basew, d->extra->topextra->baseh)
3942bool QWidgetPrivate::setMinimumSize_helper(
int &minw,
int &minh)
3946 int mw = minw, mh = minh;
3952 qWarning(
"QWidget::setMinimumSize: (%s/%s) "
3953 "The largest allowed size is (%d,%d)",
3954 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
3959 if (Q_UNLIKELY(minw < 0 || minh < 0)) {
3960 qWarning(
"QWidget::setMinimumSize: (%s/%s) Negative sizes (%d,%d) "
3962 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), minw, minh);
3963 minw = mw = qMax(minw, 0);
3964 minh = mh = qMax(minh, 0);
3967 if (extra->minw == mw && extra->minh == mh)
3971 extra->explicitMinSize = (mw ? Qt::Horizontal : 0) | (mh ? Qt::Vertical : 0);
3975void QWidgetPrivate::setConstraints_sys()
3978 if (extra && q->windowHandle()) {
3979 QWindow *win = q->windowHandle();
3980 QWindowPrivate *winp = qt_window_private(win);
3982 winp->minimumSize = QSize(extra->minw, extra->minh);
3983 winp->maximumSize = QSize(extra->maxw, extra->maxh);
3985 if (extra->topextra) {
3986 winp->baseSize = QSize(extra->topextra->basew, extra->topextra->baseh);
3987 winp->sizeIncrement = QSize(extra->topextra->incw, extra->topextra->inch);
3990 if (winp->platformWindow) {
3991 fixPosIncludesFrame();
3992 winp->platformWindow->propagateSizeHints();
3998
3999
4000
4001
4002
4003
4005void QWidget::setMinimumSize(
int minw,
int minh)
4008 if (!d->setMinimumSize_helper(minw, minh))
4012 d->setConstraints_sys();
4013 if (minw > width() || minh > height()) {
4014 bool resized = testAttribute(Qt::WA_Resized);
4015 bool maximized = isMaximized();
4016 resize(qMax(minw,width()), qMax(minh,height()));
4017 setAttribute(Qt::WA_Resized, resized);
4019 data->window_state = data->window_state | Qt::WindowMaximized;
4021#if QT_CONFIG(graphicsview)
4023 if (d->extra->proxyWidget)
4024 d->extra->proxyWidget->setMinimumSize(minw, minh);
4027 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4030bool QWidgetPrivate::setMaximumSize_helper(
int &maxw,
int &maxh)
4034 qWarning(
"QWidget::setMaximumSize: (%s/%s) "
4035 "The largest allowed size is (%d,%d)",
4036 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
4041 if (Q_UNLIKELY(maxw < 0 || maxh < 0)) {
4042 qWarning(
"QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) "
4044 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), maxw, maxh);
4045 maxw = qMax(maxw, 0);
4046 maxh = qMax(maxh, 0);
4049 if (extra->maxw == maxw && extra->maxh == maxh)
4053 extra->explicitMaxSize = (maxw !=
QWIDGETSIZE_MAX ? Qt::Horizontal : 0) |
4059
4060
4061
4062
4063
4064
4065void QWidget::setMaximumSize(
int maxw,
int maxh)
4068 if (!d->setMaximumSize_helper(maxw, maxh))
4072 d->setConstraints_sys();
4073 if (maxw < width() || maxh < height()) {
4074 bool resized = testAttribute(Qt::WA_Resized);
4075 resize(qMin(maxw,width()), qMin(maxh,height()));
4076 setAttribute(Qt::WA_Resized, resized);
4079#if QT_CONFIG(graphicsview)
4081 if (d->extra->proxyWidget)
4082 d->extra->proxyWidget->setMaximumSize(maxw, maxh);
4086 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4090
4091
4092
4093
4094
4095void QWidget::setSizeIncrement(
int w,
int h)
4099 QTLWExtra* x = d->topData();
4100 if (x->incw == w && x->inch == h)
4105 d->setConstraints_sys();
4109
4110
4111
4112
4113
4114void QWidget::setBaseSize(
int basew,
int baseh)
4118 QTLWExtra* x = d->topData();
4119 if (x->basew == basew && x->baseh == baseh)
4124 d->setConstraints_sys();
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4142void QWidget::setFixedSize(
const QSize & s)
4144 setFixedSize(s.width(), s.height());
4149
4150
4151
4152
4153
4155void QWidget::setFixedSize(
int w,
int h)
4158 bool minSizeSet = d->setMinimumSize_helper(w, h);
4159 bool maxSizeSet = d->setMaximumSize_helper(w, h);
4160 if (!minSizeSet && !maxSizeSet)
4164 d->setConstraints_sys();
4166 d->updateGeometry_helper(
true);
4172void QWidget::setMinimumWidth(
int w)
4176 uint expl = d->extra->explicitMinSize | (w ? Qt::Horizontal : 0);
4177 setMinimumSize(w, minimumSize().height());
4178 d->extra->explicitMinSize = expl;
4181void QWidget::setMinimumHeight(
int h)
4185 uint expl = d->extra->explicitMinSize | (h ? Qt::Vertical : 0);
4186 setMinimumSize(minimumSize().width(), h);
4187 d->extra->explicitMinSize = expl;
4190void QWidget::setMaximumWidth(
int w)
4194 uint expl = d->extra->explicitMaxSize | (w ==
QWIDGETSIZE_MAX ? 0 : Qt::Horizontal);
4195 setMaximumSize(w, maximumSize().height());
4196 d->extra->explicitMaxSize = expl;
4199void QWidget::setMaximumHeight(
int h)
4203 uint expl = d->extra->explicitMaxSize | (h ==
QWIDGETSIZE_MAX ? 0 : Qt::Vertical);
4204 setMaximumSize(maximumSize().width(), h);
4205 d->extra->explicitMaxSize = expl;
4209
4210
4211
4212
4213
4215void QWidget::setFixedWidth(
int w)
4219 uint explMin = d->extra->explicitMinSize | Qt::Horizontal;
4220 uint explMax = d->extra->explicitMaxSize | Qt::Horizontal;
4221 setMinimumSize(w, minimumSize().height());
4222 setMaximumSize(w, maximumSize().height());
4223 d->extra->explicitMinSize = explMin;
4224 d->extra->explicitMaxSize = explMax;
4229
4230
4231
4232
4233
4235void QWidget::setFixedHeight(
int h)
4239 uint explMin = d->extra->explicitMinSize | Qt::Vertical;
4240 uint explMax = d->extra->explicitMaxSize | Qt::Vertical;
4241 setMinimumSize(minimumSize().width(), h);
4242 setMaximumSize(maximumSize().width(), h);
4243 d->extra->explicitMinSize = explMin;
4244 d->extra->explicitMaxSize = explMax;
4249
4250
4251
4252
4253
4254
4255
4257QPointF QWidget::mapTo(
const QWidget *parent,
const QPointF &pos)
const
4261 const QWidget * w =
this;
4262 while (w != parent) {
4263 p = w->mapToParent(p);
4264 w = w->parentWidget();
4266 qWarning(
"QWidget::mapTo(): parent must be in parent hierarchy");
4275
4276
4277QPoint QWidget::mapTo(
const QWidget *parent,
const QPoint &pos)
const
4279 return mapTo(parent, QPointF(pos)).toPoint();
4283
4284
4285
4286
4287
4288
4289
4291QPointF QWidget::mapFrom(
const QWidget *parent,
const QPointF &pos)
const
4295 const QWidget * w =
this;
4296 while (w != parent) {
4297 p = w->mapFromParent(p);
4298 w = w->parentWidget();
4300 qWarning(
"QWidget::mapFrom(): parent must be in parent hierarchy");
4309
4310
4311QPoint QWidget::mapFrom(
const QWidget *parent,
const QPoint &pos)
const
4313 return mapFrom(parent, QPointF(pos)).toPoint();
4317
4318
4319
4320
4321
4322
4323
4324
4326QPointF QWidget::mapToParent(
const QPointF &pos)
const
4328 return pos + QPointF(data->crect.topLeft());
4332
4333
4334QPoint QWidget::mapToParent(
const QPoint &pos)
const
4336 return pos + data->crect.topLeft();
4340
4341
4342
4343
4344
4345
4346
4347
4349QPointF QWidget::mapFromParent(
const QPointF &pos)
const
4351 return pos - QPointF(data->crect.topLeft());
4355
4356
4357QPoint QWidget::mapFromParent(
const QPoint &pos)
const
4359 return pos - data->crect.topLeft();
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4376QWidget *QWidget::window()
const
4378 QWidget *w =
const_cast<QWidget *>(
this);
4379 QWidget *p = w->parentWidget();
4380 while (!w->isWindow() && p) {
4382 p = p->parentWidget();
4388
4389
4390
4391
4392
4393
4394
4395
4396QWidget *QWidget::nativeParentWidget()
const
4398 QWidget *parent = parentWidget();
4399 while (parent && !parent->internalWinId())
4400 parent = parent->parentWidget();
4405
4406
4407
4408
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423QPalette::ColorRole QWidget::backgroundRole()
const
4426 const QWidget *w =
this;
4428 QPalette::ColorRole role = w->d_func()->bg_role;
4429 if (role != QPalette::NoRole)
4431 if (w->isWindow() || w->windowType() == Qt::SubWindow)
4433 w = w->parentWidget();
4435 return QPalette::Window;
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4454void QWidget::setBackgroundRole(QPalette::ColorRole role)
4458 d->updateSystemBackground();
4459 d->propagatePaletteChange();
4460 d->updateIsOpaque();
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474QPalette::ColorRole QWidget::foregroundRole()
const
4477 QPalette::ColorRole rl = QPalette::ColorRole(d->fg_role);
4478 if (rl != QPalette::NoRole)
4480 QPalette::ColorRole role = QPalette::WindowText;
4481 switch (backgroundRole()) {
4482 case QPalette::Button:
4483 role = QPalette::ButtonText;
4485 case QPalette::Base:
4486 role = QPalette::Text;
4488 case QPalette::Dark:
4489 case QPalette::Shadow:
4490 role = QPalette::Light;
4492 case QPalette::Highlight:
4493 role = QPalette::HighlightedText;
4495 case QPalette::ToolTipBase:
4496 role = QPalette::ToolTipText;
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519void QWidget::setForegroundRole(QPalette::ColorRole role)
4523 d->updateSystemBackground();
4524 d->propagatePaletteChange();
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577const QPalette &QWidget::palette()
const
4580 data->pal.setCurrentColorGroup(QPalette::Disabled);
4581 }
else if ((!isVisible() || isActiveWindow())
4582#if defined(Q_OS_WIN)
4583 && !QApplicationPrivate::isBlockedByModal(
const_cast<QWidget *>(
this))
4586 data->pal.setCurrentColorGroup(QPalette::Active);
4588 data->pal.setCurrentColorGroup(QPalette::Inactive);
4593void QWidget::setPalette(
const QPalette &palette)
4596 setAttribute(Qt::WA_SetPalette, palette.resolveMask() != 0);
4602 QPalette naturalPalette = d->naturalWidgetPalette(d->inheritedPaletteResolveMask);
4603 QPalette resolvedPalette = palette.resolve(naturalPalette);
4604 d->setPalette_helper(resolvedPalette);
4608
4609
4610
4611
4612
4613
4614
4615
4616QPalette QWidgetPrivate::naturalWidgetPalette(QPalette::ResolveMask inheritedMask)
const
4620 const bool useStyleSheetPropagationInWidgetStyles =
4621 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4623 QPalette naturalPalette = QApplication::palette(q);
4624 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4625 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4626#if QT_CONFIG(graphicsview)
4627 || (extra && extra->proxyWidget)
4630 if (QWidget *p = q->parentWidget()) {
4631 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4632 if (!naturalPalette.isCopyOf(QGuiApplication::palette())) {
4633 QPalette inheritedPalette = p->palette();
4634 inheritedPalette.setResolveMask(inheritedMask);
4635 naturalPalette = inheritedPalette.resolve(naturalPalette);
4637 naturalPalette = p->palette();
4641#if QT_CONFIG(graphicsview)
4642 else if (extra && extra->proxyWidget) {
4643 QPalette inheritedPalette = extra->proxyWidget->palette();
4644 inheritedPalette.setResolveMask(inheritedMask);
4645 naturalPalette = inheritedPalette.resolve(naturalPalette);
4649 naturalPalette.setResolveMask(0);
4650 return naturalPalette;
4653
4654
4655
4656
4657
4658
4659
4660void QWidgetPrivate::resolvePalette()
4662 QPalette naturalPalette = naturalWidgetPalette(inheritedPaletteResolveMask);
4663 QPalette resolvedPalette = data.pal.resolve(naturalPalette);
4664 setPalette_helper(resolvedPalette);
4667void QWidgetPrivate::setPalette_helper(
const QPalette &palette)
4670 if (data.pal == palette && data.pal.resolveMask() == palette.resolveMask())
4673 updateSystemBackground();
4674 propagatePaletteChange();
4680void QWidgetPrivate::updateSystemBackground()
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4730void QWidget::setFont(
const QFont &font)
4734#if QT_CONFIG(style_stylesheet)
4735 const QStyleSheetStyle* style;
4736 if (d->extra && (style = qt_styleSheet(d->extra->style)))
4737 style->saveWidgetFont(
this, font);
4740 setAttribute(Qt::WA_SetFont, font.resolveMask() != 0);
4746 QFont naturalFont = d->naturalWidgetFont(d->inheritedFontResolveMask);
4747 QFont resolvedFont = font.resolve(naturalFont);
4748 d->setFont_helper(resolvedFont);
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763QFont QWidgetPrivate::naturalWidgetFont(uint inheritedMask)
const
4767 const bool useStyleSheetPropagationInWidgetStyles =
4768 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4770 QFont naturalFont = QApplication::font(q);
4771 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4772 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4773#if QT_CONFIG(graphicsview)
4774 || (extra && extra->proxyWidget)
4777 if (QWidget *p = q->parentWidget()) {
4778 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4779 if (!naturalFont.isCopyOf(QApplication::font())) {
4780 if (inheritedMask != 0) {
4781 QFont inheritedFont = p->font();
4782 inheritedFont.setResolveMask(inheritedMask);
4783 naturalFont = inheritedFont.resolve(naturalFont);
4786 naturalFont = p->font();
4790#if QT_CONFIG(graphicsview)
4791 else if (extra && extra->proxyWidget) {
4792 if (inheritedMask != 0) {
4793 QFont inheritedFont = extra->proxyWidget->font();
4794 inheritedFont.setResolveMask(inheritedMask);
4795 naturalFont = inheritedFont.resolve(naturalFont);
4800 naturalFont.setResolveMask(0);
4805
4806
4807
4808
4809QFont QWidgetPrivate::localFont()
const
4811 QFont localfont = data.fnt;
4812 localfont.setResolveMask(directFontResolveMask);
4817
4818
4819
4820
4821
4822
4823
4824void QWidgetPrivate::resolveFont()
4826 QFont naturalFont = naturalWidgetFont(inheritedFontResolveMask);
4827 QFont resolvedFont = localFont().resolve(naturalFont);
4828 setFont_helper(resolvedFont);
4832
4833
4834
4835
4836
4837
4838
4839
4840void QWidgetPrivate::updateFont(
const QFont &font)
4843#if QT_CONFIG(style_stylesheet)
4844 const QStyleSheetStyle* cssStyle;
4845 cssStyle = extra ? qt_styleSheet(extra->style) :
nullptr;
4846 const bool useStyleSheetPropagationInWidgetStyles =
4847 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4850 data.fnt = QFont(font, q);
4853#if QT_CONFIG(graphicsview)
4854 if (!q->parentWidget() && extra && extra->proxyWidget) {
4855 QGraphicsProxyWidget *p = extra->proxyWidget;
4856 inheritedFontResolveMask = p->d_func()->inheritedFontResolveMask | p->font().resolveMask();
4859 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
4860 inheritedFontResolveMask = 0;
4862 uint newMask = data.fnt.resolveMask() | inheritedFontResolveMask;
4866 directFontResolveMask = data.fnt.resolveMask();
4867 data.fnt.setResolveMask(newMask);
4869 for (
int i = 0; i < children.size(); ++i) {
4870 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4873#if QT_CONFIG(style_stylesheet)
4874 }
else if (!useStyleSheetPropagationInWidgetStyles && w->testAttribute(Qt::WA_StyleSheet)) {
4877 cssStyle->updateStyleSheetFont(w);
4879 }
else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
4881 QWidgetPrivate *wd = w->d_func();
4882 wd->inheritedFontResolveMask = newMask;
4888#if QT_CONFIG(style_stylesheet)
4889 if (!useStyleSheetPropagationInWidgetStyles && cssStyle) {
4890 cssStyle->updateStyleSheetFont(q);
4894 QEvent e(QEvent::FontChange);
4895 QCoreApplication::sendEvent(q, &e);
4898void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
4902 if ( (direction == Qt::RightToLeft) == q->testAttribute(Qt::WA_RightToLeft))
4904 q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
4905 if (!children.isEmpty()) {
4906 for (
int i = 0; i < children.size(); ++i) {
4907 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4908 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_SetLayoutDirection))
4909 w->d_func()->setLayoutDirection_helper(direction);
4912 QEvent e(QEvent::LayoutDirectionChange);
4913 QCoreApplication::sendEvent(q, &e);
4916void QWidgetPrivate::resolveLayoutDirection()
4919 if (!q->testAttribute(Qt::WA_SetLayoutDirection))
4920 setLayoutDirection_helper(q->isWindow() ? QGuiApplication::layoutDirection() : q->parentWidget()->layoutDirection());
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942void QWidget::setLayoutDirection(Qt::LayoutDirection direction)
4946 if (direction == Qt::LayoutDirectionAuto) {
4947 unsetLayoutDirection();
4951 setAttribute(Qt::WA_SetLayoutDirection);
4952 d->setLayoutDirection_helper(direction);
4955Qt::LayoutDirection QWidget::layoutDirection()
const
4957 return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
4960void QWidget::unsetLayoutDirection()
4963 setAttribute(Qt::WA_SetLayoutDirection,
false);
4964 d->resolveLayoutDirection();
4968
4969
4970
4971
4972
4973
4974
4977
4978
4979
4980
4981
4982
4983
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5011QCursor QWidget::cursor()
const
5014 if (testAttribute(Qt::WA_SetCursor))
5015 return (d->extra && d->extra->curs)
5017 : QCursor(Qt::ArrowCursor);
5018 if (isWindow() || !parentWidget())
5019 return QCursor(Qt::ArrowCursor);
5020 return parentWidget()->cursor();
5023void QWidget::setCursor(
const QCursor &cursor)
5026 if (cursor.shape() != Qt::ArrowCursor
5027 || (d->extra && d->extra->curs))
5030 d->extra->curs = std::make_unique<QCursor>(cursor);
5032 setAttribute(Qt::WA_SetCursor);
5033 d->setCursor_sys(cursor);
5035 QEvent event(QEvent::CursorChange);
5036 QCoreApplication::sendEvent(
this, &event);
5039void QWidgetPrivate::setCursor_sys(
const QCursor &cursor)
5043 qt_qpa_set_cursor(q,
false);
5046void QWidget::unsetCursor()
5050 d->extra->curs.reset();
5052 setAttribute(Qt::WA_SetCursor,
false);
5053 d->unsetCursor_sys();
5055 QEvent event(QEvent::CursorChange);
5056 QCoreApplication::sendEvent(
this, &event);
5059void QWidgetPrivate::unsetCursor_sys()
5062 qt_qpa_set_cursor(q,
false);
5067 if (QWindow *window = w->windowHandle())
5068 window->setCursor(c);
5073 if (QWindow *window = w->windowHandle())
5074 window->unsetCursor();
5079 if (!w->testAttribute(Qt::WA_WState_Created))
5085 }
else if (lastUnderMouse) {
5086 const WId lastWinId = lastUnderMouse->effectiveWinId();
5087 const WId winId = w->effectiveWinId();
5088 if (lastWinId && lastWinId == winId)
5090 }
else if (!w->internalWinId()) {
5094 while (!w->internalWinId() && w->parentWidget() && !w->isWindow()
5095 && !w->testAttribute(Qt::WA_SetCursor))
5096 w = w->parentWidget();
5099 if (!w->internalWinId())
5100 nativeParent = w->nativeParentWidget();
5101 if (!nativeParent || !nativeParent->internalWinId())
5104 if (w->isWindow() || w->testAttribute(Qt::WA_SetCursor)) {
5106 applyCursor(nativeParent, w->cursor());
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155void QWidget::render(QPaintDevice *target,
const QPoint &targetOffset,
5156 const QRegion &sourceRegion, RenderFlags renderFlags)
5159 render(&p, targetOffset, sourceRegion, renderFlags);
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175void QWidget::render(QPainter *painter,
const QPoint &targetOffset,
5176 const QRegion &sourceRegion, RenderFlags renderFlags)
5178 if (Q_UNLIKELY(!painter)) {
5179 qWarning(
"QWidget::render: Null pointer to painter");
5183 if (Q_UNLIKELY(!painter->isActive())) {
5184 qWarning(
"QWidget::render: Cannot render with an inactive painter");
5188 const qreal opacity = painter->opacity();
5189 if (qFuzzyIsNull(opacity))
5193 const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
5194 const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
5196 if (toBePainted.isEmpty())
5201 d->extra->inRenderWithPainter =
true;
5203 QPaintEngine *engine = painter->paintEngine();
5205 QPaintEnginePrivate *enginePriv = engine->d_func();
5206 Q_ASSERT(enginePriv);
5207 QPaintDevice *target = engine->paintDevice();
5211 if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
5212 d->render_helper(painter, targetOffset, toBePainted, renderFlags);
5213 d->extra->inRenderWithPainter = inRenderWithPainter;
5218 QPainter *oldPainter = d->sharedPainter();
5219 d->setSharedPainter(painter);
5222 const QTransform oldTransform = enginePriv->systemTransform;
5223 const QRegion oldSystemClip = enginePriv->systemClip;
5224 const QRegion oldBaseClip = enginePriv->baseSystemClip;
5225 const QRegion oldSystemViewport = enginePriv->systemViewport;
5226 const Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection();
5229 if (painter->hasClipping()) {
5230 const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
5231 enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
5233 enginePriv->setSystemViewport(oldSystemClip);
5235 painter->setLayoutDirection(layoutDirection());
5237 d->render(target, targetOffset, toBePainted, renderFlags);
5240 enginePriv->baseSystemClip = oldBaseClip;
5241 enginePriv->setSystemTransformAndViewport(oldTransform, oldSystemViewport);
5242 enginePriv->systemStateChanged();
5243 painter->setLayoutDirection(oldLayoutDirection);
5246 d->setSharedPainter(oldPainter);
5248 d->extra->inRenderWithPainter = inRenderWithPainter;
5253 QResizeEvent e(target->size(), QSize());
5254 QCoreApplication::sendEvent(target, &e);
5256 const QObjectList children = target->children();
5257 for (
int i = 0; i < children.size(); ++i) {
5258 if (!children.at(i)->isWidgetType())
5261 if (!child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278QPixmap QWidget::grab(
const QRect &rectangle)
5281 if (testAttribute(Qt::WA_PendingResizeEvent) || !testAttribute(Qt::WA_WState_Created))
5282 sendResizeEvents(
this);
5284 const QWidget::RenderFlags renderFlags = QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask;
5286 const bool oldDirtyOpaqueChildren = d->dirtyOpaqueChildren;
5288 if (r.width() < 0 || r.height() < 0) {
5291 r = d->prepareToRender(QRegion(), renderFlags).boundingRect();
5292 r.setTopLeft(rectangle.topLeft());
5295 if (!r.intersects(rect()))
5298 const qreal dpr = devicePixelRatio();
5299 QPixmap res((QSizeF(r.size()) * dpr).toSize());
5300 res.setDevicePixelRatio(dpr);
5302 res.fill(Qt::transparent);
5303 d->render(&res, QPoint(), QRegion(r), renderFlags);
5305 d->dirtyOpaqueChildren = oldDirtyOpaqueChildren;
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319#if QT_CONFIG(graphicseffect)
5320QGraphicsEffect *QWidget::graphicsEffect()
const
5323 return d->graphicsEffect;
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349#if QT_CONFIG(graphicseffect)
5350void QWidget::setGraphicsEffect(QGraphicsEffect *effect)
5353 if (d->graphicsEffect == effect)
5356 if (d->graphicsEffect) {
5357 d->invalidateBackingStore(rect());
5358 delete d->graphicsEffect;
5359 d->graphicsEffect =
nullptr;
5364 QGraphicsEffectSourcePrivate *sourced =
new QWidgetEffectSourcePrivate(
this);
5365 QGraphicsEffectSource *source =
new QGraphicsEffectSource(*sourced);
5366 d->graphicsEffect = effect;
5367 effect->d_func()->setGraphicsEffectSource(source);
5371 d->updateIsOpaque();
5375bool QWidgetPrivate::isAboutToShow()
const
5385 QWidget *parent = q->parentWidget();
5386 return parent ? parent->d_func()->isAboutToShow() :
false;
5389QRegion QWidgetPrivate::prepareToRender(
const QRegion ®ion, QWidget::RenderFlags renderFlags)
5392 const bool isVisible = q->isVisible();
5395 if (!isVisible && !isAboutToShow()) {
5396 QWidget *topLevel = q->window();
5397 (
void)topLevel->d_func()->topData();
5398 topLevel->ensurePolished();
5402 QWidget *widget = q;
5403 QWidgetList hiddenWidgets;
5405 if (widget->isHidden()) {
5406 widget->setAttribute(Qt::WA_WState_Hidden,
false);
5407 hiddenWidgets.append(widget);
5408 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5409 widget->d_func()->updateGeometry_helper(
true);
5411 widget = widget->parentWidget();
5415 if (topLevel->d_func()->layout)
5416 topLevel->d_func()->layout->activate();
5419 QTLWExtra *topLevelExtra = topLevel->d_func()->maybeTopData();
5420 if (topLevelExtra && !topLevelExtra->sizeAdjusted
5421 && !topLevel->testAttribute(Qt::WA_Resized)) {
5422 topLevel->adjustSize();
5423 topLevel->setAttribute(Qt::WA_Resized,
false);
5427 topLevel->d_func()->activateChildLayoutsRecursively();
5430 for (
int i = 0; i < hiddenWidgets.size(); ++i) {
5431 QWidget *widget = hiddenWidgets.at(i);
5432 widget->setAttribute(Qt::WA_WState_Hidden);
5433 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5434 widget->parentWidget()->d_func()->layout->invalidate();
5436 }
else if (isVisible) {
5437 q->window()->d_func()->sendPendingMoveAndResizeEvents(
true,
true);
5441 QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
5442 if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
5443 toBePainted &= extra->mask;
5447void QWidgetPrivate::render_helper(QPainter *painter,
const QPoint &targetOffset,
const QRegion &toBePainted,
5448 QWidget::RenderFlags renderFlags)
5451 Q_ASSERT(!toBePainted.isEmpty());
5454 const QTransform originalTransform = painter->worldTransform();
5455 const bool useDeviceCoordinates = originalTransform.isScaling();
5456 if (!useDeviceCoordinates) {
5458 const QRect rect = toBePainted.boundingRect();
5459 const QSize size = rect.size();
5463 const qreal pixmapDevicePixelRatio = painter->device()->devicePixelRatio();
5464 QPixmap pixmap(size * pixmapDevicePixelRatio);
5465 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio);
5467 if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
5468 pixmap.fill(Qt::transparent);
5469 q->render(&pixmap, QPoint(), toBePainted, renderFlags);
5471 const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
5472 painter->setRenderHints(QPainter::SmoothPixmapTransform,
true);
5474 painter->drawPixmap(targetOffset, pixmap);
5477 painter->setRenderHints(QPainter::SmoothPixmapTransform,
false);
5481 QTransform transform = originalTransform;
5482 transform.translate(targetOffset.x(), targetOffset.y());
5484 QPaintDevice *device = painter->device();
5488 const QRectF rect(toBePainted.boundingRect());
5489 QRect deviceRect = transform.mapRect(QRectF(0, 0, rect.width(), rect.height())).toAlignedRect();
5490 deviceRect &= QRect(0, 0, device->width(), device->height());
5492 QPixmap pixmap(deviceRect.size());
5493 pixmap.fill(Qt::transparent);
5496 QPainter pixmapPainter(&pixmap);
5497 pixmapPainter.setRenderHints(painter->renderHints());
5498 transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
5499 pixmapPainter.setTransform(transform);
5501 q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
5502 pixmapPainter.end();
5505 painter->setTransform(QTransform());
5506 painter->drawPixmap(deviceRect.topLeft(), pixmap);
5507 painter->setTransform(originalTransform);
5511void QWidgetPrivate::drawWidget(QPaintDevice *pdev,
const QRegion &rgn,
const QPoint &offset, DrawWidgetFlags flags,
5512 QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5519 qCInfo(lcWidgetPainting) <<
"Drawing" << rgn <<
"of" << q <<
"at" << offset
5520 <<
"into paint device" << pdev <<
"with" << flags;
5522 const bool asRoot = flags & DrawAsRoot;
5523 bool onScreen = shouldPaintOnScreen();
5525#if QT_CONFIG(graphicseffect)
5526 if (graphicsEffect && graphicsEffect->isEnabled()) {
5527 QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
5528 QWidgetEffectSourcePrivate *sourced =
static_cast<QWidgetEffectSourcePrivate *>
5530 if (!sourced->context) {
5531 const QRegion effectRgn((flags & UseEffectRegionBounds) ? rgn.boundingRect() : rgn);
5532 QWidgetPaintContext context(pdev, effectRgn, offset, flags, sharedPainter, repaintManager);
5533 sourced->context = &context;
5534 if (!sharedPainter) {
5535 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), effectRgn.translated(offset));
5537 p.translate(offset);
5538 context.painter = &p;
5539 graphicsEffect->draw(&p);
5540 setSystemClip(pdev->paintEngine(), 1, QRegion());
5542 context.painter = sharedPainter;
5543 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
5544 sourced->invalidateCache();
5545 sourced->lastEffectTransform = sharedPainter->worldTransform();
5547 sharedPainter->save();
5548 sharedPainter->translate(offset);
5549 setSystemClip(sharedPainter->paintEngine(), sharedPainter->device()->devicePixelRatio(), effectRgn.translated(offset));
5550 graphicsEffect->draw(sharedPainter);
5551 setSystemClip(sharedPainter->paintEngine(), 1, QRegion());
5552 sharedPainter->restore();
5554 sourced->context =
nullptr;
5557 repaintManager->markNeedsFlush(q, effectRgn, offset);
5563 flags = flags & ~UseEffectRegionBounds;
5565 const bool alsoOnScreen = flags & DrawPaintOnScreen;
5566 const bool recursive = flags & DrawRecursive;
5567 const bool alsoInvisible = flags & DrawInvisible;
5569 Q_ASSERT(sharedPainter ? sharedPainter->isActive() :
true);
5571 QRegion toBePainted(rgn);
5572 if (asRoot && !alsoInvisible)
5573 toBePainted &= clipRect();
5574 if (!(flags & DontSubtractOpaqueChildren))
5575 subtractOpaqueChildren(toBePainted, q->rect());
5577 if (!toBePainted.isEmpty()) {
5578 if (!onScreen || alsoOnScreen) {
5580 if (Q_UNLIKELY(q->testAttribute(Qt::WA_WState_InPaintEvent)))
5581 qWarning(
"QWidget::repaint: Recursive repaint detected");
5582 q->setAttribute(Qt::WA_WState_InPaintEvent);
5585 QPaintEngine *paintEngine = pdev->paintEngine();
5587 setRedirected(pdev, -offset);
5590 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted);
5592 paintEngine->d_func()->systemRect = q->data->crect;
5595 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
5596 && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
5597 beginBackingStorePainting();
5599 p.setRenderHint(QPainter::SmoothPixmapTransform);
5600 paintBackground(&p, toBePainted, (asRoot || onScreen) ? (flags | DrawAsRoot) : DrawWidgetFlags());
5601 endBackingStorePainting();
5605 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted.translated(offset));
5607 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
5608 beginBackingStorePainting();
5610 QColor tint = q->palette().window().color();
5611 tint.setAlphaF(.6f);
5612 p.fillRect(toBePainted.boundingRect(), tint);
5613 endBackingStorePainting();
5618 qDebug() <<
"painting" << q <<
"opaque ==" << isOpaque();
5619 qDebug() <<
"clipping to" << toBePainted <<
"location == " << offset
5620 <<
"geometry ==" << QRect(q->mapTo(q->window(), QPoint(0, 0)), q->size());
5623 bool skipPaintEvent =
false;
5624 if (renderToTexture) {
5627 beginBackingStorePainting();
5628 if (!q->testAttribute(Qt::WA_AlwaysStackOnTop) && repaintManager) {
5630 p.setCompositionMode(QPainter::CompositionMode_Source);
5631 p.fillRect(q->rect(), Qt::transparent);
5632 }
else if (!repaintManager) {
5634 QImage img = grabFramebuffer();
5637 if (img.format() == QImage::Format_RGB32)
5638 img.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied);
5640 p.drawImage(q->rect(), img);
5641 skipPaintEvent =
true;
5643 endBackingStorePainting();
5644 if (renderToTextureReallyDirty)
5645 renderToTextureReallyDirty = 0;
5647 skipPaintEvent =
true;
5650 if (!skipPaintEvent) {
5652 sendPaintEvent(toBePainted);
5656 repaintManager->markNeedsFlush(q, toBePainted, offset);
5660 restoreRedirected();
5662 paintEngine->d_func()->systemRect = QRect();
5664 paintEngine->d_func()->currentClipDevice =
nullptr;
5666 setSystemClip(pdev->paintEngine(), 1, QRegion());
5668 q->setAttribute(Qt::WA_WState_InPaintEvent,
false);
5669 if (Q_UNLIKELY(q->paintingActive()))
5670 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
5672 if (paintEngine && paintEngine->autoDestruct()) {
5675 }
else if (q->isWindow()) {
5676 QPaintEngine *engine = pdev->paintEngine();
5679 p.setClipRegion(toBePainted);
5680 const QBrush bg = q->palette().brush(QPalette::Window);
5681 if (bg.style() == Qt::TexturePattern)
5682 p.drawTiledPixmap(q->rect(), bg.texture());
5684 p.fillRect(q->rect(), bg);
5686 if (engine->autoDestruct())
5692 if (recursive && !children.isEmpty()) {
5693 paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot,
5694 sharedPainter, repaintManager);
5698void QWidgetPrivate::sendPaintEvent(
const QRegion &toBePainted)
5701 QPaintEvent e(toBePainted);
5702 QCoreApplication::sendSpontaneousEvent(q, &e);
5704 if (renderToTexture)
5708void QWidgetPrivate::render(QPaintDevice *target,
const QPoint &targetOffset,
5709 const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
5711 if (Q_UNLIKELY(!target)) {
5712 qWarning(
"QWidget::render: null pointer to paint device");
5716 const bool inRenderWithPainter = extra && extra->inRenderWithPainter;
5717 QRegion paintRegion = !inRenderWithPainter
5718 ? prepareToRender(sourceRegion, renderFlags)
5720 if (paintRegion.isEmpty())
5723 QPainter *oldSharedPainter = inRenderWithPainter ? sharedPainter() :
nullptr;
5727 if (target->devType() == QInternal::Widget) {
5728 QWidgetPrivate *targetPrivate =
static_cast<QWidget *>(target)->d_func();
5729 if (targetPrivate->extra && targetPrivate->extra->inRenderWithPainter) {
5730 QPainter *targetPainter = targetPrivate->sharedPainter();
5731 if (targetPainter && targetPainter->isActive())
5732 setSharedPainter(targetPainter);
5739 QPoint offset = targetOffset;
5740 offset -= paintRegion.boundingRect().topLeft();
5741 QPoint redirectionOffset;
5742 QPaintDevice *redirected =
nullptr;
5744 if (target->devType() == QInternal::Widget)
5745 redirected =
static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
5748 target = redirected;
5749 offset -= redirectionOffset;
5752 if (!inRenderWithPainter) {
5753 if (QPaintEngine *targetEngine = target->paintEngine()) {
5754 const QRegion targetSystemClip = targetEngine->systemClip();
5755 if (!targetSystemClip.isEmpty())
5756 paintRegion &= targetSystemClip.translated(-offset);
5761 DrawWidgetFlags flags = DrawPaintOnScreen | DrawInvisible;
5762 if (renderFlags & QWidget::DrawWindowBackground)
5763 flags |= DrawAsRoot;
5765 if (renderFlags & QWidget::DrawChildren)
5766 flags |= DrawRecursive;
5768 flags |= DontSubtractOpaqueChildren;
5770 flags |= DontSetCompositionMode;
5773 drawWidget(target, paintRegion, offset, flags, sharedPainter());
5776 if (oldSharedPainter)
5777 setSharedPainter(oldSharedPainter);
5780void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev,
const QObjectList& siblings,
int index,
const QRegion &rgn,
5781 const QPoint &offset, DrawWidgetFlags flags
5782 , QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5784 QWidget *w =
nullptr;
5786 bool dirtyBoundingRect =
true;
5787 const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
5788 const bool excludeNativeChildren = (flags & DontDrawNativeChildren);
5791 QWidget *x = qobject_cast<QWidget*>(siblings.at(index));
5792 if (x && !(exludeOpaqueChildren && x->d_func()->isOpaque) && !x->isHidden() && !x->isWindow()
5793 && !(excludeNativeChildren && x->internalWinId())) {
5794 if (dirtyBoundingRect) {
5795 boundingRect = rgn.boundingRect();
5796 dirtyBoundingRect =
false;
5799 if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
5805 }
while (index >= 0);
5810 QWidgetPrivate *wd = w->d_func();
5811 const QPoint widgetPos(w->data->crect.topLeft());
5812 const bool hasMask = wd->extra && wd->extra->hasMask && !wd->graphicsEffect;
5816 wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
5817 paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags,
5818 sharedPainter, repaintManager);
5821 if (w->updatesEnabled()
5822#if QT_CONFIG(graphicsview)
5823 && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
5826 QRegion wRegion(rgn);
5827 wRegion &= wd->effectiveRectFor(w->data->crect);
5828 wRegion.translate(-widgetPos);
5830 wRegion &= wd->extra->mask;
5831 wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, repaintManager);
5835#if QT_CONFIG(graphicseffect)
5836QRectF QWidgetEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system)
const
5838 if (system != Qt::DeviceCoordinates)
5839 return m_widget->rect();
5841 if (Q_UNLIKELY(!context)) {
5843 qWarning(
"QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
5847 return context->painter->worldTransform().mapRect(m_widget->rect());
5850void QWidgetEffectSourcePrivate::draw(QPainter *painter)
5852 if (!context || context->painter != painter) {
5853 m_widget->render(painter);
5859 QRegion toBePainted = context->rgn;
5860 toBePainted &= m_widget->rect();
5861 QWidgetPrivate *wd = qt_widget_private(m_widget);
5862 if (wd->extra && wd->extra->hasMask)
5863 toBePainted &= wd->extra->mask;
5865 wd->drawWidget(context->pdev, toBePainted, context->offset, context->flags,
5866 context->sharedPainter, context->repaintManager);
5869QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
5870 QGraphicsEffect::PixmapPadMode mode)
const
5872 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
5873 if (Q_UNLIKELY(!context && deviceCoordinates)) {
5875 qWarning(
"QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
5879 QPoint pixmapOffset;
5880 QRectF sourceRect = m_widget->rect();
5882 if (deviceCoordinates) {
5883 const QTransform &painterTransform = context->painter->worldTransform();
5884 sourceRect = painterTransform.mapRect(sourceRect);
5885 pixmapOffset = painterTransform.map(pixmapOffset);
5890 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect)
5891 effectRect = m_widget->graphicsEffect()->boundingRectFor(sourceRect).toAlignedRect();
5892 else if (mode == QGraphicsEffect::PadToTransparentBorder)
5893 effectRect = sourceRect.adjusted(-1, -1, 1, 1).toAlignedRect();
5895 effectRect = sourceRect.toAlignedRect();
5898 *offset = effectRect.topLeft();
5900 pixmapOffset -= effectRect.topLeft();
5903 if (
const auto *paintDevice = context->painter->device())
5904 dpr = paintDevice->devicePixelRatio();
5906 qWarning(
"QWidgetEffectSourcePrivate::pixmap: Painter not active");
5907 QPixmap pixmap(effectRect.size() * dpr);
5908 pixmap.setDevicePixelRatio(dpr);
5910 pixmap.fill(Qt::transparent);
5911 m_widget->render(&pixmap, pixmapOffset, QRegion(), QWidget::DrawChildren);
5916#if QT_CONFIG(graphicsview)
5918
5919
5920
5921
5922
5923
5924
5925QGraphicsProxyWidget *QWidgetPrivate::nearestGraphicsProxyWidget(
const QWidget *origin)
5928 const auto &extra = origin->d_func()->extra;
5929 if (extra && extra->proxyWidget)
5930 return extra->proxyWidget;
5931 return nearestGraphicsProxyWidget(origin->parentWidget());
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5952void QWidgetPrivate::setLocale_helper(
const QLocale &loc,
bool forceUpdate)
5955 if (locale == loc && !forceUpdate)
5960 if (!children.isEmpty()) {
5961 for (
int i = 0; i < children.size(); ++i) {
5962 QWidget *w = qobject_cast<QWidget*>(children.at(i));
5965 if (w->testAttribute(Qt::WA_SetLocale))
5967 if (w->isWindow() && !w->testAttribute(Qt::WA_WindowPropagation))
5969 w->d_func()->setLocale_helper(loc, forceUpdate);
5972 QEvent e(QEvent::LocaleChange);
5973 QCoreApplication::sendEvent(q, &e);
5976void QWidget::setLocale(
const QLocale &locale)
5980 setAttribute(Qt::WA_SetLocale);
5981 d->setLocale_helper(locale);
5984QLocale QWidget::locale()
const
5991void QWidgetPrivate::resolveLocale()
5995 if (!q->testAttribute(Qt::WA_SetLocale)) {
5996 QWidget *parent = q->parentWidget();
5997 setLocale_helper(!parent || (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
5998 ? QLocale() : parent->locale());
6002void QWidget::unsetLocale()
6005 setAttribute(Qt::WA_SetLocale,
false);
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032QString QWidget::windowTitle()
const
6035 if (d->extra && d->extra->topextra) {
6036 if (!d->extra->topextra->caption.isEmpty())
6037 return d->extra->topextra->caption;
6038 if (!d->extra->topextra->filePath.isEmpty())
6039 return QFileInfo(d->extra->topextra->filePath).fileName() +
"[*]"_L1;
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6059 QString cap = title;
6063 const auto placeHolder =
"[*]"_L1;
6064 int index = cap.indexOf(placeHolder);
6067 while (index != -1) {
6068 index += placeHolder.size();
6070 while (cap.indexOf(placeHolder, index) == index) {
6072 index += placeHolder.size();
6076 int lastIndex = cap.lastIndexOf(placeHolder, index - 1);
6077 if (widget->isWindowModified()
6078 && widget->style()->styleHint(QStyle::SH_TitleBar_ModifyNotification,
nullptr, widget))
6079 cap.replace(lastIndex, 3, QWidget::tr(
"*"));
6081 cap.remove(lastIndex, 3);
6084 index = cap.indexOf(placeHolder, index);
6087 cap.replace(
"[*][*]"_L1, placeHolder);
6092void QWidgetPrivate::setWindowTitle_helper(
const QString &title)
6095 if (q->testAttribute(Qt::WA_WState_Created))
6096 setWindowTitle_sys(qt_setWindowTitle_helperHelper(title, q));
6099void QWidgetPrivate::setWindowTitle_sys(
const QString &caption)
6105 if (QWindow *window = q->windowHandle())
6107#if QT_CONFIG(accessibility)
6108 QString oldAccessibleName;
6109 const QAccessibleInterface *accessible = QAccessible::isActive()
6110 ? QAccessible::queryAccessibleInterface(q)
6113 oldAccessibleName = accessible->text(QAccessible::Name);
6116 window->setTitle(caption);
6118#if QT_CONFIG(accessibility)
6119 if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) {
6120 QAccessibleEvent event(q, QAccessible::NameChanged);
6121 QAccessible::updateAccessibility(&event);
6127void QWidgetPrivate::setWindowIconText_helper(
const QString &title)
6130 if (q->testAttribute(Qt::WA_WState_Created))
6131 setWindowIconText_sys(qt_setWindowTitle_helperHelper(title, q));
6134void QWidgetPrivate::setWindowIconText_sys(
const QString &iconText)
6140 if (QWindow *window = q->windowHandle()) {
6141 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(window->handle()))
6142 xcbWindow->setWindowIconText(iconText);
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6161void QWidget::setWindowIconText(
const QString &iconText)
6163 if (QWidget::windowIconText() == iconText)
6167 d->topData()->iconText = iconText;
6168 d->setWindowIconText_helper(iconText);
6170 QEvent e(QEvent::IconTextChange);
6171 QCoreApplication::sendEvent(
this, &e);
6173 emit windowIconTextChanged(iconText);
6177
6178
6179
6180
6181
6182
6183
6185void QWidget::setWindowTitle(
const QString &title)
6187 if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
6191 d->topData()->caption = title;
6192 d->setWindowTitle_helper(title);
6194 QEvent e(QEvent::WindowTitleChange);
6195 QCoreApplication::sendEvent(
this, &e);
6197 emit windowTitleChanged(title);
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215QIcon QWidget::windowIcon()
const
6217 const QWidget *w =
this;
6219 const QWidgetPrivate *d = w->d_func();
6220 if (d->extra && d->extra->topextra && d->extra->topextra->icon)
6221 return *d->extra->topextra->icon;
6222 w = w->parentWidget();
6224 return QApplication::windowIcon();
6227void QWidgetPrivate::setWindowIcon_helper()
6230 QEvent e(QEvent::WindowIconChange);
6236 if (!q->windowHandle())
6237 QCoreApplication::sendEvent(q, &e);
6238 for (
int i = 0; i < children.size(); ++i) {
6239 QWidget *w = qobject_cast<QWidget *>(children.at(i));
6240 if (w && !w->isWindow())
6241 QCoreApplication::sendEvent(w, &e);
6246
6247
6248
6249
6250
6251
6252
6254void QWidget::setWindowIcon(
const QIcon &icon)
6258 setAttribute(Qt::WA_SetWindowIcon, !icon.isNull());
6261 if (!d->extra->topextra->icon)
6262 d->extra->topextra->icon = std::make_unique<QIcon>(icon);
6264 *d->extra->topextra->icon = icon;
6266 d->setWindowIcon_sys();
6267 d->setWindowIcon_helper();
6269 emit windowIconChanged(icon);
6272void QWidgetPrivate::setWindowIcon_sys()
6275 if (QWindow *window = q->windowHandle())
6276 window->setIcon(q->windowIcon());
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6294QString QWidget::windowIconText()
const
6297 return (d->extra && d->extra->topextra) ? d->extra->topextra->iconText : QString();
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6324QString QWidget::windowFilePath()
const
6327 return (d->extra && d->extra->topextra) ? d->extra->topextra->filePath : QString();
6330void QWidget::setWindowFilePath(
const QString &filePath)
6332 if (filePath == windowFilePath())
6338 d->extra->topextra->filePath = filePath;
6339 d->setWindowFilePath_helper(filePath);
6342void QWidgetPrivate::setWindowFilePath_helper(
const QString &filePath)
6344 if (extra->topextra && extra->topextra->caption.isEmpty()) {
6346 setWindowTitle_helper(QFileInfo(filePath).fileName());
6350 setWindowTitle_helper(q->windowTitle());
6354 setWindowFilePath_sys(filePath);
6358void QWidgetPrivate::setWindowFilePath_sys(
const QString &filePath)
6364 if (QWindow *window = q->windowHandle())
6365 window->setFilePath(filePath);
6369
6370
6371
6372
6374QString QWidget::windowRole()
const
6377 return (d->extra && d->extra->topextra) ? d->extra->topextra->role : QString();
6381
6382
6383
6384void QWidget::setWindowRole(
const QString &role)
6386#if QT_CONFIG(xcb) || QT_CONFIG(wayland)
6389 d->topData()->role = role;
6394 if (windowHandle()) {
6396 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(windowHandle()->handle()))
6397 xcbWindow->setWindowRole(role);
6399#if QT_CONFIG(wayland)
6400 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(windowHandle()->handle()))
6401 waylandWindow->setSessionRestoreId(role);
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6455void QWidget::setFocusProxy(QWidget * w)
6458 if (!w && !d->extra)
6461 for (QWidget* fp = w; fp; fp = fp->focusProxy()) {
6462 if (Q_UNLIKELY(fp ==
this)) {
6463 qWarning(
"QWidget: %s (%s) already in focus proxy chain", metaObject()->className(), objectName().toLocal8Bit().constData());
6468 const bool moveFocusToProxy = (QApplicationPrivate::focus_widget ==
this);
6471 d->extra->focus_proxy = w;
6473 if (w && isAncestorOf(w)) {
6479 const QWidget *parentOfW = w->parentWidget();
6480 Q_ASSERT(parentOfW);
6481 QWidget *firstChild =
nullptr;
6482 const auto childList = children();
6483 for (QObject *child : childList) {
6484 if ((firstChild = qobject_cast<QWidget *>(child)))
6487 Q_ASSERT(firstChild);
6488 d->insertIntoFocusChainBefore(firstChild);
6489 }
else if (w && w->isAncestorOf(
this)) {
6494 QWidget *parentsNext = w->nextInFocusChain();
6495 if (parentsNext ==
this) {
6497 Q_ASSERT(previousInFocusChain() == w);
6499 d->QWidgetPrivate::insertIntoFocusChainAfter(w);
6503 if (moveFocusToProxy)
6504 setFocus(Qt::OtherFocusReason);
6509
6510
6511
6512
6514QWidget *QWidget::focusProxy()
const
6517 return d->extra ? d->extra->focus_proxy.data() :
nullptr;
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533bool QWidget::hasFocus()
const
6535 const QWidget* w =
this;
6536 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
6537 w = w->d_func()->extra->focus_proxy;
6538#if QT_CONFIG(graphicsview)
6539 if (QWidget *window = w->window()) {
6540 const auto &e = window->d_func()->extra;
6541 if (e && e->proxyWidget && e->proxyWidget->hasFocus() && window->focusWidget() == w)
6545 return (QApplication::focusWidget() == w);
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6583void QWidget::setFocus(Qt::FocusReason reason)
6588 QWidget *f = d_func()->deepestFocusProxy();
6592 if (QApplication::focusWidget() == f)
6595#if QT_CONFIG(graphicsview)
6596 QWidget *previousProxyFocus =
nullptr;
6597 if (
const auto &topData = window()->d_func()->extra) {
6598 if (topData->proxyWidget && topData->proxyWidget->hasFocus()) {
6599 previousProxyFocus = topData->proxyWidget->widget()->focusWidget();
6600 if (previousProxyFocus && previousProxyFocus->focusProxy())
6601 previousProxyFocus = previousProxyFocus->focusProxy();
6602 if (previousProxyFocus == f && !topData->proxyWidget->d_func()->proxyIsGivingFocus)
6608#if QT_CONFIG(graphicsview)
6610 if (
const auto &topData = window()->d_func()->extra) {
6611 if (topData->proxyWidget && !topData->proxyWidget->hasFocus()) {
6612 f->d_func()->updateFocusChild();
6613 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 1;
6614 topData->proxyWidget->setFocus(reason);
6615 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 0;
6620 if (f->isActiveWindow()) {
6621 QWidget *prev = QApplicationPrivate::focus_widget;
6623 if (reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
6624 && prev->testAttribute(Qt::WA_InputMethodEnabled)) {
6625 QGuiApplication::inputMethod()->commit();
6628 if (reason != Qt::NoFocusReason) {
6629 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason);
6630 QCoreApplication::sendEvent(prev, &focusAboutToChange);
6634 f->d_func()->updateFocusChild();
6636 QApplicationPrivate::setFocusWidget(f, reason);
6637#if QT_CONFIG(accessibility)
6639 if (!(f->inherits(
"QMenuBar") || f->inherits(
"QMenu") || f->inherits(
"QMenuItem")))
6641 QAccessibleEvent event(f, QAccessible::Focus);
6642 QAccessible::updateAccessibility(&event);
6645#if QT_CONFIG(graphicsview)
6646 if (
const auto &topData = window()->d_func()->extra) {
6647 if (topData->proxyWidget) {
6648 if (previousProxyFocus && previousProxyFocus != f) {
6650 QFocusEvent event(QEvent::FocusOut, reason);
6651 QPointer<QWidget> that = previousProxyFocus;
6652 QCoreApplication::sendEvent(previousProxyFocus, &event);
6654 QCoreApplication::sendEvent(that->style(), &event);
6657#if QT_CONFIG(graphicsview)
6659 if (
const auto &topData = window()->d_func()->extra)
6660 if (topData->proxyWidget && topData->proxyWidget->hasFocus())
6661 topData->proxyWidget->d_func()->updateProxyInputMethodAcceptanceFromWidget();
6664 QFocusEvent event(QEvent::FocusIn, reason);
6665 QPointer<QWidget> that = f;
6666 QCoreApplication::sendEvent(f, &event);
6668 QCoreApplication::sendEvent(that->style(), &event);
6674 f->d_func()->updateFocusChild();
6680
6681
6682
6683
6684
6685QWidget *QWidgetPrivate::deepestFocusProxy()
const
6689 QWidget *focusProxy = q->focusProxy();
6693 while (QWidget *nextFocusProxy = focusProxy->focusProxy())
6694 focusProxy = nextFocusProxy;
6701 const auto platformWindow = w->handle();
6702 return platformWindow && platformWindow->isEmbedded();
6705void QWidgetPrivate::setFocus_sys()
6710 if (extra && extra->hasWindowContainer)
6714 if (QWindow *nativeWindow = q->testAttribute(Qt::WA_WState_Created) ? q->window()->windowHandle() :
nullptr) {
6715 if (nativeWindow->type() != Qt::Popup && nativeWindow != QGuiApplication::focusWindow()
6716 && (QGuiApplication::applicationState() == Qt::ApplicationActive
6717 || QCoreApplication::testAttribute(Qt::AA_PluginApplication)
6718 || isEmbedded(nativeWindow))) {
6719 nativeWindow->requestActivate();
6725void QWidgetPrivate::updateFocusChild()
6730 if (q->isHidden()) {
6731 while (w && w->isHidden()) {
6732 w->d_func()->focus_child = q;
6733 w = w->isWindow() ?
nullptr : w->parentWidget();
6737 w->d_func()->focus_child = q;
6738 w = w->isWindow() ?
nullptr : w->parentWidget();
6742 if (QTLWExtra *extra = q->window()->d_func()->maybeTopData()) {
6744 emit extra->window->focusObjectChanged(q);
6749
6750
6751
6752
6753
6754
6755
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6770void QWidget::clearFocus()
6773 if (testAttribute(Qt::WA_InputMethodEnabled))
6774 QGuiApplication::inputMethod()->commit();
6776 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange);
6777 QCoreApplication::sendEvent(
this, &focusAboutToChange);
6780 QTLWExtra *extra = window()->d_func()->maybeTopData();
6781 QObject *originalFocusObject =
nullptr;
6782 if (extra && extra->window) {
6783 originalFocusObject = extra->window->focusObject();
6786 if (!originalFocusObject)
6787 originalFocusObject = focusWidget();
6793 if (w->d_func()->focus_child ==
this)
6794 w->d_func()->focus_child =
nullptr;
6795 w = w->parentWidget();
6802 if (originalFocusObject && originalFocusObject != extra->window->focusObject())
6803 emit extra->window->focusObjectChanged(extra->window->focusObject());
6805#if QT_CONFIG(graphicsview)
6806 const auto &topData = d_func()->extra;
6807 if (topData && topData->proxyWidget)
6808 topData->proxyWidget->clearFocus();
6813 QApplicationPrivate::setFocusWidget(
nullptr, Qt::OtherFocusReason);
6814#if QT_CONFIG(accessibility)
6815 QAccessibleEvent event(
this, QAccessible::Focus);
6816 QAccessible::updateAccessibility(&event);
6823
6824
6825
6826
6827
6828
6829
6830
6833
6834
6835
6836
6837
6838
6839
6840
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6864bool QWidget::focusNextPrevChild(
bool next)
6866 QWidget* p = parentWidget();
6867 bool isSubWindow = (windowType() == Qt::SubWindow);
6868 if (!isWindow() && !isSubWindow && p)
6869 return p->focusNextPrevChild(next);
6870#if QT_CONFIG(graphicsview)
6872 if (d->extra && d->extra->proxyWidget)
6873 return d->extra->proxyWidget->focusNextPrevChild(next);
6876 bool wrappingOccurred =
false;
6877 QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(
this, next,
6879 if (!w)
return false;
6881 Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
6884
6885
6886
6887
6888 if (wrappingOccurred) {
6889 QWindow *window = windowHandle();
6890 if (window !=
nullptr) {
6891 QWindowPrivate *winp = qt_window_private(window);
6893 if (winp->platformWindow !=
nullptr) {
6894 QFocusEvent event(QEvent::FocusIn, reason);
6896 winp->platformWindow->windowEvent(&event);
6897 if (event.isAccepted())
return true;
6902 w->setFocus(reason);
6907
6908
6909
6910
6911
6912
6913
6915QWidget *QWidget::focusWidget()
const
6917 return const_cast<QWidget *>(d_func()->focus_child);
6920QObject *QWidgetPrivate::focusObject()
6923 QWidget *proxy = deepestFocusProxy();
6924 return proxy ? proxy : q;
6928
6929
6930
6931
6932QWidget *QWidget::nextInFocusChain()
const
6935 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Next);
6939
6940
6941
6942
6943
6944
6945
6946QWidget *QWidget::previousInFocusChain()
const
6949 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Previous);
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967bool QWidget::isActiveWindow()
const
6969 QWidget *tlw = window();
6970 if (tlw == QApplication::activeWindow() || (isVisible() && (tlw->windowType() == Qt::Popup)))
6973#if QT_CONFIG(graphicsview)
6974 if (
const auto &tlwExtra = tlw->d_func()->extra) {
6975 if (isVisible() && tlwExtra->proxyWidget)
6976 return tlwExtra->proxyWidget->isActiveWindow();
6980 if (style()->styleHint(QStyle::SH_Widget_ShareActivation,
nullptr,
this)) {
6981 if (tlw->windowType() == Qt::Tool &&
6983 (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow()))
6985 QWidget *w = QApplication::activeWindow();
6986 while(w && tlw->windowType() == Qt::Tool &&
6987 !w->isModal() && w->parentWidget()) {
6988 w = w->parentWidget()->window();
6995 if (QWindow *ww = QGuiApplication::focusWindow()) {
6997 QWidgetWindow *qww = qobject_cast<QWidgetWindow *>(ww);
6998 QWindowContainer *qwc = qww ? qobject_cast<QWindowContainer *>(qww->widget()) : 0;
6999 if (qwc && qwc->topLevelWidget() == tlw)
7010 if (
const QWindow *w = tlw->windowHandle()) {
7012 return w->handle()->isActive();
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068void QWidget::setTabOrder(QWidget* first, QWidget *second)
7070 if (!first || !second || first == second
7071 || first->focusPolicy() == Qt::NoFocus
7072 || second->focusPolicy() == Qt::NoFocus)
7075 if (Q_UNLIKELY(first->window() != second->window())) {
7076 qWarning(
"QWidget::setTabOrder: 'first' and 'second' must be in the same window");
7080 const auto determineLastFocusChild = [](QWidget *target, QWidget *noFurtherThan)
7086 QWidget *lastFocusChild = target;
7088 QWidget *focusProxy = target->d_func()->deepestFocusProxy();
7095 for (
auto *object : target->children()) {
7096 QWidget *w = qobject_cast<QWidget*>(object);
7097 if (w && w->focusProxy() == target) {
7102 }
else if (target->isAncestorOf(focusProxy)) {
7103 lastFocusChild = focusProxy;
7104 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
7105 focusNext != focusProxy && target->isAncestorOf(focusNext) && focusNext->window() == focusProxy->window();
7106 focusNext = focusNext->nextInFocusChain()) {
7107 if (focusNext == noFurtherThan)
7109 if (focusNext->focusPolicy() != Qt::NoFocus)
7110 lastFocusChild = focusNext;
7113 return lastFocusChild;
7116 QWidget *lastFocusChildOfFirst = determineLastFocusChild(first, second);
7117 if (lastFocusChildOfFirst == second)
7118 lastFocusChildOfFirst = first;
7119 QWidget *lastFocusChildOfSecond = determineLastFocusChild(second, first);
7120 if (lastFocusChildOfSecond == first)
7121 lastFocusChildOfSecond = second;
7124 QWidget *oldPrev = second->previousInFocusChain();
7125 QWidget *prevWithFocus = oldPrev;
7126 while (prevWithFocus->focusPolicy() == Qt::NoFocus)
7127 prevWithFocus = prevWithFocus->previousInFocusChain();
7128 if (prevWithFocus == first)
7130 const QWidgetList chain = QWidgetPrivate::takeFromFocusChain(second, lastFocusChildOfSecond);
7131 QWidgetPrivate::insertIntoFocusChain(chain, QWidgetPrivate::FocusDirection::Next, lastFocusChildOfFirst);
7134void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets)
7136 QWidget *prev =
nullptr;
7137 for (
const auto &widget : widgets) {
7141 QWidget::setTabOrder(prev, widget);
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7160void QWidgetPrivate::reparentFocusWidgets(QWidget * oldtlw)
7163 if (oldtlw == q->window())
7167 focus_child->clearFocus();
7169 reparentFocusChildren(QWidgetPrivate::FocusDirection::Next);
7173
7174
7175
7176
7177
7178
7179QSize QWidget::frameSize()
const
7182 if (isWindow() && !(windowType() == Qt::Popup)) {
7183 QRect fs = d->frameStrut();
7184 return QSize(data->crect.width() + fs.left() + fs.right(),
7185 data->crect.height() + fs.top() + fs.bottom());
7187 return data->crect.size();
7191
7192
7193
7194
7195
7197void QWidget::move(
const QPoint &p)
7200 setAttribute(Qt::WA_Moved);
7201 if (testAttribute(Qt::WA_WState_Created)) {
7203 d->topData()->posIncludesFrame =
false;
7204 d->setGeometry_sys(p.x() + geometry().x() - QWidget::x(),
7205 p.y() + geometry().y() - QWidget::y(),
7206 width(), height(),
true);
7207 d->setDirtyOpaqueRegion();
7211 d->topData()->posIncludesFrame =
true;
7212 data->crect.moveTopLeft(p);
7213 setAttribute(Qt::WA_PendingMoveEvent);
7216 if (d->extra && d->extra->hasWindowContainer)
7217 QWindowContainer::parentWasMoved(
this);
7229void QWidgetPrivate::fixPosIncludesFrame()
7232 if (QTLWExtra *te = maybeTopData()) {
7233 if (te->posIncludesFrame) {
7236 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
7237 te->posIncludesFrame = 0;
7239 if (q->windowHandle() && q->windowHandle()->handle()) {
7241 if (!q->data->fstrut_dirty) {
7242 data.crect.translate(te->frameStrut.x(), te->frameStrut.y());
7243 te->posIncludesFrame = 0;
7252
7253
7254
7255
7257void QWidget::resize(
const QSize &s)
7260 setAttribute(Qt::WA_Resized);
7261 if (testAttribute(Qt::WA_WState_Created)) {
7262 d->fixPosIncludesFrame();
7263 d->setGeometry_sys(geometry().x(), geometry().y(), s.width(), s.height(),
false);
7264 d->setDirtyOpaqueRegion();
7266 const auto oldRect = data->crect;
7267 data->crect.setSize(s.boundedTo(maximumSize()).expandedTo(minimumSize()));
7268 if (oldRect != data->crect)
7269 setAttribute(Qt::WA_PendingResizeEvent);
7273void QWidget::setGeometry(
const QRect &r)
7276 setAttribute(Qt::WA_Resized);
7277 setAttribute(Qt::WA_Moved);
7279 d->topData()->posIncludesFrame = 0;
7280 if (testAttribute(Qt::WA_WState_Created)) {
7281 d->setGeometry_sys(r.x(), r.y(), r.width(), r.height(),
true);
7282 d->setDirtyOpaqueRegion();
7284 const auto oldRect = data->crect;
7285 data->crect.setTopLeft(r.topLeft());
7286 data->crect.setSize(r.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
7287 if (oldRect != data->crect) {
7288 setAttribute(Qt::WA_PendingMoveEvent);
7289 setAttribute(Qt::WA_PendingResizeEvent);
7293 if (d->extra && d->extra->hasWindowContainer)
7294 QWindowContainer::parentWasMoved(
this);
7297void QWidgetPrivate::setGeometry_sys(
int x,
int y,
int w,
int h,
bool isMove)
7301 w = qMin(w,extra->maxw);
7302 h = qMin(h,extra->maxh);
7303 w = qMax(w,extra->minw);
7304 h = qMax(h,extra->minh);
7307 if (q->isWindow() && q->windowHandle()) {
7308 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
7309 if (!integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) {
7312 w = q->windowHandle()->width();
7313 h = q->windowHandle()->height();
7317 QPoint oldp = q->geometry().topLeft();
7318 QSize olds = q->size();
7319 QRect r(x, y, w, h);
7321 bool isResize = olds != r.size();
7323 isMove = oldp != r.topLeft();
7328 if (r.size() == olds && oldp == r.topLeft())
7331 if (!data.in_set_window_state) {
7332 q->data->window_state &= ~Qt::WindowMaximized;
7333 q->data->window_state &= ~Qt::WindowFullScreen;
7335 topData()->normalGeometry = QRect(0, 0, -1, -1);
7338 QPoint oldPos = q->pos();
7341 bool needsShow =
false;
7343 if (q->isWindow() || q->windowHandle()) {
7344 if (!(data.window_state & Qt::WindowFullScreen) && (w == 0 || h == 0)) {
7345 q->setAttribute(Qt::WA_OutsideWSRange,
true);
7348 data.crect = QRect(x, y, w, h);
7349 }
else if (q->testAttribute(Qt::WA_OutsideWSRange)) {
7350 q->setAttribute(Qt::WA_OutsideWSRange,
false);
7355 if (q->isVisible()) {
7356 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && !q->testAttribute(Qt::WA_OutsideWSRange)) {
7357 if (QWindow *win = q->windowHandle()) {
7358 if (q->isWindow()) {
7359 if (isResize && !isMove)
7361 else if (isMove && !isResize)
7362 win->setPosition(x, y);
7364 win->setGeometry(q->geometry());
7366 QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());
7367 win->setGeometry(QRect(posInNativeParent,r.size()));
7374 if (!q->isWindow()) {
7375 if (renderToTexture) {
7376 QRegion updateRegion(q->geometry());
7377 updateRegion += QRect(oldPos, olds);
7378 q->parentWidget()->d_func()->invalidateBackingStore(updateRegion);
7379 }
else if (isMove && !isResize) {
7380 moveRect(QRect(oldPos, olds), x - oldPos.x(), y - oldPos.y());
7382 invalidateBackingStore_resizeHelper(oldPos, olds);
7388 QMoveEvent e(q->pos(), oldPos);
7389 QCoreApplication::sendEvent(q, &e);
7392 QResizeEvent e(r.size(), olds);
7393 QCoreApplication::sendEvent(q, &e);
7394 if (q->windowHandle())
7398 if (isMove && q->pos() != oldPos)
7399 q->setAttribute(Qt::WA_PendingMoveEvent,
true);
7401 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7404#if QT_CONFIG(accessibility)
7405 if (QAccessible::isActive() && q->isVisible()) {
7406 QAccessibleEvent event(q, QAccessible::LocationChanged);
7407 QAccessible::updateAccessibility(&event);
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429QByteArray QWidget::saveGeometry()
const
7432 QDataStream stream(&array, QIODevice::WriteOnly);
7433 stream.setVersion(QDataStream::Qt_4_0);
7434 const quint32 magicNumber = 0x1D9D0CB;
7439 quint16 majorVersion = 3;
7440 quint16 minorVersion = 0;
7441 const int screenNumber = QGuiApplication::screens().indexOf(screen());
7442 stream << magicNumber
7447 << qint32(screenNumber)
7448 << quint8(windowState() & Qt::WindowMaximized)
7449 << quint8(windowState() & Qt::WindowFullScreen)
7450 << qint32(screen()->geometry().width())
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478void QWidgetPrivate::checkRestoredGeometry(
const QRect &availableGeometry, QRect *restoredGeometry,
7482 const int height = restoredGeometry->height() + frameHeight;
7486 if (availableGeometry.height() <= height)
7487 restoredGeometry->setHeight(availableGeometry.height() - 2 - frameHeight);
7488 if (availableGeometry.width() <= restoredGeometry->width())
7489 restoredGeometry->setWidth(availableGeometry.width() - 2);
7493 const QRect restored = restoredGeometry->adjusted(0, -frameHeight, 0, 0);
7496 if (availableGeometry.contains(restored))
7502 if (restored.top() <= availableGeometry.top()) {
7503 restoredGeometry->moveTop(availableGeometry.top() + 1 + frameHeight);
7504 }
else if (restored.bottom() >= availableGeometry.bottom()) {
7506 restoredGeometry->moveBottom(availableGeometry.bottom() - 1);
7510 if (restored.left() <= availableGeometry.left()) {
7511 restoredGeometry->moveLeft(availableGeometry.left() + 1);
7512 }
else if (restored.right() >= availableGeometry.right()) {
7514 restoredGeometry->moveRight(availableGeometry.right() - 1);
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541bool QWidget::restoreGeometry(
const QByteArray &geometry)
7543 if (geometry.size() < 4)
7545 QDataStream stream(geometry);
7546 stream.setVersion(QDataStream::Qt_4_0);
7548 const quint32 magicNumber = 0x1D9D0CB;
7549 quint32 storedMagicNumber;
7550 stream >> storedMagicNumber;
7551 if (storedMagicNumber != magicNumber)
7554 const quint16 currentMajorVersion = 3;
7555 quint16 majorVersion = 0;
7556 quint16 minorVersion = 0;
7558 stream >> majorVersion >> minorVersion;
7560 if (majorVersion > currentMajorVersion)
7564 QRect restoredFrameGeometry;
7565 QRect restoredGeometry;
7566 QRect restoredNormalGeometry;
7567 qint32 restoredScreenNumber;
7570 qint32 restoredScreenWidth = 0;
7572 stream >> restoredFrameGeometry
7573 >> restoredNormalGeometry
7574 >> restoredScreenNumber
7578 if (majorVersion > 1)
7579 stream >> restoredScreenWidth;
7580 if (majorVersion > 2)
7581 stream >> restoredGeometry;
7585 if (restoredScreenNumber >= qMax(QGuiApplication::screens().size(), 1))
7586 restoredScreenNumber = 0;
7587 const QScreen *restoredScreen = QGuiApplication::screens().value(restoredScreenNumber,
nullptr);
7588 const qreal screenWidthF = restoredScreen ? qreal(restoredScreen->geometry().width()) : 0;
7591 if (restoredScreenWidth) {
7592 const qreal factor = qreal(restoredScreenWidth) / screenWidthF;
7593 if (factor < 0.8 || factor > 1.25)
7598 if (!maximized && !fullScreen && qreal(restoredFrameGeometry.width()) / screenWidthF > 1.5)
7602 const int frameHeight = QApplication::style()
7603 ? QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight,
nullptr,
this)
7606 if (!restoredNormalGeometry.isValid())
7607 restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
7608 if (!restoredNormalGeometry.isValid()) {
7610 restoredNormalGeometry.setSize(restoredNormalGeometry
7612 .expandedTo(d_func()->adjustedSize()));
7615 const QRect availableGeometry = restoredScreen ? restoredScreen->availableGeometry()
7623 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredGeometry, frameHeight);
7624 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredNormalGeometry, frameHeight);
7626 if (maximized || fullScreen) {
7629 Qt::WindowStates ws = windowState();
7631 setGeometry(restoredNormalGeometry);
7633 if (ws & Qt::WindowFullScreen) {
7635 move(availableGeometry.topLeft());
7636 }
else if (ws & Qt::WindowMaximized) {
7640 if (restoredScreen != screen()) {
7641 setWindowState(Qt::WindowNoState);
7642 setGeometry(restoredNormalGeometry);
7645 setGeometry(restoredNormalGeometry);
7649 ws |= Qt::WindowMaximized;
7651 ws |= Qt::WindowFullScreen;
7653 d_func()->topData()->normalGeometry = restoredNormalGeometry;
7655 setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
7658 if (majorVersion > 2)
7659 setGeometry(restoredGeometry);
7661 setGeometry(restoredNormalGeometry);
7667
7668
7669
7670
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682void QWidget::setContentsMargins(
int left,
int top,
int right,
int bottom)
7685 if (left == d->leftmargin && top == d->topmargin
7686 && right == d->rightmargin && bottom == d->bottommargin)
7688 d->leftmargin = left;
7690 d->rightmargin = right;
7691 d->bottommargin = bottom;
7693 d->updateContentsRect();
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712void QWidget::setContentsMargins(
const QMargins &margins)
7714 setContentsMargins(margins.left(), margins.top(),
7715 margins.right(), margins.bottom());
7718void QWidgetPrivate::updateContentsRect()
7725 q->updateGeometry();
7727 if (q->isVisible()) {
7729 QResizeEvent e(q->data->crect.size(), q->data->crect.size());
7730 QCoreApplication::sendEvent(q, &e);
7732 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7735 QEvent e(QEvent::ContentsRectChange);
7736 QCoreApplication::sendEvent(q, &e);
7740
7741
7742
7743
7744
7745
7746QMargins QWidget::contentsMargins()
const
7749 QMargins userMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
7750 return testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea) ?
7751 userMargins | d->safeAreaMargins() : userMargins;
7755
7756
7757
7758
7759QRect QWidget::contentsRect()
const
7761 return rect() - contentsMargins();
7764QMargins QWidgetPrivate::safeAreaMargins()
const
7768#if QT_CONFIG(graphicsview)
7771 if (nearestGraphicsProxyWidget(q))
7775 QWidget *nativeWidget = q->window();
7776 if (!nativeWidget->windowHandle())
7779 QMargins safeAreaMargins = nativeWidget->windowHandle()->safeAreaMargins();
7781 if (!q->isWindow()) {
7789 if (safeAreaMargins.isNull())
7796 const QWidget *assumedSafeWidget =
nullptr;
7797 for (
const QWidget *w = q; w != nativeWidget; w = w->parentWidget()) {
7798 QWidget *parentWidget = w->parentWidget();
7799 if (!parentWidget->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
7802 if (parentWidget->testAttribute(Qt::WA_LayoutOnEntireRect))
7805 QLayout *layout = parentWidget->layout();
7809 if (layout->geometry().isNull())
7812 if (layout->indexOf(w) < 0)
7815 assumedSafeWidget = w;
7819#if !defined(QT_DEBUG)
7820 if (assumedSafeWidget) {
7831 QPoint topLeftMargins = q->mapFrom(nativeWidget, QPoint(safeAreaMargins.left(), safeAreaMargins.top()));
7832 QRect widgetRect = q->isVisible() ? q->visibleRegion().boundingRect() : q->rect();
7833 QPoint bottomRightMargins = widgetRect.bottomRight() - q->mapFrom(nativeWidget,
7834 nativeWidget->rect().bottomRight() - QPoint(safeAreaMargins.right(), safeAreaMargins.bottom()));
7837 safeAreaMargins = QMargins(qMax(0, topLeftMargins.x()), qMax(0, topLeftMargins.y()),
7838 qMax(0, bottomRightMargins.x()), qMax(0, bottomRightMargins.y()));
7840 if (!safeAreaMargins.isNull() && assumedSafeWidget) {
7841 QLayout *layout = assumedSafeWidget->parentWidget()->layout();
7842 qWarning() << layout <<
"is laying out" << assumedSafeWidget
7843 <<
"outside of the contents rect of" << layout->parentWidget();
7848 return safeAreaMargins;
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7881Qt::ContextMenuPolicy QWidget::contextMenuPolicy()
const
7883 return (Qt::ContextMenuPolicy)data->context_menu_policy;
7886void QWidget::setContextMenuPolicy(Qt::ContextMenuPolicy policy)
7888 data->context_menu_policy = (uint) policy;
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7913Qt::FocusPolicy QWidget::focusPolicy()
const
7915 return (Qt::FocusPolicy)data->focus_policy;
7918void QWidget::setFocusPolicy(Qt::FocusPolicy policy)
7920 data->focus_policy = (uint) policy;
7922 if (d->extra && d->extra->focus_proxy)
7923 d->extra->focus_proxy->setFocusPolicy(policy);
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954void QWidget::setUpdatesEnabled(
bool enable)
7957 setAttribute(Qt::WA_ForceUpdatesDisabled, !enable);
7958 d->setUpdatesEnabled_helper(enable);
7962
7963
7964
7965
7966
7967
7968
7969
7970
7978 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
7979 Qt::WindowState defaultState = platformIntegration->defaultWindowState(data->window_flags);
7980 if (defaultState == Qt::WindowFullScreen)
7982 else if (defaultState == Qt::WindowMaximized)
7990
7991
7992
7993
7994void QWidgetPrivate::show_recursive()
7999 if (!q->testAttribute(Qt::WA_WState_Created))
8000 createRecursively();
8001 q->ensurePolished();
8003 if (!q->isWindow() && q->parentWidget()->d_func()->layout && !q->parentWidget()->data->in_show)
8004 q->parentWidget()->d_func()->layout->activate();
8012void QWidgetPrivate::sendPendingMoveAndResizeEvents(
bool recursive,
bool disableUpdates)
8016 disableUpdates = disableUpdates && q->updatesEnabled();
8018 q->setAttribute(Qt::WA_UpdatesDisabled);
8020 if (q->testAttribute(Qt::WA_PendingMoveEvent)) {
8021 QMoveEvent e(data.crect.topLeft(), data.crect.topLeft());
8022 QCoreApplication::sendEvent(q, &e);
8023 q->setAttribute(Qt::WA_PendingMoveEvent,
false);
8026 if (q->testAttribute(Qt::WA_PendingResizeEvent)) {
8027 QResizeEvent e(data.crect.size(), QSize());
8028 QCoreApplication::sendEvent(q, &e);
8029 q->setAttribute(Qt::WA_PendingResizeEvent,
false);
8033 q->setAttribute(Qt::WA_UpdatesDisabled,
false);
8038 for (
int i = 0; i < children.size(); ++i) {
8039 if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
8040 child->d_func()->sendPendingMoveAndResizeEvents(recursive, disableUpdates);
8044void QWidgetPrivate::activateChildLayoutsRecursively()
8046 sendPendingMoveAndResizeEvents(
false,
true);
8048 for (
int i = 0; i < children.size(); ++i) {
8049 QWidget *child = qobject_cast<QWidget *>(children.at(i));
8050 if (!child || child->isHidden() || child->isWindow())
8053 child->ensurePolished();
8056 QWidgetPrivate *childPrivate = child->d_func();
8057 if (childPrivate->layout)
8058 childPrivate->layout->activate();
8061 const bool wasVisible = child->isVisible();
8063 child->setAttribute(Qt::WA_WState_Visible);
8066 childPrivate->activateChildLayoutsRecursively();
8070 child->setAttribute(Qt::WA_WState_Visible,
false);
8074void QWidgetPrivate::show_helper()
8077 data.in_show =
true;
8079 sendPendingMoveAndResizeEvents();
8082 q->setAttribute(Qt::WA_WState_Visible);
8085 showChildren(
false);
8089 const bool isWindow = q->isWindow();
8090#if QT_CONFIG(graphicsview)
8091 bool isEmbedded = isWindow && q->graphicsProxyWidget() !=
nullptr;
8093 bool isEmbedded =
false;
8099 if (isWindow && !isEmbedded) {
8100 if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
8102 if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
8103 q->setAttribute(Qt::WA_KeyboardFocusChange);
8105 while (QApplication::activePopupWidget()) {
8106 if (!QApplication::activePopupWidget()->close())
8114#if QT_CONFIG(graphicsview)
8116 if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
8117 QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
8118 if (ancestorProxy) {
8120 ancestorProxy->d_func()->embedSubWindow(q);
8125 Q_UNUSED(isEmbedded);
8129 QShowEvent showEvent;
8130 QCoreApplication::sendEvent(q, &showEvent);
8134 if (!isEmbedded && q->windowType() == Qt::Popup)
8135 qApp->d_func()->openPopup(q);
8137#if QT_CONFIG(accessibility)
8138 if (q->windowType() != Qt::ToolTip) {
8139 QAccessibleEvent event(q, QAccessible::ObjectShow);
8140 QAccessible::updateAccessibility(&event);
8144 if (QApplicationPrivate::hidden_focus_widget == q) {
8145 QApplicationPrivate::hidden_focus_widget =
nullptr;
8146 q->setFocus(Qt::OtherFocusReason);
8152 if (!
qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
8153 QCoreApplication::processEvents();
8155 data.in_show =
false;
8158void QWidgetPrivate::show_sys()
8162 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8164 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8165 invalidateBackingStore(q->rect());
8166 q->setAttribute(Qt::WA_Mapped);
8168 if (window && q->isWindow()
8169#if QT_CONFIG(graphicsview)
8170 && (!extra || !extra->proxyWidget)
8172 && q->windowModality() != Qt::NonModal) {
8173 QGuiApplicationPrivate::showModalWindow(window);
8178 if (renderToTexture && !q->isWindow())
8179 QCoreApplication::postEvent(q->parentWidget(),
new QUpdateLaterEvent(q->geometry()));
8181 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(q->rect()));
8183 if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow))
8184 || q->testAttribute(Qt::WA_OutsideWSRange)) {
8190 fixPosIncludesFrame();
8191 QRect geomRect = q->geometry();
8192 if (!q->isWindow()) {
8193 QPoint topLeftOfWindow = q->mapTo(q->nativeParentWidget(),QPoint());
8194 geomRect.moveTopLeft(topLeftOfWindow);
8196 const QRect windowRect = window->geometry();
8197 if (windowRect != geomRect) {
8198 if (q->testAttribute(Qt::WA_Moved)
8199 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
8200 window->setGeometry(geomRect);
8202 window->resize(geomRect.size());
8206 qt_qpa_set_cursor(q,
false);
8208 invalidateBackingStore(q->rect());
8209 window->setNativeWindowVisibility(
true);
8211 if (window->isTopLevel()) {
8212 const QPoint crectTopLeft = q->data->crect.topLeft();
8213 const QPoint windowTopLeft = window->geometry().topLeft();
8214 if (crectTopLeft == QPoint(0, 0) && windowTopLeft != crectTopLeft)
8215 q->data->crect.moveTopLeft(windowTopLeft);
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8237
8238void QWidgetPrivate::hide_helper()
8242 bool isEmbedded =
false;
8243#if QT_CONFIG(graphicsview)
8244 isEmbedded = q->isWindow() && !bypassGraphicsProxyWidget(q) && nearestGraphicsProxyWidget(q->parentWidget()) !=
nullptr;
8246 Q_UNUSED(isEmbedded);
8249 if (!isEmbedded && (q->windowType() == Qt::Popup))
8250 qApp->d_func()->closePopup(q);
8252 q->setAttribute(Qt::WA_Mapped,
false);
8255 bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
8258 q->setAttribute(Qt::WA_WState_Visible,
false);
8262 QHideEvent hideEvent;
8263 QCoreApplication::sendEvent(q, &hideEvent);
8264 hideChildren(
false);
8269 qApp->d_func()->sendSyntheticEnterLeave(q);
8270 QWidget *fw = QApplication::focusWidget();
8271 while (fw && !fw->isWindow()) {
8273 q->focusNextPrevChild(
true);
8276 fw = fw->parentWidget();
8280 if (QWidgetRepaintManager *repaintManager = maybeRepaintManager())
8281 repaintManager->removeDirtyWidget(q);
8283#if QT_CONFIG(accessibility)
8285 QAccessibleEvent event(q, QAccessible::ObjectHide);
8286 QAccessible::updateAccessibility(&event);
8291void QWidgetPrivate::hide_sys()
8295 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8297 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8298 q->setAttribute(Qt::WA_Mapped,
false);
8300 if (window && q->isWindow()
8301#if QT_CONFIG(graphicsview)
8302 && (!extra || !extra->proxyWidget)
8304 && q->windowModality() != Qt::NonModal) {
8305 QGuiApplicationPrivate::hideModalWindow(window);
8310 deactivateWidgetCleanup();
8312 if (!q->isWindow()) {
8313 QWidget *p = q->parentWidget();
8314 if (p &&p->isVisible()) {
8315 if (renderToTexture)
8316 p->d_func()->invalidateBackingStore(q->geometry());
8318 invalidateBackingStore(q->rect());
8321 invalidateBackingStore(q->rect());
8325 window->setNativeWindowVisibility(
false);
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8351void QWidget::setVisible(
bool visible)
8354 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" <<
this
8355 <<
"with attributes" << WidgetAttributes{
this}
8356 <<
"to" << visible <<
"via QWidget";
8358 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) == !visible)
8361 if (d->dontSetExplicitShowHide) {
8362 d->dontSetExplicitShowHide =
false;
8365 setAttribute(Qt::WA_WState_ExplicitShowHide);
8368 d->setVisible(visible);
8374void QWidgetPrivate::setVisible(
bool visible)
8377 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" << q
8378 <<
"with attributes" << WidgetAttributes{q}
8379 <<
"to" << visible <<
"via QWidgetPrivate";
8383 if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
8384 && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
8385 q->parentWidget()->window()->d_func()->createRecursively();
8388 QWidget *pw = q->parentWidget();
8389 if (!q->testAttribute(Qt::WA_WState_Created)
8390 && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
8394 bool wasResized = q->testAttribute(Qt::WA_Resized);
8395 Qt::WindowStates initialWindowState = q->windowState();
8398 q->ensurePolished();
8401 bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
8403 q->setAttribute(Qt::WA_WState_Hidden,
false);
8405 if (needUpdateGeometry)
8406 updateGeometry_helper(
true);
8412 if (!q->isWindow()) {
8413 QWidget *parent = q->parentWidget();
8414 while (parent && parent->isVisible() && parent->d_func()->layout && !parent->data->in_show) {
8415 parent->d_func()->layout->activate();
8416 if (parent->isWindow())
8418 parent = parent->parentWidget();
8421 parent->d_func()->setDirtyOpaqueRegion();
8426 && (q->isWindow() || !q->parentWidget()->d_func()->layout)) {
8427 if (q->isWindow()) {
8429 if (q->windowState() != initialWindowState)
8430 q->setWindowState(initialWindowState);
8434 q->setAttribute(Qt::WA_Resized,
false);
8437 q->setAttribute(Qt::WA_KeyboardFocusChange,
false);
8439 if (q->isWindow() || q->parentWidget()->isVisible()) {
8442 qApp->d_func()->sendSyntheticEnterLeave(q);
8445 QEvent showToParentEvent(QEvent::ShowToParent);
8446 QCoreApplication::sendEvent(q, &showToParentEvent);
8448 if (QApplicationPrivate::hidden_focus_widget == q)
8449 QApplicationPrivate::hidden_focus_widget =
nullptr;
8455 if (!q->isWindow() && q->parentWidget())
8456 q->parentWidget()->d_func()->setDirtyOpaqueRegion();
8458 if (!q->testAttribute(Qt::WA_WState_Hidden)) {
8459 q->setAttribute(Qt::WA_WState_Hidden);
8464 if (!q->isWindow() && q->parentWidget()) {
8465 if (q->parentWidget()->d_func()->layout)
8466 q->parentWidget()->d_func()->layout->invalidate();
8467 else if (q->parentWidget()->isVisible())
8468 QCoreApplication::postEvent(q->parentWidget(),
new QEvent(QEvent::LayoutRequest));
8471 QEvent hideToParentEvent(QEvent::HideToParent);
8472 QCoreApplication::sendEvent(q, &hideToParentEvent);
8477
8478
8479void QWidget::setHidden(
bool hidden)
8481 setVisible(!hidden);
8484bool QWidgetPrivate::isExplicitlyHidden()
const
8487 return q->isHidden() && q->testAttribute(Qt::WA_WState_ExplicitShowHide);
8490void QWidgetPrivate::_q_showIfNotHidden()
8493 if (!isExplicitlyHidden())
8494 q->setVisible(
true);
8497void QWidgetPrivate::showChildren(
bool spontaneous)
8500 qCDebug(lcWidgetShowHide) <<
"Showing children of" << q
8501 <<
"spontaneously" << spontaneous;
8503 QList<QObject*> childList = children;
8504 for (
int i = 0; i < childList.size(); ++i) {
8505 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8508 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8509 <<
"with attributes" << WidgetAttributes{widget};
8510 if (widget->windowHandle() && !widget->testAttribute(Qt::WA_WState_ExplicitShowHide))
8511 widget->setAttribute(Qt::WA_WState_Hidden,
false);
8512 if (widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden))
8515 widget->setAttribute(Qt::WA_Mapped);
8516 widget->d_func()->showChildren(
true);
8518 QApplication::sendSpontaneousEvent(widget, &e);
8520 if (widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) {
8521 widget->d_func()->show_recursive();
8527 widget->d_func()->dontSetExplicitShowHide =
true;
8528 widget->setVisible(
true);
8529 widget->d_func()->dontSetExplicitShowHide =
false;
8535void QWidgetPrivate::hideChildren(
bool spontaneous)
8538 qCDebug(lcWidgetShowHide) <<
"Hiding children of" << q
8539 <<
"spontaneously" << spontaneous;
8541 QList<QObject*> childList = children;
8542 for (
int i = 0; i < childList.size(); ++i) {
8543 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8546 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8547 <<
"with attributes" << WidgetAttributes{widget};
8548 if (widget->isWindow() || !widget->isVisible())
8552 widget->setAttribute(Qt::WA_Mapped,
false);
8554 widget->setAttribute(Qt::WA_WState_Visible,
false);
8555 widget->d_func()->hideChildren(spontaneous);
8558 QApplication::sendSpontaneousEvent(widget, &e);
8560 QCoreApplication::sendEvent(widget, &e);
8561 if (widget->internalWinId()
8562 && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) {
8565 widget->d_func()->hide_sys();
8568 qApp->d_func()->sendSyntheticEnterLeave(widget);
8569#if QT_CONFIG(accessibility)
8571 QAccessibleEvent event(widget, QAccessible::ObjectHide);
8572 QAccessible::updateAccessibility(&event);
8580 if (QWidget* widgetWindow = q->window();
8581 widgetWindow && widgetWindow->data->is_closing) {
8582 q->setAttribute(Qt::WA_UnderMouse,
false);
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604bool QWidgetPrivate::handleClose(CloseMode mode)
8607 qCDebug(lcWidgetShowHide) <<
"Handling close event for" << q;
8609 if (data.is_closing)
8613 data.is_closing =
true;
8615 QPointer<QWidget> that = q;
8617 if (data.in_destructor)
8618 mode = CloseNoEvent;
8620 if (mode != CloseNoEvent) {
8622 if (mode == CloseWithSpontaneousEvent)
8623 QApplication::sendSpontaneousEvent(q, &e);
8625 QCoreApplication::sendEvent(q, &e);
8626 if (!that.isNull() && !e.isAccepted()) {
8627 data.is_closing =
false;
8633 if (!that.isNull() && !q->isHidden())
8636 if (!that.isNull()) {
8637 data.is_closing =
false;
8638 if (q->testAttribute(Qt::WA_DeleteOnClose)) {
8639 q->setAttribute(Qt::WA_DeleteOnClose,
false);
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8669bool QWidget::close()
8671 return d_func()->close();
8674bool QWidgetPrivate::close()
8684 if (QWindow *widgetWindow = windowHandle()) {
8685 if (widgetWindow->isTopLevel())
8686 return widgetWindow->close();
8689 return handleClose(QWidgetPrivate::CloseWithEvent);
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8750bool QWidget::isVisibleTo(
const QWidget *ancestor)
const
8754 const QWidget * w =
this;
8755 while (!w->isHidden()
8757 && w->parentWidget()
8758 && w->parentWidget() != ancestor)
8759 w = w->parentWidget();
8760 return !w->isHidden();
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774QRegion QWidget::visibleRegion()
const
8778 QRect clipRect = d->clipRect();
8779 if (clipRect.isEmpty())
8781 QRegion r(clipRect);
8782 d->subtractOpaqueChildren(r, clipRect);
8783 d->subtractOpaqueSiblings(r);
8788QSize QWidgetPrivate::adjustedSize()
const
8792 QSize s = q->sizeHint();
8794 if (q->isWindow()) {
8795 Qt::Orientations exp;
8797 if (layout->hasHeightForWidth())
8798 s.setHeight(layout->totalHeightForWidth(s.width()));
8799 exp = layout->expandingDirections();
8802 if (q->sizePolicy().hasHeightForWidth())
8803 s.setHeight(q->heightForWidth(s.width()));
8804 exp = q->sizePolicy().expandingDirections();
8806 if (exp & Qt::Horizontal)
8807 s.setWidth(qMax(s.width(), 200));
8808 if (exp & Qt::Vertical)
8809 s.setHeight(qMax(s.height(), 100));
8812 if (
const QScreen *screenAtPoint = QGuiApplication::screenAt(q->pos()))
8813 screen = screenAtPoint->geometry();
8815 screen = QGuiApplication::primaryScreen()->geometry();
8817 s.setWidth(qMin(s.width(), screen.width()*2/3));
8818 s.setHeight(qMin(s.height(), screen.height()*2/3));
8820 if (QTLWExtra *extra = maybeTopData())
8821 extra->sizeAdjusted =
true;
8825 QRect r = q->childrenRect();
8828 s = r.size() + QSize(2 * r.x(), 2 * r.y());
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8850void QWidget::adjustSize()
8854 QSize s = d->adjustedSize();
8857 d->layout->activate();
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8879QSize QWidget::sizeHint()
const
8883 return d->layout->totalSizeHint();
8884 return QSize(-1, -1);
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906QSize QWidget::minimumSizeHint()
const
8910 return d->layout->totalMinimumSize();
8911 return QSize(-1, -1);
8916
8917
8918
8919
8920
8924
8925
8926
8927
8929bool QWidget::isAncestorOf(
const QWidget *child)
const
8934 if (child->isWindow())
8936 child = child->parentWidget();
8942
8943
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8973bool QWidget::event(QEvent *event)
8979 switch(event->type()) {
8980 case QEvent::TabletPress:
8981 case QEvent::TabletRelease:
8982 case QEvent::TabletMove:
8983 case QEvent::MouseButtonPress:
8984 case QEvent::MouseButtonRelease:
8985 case QEvent::MouseButtonDblClick:
8986 case QEvent::MouseMove:
8987 case QEvent::TouchBegin:
8988 case QEvent::TouchUpdate:
8989 case QEvent::TouchEnd:
8990 case QEvent::TouchCancel:
8991 case QEvent::ContextMenu:
8992 case QEvent::KeyPress:
8993 case QEvent::KeyRelease:
8994#if QT_CONFIG(wheelevent)
9002 switch (event->type()) {
9003 case QEvent::PlatformSurface: {
9005 switch (
static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
9006 case QPlatformSurfaceEvent::SurfaceCreated:
9007 if (!testAttribute(Qt::WA_WState_Created))
9010 case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
9011 if (testAttribute(Qt::WA_WState_Created)) {
9014 destroy(
false,
false);
9020 case QEvent::MouseMove:
9021 mouseMoveEvent((QMouseEvent*)event);
9024 case QEvent::MouseButtonPress:
9025 mousePressEvent((QMouseEvent*)event);
9028 case QEvent::MouseButtonRelease:
9029 mouseReleaseEvent((QMouseEvent*)event);
9032 case QEvent::MouseButtonDblClick:
9033 mouseDoubleClickEvent((QMouseEvent*)event);
9035#if QT_CONFIG(wheelevent)
9037 wheelEvent((QWheelEvent*)event);
9040#if QT_CONFIG(tabletevent)
9041 case QEvent::TabletMove:
9042 if (
static_cast<QTabletEvent *>(event)->buttons() == Qt::NoButton && !testAttribute(Qt::WA_TabletTracking))
9045 case QEvent::TabletPress:
9046 case QEvent::TabletRelease:
9047 tabletEvent((QTabletEvent*)event);
9050 case QEvent::KeyPress: {
9051 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
9053 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
9054 if (k->key() == Qt::Key_Backtab
9055 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
9056 res = focusNextPrevChild(
false);
9057 else if (k->key() == Qt::Key_Tab)
9058 res = focusNextPrevChild(
true);
9063#ifdef QT_KEYPAD_NAVIGATION
9064 if (!k->isAccepted() && QApplication::keypadNavigationEnabled()
9065 && !(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::ShiftModifier))) {
9066 if (QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder) {
9067 if (k->key() == Qt::Key_Up)
9068 res = focusNextPrevChild(
false);
9069 else if (k->key() == Qt::Key_Down)
9070 res = focusNextPrevChild(
true);
9071 }
else if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
9072 if (k->key() == Qt::Key_Up)
9073 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionNorth);
9074 else if (k->key() == Qt::Key_Right)
9075 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionEast);
9076 else if (k->key() == Qt::Key_Down)
9077 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionSouth);
9078 else if (k->key() == Qt::Key_Left)
9079 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionWest);
9087#if QT_CONFIG(whatsthis)
9088 if (!k->isAccepted()
9089 && k->modifiers() & Qt::ShiftModifier && k->key() == Qt::Key_F1
9090 && d->whatsThis.size()) {
9091 QWhatsThis::showText(mapToGlobal(inputMethodQuery(Qt::ImCursorRectangle).toRect().center()), d->whatsThis,
this);
9098 case QEvent::KeyRelease:
9099 keyReleaseEvent((QKeyEvent*)event);
9101 case QEvent::ShortcutOverride:
9104 case QEvent::InputMethod:
9105 inputMethodEvent((QInputMethodEvent *) event);
9108 case QEvent::InputMethodQuery: {
9109 QInputMethodQueryEvent *query =
static_cast<QInputMethodQueryEvent *>(event);
9110 Qt::InputMethodQueries queries = query->queries();
9111 for (uint i = 0; i < 32; ++i) {
9112 Qt::InputMethodQuery q = (Qt::InputMethodQuery)(
int)(queries & (1<<i));
9114 QVariant v = inputMethodQuery(q);
9115 if (q == Qt::ImEnabled && !v.isValid() && isEnabled()) {
9122 v = QVariant(testAttribute(Qt::WA_InputMethodEnabled));
9124 query->setValue(q, v);
9131 case QEvent::PolishRequest:
9135 case QEvent::Polish: {
9136 style()->polish(
this);
9137 setAttribute(Qt::WA_WState_Polished);
9138 if (!QApplication::font(
this).isCopyOf(QApplication::font()))
9140 if (!QApplication::palette(
this).isCopyOf(QGuiApplication::palette()))
9141 d->resolvePalette();
9145 case QEvent::ApplicationWindowIconChange:
9146 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
9147 d->setWindowIcon_sys();
9148 d->setWindowIcon_helper();
9151 case QEvent::FocusIn:
9152 focusInEvent((QFocusEvent*)event);
9153 d->updateWidgetTransform(event);
9156 case QEvent::FocusOut:
9157 focusOutEvent((QFocusEvent*)event);
9161#if QT_CONFIG(statustip)
9162 if (d->statusTip.size()) {
9163 QStatusTipEvent tip(d->statusTip);
9164 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9167 enterEvent(
static_cast<QEnterEvent*>(event));
9171#if QT_CONFIG(statustip)
9172 if (d->statusTip.size()) {
9174 QStatusTipEvent tip(empty);
9175 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9181 case QEvent::HoverEnter:
9182 case QEvent::HoverLeave:
9190 paintEvent((QPaintEvent*)event);
9194 moveEvent((QMoveEvent*)event);
9195 d->updateWidgetTransform(event);
9198 case QEvent::Resize:
9199 resizeEvent((QResizeEvent*)event);
9200 d->updateWidgetTransform(event);
9204 closeEvent((QCloseEvent *)event);
9207#ifndef QT_NO_CONTEXTMENU
9208 case QEvent::ContextMenu:
9209 switch (data->context_menu_policy) {
9210 case Qt::PreventContextMenu:
9212 case Qt::DefaultContextMenu:
9213 contextMenuEvent(
static_cast<QContextMenuEvent *>(event));
9215 case Qt::CustomContextMenu:
9216 emit customContextMenuRequested(
static_cast<QContextMenuEvent *>(event)->pos());
9219 case Qt::ActionsContextMenu:
9220 if (d->actions.size()) {
9221 QMenu::exec(d->actions,
static_cast<QContextMenuEvent *>(event)->globalPos(),
9234#if QT_CONFIG(draganddrop)
9236 dropEvent((QDropEvent*) event);
9239 case QEvent::DragEnter:
9240 dragEnterEvent((QDragEnterEvent*) event);
9243 case QEvent::DragMove:
9244 dragMoveEvent((QDragMoveEvent*) event);
9247 case QEvent::DragLeave:
9248 dragLeaveEvent((QDragLeaveEvent*) event);
9253 showEvent((QShowEvent*) event);
9257 hideEvent((QHideEvent*) event);
9260 case QEvent::ShowWindowRequest:
9265 case QEvent::ApplicationFontChange:
9268 case QEvent::ApplicationPaletteChange:
9269 d->resolvePalette();
9272 case QEvent::ToolBarChange:
9273 case QEvent::ActivationChange:
9274 case QEvent::EnabledChange:
9275 case QEvent::FontChange:
9276 case QEvent::StyleChange:
9277 case QEvent::PaletteChange:
9278 case QEvent::WindowTitleChange:
9279 case QEvent::IconTextChange:
9280 case QEvent::ModifiedChange:
9281 case QEvent::MouseTrackingChange:
9282 case QEvent::TabletTrackingChange:
9283 case QEvent::ParentChange:
9284 case QEvent::LocaleChange:
9285 case QEvent::MacSizeChange:
9286 case QEvent::ContentsRectChange:
9287 case QEvent::ThemeChange:
9288 case QEvent::ReadOnlyChange:
9292 case QEvent::WindowStateChange: {
9293 const bool wasMinimized =
static_cast<
const QWindowStateChangeEvent *>(event)->oldState() & Qt::WindowMinimized;
9294 if (wasMinimized != isMinimized()) {
9295 QWidget *widget =
const_cast<QWidget *>(
this);
9298 if (!d->childrenShownByExpose) {
9300 d->showChildren(
true);
9301 QShowEvent showEvent;
9302 QCoreApplication::sendSpontaneousEvent(widget, &showEvent);
9304 d->childrenHiddenByWState =
false;
9306 QHideEvent hideEvent;
9307 QCoreApplication::sendSpontaneousEvent(widget, &hideEvent);
9308 d->hideChildren(
true);
9309 d->childrenHiddenByWState =
true;
9311 d->childrenShownByExpose =
false;
9317 case QEvent::WindowActivate:
9318 case QEvent::WindowDeactivate: {
9319 if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
9321 QList<QObject*> childList = d->children;
9322 for (
int i = 0; i < childList.size(); ++i) {
9323 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9324 if (w && w->isVisible() && !w->isWindow())
9325 QCoreApplication::sendEvent(w, event);
9329 case QEvent::LanguageChange:
9332 QList<QObject*> childList = d->children;
9333 for (
int i = 0; i < childList.size(); ++i) {
9334 QObject *o = childList.at(i);
9336 QCoreApplication::sendEvent(o, event);
9342 case QEvent::ApplicationLayoutDirectionChange:
9343 d->resolveLayoutDirection();
9346 case QEvent::LayoutDirectionChange:
9348 d->layout->invalidate();
9352 case QEvent::UpdateRequest:
9353 d->syncBackingStore();
9355 case QEvent::UpdateLater:
9356 update(
static_cast<QUpdateLaterEvent*>(event)->region());
9358 case QEvent::StyleAnimationUpdate:
9359 if (isVisible() && !window()->isMinimized()) {
9365 case QEvent::WindowBlocked:
9366 case QEvent::WindowUnblocked:
9367 if (!d->children.isEmpty()) {
9368 QWidget *modalWidget = QApplication::activeModalWidget();
9369 for (
int i = 0; i < d->children.size(); ++i) {
9370 QObject *o = d->children.at(i);
9371 if (o && o != modalWidget && o->isWidgetType()) {
9372 QWidget *w =
static_cast<QWidget *>(o);
9375 QCoreApplication::sendEvent(w, event);
9380#if QT_CONFIG(tooltip)
9381 case QEvent::ToolTip:
9382 if (!d->toolTip.isEmpty())
9383 QToolTip::showText(
static_cast<QHelpEvent*>(event)->globalPos(), d->toolTip,
this, QRect(), d->toolTipDuration);
9388#if QT_CONFIG(whatsthis)
9389 case QEvent::WhatsThis:
9390 if (d->whatsThis.size())
9391 QWhatsThis::showText(
static_cast<QHelpEvent *>(event)->globalPos(), d->whatsThis,
this);
9395 case QEvent::QueryWhatsThis:
9396 if (d->whatsThis.isEmpty())
9400 case QEvent::EmbeddingControl:
9401 d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
9402 data->fstrut_dirty =
false;
9405 case QEvent::ActionAdded:
9406 case QEvent::ActionRemoved:
9407 case QEvent::ActionChanged:
9408 actionEvent((QActionEvent*)event);
9412 case QEvent::KeyboardLayoutChange:
9417 QList<QObject*> childList = d->children;
9418 for (
int i = 0; i < childList.size(); ++i) {
9419 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9420 if (w && w->isVisible() && !w->isWindow())
9421 QCoreApplication::sendEvent(w, event);
9425 case QEvent::TouchBegin:
9426 case QEvent::TouchUpdate:
9427 case QEvent::TouchEnd:
9428 case QEvent::TouchCancel:
9433#ifndef QT_NO_GESTURES
9434 case QEvent::Gesture:
9438 case QEvent::ScreenChangeInternal:
9439 if (
const QTLWExtra *te = d->maybeTopData()) {
9440 const QWindow *win = te->window;
9441 d->setWinId((win && win->handle()) ? win->handle()->winId() : 0);
9444 case QEvent::DevicePixelRatioChange:
9445 if (d->data.fnt.d->dpi != logicalDpiY())
9446 d->updateFont(d->data.fnt);
9447 d->renderToTextureReallyDirty = 1;
9449 case QEvent::DynamicPropertyChange: {
9450 const QByteArray &propName =
static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
9451 if (propName.size() == 13 && !qstrncmp(propName,
"_q_customDpi", 12)) {
9452 uint value = property(propName.constData()).toUInt();
9455 const char axis = propName.at(12);
9457 d->extra->customDpiX = value;
9458 else if (axis ==
'Y')
9459 d->extra->customDpiY = value;
9460 d->updateFont(d->data.fnt);
9462 if (windowHandle() && !qstrncmp(propName,
"_q_platform_", 12))
9463 windowHandle()->setProperty(propName, property(propName));
9467 return QObject::event(event);
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488void QWidget::changeEvent(QEvent * event)
9490 switch(event->type()) {
9491 case QEvent::EnabledChange: {
9493#if QT_CONFIG(accessibility)
9494 QAccessible::State s;
9496 QAccessibleStateChangeEvent event(
this, s);
9497 QAccessible::updateAccessibility(&event);
9502 case QEvent::FontChange:
9503 case QEvent::StyleChange: {
9508 d->layout->invalidate();
9512 case QEvent::PaletteChange:
9516 case QEvent::ThemeChange:
9517 if (QGuiApplication::desktopSettingsAware()
9518 &&
qApp && !QCoreApplication::closingDown()) {
9519 if (testAttribute(Qt::WA_WState_Polished))
9520 QApplication::style()->unpolish(
this);
9521 if (testAttribute(Qt::WA_WState_Polished))
9522 QApplication::style()->polish(
this);
9523 QEvent styleChangedEvent(QEvent::StyleChange);
9524 QCoreApplication::sendEvent(
this, &styleChangedEvent);
9531 case QEvent::MacSizeChange:
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9567void QWidget::mouseMoveEvent(QMouseEvent *event)
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9589void QWidget::mousePressEvent(QMouseEvent *event)
9592 if ((windowType() == Qt::Popup)) {
9595 while ((w = QApplication::activePopupWidget()) && w !=
this){
9597 if (QApplication::activePopupWidget() == w)
9600 if (!rect().contains(event->position().toPoint())){
9607
9608
9609
9610
9611
9612
9614void QWidget::mouseReleaseEvent(QMouseEvent *event)
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9636void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
9638 mousePressEvent(event);
9641#if QT_CONFIG(wheelevent)
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9656void QWidget::wheelEvent(QWheelEvent *event)
9662#if QT_CONFIG(tabletevent)
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9683void QWidget::tabletEvent(QTabletEvent *event)
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9712void QWidget::keyPressEvent(QKeyEvent *event)
9714#ifndef QT_NO_SHORTCUT
9715 if ((windowType() == Qt::Popup) && event->matches(QKeySequence::Cancel)) {
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9747void QWidget::keyReleaseEvent(QKeyEvent *event)
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9771void QWidget::focusInEvent(QFocusEvent *)
9773 if (focusPolicy() != Qt::NoFocus || !isWindow()) {
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9797void QWidget::focusOutEvent(QFocusEvent *)
9799 if (focusPolicy() != Qt::NoFocus || !isWindow())
9802#if !defined(QT_PLATFORM_UIKIT)
9804 if (
qApp->autoSipEnabled() && testAttribute(Qt::WA_InputMethodEnabled))
9805 QGuiApplication::inputMethod()->hide();
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9821void QWidget::enterEvent(QEnterEvent *)
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9837void QWidget::leaveEvent(QEvent *)
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9889void QWidget::paintEvent(QPaintEvent *)
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9907void QWidget::moveEvent(QMoveEvent *)
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9928void QWidget::resizeEvent(QResizeEvent * )
9934
9935
9936
9937
9938
9939
9940
9941void QWidget::actionEvent(QActionEvent *)
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9962void QWidget::closeEvent(QCloseEvent *event)
9967#ifndef QT_NO_CONTEXTMENU
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9981void QWidget::contextMenuEvent(QContextMenuEvent *event)
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004void QWidget::inputMethodEvent(QInputMethodEvent *event)
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019QVariant QWidget::inputMethodQuery(Qt::InputMethodQuery query)
const
10022 case Qt::ImCursorRectangle:
10023 return QRect(width()/2, 0, 1, height());
10026 case Qt::ImAnchorPosition:
10028 return inputMethodQuery(Qt::ImCursorPosition);
10030 return (
int)inputMethodHints();
10031 case Qt::ImInputItemClipRectangle:
10032 return d_func()->clipRect();
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063Qt::InputMethodHints QWidget::inputMethodHints()
const
10066 const QWidgetPrivate *priv = d_func();
10067 while (priv->inheritsInputMethodHints) {
10068 priv = priv->q_func()->parentWidget()->d_func();
10071 return priv->imHints;
10073 return Qt::ImhNone;
10077void QWidget::setInputMethodHints(Qt::InputMethodHints hints)
10081 if (d->imHints == hints)
10083 d->imHints = hints;
10084 if (
this == QGuiApplication::focusObject())
10085 QGuiApplication::inputMethod()->update(Qt::ImHints);
10092#if QT_CONFIG(draganddrop)
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108void QWidget::dragEnterEvent(QDragEnterEvent *)
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126void QWidget::dragMoveEvent(QDragMoveEvent *)
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142void QWidget::dragLeaveEvent(QDragLeaveEvent *)
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157void QWidget::dropEvent(QDropEvent *)
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182void QWidget::showEvent(QShowEvent *)
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204void QWidget::hideEvent(QHideEvent *)
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10235bool QWidget::nativeEvent(
const QByteArray &eventType,
void *message, qintptr *result)
10237 Q_UNUSED(eventType);
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266void QWidget::ensurePolished()
const
10268 Q_D(
const QWidget);
10270 const QMetaObject *m = metaObject();
10271 if (m == d->polished)
10275 QEvent e(QEvent::Polish);
10276 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &e);
10279 QList<QObject*> children = d->children;
10280 for (
int i = 0; i < children.size(); ++i) {
10281 QObject *o = children.at(i);
10282 if (!o->isWidgetType())
10284 if (QWidget *w = qobject_cast<QWidget *>(o))
10285 w->ensurePolished();
10288 if (d->parent && d->sendChildEvents) {
10289 QChildEvent e(QEvent::ChildPolished,
const_cast<QWidget *>(
this));
10290 QCoreApplication::sendEvent(d->parent, &e);
10295
10296
10297
10298
10299
10300QRegion QWidget::mask()
const
10302 Q_D(
const QWidget);
10303 return d->extra ? d->extra->mask : QRegion();
10307
10308
10309
10310
10311
10312
10313
10314
10315QLayout *QWidget::layout()
const
10317 return d_func()->layout;
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10346void QWidget::setLayout(QLayout *l)
10348 if (Q_UNLIKELY(!l)) {
10349 qWarning(
"QWidget::setLayout: Cannot set layout to 0");
10353 if (Q_UNLIKELY(layout() != l))
10354 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
10355 " layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
10356 objectName().toLocal8Bit().data());
10360 QObject *oldParent = l->parent();
10361 if (oldParent && oldParent !=
this) {
10362 if (oldParent->isWidgetType()) {
10365 QWidget *oldParentWidget =
static_cast<QWidget *>(oldParent);
10366 oldParentWidget->takeLayout();
10368 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
10369 l->objectName().toLocal8Bit().data(), metaObject()->className(),
10370 objectName().toLocal8Bit().data());
10376 l->d_func()->topLevel =
true;
10378 if (oldParent !=
this) {
10379 l->setParent(
this);
10380 l->d_func()->reparentChildWidgets(
this);
10384 if (isWindow() && d->maybeTopData())
10385 d->topData()->sizeAdjusted =
false;
10389
10390
10391
10392
10393
10395QLayout *QWidget::takeLayout()
10398 QLayout *l = layout();
10401 d->layout =
nullptr;
10402 l->setParent(
nullptr);
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431QSizePolicy QWidget::sizePolicy()
const
10433 Q_D(
const QWidget);
10434 return d->size_policy;
10437void QWidget::setSizePolicy(QSizePolicy policy)
10440 setAttribute(Qt::WA_WState_OwnSizePolicy);
10441 if (policy == d->size_policy)
10444 if (d->size_policy.retainSizeWhenHidden() != policy.retainSizeWhenHidden())
10445 d->retainSizeWhenHiddenChanged = 1;
10447 d->size_policy = policy;
10449#if QT_CONFIG(graphicsview)
10450 if (
const auto &extra = d->extra) {
10451 if (extra->proxyWidget)
10452 extra->proxyWidget->setSizePolicy(policy);
10457 d->retainSizeWhenHiddenChanged = 0;
10459 if (isWindow() && d->maybeTopData())
10460 d->topData()->sizeAdjusted =
false;
10464
10465
10466
10467
10468
10469
10470
10471
10474
10475
10476
10477
10478
10479
10480
10482int QWidget::heightForWidth(
int w)
const
10484 if (layout() && layout()->hasHeightForWidth())
10485 return layout()->totalHeightForWidth(w);
10491
10492
10493
10494
10495bool QWidget::hasHeightForWidth()
const
10497 Q_D(
const QWidget);
10498 return d->layout ? d->layout->hasHeightForWidth() : d->size_policy.hasHeightForWidth();
10502
10503
10504
10505
10506
10507
10510
10511
10512
10513
10514
10516QWidget *QWidget::childAt(
const QPoint &p)
const
10518 return d_func()->childAt_helper(QPointF(p),
false);
10522
10523
10524
10525
10526
10527
10529QWidget *QWidget::childAt(
const QPointF &p)
const
10531 return d_func()->childAt_helper(p,
false);
10534QWidget *QWidgetPrivate::childAt_helper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10536 if (children.isEmpty())
10539 if (!pointInsideRectAndMask(p))
10541 return childAtRecursiveHelper(p, ignoreChildrenInDestructor);
10544QWidget *QWidgetPrivate::childAtRecursiveHelper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10546 for (
int i = children.size() - 1; i >= 0; --i) {
10547 QWidget *child = qobject_cast<QWidget *>(children.at(i));
10548 if (!child || child->isWindow() || child->isHidden() || child->testAttribute(Qt::WA_TransparentForMouseEvents)
10549 || (ignoreChildrenInDestructor && child->data->in_destructor)) {
10554 QPointF childPoint = p;
10555 childPoint -= child->data->crect.topLeft();
10558 if (!child->d_func()->pointInsideRectAndMask(childPoint))
10562 if (QWidget *w = child->d_func()->childAtRecursiveHelper(childPoint, ignoreChildrenInDestructor))
10571void QWidgetPrivate::updateGeometry_helper(
bool forceUpdate)
10575 widgetItem->invalidateSizeCache();
10577 if (forceUpdate || !extra || extra->minw != extra->maxw || extra->minh != extra->maxh) {
10578 const int isHidden = q->isHidden() && !size_policy.retainSizeWhenHidden() && !retainSizeWhenHiddenChanged;
10580 if (!q->isWindow() && !isHidden && (parent = q->parentWidget())) {
10581 if (parent->d_func()->layout)
10582 parent->d_func()->layout->invalidate();
10583 else if (parent->isVisible())
10584 QCoreApplication::postEvent(parent,
new QEvent(QEvent::LayoutRequest));
10590
10591
10592
10593
10594
10595
10596
10597
10599void QWidget::updateGeometry()
10602 d->updateGeometry_helper(
false);
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623void QWidget::setWindowFlags(Qt::WindowFlags flags)
10626 d->setWindowFlags(flags);
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641void QWidget::setWindowFlag(Qt::WindowType flag,
bool on)
10645 d->setWindowFlags(data->window_flags | flag);
10647 d->setWindowFlags(data->window_flags & ~flag);
10651
10652
10653
10654void QWidgetPrivate::setWindowFlags(Qt::WindowFlags flags)
10657#if QT_DEPRECATED_SINCE(6
, 11
)
10658 QT_IGNORE_DEPRECATIONS(
10659 if (flags.testFlag(Qt::WindowType::Desktop)) {
10660 qWarning() <<
"Qt::WindowType::Desktop has been deprecated in Qt 6. Ignoring.";
10661 flags.setFlag(Qt::WindowType::Desktop,
false);
10666 if (q->data->window_flags == flags)
10669 if ((q->data->window_flags | flags) & Qt::Window) {
10671 QPoint oldPos = q->pos();
10672 bool visible = q->isVisible();
10673 const bool windowFlagChanged = (q->data->window_flags ^ flags) & Qt::Window;
10674 q->setParent(q->parentWidget(), flags);
10678 if (!windowFlagChanged && (visible || q->testAttribute(Qt::WA_Moved)))
10681 adjustQuitOnCloseAttribute();
10683 q->data->window_flags = flags;
10688
10689
10690
10691
10692
10693
10694
10695
10696void QWidget::overrideWindowFlags(Qt::WindowFlags flags)
10698 data->window_flags = flags;
10702
10703
10704
10705
10706
10707
10708
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738void QWidget::setParent(QWidget *parent)
10740 if (parent == parentWidget())
10742 setParent((QWidget*)parent, windowFlags() & ~Qt::WindowType_Mask);
10748 if (d->renderToTexture) {
10749 QEvent e(eventType);
10750 QCoreApplication::sendEvent(widget, &e);
10753 for (
int i = 0; i < d->children.size(); ++i) {
10755 if (w && !w->isWindow())
10756 qSendWindowChangeToTextureChildrenRecursively(w, eventType);
10760 if (
auto *window = d->windowHandle(QWidgetPrivate::WindowHandleMode::Direct)) {
10761 QEvent e(eventType);
10762 QCoreApplication::sendEvent(window, &e);
10767
10768
10769
10770
10772void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
10775 Q_ASSERT_X(
this != parent, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
10777 const auto checkForParentChildLoops = qScopeGuard([&](){
10779 auto p = parentWidget();
10781 if (++depth == QObjectPrivate::CheckForParentChildLoopsWarnDepth) {
10782 qWarning(
"QWidget %p (class: '%s', object name: '%s') may have a loop in its parent-child chain; "
10783 "this is undefined behavior",
10784 this, metaObject()->className(), qPrintable(objectName()));
10786 p = p->parentWidget();
10791 const bool resized = testAttribute(Qt::WA_Resized);
10792 const bool wasCreated = testAttribute(Qt::WA_WState_Created);
10793 QWidget *oldtlw = window();
10795 QWidget *oldParentWithWindow = d->closestParentWidgetWithWindowHandle();
10797 if (f & Qt::Window)
10798 d->data.fstrut_dirty =
true;
10800 bool newParent = (parent != parentWidget());
10802 if (newParent && parent) {
10803 if (testAttribute(Qt::WA_NativeWindow) && !QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings))
10804 parent->d_func()->enforceNativeChildren();
10805 else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen))
10806 setAttribute(Qt::WA_NativeWindow);
10810 if (!testAttribute(Qt::WA_WState_Hidden)) {
10825 setAttribute(Qt::WA_WState_ExplicitShowHide,
false);
10828 QEvent e(QEvent::ParentAboutToChange);
10829 QCoreApplication::sendEvent(
this, &e);
10835 const bool oldWidgetUsesRhiFlush = oldParentWithWindow ? oldParentWithWindow->d_func()->usesRhiFlush
10836 : oldtlw->d_func()->usesRhiFlush;
10837 if (oldWidgetUsesRhiFlush && ((!parent && parentWidget()) || (parent && parent->window() != oldtlw)))
10838 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowAboutToChangeInternal);
10842 if (newParent && isAncestorOf(focusWidget()) && !(f & Qt::Window))
10843 focusWidget()->clearFocus();
10845 d->setParent_sys(parent, f);
10847 if (d->textureChildSeen && parent) {
10849 QWidgetPrivate::get(parent)->setTextureChildSeen();
10852 if (QWidgetRepaintManager *oldPaintManager = oldtlw->d_func()->maybeRepaintManager()) {
10854 oldPaintManager->removeDirtyWidget(
this);
10857 oldPaintManager->moveStaticWidgets(
this);
10860 d->reparentFocusWidgets(oldtlw);
10861 setAttribute(Qt::WA_Resized, resized);
10863 const bool useStyleSheetPropagationInWidgetStyles =
10864 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
10866 if (!useStyleSheetPropagationInWidgetStyles && !testAttribute(Qt::WA_StyleSheet)
10867 && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
10870 const auto pd = parent->d_func();
10871 d->inheritedFontResolveMask = pd->directFontResolveMask | pd->inheritedFontResolveMask;
10872 d->inheritedPaletteResolveMask = pd->directPaletteResolveMask | pd->inheritedPaletteResolveMask;
10875 d->resolvePalette();
10877 d->resolveLayoutDirection();
10878 d->resolveLocale();
10884 if (newParent || !wasCreated
10885#if QT_CONFIG(opengles2)
10886 || (f & Qt::MSWindowsOwnDC)
10891 if (!testAttribute(Qt::WA_ForceDisabled))
10892 d->setEnabled_helper(parent ? parent->isEnabled() :
true);
10893 if (!testAttribute(Qt::WA_ForceUpdatesDisabled))
10894 d->setUpdatesEnabled_helper(parent ? parent->updatesEnabled() :
true);
10899 if (parent && d->sendChildEvents) {
10900 QChildEvent e(QEvent::ChildAdded,
this);
10901 QCoreApplication::sendEvent(parent, &e);
10904 if (parent && d->sendChildEvents && d->polished) {
10905 QChildEvent e(QEvent::ChildPolished,
this);
10906 QCoreApplication::sendEvent(parent, &e);
10909 QEvent e(QEvent::ParentChange);
10910 QCoreApplication::sendEvent(
this, &e);
10915 if (oldWidgetUsesRhiFlush && oldtlw != window())
10916 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowChangeInternal);
10919 if (isWindow() || parentWidget()->isVisible())
10920 setAttribute(Qt::WA_WState_Hidden,
true);
10921 else if (!testAttribute(Qt::WA_WState_ExplicitShowHide))
10922 setAttribute(Qt::WA_WState_Hidden,
false);
10925 d->updateIsOpaque();
10927#if QT_CONFIG(graphicsview)
10930 if (oldtlw->graphicsProxyWidget()) {
10931 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
10932 ancestorProxy->d_func()->unembedSubWindow(
this);
10934 if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(
this)) {
10935 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
10936 ancestorProxy->d_func()->embedSubWindow(
this);
10940 if (d->extra && d->extra->hasWindowContainer)
10941 QWindowContainer::parentWasChanged(
this);
10943 QWidget *newParentWithWindow = d->closestParentWidgetWithWindowHandle();
10944 if (newParentWithWindow && newParentWithWindow != oldParentWithWindow) {
10946 qCDebug(lcWidgetPainting) <<
"Evaluating whether reparenting of" <<
this
10947 <<
"into" << parent <<
"requires RHI enablement for" << newParentWithWindow;
10949 QPlatformBackingStoreRhiConfig rhiConfig;
10950 QSurface::SurfaceType surfaceType = QSurface::RasterSurface;
10956 if (q_evaluateRhiConfig(
this, &rhiConfig, &surfaceType)) {
10960 if (q_evaluateRhiConfig(newParentWithWindow,
nullptr,
nullptr)) {
10963 auto *existingWindow = newParentWithWindow->windowHandle();
10964 auto existingSurfaceType = existingWindow->surfaceType();
10965 if (existingSurfaceType != surfaceType) {
10966 qCDebug(lcWidgetPainting)
10967 <<
"Recreating" << existingWindow
10968 <<
"with current type" << existingSurfaceType
10969 <<
"to support" << surfaceType;
10970 const auto windowStateBeforeDestroy = newParentWithWindow->windowState();
10971 const auto visibilityBeforeDestroy = newParentWithWindow->isVisible();
10972 const auto positionBeforeDestroy = newParentWithWindow->pos();
10973 newParentWithWindow->destroy();
10974 newParentWithWindow->create();
10975 Q_ASSERT(newParentWithWindow->windowHandle());
10976 newParentWithWindow->windowHandle()->setWindowStates(windowStateBeforeDestroy);
10977 newParentWithWindow->move(positionBeforeDestroy);
10978 QWidgetPrivate::get(newParentWithWindow)->setVisible(visibilityBeforeDestroy);
10979 }
else if (
auto *backingStore = newParentWithWindow->backingStore()) {
10982 backingStore->handle()->createRhi(existingWindow, rhiConfig);
10984 QWidgetPrivate::get(newParentWithWindow)->usesRhiFlush =
true;
10990#if QT_CONFIG(accessibility)
10991 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing) {
10992 QAccessibleEvent qaEvent(
this, QAccessible::ParentChanged);
10993 QAccessible::updateAccessibility(&qaEvent);
10999void QWidgetPrivate::setParent_sys(QWidget *newparent, Qt::WindowFlags f)
11003 Qt::WindowFlags oldFlags = data.window_flags;
11004 bool wasCreated = q->testAttribute(Qt::WA_WState_Created);
11006 QScreen *targetScreen =
nullptr;
11012 targetScreen = q->parentWidget()->window()->screen();
11015 const bool destroyWindow = (
11017 (oldFlags & Qt::Window) && !(f & Qt::Window)
11019 && wasCreated && !q->testAttribute(Qt::WA_NativeWindow)
11022 if (parent != newparent) {
11024 QObjectPrivate::setParent_helper(newparent);
11026 if (q->windowHandle())
11027 q->windowHandle()->setFlags(f);
11031 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11034 if (destroyWindow) {
11035 reparentWidgetWindowChildren(parentWithWindow);
11045 reparentWidgetWindows(parentWithWindow, f);
11049 bool explicitlyHidden = isExplicitlyHidden();
11051 if (destroyWindow) {
11052 if (extra && extra->hasWindowContainer)
11053 QWindowContainer::toplevelAboutToBeDestroyed(q);
11057 if (!q->windowHandle()->children().isEmpty()) {
11058 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11059 QWindow *newParentWindow = parentWithWindow ? parentWithWindow->windowHandle() :
nullptr;
11060 for (QObject *child : q->windowHandle()->children()) {
11061 if (QWindow *childWindow = qobject_cast<QWindow *>(child)) {
11062 qCWarning(lcWidgetWindow) <<
"Reparenting" << childWindow
11063 <<
"before destroying" <<
this;
11064 childWindow->setParent(newParentWindow);
11072 q->destroy(
true,
false);
11076 data.window_flags = f;
11077 q->setAttribute(Qt::WA_WState_Created,
false);
11078 q->setAttribute(Qt::WA_WState_Visible,
false);
11079 q->setAttribute(Qt::WA_WState_Hidden,
false);
11081 if (newparent && wasCreated && (q->testAttribute(Qt::WA_NativeWindow) || (f & Qt::Window)))
11084 if (q->isWindow() || (!newparent || newparent->isVisible()) || explicitlyHidden)
11085 q->setAttribute(Qt::WA_WState_Hidden);
11086 q->setAttribute(Qt::WA_WState_ExplicitShowHide, explicitlyHidden);
11089 if (!newparent && targetScreen) {
11091 if (q->testAttribute(Qt::WA_WState_Created))
11092 q->windowHandle()->setScreen(targetScreen);
11094 topData()->initialScreen = targetScreen;
11098void QWidgetPrivate::reparentWidgetWindows(QWidget *parentWithWindow, Qt::WindowFlags windowFlags)
11100 if (QWindow *window = windowHandle()) {
11102 if (parentWithWindow) {
11103 if (windowFlags & Qt::Window) {
11106 QWidget *topLevel = parentWithWindow->window();
11107 auto *transientParent = topLevel->windowHandle();
11108 Q_ASSERT(transientParent);
11109 qCDebug(lcWidgetWindow) <<
"Setting" << window <<
"transient parent to" << transientParent;
11110 window->setTransientParent(transientParent);
11111 window->setParent(
nullptr);
11113 auto *parentWindow = parentWithWindow->windowHandle();
11114 qCDebug(lcWidgetWindow) <<
"Reparenting" << window <<
"into" << parentWindow;
11115 window->setTransientParent(
nullptr);
11116 window->setParent(parentWindow);
11119 qCDebug(lcWidgetWindow) <<
"Making" << window <<
"top level window";
11120 window->setTransientParent(
nullptr);
11121 window->setParent(
nullptr);
11124 reparentWidgetWindowChildren(parentWithWindow);
11128void QWidgetPrivate::reparentWidgetWindowChildren(QWidget *parentWithWindow)
11130 for (
auto *child : std::as_const(children)) {
11131 if (
auto *childWidget = qobject_cast<QWidget*>(child)) {
11132 auto *childPrivate = QWidgetPrivate::get(childWidget);
11135 childPrivate->reparentWidgetWindows(parentWithWindow, childWidget->windowFlags());
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11161void QWidget::scroll(
int dx,
int dy)
11163 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11165 if (dx == 0 && dy == 0)
11168#if QT_CONFIG(graphicsview)
11169 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11173 for (
const QRect &rect : d->dirty)
11174 proxy->update(rect.translated(dx, dy));
11175 proxy->scroll(dx, dy, proxy->subWidgetRect(
this));
11176 d->scrollChildren(dx, dy);
11180 d->setDirtyOpaqueRegion();
11181 d->scroll_sys(dx, dy);
11184void QWidgetPrivate::scroll_sys(
int dx,
int dy)
11187 scrollChildren(dx, dy);
11188 scrollRect(q->rect(), dx, dy);
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201void QWidget::scroll(
int dx,
int dy,
const QRect &r)
11204 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11206 if (dx == 0 && dy == 0)
11209#if QT_CONFIG(graphicsview)
11210 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11214 if (!d->dirty.isEmpty()) {
11215 for (
const QRect &rect : d->dirty.translated(dx, dy) & r)
11216 proxy->update(rect);
11218 proxy->scroll(dx, dy, r.translated(proxy->subWidgetRect(
this).topLeft().toPoint()));
11222 d->scroll_sys(dx, dy, r);
11225void QWidgetPrivate::scroll_sys(
int dx,
int dy,
const QRect &r)
11227 scrollRect(r, dx, dy);
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11246void QWidget::repaint()
11252
11253
11254
11255
11256
11257
11258
11259void QWidget::repaint(
int x,
int y,
int w,
int h)
11261 if (x > data->crect.width() || y > data->crect.height())
11265 w = data->crect.width() - x;
11267 h = data->crect.height() - y;
11269 repaint(QRect(x, y, w, h));
11273
11274
11275
11276void QWidget::repaint(
const QRect &rect)
11283
11284
11285
11286
11287void QWidget::repaint(
const QRegion &rgn)
11293template <
typename T>
11294void QWidgetPrivate::repaint(T r)
11298 if (!q->isVisible() || !q->updatesEnabled() || r.isEmpty())
11301 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11302 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11303 tlwExtra->repaintManager->markDirty(r, q, QWidgetRepaintManager::UpdateNow);
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324void QWidget::update()
11330
11331
11332
11333
11334
11337
11338
11339
11340
11341void QWidget::update(
const QRect &rect)
11348
11349
11350
11351
11352void QWidget::update(
const QRegion &rgn)
11358template <
typename T>
11359void QWidgetPrivate::update(T r)
11363 if (renderToTexture && !q->isVisible()) {
11364 renderToTextureReallyDirty = 1;
11368 if (!q->isVisible() || !q->updatesEnabled())
11371 T clipped = r & q->rect();
11373 if (clipped.isEmpty())
11376 if (q->testAttribute(Qt::WA_WState_InPaintEvent)) {
11377 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(clipped));
11381 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11382 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11383 tlwExtra->repaintManager->markDirty(clipped, q);
11387
11388
11389
11390
11394 if (attribute <
int(8*
sizeof(uint))) {
11396 data->widget_attributes |= (1<<attribute);
11398 data->widget_attributes &= ~(1<<attribute);
11400 const int x = attribute - 8*
sizeof(uint);
11401 const int int_off = x / (8*
sizeof(uint));
11403 d->high_attributes[int_off] |= (1<<(x-(int_off*8*
sizeof(uint))));
11405 d->high_attributes[int_off] &= ~(1<<(x-(int_off*8*
sizeof(uint))));
11410void QWidgetPrivate::macUpdateSizeAttribute()
11413 QEvent event(QEvent::MacSizeChange);
11414 QCoreApplication::sendEvent(q, &event);
11415 for (
int i = 0; i < children.size(); ++i) {
11416 QWidget *w = qobject_cast<QWidget *>(children.at(i));
11417 if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
11418 && !w->testAttribute(Qt::WA_MacMiniSize)
11419 && !w->testAttribute(Qt::WA_MacSmallSize)
11420 && !w->testAttribute(Qt::WA_MacNormalSize))
11421 w->d_func()->macUpdateSizeAttribute();
11428
11429
11430
11431
11432
11433void QWidget::setAttribute(Qt::WidgetAttribute attribute,
bool on)
11437 if (attribute == Qt::WA_ContentsMarginsRespectsSafeArea) {
11439 auto *topExtra = d->topData();
11440 topExtra->explicitContentsMarginsRespectsSafeArea =
true;
11444 if (testAttribute(attribute) == on)
11447 static_assert(
sizeof(d->high_attributes)*8 >= (Qt::WA_AttributeCount -
sizeof(uint)*8),
11448 "QWidget::setAttribute(WidgetAttribute, bool): "
11449 "QWidgetPrivate::high_attributes[] too small to contain all attributes in WidgetAttribute");
11452 if (attribute == Qt::WA_PaintOnScreen && on && !inherits(
"QGLWidget")) {
11455 if (d->noPaintOnScreen)
11461 if (attribute == Qt::WA_NativeWindow && !d->mustHaveWindowHandle) {
11462 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
11463 if (!platformIntegration->hasCapability(QPlatformIntegration::NativeWidgets))
11467 setAttribute_internal(attribute, on, data, d);
11469 switch (attribute) {
11471#if QT_CONFIG(draganddrop)
11472 case Qt::WA_AcceptDrops: {
11473 if (on && !testAttribute(Qt::WA_DropSiteRegistered))
11474 setAttribute(Qt::WA_DropSiteRegistered,
true);
11475 else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)))
11476 setAttribute(Qt::WA_DropSiteRegistered,
false);
11477 QEvent e(QEvent::AcceptDropsChange);
11478 QCoreApplication::sendEvent(
this, &e);
11481 case Qt::WA_DropSiteRegistered: {
11482 for (
int i = 0; i < d->children.size(); ++i) {
11483 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
11484 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_AcceptDrops) && w->testAttribute(Qt::WA_DropSiteRegistered) != on)
11485 w->setAttribute(Qt::WA_DropSiteRegistered, on);
11491 case Qt::WA_NoChildEventsForParent:
11492 d->sendChildEvents = !on;
11494 case Qt::WA_NoChildEventsFromChildren:
11495 d->receiveChildEvents = !on;
11497 case Qt::WA_MacNormalSize:
11498 case Qt::WA_MacSmallSize:
11499 case Qt::WA_MacMiniSize:
11503 const Qt::WidgetAttribute MacSizes[] = { Qt::WA_MacNormalSize, Qt::WA_MacSmallSize,
11504 Qt::WA_MacMiniSize };
11505 for (
int i = 0; i < 3; ++i) {
11506 if (MacSizes[i] != attribute)
11507 setAttribute_internal(MacSizes[i],
false, data, d);
11509 d->macUpdateSizeAttribute();
11513 case Qt::WA_ShowModal:
11516 data->window_modality = Qt::NonModal;
11517 }
else if (data->window_modality == Qt::NonModal) {
11520 data->window_modality = Qt::ApplicationModal;
11525 if (testAttribute(Qt::WA_WState_Created)) {
11530 case Qt::WA_MouseTracking: {
11531 QEvent e(QEvent::MouseTrackingChange);
11532 QCoreApplication::sendEvent(
this, &e);
11534 case Qt::WA_TabletTracking: {
11535 QEvent e(QEvent::TabletTrackingChange);
11536 QCoreApplication::sendEvent(
this, &e);
11538 case Qt::WA_NativeWindow: {
11539 d->createTLExtra();
11541 d->createTLSysExtra();
11543 QWidget *focusWidget = d->effectiveFocusWidget();
11544 if (on && !internalWinId() &&
this == QGuiApplication::focusObject()
11545 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11546 QGuiApplication::inputMethod()->commit();
11547 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11549 if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget())
11550 parentWidget()->d_func()->enforceNativeChildren();
11551 if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created))
11553 if (isEnabled() && focusWidget->isEnabled() &&
this == QGuiApplication::focusObject()
11554 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11555 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11560 case Qt::WA_PaintOnScreen:
11561 d->updateIsOpaque();
11563 case Qt::WA_OpaquePaintEvent:
11564 d->updateIsOpaque();
11566 case Qt::WA_NoSystemBackground:
11567 d->updateIsOpaque();
11569 case Qt::WA_UpdatesDisabled:
11570 d->updateSystemBackground();
11572 case Qt::WA_TransparentForMouseEvents:
11574 case Qt::WA_InputMethodEnabled: {
11576 if (QGuiApplication::focusObject() ==
this) {
11578 QGuiApplication::inputMethod()->commit();
11579 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11584 case Qt::WA_WindowPropagation:
11585 d->resolvePalette();
11587 d->resolveLocale();
11589 case Qt::WA_DontShowOnScreen: {
11590 if (on && isVisible()) {
11600 case Qt::WA_X11NetWmWindowTypeDesktop:
11601 case Qt::WA_X11NetWmWindowTypeDock:
11602 case Qt::WA_X11NetWmWindowTypeToolBar:
11603 case Qt::WA_X11NetWmWindowTypeMenu:
11604 case Qt::WA_X11NetWmWindowTypeUtility:
11605 case Qt::WA_X11NetWmWindowTypeSplash:
11606 case Qt::WA_X11NetWmWindowTypeDialog:
11607 case Qt::WA_X11NetWmWindowTypeDropDownMenu:
11608 case Qt::WA_X11NetWmWindowTypePopupMenu:
11609 case Qt::WA_X11NetWmWindowTypeToolTip:
11610 case Qt::WA_X11NetWmWindowTypeNotification:
11611 case Qt::WA_X11NetWmWindowTypeCombo:
11612 case Qt::WA_X11NetWmWindowTypeDND:
11613 d->setNetWmWindowTypes();
11616 case Qt::WA_StaticContents:
11617 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
11619 repaintManager->addStaticWidget(
this);
11621 repaintManager->removeStaticWidget(
this);
11624 case Qt::WA_TranslucentBackground:
11626 setAttribute(Qt::WA_NoSystemBackground);
11627 d->updateIsTranslucent();
11630 case Qt::WA_AcceptTouchEvents:
11638
11639
11640
11641
11642
11643
11644bool QWidget::testAttribute_helper(Qt::WidgetAttribute attribute)
const
11646 Q_D(
const QWidget);
11647 const int x = attribute - 8*
sizeof(uint);
11648 const int int_off = x / (8*
sizeof(uint));
11649 return (d->high_attributes[int_off] & (1<<(x-(int_off*8*
sizeof(uint)))));
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677qreal QWidget::windowOpacity()
const
11679 Q_D(
const QWidget);
11680 return (isWindow() && d->maybeTopData()) ? d->maybeTopData()->opacity / 255. : 1.0;
11683void QWidget::setWindowOpacity(qreal opacity)
11689 opacity = qBound(qreal(0.0), opacity, qreal(1.0));
11690 QTLWExtra *extra = d->topData();
11691 extra->opacity = uint(opacity * 255);
11692 setAttribute(Qt::WA_WState_WindowOpacitySet);
11693 d->setWindowOpacity_sys(opacity);
11695 if (!testAttribute(Qt::WA_WState_Created))
11698#if QT_CONFIG(graphicsview)
11699 if (QGraphicsProxyWidget *proxy = graphicsProxyWidget()) {
11701 if (proxy->cacheMode() == QGraphicsItem::NoCache)
11703 else if (QGraphicsScene *scene = proxy->scene())
11704 scene->update(proxy->sceneBoundingRect());
11710void QWidgetPrivate::setWindowOpacity_sys(qreal level)
11713 if (q->windowHandle())
11714 q->windowHandle()->setOpacity(level);
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741bool QWidget::isWindowModified()
const
11743 return testAttribute(Qt::WA_WindowModified);
11746void QWidget::setWindowModified(
bool mod)
11749 setAttribute(Qt::WA_WindowModified, mod);
11751 d->setWindowModified_helper();
11753 QEvent e(QEvent::ModifiedChange);
11754 QCoreApplication::sendEvent(
this, &e);
11757void QWidgetPrivate::setWindowModified_helper()
11760 QWindow *window = q->windowHandle();
11763 QPlatformWindow *platformWindow = window->handle();
11764 if (!platformWindow)
11766 bool on = q->testAttribute(Qt::WA_WindowModified);
11767 if (!platformWindow->setWindowModified(on)) {
11768 if (Q_UNLIKELY(on && !q->windowTitle().contains(
"[*]"_L1)))
11769 qWarning(
"QWidget::setWindowModified: The window title does not contain a '[*]' placeholder");
11770 setWindowTitle_helper(q->windowTitle());
11771 setWindowIconText_helper(q->windowIconText());
11775#if QT_CONFIG(tooltip)
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794void QWidget::setToolTip(
const QString &s)
11799 QEvent event(QEvent::ToolTipChange);
11800 QCoreApplication::sendEvent(
this, &event);
11803QString QWidget::toolTip()
const
11805 Q_D(
const QWidget);
11810
11811
11812
11813
11814
11815
11816
11817
11818
11820void QWidget::setToolTipDuration(
int msec)
11823 d->toolTipDuration = msec;
11826int QWidget::toolTipDuration()
const
11828 Q_D(
const QWidget);
11829 return d->toolTipDuration;
11835#if QT_CONFIG(statustip)
11837
11838
11839
11840
11841
11842
11843
11844void QWidget::setStatusTip(
const QString &s)
11850QString QWidget::statusTip()
const
11852 Q_D(
const QWidget);
11853 return d->statusTip;
11857#if QT_CONFIG(whatsthis)
11859
11860
11861
11862
11863
11864
11865
11866
11867void QWidget::setWhatsThis(
const QString &s)
11873QString QWidget::whatsThis()
const
11875 Q_D(
const QWidget);
11876 return d->whatsThis;
11880#if QT_CONFIG(accessibility)
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902void QWidget::setAccessibleName(
const QString &name)
11905 if (d->accessibleName == name)
11908 d->accessibleName = name;
11909 QAccessibleEvent event(
this, QAccessible::NameChanged);
11910 QAccessible::updateAccessibility(&event);
11913QString QWidget::accessibleName()
const
11915 Q_D(
const QWidget);
11916 return d->accessibleName;
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935void QWidget::setAccessibleDescription(
const QString &description)
11938 if (d->accessibleDescription == description)
11941 d->accessibleDescription = description;
11942 QAccessibleEvent event(
this, QAccessible::DescriptionChanged);
11943 QAccessible::updateAccessibility(&event);
11946QString QWidget::accessibleDescription()
const
11948 Q_D(
const QWidget);
11949 return d->accessibleDescription;
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963void QWidget::setAccessibleIdentifier(
const QString &identifier)
11966 if (d->accessibleIdentifier == identifier)
11969 d->accessibleIdentifier = identifier;
11970 QAccessibleEvent event(
this, QAccessible::IdentifierChanged);
11971 QAccessible::updateAccessibility(&event);
11974QString QWidget::accessibleIdentifier()
const
11976 Q_D(
const QWidget);
11977 return d->accessibleIdentifier;
11982#ifndef QT_NO_SHORTCUT
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006int QWidget::grabShortcut(
const QKeySequence &key, Qt::ShortcutContext context)
12011 setAttribute(Qt::WA_GrabbedShortcut);
12012 return QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(
this, key, context, qWidgetShortcutContextMatcher);
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030void QWidget::releaseShortcut(
int id)
12034 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id,
this, 0);
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049void QWidget::setShortcutEnabled(
int id,
bool enable)
12053 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id,
this, 0);
12057
12058
12059
12060
12061
12062
12063
12064void QWidget::setShortcutAutoRepeat(
int id,
bool enable)
12068 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(enable, id,
this, 0);
12073
12074
12075
12076void QWidget::updateMicroFocus(Qt::InputMethodQuery query)
12078 if (
this == QGuiApplication::focusObject())
12079 QGuiApplication::inputMethod()->update(query);
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12094void QWidget::raise()
12098 QWidget *p = parentWidget();
12099 const int parentChildCount = p->d_func()->children.size();
12100 if (parentChildCount < 2)
12102 const int from = p->d_func()->children.indexOf(
this);
12103 Q_ASSERT(from >= 0);
12105 if (from != parentChildCount -1)
12106 p->d_func()->children.move(from, parentChildCount - 1);
12107 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12109 else if (from == parentChildCount - 1)
12112 QRegion region(rect());
12113 d->subtractOpaqueSiblings(region);
12114 d->invalidateBackingStore(region);
12116 if (testAttribute(Qt::WA_WState_Created))
12119 if (d->extra && d->extra->hasWindowContainer)
12120 QWindowContainer::parentWasRaised(
this);
12122 QEvent e(QEvent::ZOrderChange);
12123 QCoreApplication::sendEvent(
this, &e);
12126void QWidgetPrivate::raise_sys()
12129 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12130 q->windowHandle()->raise();
12131 }
else if (renderToTexture) {
12132 if (QWidget *p = q->parentWidget()) {
12133 setDirtyOpaqueRegion();
12134 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12140
12141
12142
12143
12144
12145
12146
12148void QWidget::lower()
12152 QWidget *p = parentWidget();
12153 const int parentChildCount = p->d_func()->children.size();
12154 if (parentChildCount < 2)
12156 const int from = p->d_func()->children.indexOf(
this);
12157 Q_ASSERT(from >= 0);
12160 p->d_func()->children.move(from, 0);
12161 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12163 else if (from == 0)
12166 if (testAttribute(Qt::WA_WState_Created))
12169 if (d->extra && d->extra->hasWindowContainer)
12170 QWindowContainer::parentWasLowered(
this);
12172 QEvent e(QEvent::ZOrderChange);
12173 QCoreApplication::sendEvent(
this, &e);
12176void QWidgetPrivate::lower_sys()
12179 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12180 Q_ASSERT(q->testAttribute(Qt::WA_WState_Created));
12181 q->windowHandle()->lower();
12182 }
else if (QWidget *p = q->parentWidget()) {
12183 setDirtyOpaqueRegion();
12184 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12189
12190
12191
12192
12193
12194
12195void QWidget::stackUnder(QWidget* w)
12198 QWidget *p = parentWidget();
12199 if (!w || isWindow() || p != w->parentWidget() ||
this == w)
12202 int from = p->d_func()->children.indexOf(
this);
12203 int to = p->d_func()->children.indexOf(w);
12204 Q_ASSERT(from >= 0);
12210 p->d_func()->children.move(from, to);
12211 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12213 else if (from == to)
12216 if (testAttribute(Qt::WA_WState_Created))
12217 d->stackUnder_sys(w);
12219 QEvent e(QEvent::ZOrderChange);
12220 QCoreApplication::sendEvent(
this, &e);
12223void QWidgetPrivate::stackUnder_sys(QWidget*)
12226 if (QWidget *p = q->parentWidget()) {
12227 setDirtyOpaqueRegion();
12228 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12233
12234
12235
12236
12237
12240
12241
12242
12245
12246
12247
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12263
12264
12265
12266
12267
12268
12269
12270
12272QRect QWidgetPrivate::frameStrut()
const
12274 Q_Q(
const QWidget);
12275 if (!q->isWindow() || q->testAttribute(Qt::WA_DontShowOnScreen)) {
12277 return QRect(0, 0, 1, 1);
12280 if (data.fstrut_dirty
12283 && q->testAttribute(Qt::WA_WState_Created))
12284 const_cast<QWidgetPrivate *>(
this)->updateFrameStrut();
12286 return maybeTopData() ? maybeTopData()->frameStrut : QRect();
12289void QWidgetPrivate::updateFrameStrut()
12292 if (q->data->fstrut_dirty) {
12293 if (QTLWExtra *te = maybeTopData()) {
12294 if (te->window && te->window->handle()) {
12295 const QMargins margins = te->window->frameMargins();
12296 if (!margins.isNull()) {
12297 te->frameStrut.setCoords(margins.left(), margins.top(), margins.right(), margins.bottom());
12298 q->data->fstrut_dirty =
false;
12305#ifdef QT_KEYPAD_NAVIGATION
12307
12308
12309
12310
12311
12312
12313
12314bool QWidgetPrivate::navigateToDirection(Direction direction)
12316 QWidget *targetWidget = widgetInNavigationDirection(direction);
12318 targetWidget->setFocus();
12319 return (targetWidget != 0);
12323
12324
12325
12326
12327
12328
12329
12330
12331QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
12333 const QWidget *sourceWidget = QApplication::focusWidget();
12336 const QRect sourceRect = sourceWidget->rect().translated(sourceWidget->mapToGlobal(QPoint()));
12337 const int sourceX =
12338 (direction == DirectionNorth || direction == DirectionSouth) ?
12339 (sourceRect.left() + (sourceRect.right() - sourceRect.left()) / 2)
12340 :(direction == DirectionEast ? sourceRect.right() : sourceRect.left());
12341 const int sourceY =
12342 (direction == DirectionEast || direction == DirectionWest) ?
12343 (sourceRect.top() + (sourceRect.bottom() - sourceRect.top()) / 2)
12344 :(direction == DirectionSouth ? sourceRect.bottom() : sourceRect.top());
12345 const QPoint sourcePoint(sourceX, sourceY);
12346 const QPoint sourceCenter = sourceRect.center();
12347 const QWidget *sourceWindow = sourceWidget->window();
12349 QWidget *targetWidget =
nullptr;
12350 int shortestDistance = INT_MAX;
12352 const auto targetCandidates = QApplication::allWidgets();
12353 for (QWidget *targetCandidate : targetCandidates) {
12355 const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));
12360 if (targetCandidate->focusProxy() || targetCandidateRect.isEmpty())
12364 if ( targetCandidate != sourceWidget
12366 && targetCandidate->focusPolicy() & Qt::TabFocus
12368 && !(direction == DirectionNorth && targetCandidateRect.bottom() > sourceRect.top())
12370 && !(direction == DirectionEast && targetCandidateRect.left() < sourceRect.right())
12372 && !(direction == DirectionSouth && targetCandidateRect.top() < sourceRect.bottom())
12374 && !(direction == DirectionWest && targetCandidateRect.right() > sourceRect.left())
12376 && targetCandidate->isEnabled()
12378 && targetCandidate->isVisible()
12380 && targetCandidate->window() == sourceWindow) {
12381 const int targetCandidateDistance = [](
const QPoint &sourcePoint,
12382 const QRect &targetCandidateRect) {
12385 if (p.x() < r.left())
12386 dx = r.left() - p.x();
12387 else if (p.x() > r.right())
12388 dx = p.x() - r.right();
12389 if (p.y() < r.top())
12390 dy = r.top() - p.y();
12391 else if (p.y() > r.bottom())
12392 dy = p.y() - r.bottom();
12395 if (targetCandidateDistance < shortestDistance) {
12396 shortestDistance = targetCandidateDistance;
12397 targetWidget = targetCandidate;
12401 return targetWidget;
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415bool QWidgetPrivate::canKeypadNavigate(Qt::Orientation orientation)
12417 return orientation == Qt::Horizontal?
12418 (QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionEast)
12419 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionWest))
12420 :(QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionNorth)
12421 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionSouth));
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435bool QWidgetPrivate::inTabWidget(QWidget *widget)
12437 for (QWidget *tabWidget = widget; tabWidget; tabWidget = tabWidget->parentWidget())
12438 if (qobject_cast<
const QTabWidget*>(tabWidget))
12445
12446
12447
12448
12449
12450
12451void QWidget::setBackingStore(QBackingStore *store)
12460 QTLWExtra *topData = d->topData();
12461 if (topData->backingStore == store)
12464 QBackingStore *oldStore = topData->backingStore;
12465 delete topData->backingStore;
12466 topData->backingStore = store;
12468 QWidgetRepaintManager *repaintManager = d->maybeRepaintManager();
12469 if (!repaintManager)
12473 if (repaintManager->backingStore() != oldStore && repaintManager->backingStore() != store)
12474 delete repaintManager->backingStore();
12475 repaintManager->setBackingStore(store);
12480
12481
12482
12483
12484QBackingStore *QWidget::backingStore()
const
12486 Q_D(
const QWidget);
12487 QTLWExtra *extra = d->maybeTopData();
12488 if (extra && extra->backingStore)
12489 return extra->backingStore;
12492 return window()->backingStore();
12497void QWidgetPrivate::getLayoutItemMargins(
int *left,
int *top,
int *right,
int *bottom)
const
12500 *left = (
int)leftLayoutItemMargin;
12502 *top = (
int)topLayoutItemMargin;
12504 *right = (
int)rightLayoutItemMargin;
12506 *bottom = (
int)bottomLayoutItemMargin;
12509void QWidgetPrivate::setLayoutItemMargins(
int left,
int top,
int right,
int bottom)
12511 if (leftLayoutItemMargin == left
12512 && topLayoutItemMargin == top
12513 && rightLayoutItemMargin == right
12514 && bottomLayoutItemMargin == bottom)
12518 leftLayoutItemMargin = (
signed char)left;
12519 topLayoutItemMargin = (
signed char)top;
12520 rightLayoutItemMargin = (
signed char)right;
12521 bottomLayoutItemMargin = (
signed char)bottom;
12522 q->updateGeometry();
12525void QWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element,
const QStyleOption *opt)
12528 QStyleOption myOpt;
12531 myOpt.rect.setRect(0, 0, 32768, 32768);
12535 QRect liRect = q->style()->subElementRect(element, opt, q);
12536 if (liRect.isValid()) {
12537 leftLayoutItemMargin = (
signed char)(opt->rect.left() - liRect.left());
12538 topLayoutItemMargin = (
signed char)(opt->rect.top() - liRect.top());
12539 rightLayoutItemMargin = (
signed char)(liRect.right() - opt->rect.right());
12540 bottomLayoutItemMargin = (
signed char)(liRect.bottom() - opt->rect.bottom());
12542 leftLayoutItemMargin = 0;
12543 topLayoutItemMargin = 0;
12544 rightLayoutItemMargin = 0;
12545 bottomLayoutItemMargin = 0;
12549void QWidgetPrivate::adjustQuitOnCloseAttribute()
12553 if (!q->parentWidget()) {
12554 Qt::WindowType type = q->windowType();
12555 if (type == Qt::Widget || type == Qt::SubWindow)
12557 if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
12558 q->setAttribute(Qt::WA_QuitOnClose,
false);
12562void QWidgetPrivate::sendComposeStatus(QWidget *w,
bool end)
12564 QWidgetPrivate *wd = QWidgetPrivate::get(w);
12565 if (!wd->textureChildSeen)
12570 wd->beginCompose();
12571 for (
int i = 0; i < wd->children.size(); ++i) {
12572 w = qobject_cast<QWidget *>(wd->children.at(i));
12573 if (w && !w->isWindow() && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen)
12574 sendComposeStatus(w, end);
12578Q_WIDGETS_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget)
12580 return widget->data;
12585 return widget->d_func();
12589#if QT_CONFIG(graphicsview)
12591
12592
12593
12594
12595
12596
12597
12598
12599QGraphicsProxyWidget *QWidget::graphicsProxyWidget()
const
12601 Q_D(
const QWidget);
12603 return d->extra->proxyWidget;
12609#ifndef QT_NO_GESTURES
12611
12612
12613
12614
12615
12616void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
12619 d->gestureContext.insert(gesture, flags);
12620 (
void)QGestureManager::instance();
12624
12625
12626
12627
12628
12629void QWidget::ungrabGesture(Qt::GestureType gesture)
12633 if (d->gestureContext.remove(gesture)) {
12634 if (QGestureManager *manager = QGestureManager::instance())
12635 manager->cleanupCachedGestures(
this, gesture);
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653void QWidget::destroy(
bool destroyWindow,
bool destroySubWindows)
12657 d->aboutToDestroy();
12658 if (!isWindow() && parentWidget())
12659 parentWidget()->d_func()->invalidateBackingStore(d->effectiveRectFor(geometry()));
12660 d->deactivateWidgetCleanup();
12662 if ((windowType() == Qt::Popup) &&
qApp)
12663 qApp->d_func()->closePopup(
this);
12665 if (
this ==
qApp->activeWindow())
12666 QApplicationPrivate::setActiveWindow(
nullptr);
12667 if (QWidget::mouseGrabber() ==
this)
12669 if (QWidget::keyboardGrabber() ==
this)
12672 setAttribute(Qt::WA_WState_Created,
false);
12674 if (destroySubWindows) {
12675 QObjectList childList(children());
12676 for (
int i = 0; i < childList.size(); i++) {
12677 QWidget *widget = qobject_cast<QWidget *>(childList.at(i));
12678 if (widget && widget->testAttribute(Qt::WA_NativeWindow)) {
12679 if (widget->windowHandle()) {
12685 if (destroyWindow) {
12686 d->deleteTLSysExtra();
12688 if (parentWidget() && parentWidget()->testAttribute(Qt::WA_WState_Created)) {
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706QPaintEngine *QWidget::paintEngine()
const
12708 qWarning(
"QWidget::paintEngine: Should no longer be called");
12725 const_cast<QWidgetPrivate *>(d_func())->noPaintOnScreen = 1;
12734 return window->handle() && !qt_window_private(window)->resizeEventPending;
12737#if QT_CONFIG(graphicsview)
12738static inline QGraphicsProxyWidget *graphicsProxyWidget(
const QWidget *w)
12740 QGraphicsProxyWidget *result =
nullptr;
12741 const QWidgetPrivate *d = qt_widget_private(
const_cast<QWidget *>(w));
12743 result = d->extra->proxyWidget;
12757 for ( ; w ; w = w->parentWidget()) {
12758#if QT_CONFIG(graphicsview)
12759 if (QGraphicsProxyWidget *qgpw = graphicsProxyWidget(w)) {
12760 if (
const QGraphicsScene *scene = qgpw->scene()) {
12761 const QList <QGraphicsView *> views = scene->views();
12762 if (!views.isEmpty()) {
12763 auto *viewP =
static_cast<QGraphicsViewPrivate *>(qt_widget_private(views.constFirst()));
12764 result.transform *= viewP->mapToViewTransform(qgpw);
12765 w = views.first()->viewport();
12770 QWindow *window = w->windowHandle();
12776 const auto &geometry = w->geometry();
12777 result.transform *= QTransform::fromTranslate(geometry.x(), geometry.y());
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794QPointF QWidget::mapToGlobal(
const QPointF &pos)
const
12796 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12797 const QPointF g = t.transform.map(pos);
12798 return t.window ? t.window->mapToGlobal(g) : g;
12802
12803
12804QPoint QWidget::mapToGlobal(
const QPoint &pos)
const
12806 return mapToGlobal(QPointF(pos)).toPoint();
12810
12811
12812
12813
12814
12815
12816
12817
12818QPointF QWidget::mapFromGlobal(
const QPointF &pos)
const
12820 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12821 const QPointF windowLocal = t.window ? t.window->mapFromGlobal(pos) : pos;
12822 return t.transform.inverted().map(windowLocal);
12826
12827
12828QPoint QWidget::mapFromGlobal(
const QPoint &pos)
const
12830 return mapFromGlobal(QPointF(pos)).toPoint();
12839 QWindow *window = w->windowHandle();
12841 if (
const QWidget *nativeParent = w->nativeParentWidget())
12842 window = nativeParent->windowHandle();
12846#ifndef QT_NO_CURSOR
12857#ifndef QT_NO_CURSOR
12860 QGuiApplication::setOverrideCursor(*cursor);
12863 window->setMouseGrabEnabled(
true);
12873#ifndef QT_NO_CURSOR
12875 QGuiApplication::restoreOverrideCursor();
12879 window->setMouseGrabEnabled(
false);
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913void QWidget::grabMouse()
12915 grabMouseForWidget(
this);
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934#ifndef QT_NO_CURSOR
12935void QWidget::grabMouse(
const QCursor &cursor)
12937 grabMouseForWidget(
this, &cursor);
12941bool QWidgetPrivate::stealMouseGrab(
bool grab)
12946 QWindow *window = grabberWindow(q);
12947 return window ? window->setMouseGrabEnabled(grab) :
false;
12951
12952
12953
12954
12955
12956
12957void QWidget::releaseMouse()
12959 releaseMouseGrabOfWidget(
this);
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981void QWidget::grabKeyboard()
12984 keyboardGrb->releaseKeyboard();
12985 if (QWindow *window = grabberWindow(
this))
12986 window->setKeyboardGrabEnabled(
true);
12987 keyboardGrb =
this;
12990bool QWidgetPrivate::stealKeyboardGrab(
bool grab)
12995 QWindow *window = grabberWindow(q);
12996 return window ? window->setKeyboardGrabEnabled(grab) :
false;
13000
13001
13002
13003
13004
13005
13006void QWidget::releaseKeyboard()
13008 if (keyboardGrb ==
this) {
13009 if (QWindow *window = grabberWindow(
this))
13010 window->setKeyboardGrabEnabled(
false);
13011 keyboardGrb =
nullptr;
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025QWidget *QWidget::mouseGrabber()
13027 return qt_mouseGrb;
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040QWidget *QWidget::keyboardGrabber()
13042 return keyboardGrb;
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069void QWidget::activateWindow()
13071 QWindow *
const wnd = window()->windowHandle();
13074 wnd->requestActivate();
13078
13079
13080
13081
13082
13083
13084int QWidget::metric(PaintDeviceMetric m)
const
13086 QScreen *screen =
this->screen();
13089 if (m == PdmDpiX || m == PdmDpiY)
13091 return QPaintDevice::metric(m);
13094 auto resolveDevicePixelRatio = [
this, screen]() -> qreal {
13097 static bool downscale = qEnvironmentVariableIntValue(
"QT_WIDGETS_HIGHDPI_DOWNSCALE") > 0;
13098 QWindow *window =
this->window()->windowHandle();
13100 return downscale ? std::ceil(window->devicePixelRatio()) : window->devicePixelRatio();
13101 return screen->devicePixelRatio();
13106 return data->crect.width();
13108 return data->crect.width() * screen->physicalSize().width() / screen->geometry().width();
13110 return data->crect.height();
13112 return data->crect.height() * screen->physicalSize().height() / screen->geometry().height();
13114 return screen->depth();
13116 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13117 if (p->d_func()->extra && p->d_func()->extra->customDpiX)
13118 return p->d_func()->extra->customDpiX;
13120 return qRound(screen->logicalDotsPerInchX());
13122 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13123 if (p->d_func()->extra && p->d_func()->extra->customDpiY)
13124 return p->d_func()->extra->customDpiY;
13126 return qRound(screen->logicalDotsPerInchY());
13127 case PdmPhysicalDpiX:
13128 return qRound(screen->physicalDotsPerInchX());
13129 case PdmPhysicalDpiY:
13130 return qRound(screen->physicalDotsPerInchY());
13131 case PdmDevicePixelRatio:
13132 return resolveDevicePixelRatio();
13133 case PdmDevicePixelRatioScaled:
13134 return QPaintDevice::devicePixelRatioFScale() * resolveDevicePixelRatio();
13135 case PdmDevicePixelRatioF_EncodedA:
13137 case PdmDevicePixelRatioF_EncodedB:
13138 return QPaintDevice::encodeMetricF(m, resolveDevicePixelRatio());
13142 return QPaintDevice::metric(m);
13146
13147
13148
13149
13150void QWidget::initPainter(QPainter *painter)
const
13152 const QPalette &pal = palette();
13153 QPainterPrivate *painterPrivate = QPainterPrivate::get(painter);
13155 painterPrivate->state->pen = QPen(pal.brush(foregroundRole()), 1);
13156 painterPrivate->state->bgBrush = pal.brush(backgroundRole());
13157 QFont f(font(),
this);
13158 painterPrivate->state->deviceFont = f;
13159 painterPrivate->state->font = f;
13161 painterPrivate->setEngineDirtyFlags({
13162 QPaintEngine::DirtyPen,
13163 QPaintEngine::DirtyBrush,
13164 QPaintEngine::DirtyFont,
13167 if (painterPrivate->extended)
13168 painterPrivate->extended->penChanged();
13172
13173
13174
13175
13176QPaintDevice *QWidget::redirected(QPoint *offset)
const
13178 return d_func()->redirected(offset);
13182
13183
13184
13185
13186QPainter *QWidget::sharedPainter()
const
13189 if (!d_func()->redirectDev)
13192 QPainter *sp = d_func()->sharedPainter();
13193 if (!sp || !sp->isActive())
13196 if (sp->paintEngine()->paintDevice() != d_func()->redirectDev)
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224void QWidget::setMask(
const QRegion &newMask)
13229 if (newMask == d->extra->mask)
13232#ifndef QT_NO_BACKINGSTORE
13233 const QRegion oldMask(d->extra->mask);
13236 d->extra->mask = newMask;
13237 d->extra->hasMask = !newMask.isEmpty();
13239 if (!testAttribute(Qt::WA_WState_Created))
13242 d->setMask_sys(newMask);
13244#ifndef QT_NO_BACKINGSTORE
13248 if (!d->extra->hasMask) {
13250 QRegion expose(rect());
13252 if (!expose.isEmpty()) {
13253 d->setDirtyOpaqueRegion();
13261 QRegion parentExpose(rect());
13262 parentExpose -= newMask;
13263 if (!parentExpose.isEmpty()) {
13264 d->setDirtyOpaqueRegion();
13265 parentExpose.translate(data->crect.topLeft());
13266 parentWidget()->update(parentExpose);
13270 if (!oldMask.isEmpty())
13271 update(newMask - oldMask);
13276void QWidgetPrivate::setMask_sys(
const QRegion ®ion)
13279 if (QWindow *window = q->windowHandle())
13280 window->setMask(region);
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308void QWidget::setMask(
const QBitmap &bitmap)
13310 setMask(QRegion(bitmap));
13314
13315
13316
13317
13318
13319
13320void QWidget::clearMask()
13323 if (!d->extra || !d->extra->hasMask)
13325 setMask(QRegion());
13328void QWidgetPrivate::setWidgetParentHelper(QObject *widgetAsObject, QObject *newParent)
13330 Q_ASSERT(widgetAsObject->isWidgetType());
13331 Q_ASSERT(!newParent || newParent->isWidgetType());
13332 QWidget *widget =
static_cast<QWidget*>(widgetAsObject);
13333 widget->setParent(
static_cast<QWidget*>(newParent));
13336std::string QWidgetPrivate::flagsForDumping()
const
13338 Q_Q(
const QWidget);
13339 std::string flags = QObjectPrivate::flagsForDumping();
13340 if (QApplication::focusWidget() == q)
13342 if (q->isVisible()) {
13343 std::stringstream s;
13345 << q->width() <<
'x' << q->height()
13346 << std::showpos << q->x() << q->y()
13355void QWidgetPrivate::setNetWmWindowTypes(
bool skipIfMissing)
13360 if (!q->windowHandle())
13363 QXcbWindow::WindowTypes wmWindowType = QXcbWindow::None;
13364 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDesktop))
13365 wmWindowType |= QXcbWindow::Desktop;
13366 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDock))
13367 wmWindowType |= QXcbWindow::Dock;
13368 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolBar))
13369 wmWindowType |= QXcbWindow::Toolbar;
13370 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeMenu))
13371 wmWindowType |= QXcbWindow::Menu;
13372 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeUtility))
13373 wmWindowType |= QXcbWindow::Utility;
13374 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeSplash))
13375 wmWindowType |= QXcbWindow::Splash;
13376 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDialog))
13377 wmWindowType |= QXcbWindow::Dialog;
13378 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu))
13379 wmWindowType |= QXcbWindow::DropDownMenu;
13380 if (q->testAttribute(Qt::WA_X11NetWmWindowTypePopupMenu))
13381 wmWindowType |= QXcbWindow::PopupMenu;
13382 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip))
13383 wmWindowType |= QXcbWindow::Tooltip;
13384 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeNotification))
13385 wmWindowType |= QXcbWindow::Notification;
13386 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeCombo))
13387 wmWindowType |= QXcbWindow::Combo;
13388 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDND))
13389 wmWindowType |= QXcbWindow::Dnd;
13391 if (wmWindowType == QXcbWindow::None && skipIfMissing)
13394 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(q->windowHandle()->handle()))
13395 xcbWindow->setWindowType(wmWindowType);
13397 Q_UNUSED(skipIfMissing);
13402
13403
13404
13405
13406bool QWidgetPrivate::hasChildWithFocusPolicy(Qt::FocusPolicy policy,
const QWidget *excludeChildrenOf)
const
13408 Q_Q(
const QWidget);
13409 const QWidgetList &children = q->findChildren<QWidget *>(Qt::FindChildrenRecursively);
13410 for (
const auto *child : children) {
13411 if (child->focusPolicy() == policy && child->isEnabled()
13412 && (!excludeChildrenOf || !excludeChildrenOf->isAncestorOf(child))) {
13419#ifndef QT_NO_DEBUG_STREAM
13422QDebug operator<<(QDebug debug,
const WidgetAttributes &attributes)
13424 const QDebugStateSaver saver(debug);
13427 if (
const QWidget *widget = attributes.widget) {
13428 const QMetaObject *qtMo = qt_getEnumMetaObject(Qt::WA_AttributeCount);
13429 const QMetaEnum me = qtMo->enumerator(qtMo->indexOfEnumerator(
"WidgetAttribute"));
13431 for (
int a = 0; a < Qt::WA_AttributeCount; ++a) {
13432 if (widget->testAttribute(
static_cast<Qt::WidgetAttribute>(a))) {
13435 debug << me.valueToKey(a);
13446 const QDebugStateSaver saver(debug);
13449 debug << widget->metaObject()->className() <<
'(' << (
const void *)widget;
13450 if (!widget->objectName().isEmpty())
13451 debug <<
", name=" << widget->objectName();
13452 if (debug.verbosity() > 2) {
13453 const QRect geometry = widget->geometry();
13454 const QRect frameGeometry = widget->frameGeometry();
13455 if (widget->isVisible())
13456 debug <<
", visible";
13457 if (!widget->isEnabled())
13458 debug <<
", disabled";
13459 debug <<
", states=" << widget->windowState()
13460 <<
", type=" << widget->windowType() <<
", flags=" << widget->windowFlags();
13461 debug <<
", attributes=" << WidgetAttributes{widget};
13462 if (widget->isWindow())
13463 debug <<
", window";
13464 debug <<
", " << geometry.width() <<
'x' << geometry.height()
13465 << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
13466 if (frameGeometry != geometry) {
13467 const QMargins margins(geometry.x() - frameGeometry.x(),
13468 geometry.y() - frameGeometry.y(),
13469 frameGeometry.right() - geometry.right(),
13470 frameGeometry.bottom() - geometry.bottom());
13471 debug <<
", margins=" << margins;
13473 debug <<
", devicePixelRatio=" << widget->devicePixelRatio();
13474 if (
const WId wid = widget->internalWinId())
13475 debug <<
", winId=0x" << Qt::hex << wid << Qt::dec;
13479 debug <<
"QWidget(0x0)";
13488#define FOCUS_NEXT(w) w->d_func()->focus_next
13489#define FOCUS_PREV(w) w->d_func()->focus_prev
13492
13493
13494
13495
13496QWidget *QWidgetPrivate::nextPrevElementInFocusChain(FocusDirection direction)
const
13498 Q_Q(
const QWidget);
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513bool QWidgetPrivate::removeFromFocusChain(FocusChainRemovalRules rules, FocusDirection direction)
13516 if (!isFocusChainConsistent()) {
13518 if (rules.testFlag(FocusChainRemovalRule::AssertConsistency))
13519 qFatal() << q <<
"has inconsistent focus chain.";
13521 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because of inconsistent focus chain.";
13525 if (!isInFocusChain()) {
13526 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because it is not part of a focus chain.";
13530 if (rules.testFlag(FocusChainRemovalRule::EnsureFocusOut))
13531 q->focusNextPrevChild(direction == FocusDirection::Next);
13536 qCDebug(lcWidgetFocus) << q <<
"removed from focus chain.";
13541
13542
13543
13544void QWidgetPrivate::initFocusChain()
13547 qCDebug(lcWidgetFocus) <<
"Initializing focus chain of" << q;
13553
13554
13555
13556
13557
13558
13559void QWidgetPrivate::reparentFocusChildren(FocusDirection direction)
13564 QWidget *firstOld =
nullptr;
13565 QWidget *lastOld =
nullptr;
13566 QWidget *lastNew = q;
13567 bool prevWasNew =
true;
13568 QWidget *widget = nextPrevElementInFocusChain(direction);
13573 while (widget != q) {
13574 bool currentIsNew = q->isAncestorOf(widget);
13575 if (currentIsNew) {
13595 widget = widget->d_func()->nextPrevElementInFocusChain(direction);
13596 prevWasNew = currentIsNew;
13605 if (!q->isWindow()) {
13606 QWidget *topLevel = q->window();
13621
13622
13623
13624
13625
13626bool QWidgetPrivate::insertIntoFocusChain(FocusDirection direction, QWidget *position)
13629 Q_ASSERT(position);
13633 switch (direction) {
13634 case FocusDirection::Next:
13635 if (previous == position) {
13636 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already before" << position;
13640 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13646 qCDebug(lcWidgetFocus) << q <<
"inserted after" << position;
13649 case FocusDirection::Previous:
13650 if (next == position) {
13651 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already after" << position;
13655 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13661 qCDebug(lcWidgetFocus) << q <<
"inserted before" << position;
13665 Q_ASSERT(isFocusChainConsistent());
13670
13671
13672
13673
13674
13675
13676
13677bool QWidgetPrivate::insertIntoFocusChain(
const QWidgetList &toBeInserted,
13678 FocusDirection direction, QWidget *position)
13680 if (toBeInserted.isEmpty()) {
13681 qCDebug(lcWidgetFocus) <<
"No-op insertion of an empty list";
13685 Q_ASSERT_X(!toBeInserted.contains(position),
13687 "Coding error: toBeInserted contains position");
13689 QWidget *first = toBeInserted.constFirst();
13690 QWidget *last = toBeInserted.constLast();
13693 if (toBeInserted.count() == 1)
13694 return first->d_func()->insertIntoFocusChain(direction, position);
13696 Q_ASSERT(first != last);
13697 switch (direction) {
13698 case FocusDirection::Previous:
13700 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already before"
13708 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted before" << position;
13710 case FocusDirection::Next:
13712 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already after"
13720 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted after" << position;
13724 Q_ASSERT(position->d_func()->isFocusChainConsistent());
13729
13730
13731
13732
13735 QWidgetList path({from});
13741 switch (direction) {
13742 case QWidgetPrivate::FocusDirection::Previous:
13743 current = current->previousInFocusChain();
13745 case QWidgetPrivate::FocusDirection::Next:
13746 current = current->nextInFocusChain();
13749 if (path.contains(current))
13750 return QWidgetList();
13752 }
while (current != to);
13758
13759
13760
13761
13762
13763QWidgetList QWidgetPrivate::takeFromFocusChain(QWidget *from,
13765 FocusDirection direction)
13768 const QWidgetList path = focusPath(from, to , direction);
13769 if (path.isEmpty()) {
13770 qCDebug(lcWidgetFocus) <<
"No-op removal. Focus chain from" << from <<
"doesn't lead to " << to;
13771 return QWidgetList();
13774 QWidget *first = path.constFirst();
13775 QWidget *last = path.constLast();
13776 if (first == last) {
13777 first->d_func()->removeFromFocusChain();
13778 return QWidgetList({first});
13785 qCDebug(lcWidgetFocus) << path <<
"removed from focus chain";
13790
13791
13792
13793
13794QWidget *QWidgetPrivate::determineLastFocusChild(QWidget *noFurtherThan)
13801 QWidget *lastFocusChild = q;
13803 QWidget *focusProxy = deepestFocusProxy();
13810 for (
auto *object : std::as_const(q->children())) {
13811 QWidget *w = qobject_cast<QWidget *>(object);
13812 if (w && w->focusProxy() == q) {
13813 lastFocusChild = w;
13817 }
else if (q->isAncestorOf(focusProxy)) {
13818 lastFocusChild = focusProxy;
13819 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
13820 focusNext != focusProxy && q->isAncestorOf(focusNext)
13821 && focusNext->window() == focusProxy->window();
13822 focusNext = focusNext->nextInFocusChain()) {
13823 if (focusNext == noFurtherThan)
13825 if (focusNext->focusPolicy() != Qt::NoFocus)
13826 lastFocusChild = focusNext;
13829 return lastFocusChild;
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844bool QWidgetPrivate::isInFocusChain()
const
13846 Q_Q(
const QWidget);
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872bool QWidgetPrivate::isFocusChainConsistent()
const
13874 Q_Q(
const QWidget);
13875 const bool skip = !QLoggingCategory(
"qt.widgets.focus").isDebugEnabled();
13879 if (!isInFocusChain())
13882 const QWidget *position = q;
13884 for (
int i = 0; i < QApplication::allWidgets().count(); ++i) {
13886 qCDebug(lcWidgetFocus) <<
"Nullptr found at:" << position
13887 <<
"Previous pointing to" <<
FOCUS_PREV(position)
13888 <<
"Next pointing to" <<
FOCUS_NEXT(position);
13893 qCDebug(lcWidgetFocus) <<
"Inconsistent focus chain at:" << position
13904 qCDebug(lcWidgetFocus) <<
"Focus chain leading from" << q <<
"to" << position <<
"is not closed.";
13914#include "moc_qwidget.cpp"
13915#include "moc_qwidget_p.cpp"