573
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
781QWidgetMapper *QWidgetPrivate::mapper =
nullptr;
782QWidgetSet *QWidgetPrivate::allWidgets =
nullptr;
786
787
790
791
792
793
794
795
796
797
798
799
800
801
802
803
810#ifdef QT_NO_EXCEPTIONS
814 QWidgetPrivate::allWidgets->remove(that);
815 d->removeFromFocusChain();
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
846 : QWidget(*
new QWidgetPrivate, parent, f)
852
853QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
854 : QObject(dd,
nullptr), QPaintDevice()
861 QWidgetExceptionCleaner::cleanup(
this, d_func());
867
868
869int QWidget::devType()
const
871 return QInternal::Widget;
876void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
878 bool customize = (flags & (Qt::CustomizeWindowHint
879 | Qt::FramelessWindowHint
880 | Qt::WindowTitleHint
881 | Qt::WindowSystemMenuHint
882 | Qt::WindowMinimizeButtonHint
883 | Qt::WindowMaximizeButtonHint
884 | Qt::WindowCloseButtonHint
885 | Qt::WindowContextHelpButtonHint));
887 uint type = (flags & Qt::WindowType_Mask);
889 if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
894 if (flags & Qt::CustomizeWindowHint) {
899 if ((flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint))
901 && type != Qt::Dialog
904 flags |= Qt::WindowSystemMenuHint;
905 flags |= Qt::WindowTitleHint;
906 flags &= ~Qt::FramelessWindowHint;
908 }
else if (customize && !(flags & Qt::FramelessWindowHint)) {
912 flags |= Qt::WindowSystemMenuHint;
913 flags |= Qt::WindowTitleHint;
916 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
917 if (type != Qt::Dialog && type != Qt::Sheet && type != Qt::Tool)
918 flags |= Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowFullscreenButtonHint;
920 if (w->testAttribute(Qt::WA_TransparentForMouseEvents))
921 flags |= Qt::WindowTransparentForInput;
924void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
930 Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
932 if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
933 qFatal(
"QWidget: Cannot create a QWidget without QApplication");
935 Q_ASSERT(allWidgets);
937 allWidgets->insert(q);
941 Q_ASSERT_X(QThread::isMainThread(),
"QWidget",
942 "Widgets must be created in the GUI thread.");
946 data.fstrut_dirty =
true;
949 data.widget_attributes = 0;
950 data.window_flags = f;
951 data.window_state = 0;
952 data.focus_policy = 0;
953 data.context_menu_policy = Qt::DefaultContextMenu;
954 data.window_modality = Qt::NonModal;
956 data.sizehint_forced = 0;
957 data.is_closing =
false;
959 data.in_set_window_state = 0;
960 data.in_destructor =
false;
963 if (f & Qt::MSWindowsOwnDC) {
964 mustHaveWindowHandle = 1;
965 q->setAttribute(Qt::WA_NativeWindow);
968 q->setAttribute(Qt::WA_QuitOnClose);
969 adjustQuitOnCloseAttribute();
971 q->setAttribute(Qt::WA_ContentsMarginsRespectsSafeArea);
972 q->setAttribute(Qt::WA_WState_Hidden);
975 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
978 if ((f & Qt::WindowType_Mask) == Qt::Desktop)
980 else if (parentWidget)
981 q->setParent(parentWidget, data.window_flags);
983 adjustFlags(data.window_flags, q);
984 resolveLayoutDirection();
986 const QBrush &background = q->palette().brush(QPalette::Window);
987 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
989 data.fnt = QFont(data.fnt, q);
991 q->setAttribute(Qt::WA_PendingMoveEvent);
992 q->setAttribute(Qt::WA_PendingResizeEvent);
994 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
995 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
997 QEvent e(QEvent::Create);
998 QCoreApplication::sendEvent(q, &e);
999 QCoreApplication::postEvent(q,
new QEvent(QEvent::PolishRequest));
1001 extraPaintEngine =
nullptr;
1004void QWidgetPrivate::createRecursively()
1007 q->create(0,
true,
true);
1008 for (
int i = 0; i < children.size(); ++i) {
1009 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1010 if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
1011 child->d_func()->createRecursively();
1015QRhi *QWidgetPrivate::rhi()
const
1018 if (
auto *backingStore = q->backingStore()) {
1019 auto *window = windowHandle(WindowHandleMode::Closest);
1020 return backingStore->handle()->rhi(window);
1027
1028
1029
1030
1031
1032
1033
1034QWidget *QWidgetPrivate::closestParentWidgetWithWindowHandle()
const
1037 QWidget *parent = q->parentWidget();
1038 while (parent && !parent->windowHandle())
1039 parent = parent->parentWidget();
1043QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode)
const
1045 if (mode == WindowHandleMode::Direct || mode == WindowHandleMode::Closest) {
1046 if (QTLWExtra *x = maybeTopData()) {
1047 if (x->window !=
nullptr || mode == WindowHandleMode::Direct)
1051 if (mode == WindowHandleMode::Closest) {
1053 if (
auto nativeParent = q_func()->nativeParentWidget()) {
1054 if (
auto window = nativeParent->windowHandle())
1058 if (mode == WindowHandleMode::TopLevel || mode == WindowHandleMode::Closest) {
1059 if (
auto topLevel = q_func()->topLevelWidget()) {
1060 if (
auto window = topLevel ->windowHandle())
1068
1069
1070
1071
1072
1073QWindow *QWidgetPrivate::_q_closestWindowHandle()
const
1075 return windowHandle(QWidgetPrivate::WindowHandleMode::Closest);
1078QScreen *QWidgetPrivate::associatedScreen()
const
1080#if QT_CONFIG(graphicsview)
1082 if (nearestGraphicsProxyWidget(q_func()))
1085 if (
auto window = windowHandle(WindowHandleMode::Closest))
1086 return window->screen();
1093 QPlatformBackingStoreRhiConfig config = QWidgetPrivate::get(w)->rhiConfig();
1094 if (config.isEnabled()) {
1096 *outConfig = config;
1098 *outType = QBackingStoreRhiSupport::surfaceTypeForConfig(config);
1101 for (
const QObject *child : w->children()) {
1102 if (
const QWidget *childWidget = qobject_cast<
const QWidget *>(child)) {
1103 if (q_evaluateRhiConfigRecursive(childWidget, outConfig, outType)) {
1104 static bool optOut = qEnvironmentVariableIsSet(
"QT_WIDGETS_NO_CHILD_RHI");
1107 if (!optOut && childWidget->testAttribute(Qt::WA_NativeWindow))
1123 if (QBackingStoreRhiSupport::checkForceRhi(outConfig, outType)) {
1124 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluated to forced flushing with QRhi";
1130 if (q_evaluateRhiConfigRecursive(w, outConfig, outType)) {
1131 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluates to flushing with QRhi";
1141
1142
1143
1144
1145
1146
1147
1148
1149
1151void QWidget::create(WId window,
bool initializeWindow,
bool destroyOldWindow)
1153 Q_UNUSED(initializeWindow);
1154 Q_UNUSED(destroyOldWindow);
1157 if (Q_UNLIKELY(window))
1158 qWarning(
"QWidget::create(): Parameter 'window' does not have any effect.");
1159 if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
1162 if (d->data.in_destructor)
1165 Qt::WindowType type = windowType();
1166 Qt::WindowFlags &flags = data->window_flags;
1168 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1170 flags |= Qt::Window;
1173 if (QWidget *parent = parentWidget()) {
1174 if (type & Qt::Window) {
1175 if (!parent->testAttribute(Qt::WA_WState_Created))
1176 parent->createWinId();
1177 }
else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
1178 && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
1184 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1185 Q_ASSERT(internalWinId());
1191 static const bool paintOnScreenEnv = qEnvironmentVariableIntValue(
"QT_ONSCREEN_PAINT") > 0;
1192 if (paintOnScreenEnv)
1193 setAttribute(Qt::WA_PaintOnScreen);
1195 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1196 setAttribute(Qt::WA_NativeWindow);
1198 d->updateIsOpaque();
1200 setAttribute(Qt::WA_WState_Created);
1204 if (isWindow() && windowType() != Qt::Desktop)
1205 d->topData()->repaintManager.reset(
new QWidgetRepaintManager(
this));
1209 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1210 setAttribute(Qt::WA_DropSiteRegistered,
true);
1213 if (testAttribute(Qt::WA_SetWindowIcon))
1214 d->setWindowIcon_sys();
1216 if (isWindow() && !d->topData()->iconText.isEmpty())
1217 d->setWindowIconText_helper(d->topData()->iconText);
1218 if (isWindow() && !d->topData()->caption.isEmpty())
1219 d->setWindowTitle_helper(d->topData()->caption);
1220 if (isWindow() && !d->topData()->filePath.isEmpty())
1221 d->setWindowFilePath_helper(d->topData()->filePath);
1222 if (windowType() != Qt::Desktop) {
1223 d->updateSystemBackground();
1225 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1226 d->setWindowIcon_sys();
1234 d->updateFrameStrut();
1239 QObjectList children = parentWidget->children();
1240 for (
int i = 0; i < children.size(); i++) {
1241 if (children.at(i)->isWidgetType()) {
1242 const QWidget *childWidget = qobject_cast<
const QWidget *>(children.at(i));
1244 if (childWidget->testAttribute(Qt::WA_NativeWindow)) {
1245 if (!childWidget->internalWinId())
1246 childWidget->winId();
1247 if (childWidget->windowHandle()) {
1248 if (childWidget->isWindow()) {
1249 childWidget->windowHandle()->setTransientParent(parentWidget->window()->windowHandle());
1251 childWidget->windowHandle()->setParent(childWidget->nativeParentWidget()->windowHandle());
1263void QWidgetPrivate::create()
1267 if (!q->testAttribute(Qt::WA_NativeWindow) && !q->isWindow())
1270 QWidgetWindow *win = topData()->window;
1275 Q_ASSERT(topData()->window);
1276 win = topData()->window;
1279 const auto dynamicPropertyNames = q->dynamicPropertyNames();
1280 for (
const QByteArray &propertyName : dynamicPropertyNames) {
1281 if (!qstrncmp(propertyName,
"_q_platform_", 12))
1282 win->setProperty(propertyName, q->property(propertyName));
1285 Qt::WindowFlags &flags = data.window_flags;
1287#if defined(QT_PLATFORM_UIKIT)
1288 if (q->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
1289 flags |= Qt::ExpandedClientAreaHint;
1292 if (q->testAttribute(Qt::WA_ShowWithoutActivating))
1293 win->setProperty(
"_q_showWithoutActivating", QVariant(
true));
1294 if (q->testAttribute(Qt::WA_MacAlwaysShowToolWindow))
1295 win->setProperty(
"_q_macAlwaysShowToolWindow", QVariant(
true));
1296 win->setFlags(flags);
1297 fixPosIncludesFrame();
1298 if (q->testAttribute(Qt::WA_Moved)
1299 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
1300 win->setGeometry(q->geometry());
1302 win->resize(q->size());
1303 if (win->isTopLevel()) {
1304 QScreen *targetScreen = topData()->initialScreen;
1305 topData()->initialScreen =
nullptr;
1306 if (!targetScreen) {
1307 targetScreen = q->windowType() != Qt::Desktop
1308 ? q->screen() :
nullptr;
1310 win->setScreen(targetScreen);
1313 QSurfaceFormat format = win->requestedFormat();
1314 if ((flags & Qt::Window) && win->surfaceType() != QSurface::OpenGLSurface
1315 && q->testAttribute(Qt::WA_TranslucentBackground)) {
1316 format.setAlphaBufferSize(8);
1318 win->setFormat(format);
1320 if (QWidget *nativeParent = q->nativeParentWidget()) {
1321 if (nativeParent->windowHandle()) {
1322 if (flags & Qt::Window) {
1323 win->setTransientParent(nativeParent->window()->windowHandle());
1324 win->setParent(
nullptr);
1326 win->setTransientParent(
nullptr);
1327 win->setParent(nativeParent->windowHandle());
1332 qt_window_private(win)->positionPolicy = topData()->posIncludesFrame ?
1333 QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
1335 if (q->windowType() != Qt::Desktop || q->testAttribute(Qt::WA_NativeWindow)) {
1338 if (QPlatformWindow *platformWindow = win->handle())
1339 platformWindow->setFrameStrutEventsEnabled(
true);
1342 data.window_flags = win->flags();
1345 if (!topData()->role.isNull()) {
1346 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(win->handle()))
1347 xcbWindow->setWindowRole(topData()->role);
1351 QBackingStore *store = q->backingStore();
1352 usesRhiFlush =
false;
1354 if (q->windowType() == Qt::Desktop) {
1355 q->setAttribute(Qt::WA_PaintOnScreen,
true);
1357 if (!store && q->isWindow())
1358 q->setBackingStore(
new QBackingStore(win));
1360 QPlatformBackingStoreRhiConfig rhiConfig;
1361 usesRhiFlush = q_evaluateRhiConfig(q, &rhiConfig,
nullptr);
1362 if (usesRhiFlush && q->backingStore()) {
1365 q->backingStore()->handle()->createRhi(win, rhiConfig);
1369 setWindowModified_helper();
1371 if (win->handle()) {
1372 WId id = win->winId();
1374 Q_ASSERT(id != WId(0));
1377 setNetWmWindowTypes(
true);
1380 q_createNativeChildrenAndSetParent(q);
1382 if (extra && !extra->mask.isEmpty())
1383 setMask_sys(extra->mask);
1385 if (data.crect.width() == 0 || data.crect.height() == 0) {
1386 q->setAttribute(Qt::WA_OutsideWSRange,
true);
1388 q->setAttribute(Qt::WA_OutsideWSRange,
false);
1389 if (q->isVisible()) {
1391 win->setNativeWindowVisibility(
true);
1397static const char activeXNativeParentHandleProperty[] =
"_q_embedded_native_parent_handle";
1400void QWidgetPrivate::createTLSysExtra()
1403 if (!extra->topextra->window && (q->testAttribute(Qt::WA_NativeWindow) || q->isWindow())) {
1404 extra->topextra->window =
new QWidgetWindow(q);
1405 if (extra->minw || extra->minh)
1406 extra->topextra->window->setMinimumSize(QSize(extra->minw, extra->minh));
1408 extra->topextra->window->setMaximumSize(QSize(extra->maxw, extra->maxh));
1409 if (extra->topextra->opacity != 255 && q->isWindow())
1410 extra->topextra->window->setOpacity(qreal(extra->topextra->opacity) / qreal(255));
1412 const bool isTipLabel = q->inherits(
"QTipLabel");
1413 const bool isAlphaWidget = !isTipLabel && q->inherits(
"QAlphaWidget");
1416 const QVariant activeXNativeParentHandle = q->property(activeXNativeParentHandleProperty);
1417 if (activeXNativeParentHandle.isValid())
1418 extra->topextra->window->setProperty(activeXNativeParentHandleProperty, activeXNativeParentHandle);
1419 if (isTipLabel || isAlphaWidget)
1420 extra->topextra->window->setProperty(
"_q_windowsDropShadow", QVariant(
true));
1422 if (isTipLabel || isAlphaWidget || q->inherits(
"QRollEffect"))
1423 qt_window_private(extra->topextra->window)->setAutomaticPositionAndResizeEnabled(
false);
1425 updateIsTranslucent();
1431
1432
1433
1434
1435
1440 d->data.in_destructor =
true;
1442#if defined (QT_CHECK_STATE)
1443 if (Q_UNLIKELY(paintingActive()))
1444 qWarning(
"QWidget: %s (%s) deleted while being painted", className(), name());
1447#ifndef QT_NO_GESTURES
1448 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1450 for (
auto it = d->gestureContext.keyBegin(), end = d->gestureContext.keyEnd(); it != end; ++it)
1451 manager->cleanupCachedGestures(
this, *it);
1453 d->gestureContext.clear();
1458 for (
auto action : std::as_const(d->actions)) {
1459 QActionPrivate *apriv = action->d_func();
1460 apriv->associatedObjects.removeAll(
this);
1465#ifndef QT_NO_SHORTCUT
1468 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1469 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0,
this, QKeySequence());
1474 d->layout =
nullptr;
1477 d->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency);
1480#if QT_CONFIG(graphicsview)
1481 const QWidget* w =
this;
1482 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
1483 w = w->d_func()->extra->focus_proxy;
1484 QWidget *window = w->window();
1485 QWExtra *e = window ? window->d_func()->extra.get() :
nullptr ;
1486 if (!e || !e->proxyWidget || (w->parentWidget() && w->parentWidget()->d_func()->focus_child ==
this))
1493 d->setDirtyOpaqueRegion();
1495 if (isWindow() && isVisible() && internalWinId()) {
1506 }
else if (isVisible()) {
1507 qApp->d_func()->sendSyntheticEnterLeave(
this);
1510 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
1511 repaintManager->removeDirtyWidget(
this);
1512 if (testAttribute(Qt::WA_StaticContents))
1513 repaintManager->removeStaticWidget(
this);
1516 delete d->needsFlush;
1517 d->needsFlush =
nullptr;
1521 bool blocked = d->blockSig;
1524 if (d->isSignalConnected(0)) {
1526 emit destroyed(
this);
1530 qWarning(
"Detected an unexpected exception in ~QWidget while emitting destroyed().");
1535 if (d->declarativeData) {
1536 d->wasDeleted =
true;
1537 if (QAbstractDeclarativeData::destroyed)
1538 QAbstractDeclarativeData::destroyed(d->declarativeData,
this);
1539 d->declarativeData =
nullptr;
1540 d->wasDeleted =
false;
1543 d->blockSig = blocked;
1545 if (!d->children.isEmpty())
1546 d->deleteChildren();
1548 QCoreApplication::removePostedEvents(
this);
1555 --QWidgetPrivate::instanceCounter;
1557 if (QWidgetPrivate::allWidgets)
1558 QWidgetPrivate::allWidgets->remove(
this);
1561 QEvent e(QEvent::Destroy);
1562 QCoreApplication::sendEvent(
this, &e);
1563 } QT_CATCH(
const std::exception&) {
1567#if QT_CONFIG(graphicseffect)
1568 delete d->graphicsEffect;
1572 d->isWidget =
false;
1575int QWidgetPrivate::instanceCounter = 0;
1576int QWidgetPrivate::maxInstances = 0;
1578void QWidgetPrivate::setWinId(WId id)
1581 if (mapper && data.winid) {
1582 mapper->remove(data.winid);
1585 const WId oldWinId = data.winid;
1589 mapper->insert(data.winid, q);
1592 if (oldWinId != id) {
1593 QEvent e(QEvent::WinIdChange);
1594 QCoreApplication::sendEvent(q, &e);
1598void QWidgetPrivate::createTLExtra()
1602 if (!extra->topextra) {
1603 extra->topextra = std::make_unique<QTLWExtra>();
1604 QTLWExtra* x = extra->topextra.get();
1605 x->backingStore =
nullptr;
1606 x->sharedPainter =
nullptr;
1607 x->incw = x->inch = 0;
1608 x->basew = x->baseh = 0;
1609 x->frameStrut.setCoords(0, 0, 0, 0);
1610 x->normalGeometry = QRect(0,0,-1,-1);
1611 x->savedFlags = { };
1613 x->posIncludesFrame = 0;
1614 x->sizeAdjusted =
false;
1616 x->window =
nullptr;
1617 x->initialScreen =
nullptr;
1619#ifdef QWIDGET_EXTRA_DEBUG
1620 static int count = 0;
1621 qDebug() <<
"tlextra" << ++count;
1627
1628
1629
1631void QWidgetPrivate::createExtra()
1634 extra = std::make_unique<QWExtra>();
1635 extra->glContext =
nullptr;
1636#if QT_CONFIG(graphicsview)
1637 extra->proxyWidget =
nullptr;
1643 extra->customDpiX = 0;
1644 extra->customDpiY = 0;
1645 extra->explicitMinSize = 0;
1646 extra->explicitMaxSize = 0;
1647 extra->autoFillBackground = 0;
1648 extra->nativeChildrenForced = 0;
1649 extra->inRenderWithPainter = 0;
1650 extra->hasWindowContainer =
false;
1653#ifdef QWIDGET_EXTRA_DEBUG
1654 static int count = 0;
1655 qDebug() <<
"extra" << ++count;
1660void QWidgetPrivate::createSysExtra()
1665
1666
1667
1669void QWidgetPrivate::deleteExtra()
1673#ifndef QT_NO_STYLE_STYLESHEET
1675 if (QStyleSheetStyle *proxy = qt_styleSheet(extra->style))
1678 if (extra->topextra)
1686void QWidgetPrivate::deleteSysExtra()
1690void QWidgetPrivate::deleteTLSysExtra()
1693 if (extra && extra->topextra) {
1694 if (extra->hasWindowContainer)
1695 QWindowContainer::toplevelAboutToBeDestroyed(q);
1697 delete extra->topextra->window;
1698 extra->topextra->window =
nullptr;
1703
1704
1705
1707QRegion QWidgetPrivate::overlappedRegion(
const QRect &rect,
bool breakAfterFirst)
const
1711 const QWidget *w = q;
1718 QWidgetPrivate *pd = w->parentWidget()->d_func();
1720 for (
int i = 0; i < pd->children.size(); ++i) {
1721 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
1722 if (!sibling || !sibling->isVisible() || sibling->isWindow())
1725 above = (sibling == w);
1729 const QRect siblingRect = sibling->d_func()->effectiveRectFor(sibling->data->crect);
1730 if (qRectIntersects(siblingRect, r)) {
1731 const auto &siblingExtra = sibling->d_func()->extra;
1732 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
1733 && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
1736 region += siblingRect.translated(-p);
1737 if (breakAfterFirst)
1741 w = w->parentWidget();
1742 r.translate(pd->data.crect.topLeft());
1743 p += pd->data.crect.topLeft();
1748void QWidgetPrivate::syncBackingStore()
1750 if (shouldPaintOnScreen()) {
1751 paintOnScreen(dirty);
1753 }
else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1754 repaintManager->sync();
1758void QWidgetPrivate::syncBackingStore(
const QRegion ®ion)
1760 if (shouldPaintOnScreen())
1761 paintOnScreen(region);
1762 else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1763 repaintManager->sync(q_func(), region);
1767void QWidgetPrivate::paintOnScreen(
const QRegion &rgn)
1769 if (data.in_destructor)
1772 if (shouldDiscardSyncRequest())
1776 if (q->testAttribute(Qt::WA_StaticContents)) {
1779 extra->staticContentsSize = data.crect.size();
1782 QPaintEngine *engine = q->paintEngine();
1787 const bool noPartialUpdateSupport = (engine && (engine->type() == QPaintEngine::OpenGL
1788 || engine->type() == QPaintEngine::OpenGL2))
1789 && (usesDoubleBufferedGLContext || q->autoFillBackground());
1790 QRegion toBePainted(noPartialUpdateSupport ? q->rect() : rgn);
1792 toBePainted &= clipRect();
1793 clipToEffectiveMask(toBePainted);
1794 if (toBePainted.isEmpty())
1797 drawWidget(q, toBePainted, QPoint(), QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen,
nullptr);
1799 if (Q_UNLIKELY(q->paintingActive()))
1800 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
1803void QWidgetPrivate::setUpdatesEnabled_helper(
bool enable)
1807 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
1810 if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
1813 q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
1817 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
1818 for (
int i = 0; i < children.size(); ++i) {
1819 QWidget *w = qobject_cast<QWidget *>(children.at(i));
1820 if (w && !w->isWindow() && !w->testAttribute(attribute))
1821 w->d_func()->setUpdatesEnabled_helper(enable);
1826
1827
1828
1829
1830
1831
1832void QWidgetPrivate::propagatePaletteChange()
1836#if QT_CONFIG(graphicsview)
1837 if (!q->parentWidget() && extra && extra->proxyWidget) {
1838 QGraphicsProxyWidget *p = extra->proxyWidget;
1839 inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolveMask();
1842 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
1843 inheritedPaletteResolveMask = 0;
1846 directPaletteResolveMask = data.pal.resolveMask();
1847 auto mask = directPaletteResolveMask | inheritedPaletteResolveMask;
1849 const bool useStyleSheetPropagationInWidgetStyles =
1850 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
1852 QEvent pc(QEvent::PaletteChange);
1853 QCoreApplication::sendEvent(q, &pc);
1854 for (
int i = 0; i < children.size(); ++i) {
1855 QWidget *w = qobject_cast<QWidget*>(children.at(i));
1856 if (w && (!w->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
1857 && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
1858 QWidgetPrivate *wd = w->d_func();
1859 wd->inheritedPaletteResolveMask = mask;
1860 wd->resolvePalette();
1866
1867
1868QRect QWidgetPrivate::clipRect()
const
1871 const QWidget * w = q;
1872 if (!w->isVisible())
1874 QRect r = effectiveRectFor(q->rect());
1880 && w->parentWidget()) {
1883 w = w->parentWidget();
1884 r &= QRect(ox, oy, w->width(), w->height());
1890
1891
1892QRegion QWidgetPrivate::clipRegion()
const
1895 if (!q->isVisible())
1897 QRegion r(q->rect());
1898 const QWidget * w = q;
1899 const QWidget *ignoreUpTo;
1905 && w->parentWidget()) {
1909 w = w->parentWidget();
1910 r &= QRegion(ox, oy, w->width(), w->height());
1913 while(w->d_func()->children.at(i++) !=
static_cast<
const QObject *>(ignoreUpTo))
1915 for ( ; i < w->d_func()->children.size(); ++i) {
1916 if (QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
1917 if (sibling->isVisible() && !sibling->isWindow()) {
1918 QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
1919 sibling->width(), sibling->height());
1920 if (qRectIntersects(siblingRect, q->rect()))
1921 r -= QRegion(siblingRect);
1929void QWidgetPrivate::setSystemClip(QPaintEngine *paintEngine, qreal devicePixelRatio,
const QRegion ®ion)
1932 QTransform scaleTransform;
1933 scaleTransform.scale(devicePixelRatio, devicePixelRatio);
1935 paintEngine->d_func()->baseSystemClip = region;
1936 paintEngine->d_func()->setSystemTransform(scaleTransform);
1940#if QT_CONFIG(graphicseffect)
1941void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
1946 if (w->graphicsEffect()) {
1947 QWidgetEffectSourcePrivate *sourced =
1948 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
1949 if (!sourced->updateDueToGraphicsEffect)
1950 w->graphicsEffect()->source()->d_func()->invalidateCache();
1952 w = w->parentWidget();
1957void QWidgetPrivate::setDirtyOpaqueRegion()
1961 dirtyOpaqueChildren =
true;
1963#if QT_CONFIG(graphicseffect)
1964 invalidateGraphicsEffectsRecursively();
1970 QWidget *parent = q->parentWidget();
1975 QWidgetPrivate *pd = parent->d_func();
1976 if (!pd->dirtyOpaqueChildren)
1977 pd->setDirtyOpaqueRegion();
1980const QRegion &QWidgetPrivate::getOpaqueChildren()
const
1982 if (!dirtyOpaqueChildren)
1983 return opaqueChildren;
1985 QWidgetPrivate *that =
const_cast<QWidgetPrivate*>(
this);
1986 that->opaqueChildren = QRegion();
1988 for (
int i = 0; i < children.size(); ++i) {
1989 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1990 if (!child || !child->isVisible() || child->isWindow())
1993 const QPoint offset = child->geometry().topLeft();
1994 QWidgetPrivate *childd = child->d_func();
1995 QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
1996 if (childd->extra && childd->extra->hasMask)
1997 r &= childd->extra->mask;
2000 r.translate(offset);
2001 that->opaqueChildren += r;
2004 that->opaqueChildren &= q_func()->rect();
2005 that->dirtyOpaqueChildren =
false;
2007 return that->opaqueChildren;
2010void QWidgetPrivate::subtractOpaqueChildren(QRegion &source,
const QRect &clipRect)
const
2012 if (children.isEmpty() || clipRect.isEmpty())
2015 const QRegion &r = getOpaqueChildren();
2017 source -= (r & clipRect);
2021void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion,
bool *hasDirtySiblingsAbove,
2022 bool alsoNonOpaque)
const
2025 static int disableSubtractOpaqueSiblings = qEnvironmentVariableIntValue(
"QT_NO_SUBTRACTOPAQUESIBLINGS");
2026 if (disableSubtractOpaqueSiblings || q->isWindow())
2029 QRect clipBoundingRect;
2030 bool dirtyClipBoundingRect =
true;
2033 bool dirtyParentClip =
true;
2035 QPoint parentOffset = data.crect.topLeft();
2037 const QWidget *w = q;
2042 QWidgetPrivate *pd = w->parentWidget()->d_func();
2043 const int myIndex = pd->children.indexOf(
const_cast<QWidget *>(w));
2044 const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
2045 for (
int i = myIndex + 1; i < pd->children.size(); ++i) {
2046 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
2047 if (!sibling || !sibling->isVisible() || sibling->isWindow())
2050 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2051 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2054 if (dirtyClipBoundingRect) {
2055 clipBoundingRect = sourceRegion.boundingRect();
2056 dirtyClipBoundingRect =
false;
2059 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2062 if (dirtyParentClip) {
2063 parentClip = sourceRegion.translated(parentOffset);
2064 dirtyParentClip =
false;
2067 const QPoint siblingPos(sibling->data->crect.topLeft());
2068 const QRect siblingClipRect(sibling->d_func()->clipRect());
2069 QRegion siblingDirty(parentClip);
2070 siblingDirty &= (siblingClipRect.translated(siblingPos));
2071 const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
2072 && !sibling->d_func()->graphicsEffect;
2074 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2075 if (siblingDirty.isEmpty())
2078 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2080 siblingDirty.translate(-parentOffset);
2081 sourceRegion -= siblingDirty;
2083 sourceRegion -= siblingGeometry.translated(-parentOffset);
2086 if (hasDirtySiblingsAbove)
2087 *hasDirtySiblingsAbove =
true;
2088 if (sibling->d_func()->children.isEmpty())
2090 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2091 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2092 sourceRegion -= opaqueSiblingChildren;
2094 if (sourceRegion.isEmpty())
2097 dirtyClipBoundingRect =
true;
2098 dirtyParentClip =
true;
2101 w = w->parentWidget();
2102 parentOffset += pd->data.crect.topLeft();
2103 dirtyParentClip =
true;
2107void QWidgetPrivate::clipToEffectiveMask(QRegion ®ion)
const
2111 const QWidget *w = q;
2114#if QT_CONFIG(graphicseffect)
2115 if (graphicsEffect && !w->isWindow()) {
2116 w = q->parentWidget();
2117 offset -= data.crect.topLeft();
2122 const QWidgetPrivate *wd = w->d_func();
2123 if (wd->extra && wd->extra->hasMask)
2124 region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
2127 offset -= wd->data.crect.topLeft();
2128 w = w->parentWidget();
2132bool QWidgetPrivate::shouldPaintOnScreen()
const
2134#if defined(QT_NO_BACKINGSTORE)
2138 if (q->testAttribute(Qt::WA_PaintOnScreen)
2139 || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
2147void QWidgetPrivate::updateIsOpaque()
2150 setDirtyOpaqueRegion();
2152#if QT_CONFIG(graphicseffect)
2153 if (graphicsEffect) {
2161 if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
2166 const QPalette &pal = q->palette();
2168 if (q->autoFillBackground()) {
2169 const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
2170 if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
2176 if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
2177 const QBrush &windowBrush = q->palette().brush(QPalette::Window);
2178 if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
2186void QWidgetPrivate::setOpaque(
bool opaque)
2188 if (isOpaque != opaque) {
2190 updateIsTranslucent();
2194void QWidgetPrivate::updateIsTranslucent()
2197 if (QWindow *window = q->windowHandle()) {
2198 QSurfaceFormat format = window->format();
2199 const int oldAlpha = format.alphaBufferSize();
2200 const int newAlpha = q->testAttribute(Qt::WA_TranslucentBackground) ? 8 : -1;
2201 if (oldAlpha != newAlpha) {
2218 if (!window->handle()) {
2219 format.setAlphaBufferSize(newAlpha);
2220 window->setFormat(format);
2226static inline void fillRegion(QPainter *painter,
const QRegion &rgn,
const QBrush &brush)
2230 if (brush.style() == Qt::TexturePattern) {
2231 const QRect rect(rgn.boundingRect());
2232 painter->setClipRegion(rgn);
2233 painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
2234 }
else if (brush.gradient()
2235 && (brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode
2236 || brush.gradient()->coordinateMode() == QGradient::ObjectMode)) {
2238 painter->setClipRegion(rgn);
2239 painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
2242 for (
const QRect &rect : rgn)
2243 painter->fillRect(rect, brush);
2247bool QWidgetPrivate::updateBrushOrigin(QPainter *painter,
const QBrush &brush)
const
2249#if QT_CONFIG(scrollarea)
2252 if (brush.style() == Qt::NoBrush || brush.style() == Qt::SolidPattern)
2254 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
2255 if (scrollArea && scrollArea->viewport() == q) {
2256 QObjectData *scrollPrivate =
static_cast<QWidget *>(scrollArea)->d_ptr.data();
2257 QAbstractScrollAreaPrivate *priv =
static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
2258 painter->setBrushOrigin(-priv->contentsOffset());
2264void QWidgetPrivate::paintBackground(QPainter *painter,
const QRegion &rgn, DrawWidgetFlags flags)
const
2268 bool brushOriginSet =
false;
2269 const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
2271 if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
2272 const QBrush bg = q->palette().brush(QPalette::Window);
2273 if (!brushOriginSet)
2274 brushOriginSet = updateBrushOrigin(painter, bg);
2275 if (!(flags & DontSetCompositionMode)) {
2277 QPainter::CompositionMode oldMode = painter->compositionMode();
2278 painter->setCompositionMode(QPainter::CompositionMode_Source);
2279 fillRegion(painter, rgn, bg);
2280 painter->setCompositionMode(oldMode);
2282 fillRegion(painter, rgn, bg);
2286 if (q->autoFillBackground()) {
2287 if (!brushOriginSet)
2288 brushOriginSet = updateBrushOrigin(painter, autoFillBrush);
2289 fillRegion(painter, rgn, autoFillBrush);
2292 if (q->testAttribute(Qt::WA_StyledBackground)) {
2293 painter->setClipRegion(rgn);
2296 q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
2301
2302
2303
2304
2305
2309void QWidgetPrivate::deactivateWidgetCleanup()
2313 if (QApplication::activeWindow() == q)
2314 QApplicationPrivate::setActiveWindow(
nullptr);
2316 if (q == qt_button_down)
2317 qt_button_down =
nullptr;
2322
2323
2324
2325
2326
2327
2328
2330QWidget *QWidget::find(WId id)
2332 return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) :
nullptr;
2338
2339
2340
2341
2342
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360WId QWidget::winId()
const
2362 if (!data->in_destructor
2363 && (!testAttribute(Qt::WA_WState_Created) || !internalWinId()))
2365 QWidget *that =
const_cast<QWidget*>(
this);
2366 that->setAttribute(Qt::WA_NativeWindow);
2367 that->d_func()->createWinId();
2368 return that->data->winid;
2373void QWidgetPrivate::createWinId()
2377 const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
2378 if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
2379 if (!q->isWindow()) {
2380 QWidget *parent = q->parentWidget();
2381 QWidgetPrivate *pd = parent->d_func();
2382 if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
2383 parent->setAttribute(Qt::WA_NativeWindow);
2384 if (!parent->internalWinId()) {
2388 for (
int i = 0; i < pd->children.size(); ++i) {
2389 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
2390 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
2391 || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
2402
2403
2404
2405
2406
2408bool QWidgetPrivate::setScreenForPoint(
const QPoint &pos)
2414 return setScreen(QGuiApplication::screenAt(pos));
2418
2419
2420
2421
2423bool QWidgetPrivate::setScreen(QScreen *screen)
2426 if (!screen || !q->isWindow())
2428 const QScreen *currentScreen = windowHandle() ? windowHandle()->screen() :
nullptr;
2429 if (currentScreen != screen) {
2430 topData()->initialScreen = screen;
2432 windowHandle()->setScreen(screen);
2439
2440
2441
2442
2444void QWidget::createWinId()
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465WId QWidget::effectiveWinId()
const
2467 const WId id = internalWinId();
2468 if (id || !testAttribute(Qt::WA_WState_Created))
2470 if (
const QWidget *realParent = nativeParentWidget())
2471 return realParent->internalWinId();
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486QWindow *QWidget::windowHandle()
const
2489 return d->windowHandle();
2493
2494
2495
2496
2497
2498
2499QScreen *QWidget::screen()
const
2502 if (
auto associatedScreen = d->associatedScreen())
2503 return associatedScreen;
2504 if (
auto topLevel = window()) {
2505 if (
auto topData = qt_widget_private(topLevel)->topData()) {
2506 if (topData->initialScreen)
2507 return topData->initialScreen;
2509 if (
auto screenByPos = QGuiApplication::screenAt(topLevel->geometry().center()))
2512 return QGuiApplication::primaryScreen();
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527void QWidget::setScreen(QScreen *screen)
2530 d->setScreen(screen);
2533#ifndef QT_NO_STYLE_STYLESHEET
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550QString QWidget::styleSheet()
const
2555 return d->extra->styleSheet;
2558void QWidget::setStyleSheet(
const QString& styleSheet)
2561 if (data->in_destructor)
2565 QStyleSheetStyle *proxy = qt_styleSheet(d->extra->style);
2566 d->extra->styleSheet = styleSheet;
2567 if (styleSheet.isEmpty()) {
2576 bool repolish = d->polished;
2578 const auto childWidgets = findChildren<QWidget*>();
2579 for (
auto child : childWidgets) {
2580 repolish = child->d_func()->polished;
2586 proxy->repolish(
this);
2590 if (testAttribute(Qt::WA_SetStyle)) {
2591 d->setStyle_helper(
new QStyleSheetStyle(d->extra->style),
true);
2593 d->setStyle_helper(
new QStyleSheetStyle(
nullptr),
true);
2600
2601
2603QStyle *QWidget::style()
const
2607 if (d->extra && d->extra->style)
2608 return d->extra->style;
2609 return QApplication::style();
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2633void QWidget::setStyle(QStyle *style)
2636 setAttribute(Qt::WA_SetStyle, style !=
nullptr);
2638#ifndef QT_NO_STYLE_STYLESHEET
2639 if (QStyleSheetStyle *styleSheetStyle = qt_styleSheet(style)) {
2642 styleSheetStyle->ref();
2643 d->setStyle_helper(style,
false);
2644 }
else if (qt_styleSheet(d->extra->style) || !
qApp->styleSheet().isEmpty()) {
2646 d->setStyle_helper(
new QStyleSheetStyle(style),
true);
2649 d->setStyle_helper(style,
false);
2652void QWidgetPrivate::setStyle_helper(QStyle *newStyle,
bool propagate)
2655 QStyle *oldStyle = q->style();
2659#ifndef QT_NO_STYLE_STYLESHEET
2660 QPointer<QStyle> origStyle = extra->style;
2662 extra->style = newStyle;
2665 if (polished && q->windowType() != Qt::Desktop) {
2666 oldStyle->unpolish(q);
2667 q->style()->polish(q);
2672 const QObjectList childrenList = children;
2673 for (
int i = 0; i < childrenList.size(); ++i) {
2674 QWidget *c = qobject_cast<QWidget*>(childrenList.at(i));
2676 c->d_func()->inheritStyle();
2680#ifndef QT_NO_STYLE_STYLESHEET
2681 if (!qt_styleSheet(newStyle)) {
2682 if (
const QStyleSheetStyle* cssStyle = qt_styleSheet(origStyle)) {
2683 cssStyle->clearWidgetFont(q);
2688 QEvent e(QEvent::StyleChange);
2689 QCoreApplication::sendEvent(q, &e);
2691#ifndef QT_NO_STYLE_STYLESHEET
2693 if (QStyleSheetStyle *proxy = qt_styleSheet(origStyle))
2699void QWidgetPrivate::inheritStyle()
2701#ifndef QT_NO_STYLE_STYLESHEET
2704 QStyle *extraStyle = extra ? (QStyle*)extra->style :
nullptr;
2706 QStyleSheetStyle *proxy = qt_styleSheet(extraStyle);
2708 if (!q->styleSheet().isEmpty()) {
2714 QStyle *origStyle = proxy ? proxy->base : extraStyle;
2715 QWidget *parent = q->parentWidget();
2716 QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style :
nullptr;
2719 if (!
qApp->styleSheet().isEmpty() || qt_styleSheet(parentStyle)) {
2720 QStyle *newStyle = parentStyle;
2721 if (q->testAttribute(Qt::WA_SetStyle) && qt_styleSheet(origStyle) ==
nullptr)
2722 newStyle =
new QStyleSheetStyle(origStyle);
2723 else if (
auto *styleSheetStyle = qt_styleSheet(origStyle))
2724 newStyle = styleSheetStyle;
2725 else if (QStyleSheetStyle *newProxy = qt_styleSheet(parentStyle))
2728 setStyle_helper(newStyle,
true);
2734 if (origStyle == extraStyle)
2740 if (!q->testAttribute(Qt::WA_SetStyle))
2741 origStyle =
nullptr;
2743 setStyle_helper(origStyle,
true);
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2799Qt::WindowModality QWidget::windowModality()
const
2801 return static_cast<Qt::WindowModality>(data->window_modality);
2804void QWidget::setWindowModality(Qt::WindowModality windowModality)
2806 data->window_modality = windowModality;
2808 setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
2809 setAttribute(Qt::WA_SetWindowModality,
true);
2812void QWidgetPrivate::setModal_sys()
2815 if (q->windowHandle())
2816 q->windowHandle()->setModality(q->windowModality());
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841bool QWidget::isMinimized()
const
2842{
return data->window_state & Qt::WindowMinimized; }
2845
2846
2847
2848
2849
2850
2851
2852void QWidget::showMinimized()
2854 bool isMin = isMinimized();
2855 if (isMin && isVisible())
2861 setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881bool QWidget::isMaximized()
const
2882{
return data->window_state & Qt::WindowMaximized; }
2887
2888
2889
2890
2891
2892
2893Qt::WindowStates QWidget::windowState()
const
2895 return Qt::WindowStates(data->window_state);
2899
2900
2901
2902
2903
2904
2905void QWidget::overrideWindowState(Qt::WindowStates newstate)
2907 QWindowStateChangeEvent e(Qt::WindowStates(data->window_state),
true);
2908 data->window_state = newstate;
2909 QCoreApplication::sendEvent(
this, &e);
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942void QWidget::setWindowState(Qt::WindowStates newstate)
2945 Qt::WindowStates oldstate = windowState();
2946 if (newstate.testFlag(Qt::WindowMinimized))
2947 newstate.setFlag(Qt::WindowActive,
false);
2948 if (oldstate == newstate)
2950 if (isWindow() && !testAttribute(Qt::WA_WState_Created))
2953 data->window_state = newstate;
2954 data->in_set_window_state = 1;
2957 if (!testAttribute(Qt::WA_Resized) && !isVisible())
2961 if (!(oldstate & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)))
2962 d->topData()->normalGeometry = geometry();
2964 Q_ASSERT(windowHandle());
2965 windowHandle()->setWindowStates(newstate & ~Qt::WindowActive);
2967 data->in_set_window_state = 0;
2969 if (newstate & Qt::WindowActive)
2972 QWindowStateChangeEvent e(oldstate);
2973 QCoreApplication::sendEvent(
this, &e);
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987bool QWidget::isFullScreen()
const
2988{
return data->window_state & Qt::WindowFullScreen; }
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026void QWidget::showFullScreen()
3030 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
3031 | Qt::WindowFullScreen);
3033#if !defined Q_OS_QNX
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049void QWidget::showMaximized()
3053 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
3054 | Qt::WindowMaximized);
3059
3060
3061
3062
3063
3064
3065void QWidget::showNormal()
3069 setWindowState(windowState() & ~(Qt::WindowMinimized
3070 | Qt::WindowMaximized
3071 | Qt::WindowFullScreen));
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3095bool QWidget::isEnabledTo(
const QWidget *ancestor)
const
3097 const QWidget * w =
this;
3098 while (!w->testAttribute(Qt::WA_ForceDisabled)
3100 && w->parentWidget()
3101 && w->parentWidget() != ancestor)
3102 w = w->parentWidget();
3103 return !w->testAttribute(Qt::WA_ForceDisabled);
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122void QWidget::addAction(QAction *action)
3124 insertAction(
nullptr, action);
3128
3129
3130
3131
3132void QWidget::addActions(
const QList<QAction *> &actions)
3134 for(
int i = 0; i < actions.size(); i++)
3135 insertAction(
nullptr, actions.at(i));
3139
3140
3141
3142
3143
3144
3145
3146
3147void QWidget::insertAction(QAction *before, QAction *action)
3149 if (Q_UNLIKELY(!action)) {
3150 qWarning(
"QWidget::insertAction: Attempt to insert null action");
3155 if (d->actions.contains(action))
3156 removeAction(action);
3158 int pos = d->actions.indexOf(before);
3161 pos = d->actions.size();
3163 d->actions.insert(pos, action);
3165 QActionPrivate *apriv = action->d_func();
3166 apriv->associatedObjects.append(
this);
3168 QActionEvent e(QEvent::ActionAdded, action, before);
3169 QCoreApplication::sendEvent(
this, &e);
3173
3174
3175
3176
3177
3178
3179
3180
3181void QWidget::insertActions(QAction *before,
const QList<QAction*> &actions)
3183 for(
int i = 0; i < actions.size(); ++i)
3184 insertAction(before, actions.at(i));
3188
3189
3190
3191void QWidget::removeAction(QAction *action)
3198 QActionPrivate *apriv = action->d_func();
3199 apriv->associatedObjects.removeAll(
this);
3201 if (d->actions.removeAll(action)) {
3202 QActionEvent e(QEvent::ActionRemoved, action);
3203 QCoreApplication::sendEvent(
this, &e);
3208
3209
3210
3211
3212QList<QAction*> QWidget::actions()
const
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234QAction *QWidget::addAction(
const QString &text)
3236 QAction *ret =
new QAction(text,
this);
3241QAction *QWidget::addAction(
const QIcon &icon,
const QString &text)
3243 QAction *ret =
new QAction(icon, text,
this);
3248#if QT_CONFIG(shortcut)
3249QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut)
3251 QAction *ret = addAction(text);
3252 ret->setShortcut(shortcut);
3256QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut)
3258 QAction *ret = addAction(icon, text);
3259 ret->setShortcut(shortcut);
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282QAction *QWidget::addAction(
const QString &text,
const QObject *receiver,
const char* member,
3283 Qt::ConnectionType type)
3285 QAction *action = addAction(text);
3286 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3290QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
3291 const QObject *receiver,
const char* member,
3292 Qt::ConnectionType type)
3294 QAction *action = addAction(icon, text);
3295 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3299#if QT_CONFIG(shortcut)
3300QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut,
3301 const QObject *receiver,
const char* member,
3302 Qt::ConnectionType type)
3304 QAction *action = addAction(text, receiver, member, type);
3305 action->setShortcut(shortcut);
3309QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut,
3310 const QObject *receiver,
const char* member,
3311 Qt::ConnectionType type)
3313 QAction *action = addAction(icon, text, receiver, member, type);
3314 action->setShortcut(shortcut);
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363void QWidget::setEnabled(
bool enable)
3366 setAttribute(Qt::WA_ForceDisabled, !enable);
3367 d->setEnabled_helper(enable);
3370void QWidgetPrivate::setEnabled_helper(
bool enable)
3374 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
3377 if (enable != q->testAttribute(Qt::WA_Disabled))
3380 q->setAttribute(Qt::WA_Disabled, !enable);
3381 updateSystemBackground();
3383 if (!enable && q->window()->focusWidget() == q) {
3384 bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
3385 if (!parentIsEnabled || !q->focusNextChild())
3389 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
3390 for (
int i = 0; i < children.size(); ++i) {
3391 QWidget *w = qobject_cast<QWidget *>(children.at(i));
3392 if (w && !w->testAttribute(attribute))
3393 w->d_func()->setEnabled_helper(enable);
3396 if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
3399 qt_qpa_set_cursor(q,
false);
3403 if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
3404 QWidget *focusWidget = effectiveFocusWidget();
3407 if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
3408 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3410 QGuiApplication::inputMethod()->commit();
3411 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3415 QEvent e(QEvent::EnabledChange);
3416 QCoreApplication::sendEvent(q, &e);
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436bool QWidget::acceptDrops()
const
3438 return testAttribute(Qt::WA_AcceptDrops);
3441void QWidget::setAcceptDrops(
bool on)
3443 setAttribute(Qt::WA_AcceptDrops, on);
3448
3449
3450
3451
3452
3453
3454
3455void QWidget::setDisabled(
bool disable)
3457 setEnabled(!disable);
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473QRect QWidget::frameGeometry()
const
3476 if (isWindow() && ! (windowType() == Qt::Popup)) {
3477 QRect fs = d->frameStrut();
3478 return QRect(data->crect.x() - fs.left(),
3479 data->crect.y() - fs.top(),
3480 data->crect.width() + fs.left() + fs.right(),
3481 data->crect.height() + fs.top() + fs.bottom());
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499int QWidget::x()
const
3502 if (isWindow() && ! (windowType() == Qt::Popup))
3503 return data->crect.x() - d->frameStrut().left();
3504 return data->crect.x();
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519int QWidget::y()
const
3522 if (isWindow() && ! (windowType() == Qt::Popup))
3523 return data->crect.y() - d->frameStrut().top();
3524 return data->crect.y();
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554QPoint QWidget::pos()
const
3557 QPoint result = data->crect.topLeft();
3558 if (isWindow() && ! (windowType() == Qt::Popup))
3559 if (!d->maybeTopData() || !d->maybeTopData()->posIncludesFrame)
3560 result -= d->frameStrut().topLeft();
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673QRect QWidget::normalGeometry()
const
3679 if (!isMaximized() && !isFullScreen())
3682 return d->topData()->normalGeometry;
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3698QRect QWidget::childrenRect()
const
3701 QRect r(0, 0, 0, 0);
3702 for (
int i = 0; i < d->children.size(); ++i) {
3703 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3704 if (w && !w->isWindow() && !w->isHidden())
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3722QRegion QWidget::childrenRegion()
const
3726 for (
int i = 0; i < d->children.size(); ++i) {
3727 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3728 if (w && !w->isWindow() && !w->isHidden()) {
3729 QRegion mask = w->mask();
3733 r |= mask.translated(w->pos());
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3757QSize QWidget::minimumSize()
const
3760 return d->extra ? QSize(d->extra->minw, d->extra->minh) : QSize(0, 0);
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3779QSize QWidget::maximumSize()
const
3782 return d->extra ? QSize(d->extra->maxw, d->extra->maxh)
3788
3789
3790
3791
3792
3793
3794
3795
3796
3799
3800
3801
3802
3803
3804
3805
3806
3807
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858QSize QWidget::sizeIncrement()
const
3861 return (d->extra && d->extra->topextra)
3862 ? QSize(d->extra->topextra->incw, d->extra->topextra->inch)
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3879QSize QWidget::baseSize()
const
3882 return (d->extra && d->extra->topextra)
3883 ? QSize(d->extra->topextra->basew, d->extra->topextra->baseh)
3887bool QWidgetPrivate::setMinimumSize_helper(
int &minw,
int &minh)
3891 int mw = minw, mh = minh;
3897 qWarning(
"QWidget::setMinimumSize: (%s/%s) "
3898 "The largest allowed size is (%d,%d)",
3899 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
3904 if (Q_UNLIKELY(minw < 0 || minh < 0)) {
3905 qWarning(
"QWidget::setMinimumSize: (%s/%s) Negative sizes (%d,%d) "
3907 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), minw, minh);
3908 minw = mw = qMax(minw, 0);
3909 minh = mh = qMax(minh, 0);
3912 if (extra->minw == mw && extra->minh == mh)
3916 extra->explicitMinSize = (mw ? Qt::Horizontal : 0) | (mh ? Qt::Vertical : 0);
3920void QWidgetPrivate::setConstraints_sys()
3923 if (extra && q->windowHandle()) {
3924 QWindow *win = q->windowHandle();
3925 QWindowPrivate *winp = qt_window_private(win);
3927 winp->minimumSize = QSize(extra->minw, extra->minh);
3928 winp->maximumSize = QSize(extra->maxw, extra->maxh);
3930 if (extra->topextra) {
3931 winp->baseSize = QSize(extra->topextra->basew, extra->topextra->baseh);
3932 winp->sizeIncrement = QSize(extra->topextra->incw, extra->topextra->inch);
3935 if (winp->platformWindow) {
3936 fixPosIncludesFrame();
3937 winp->platformWindow->propagateSizeHints();
3943
3944
3945
3946
3947
3948
3950void QWidget::setMinimumSize(
int minw,
int minh)
3953 if (!d->setMinimumSize_helper(minw, minh))
3957 d->setConstraints_sys();
3958 if (minw > width() || minh > height()) {
3959 bool resized = testAttribute(Qt::WA_Resized);
3960 bool maximized = isMaximized();
3961 resize(qMax(minw,width()), qMax(minh,height()));
3962 setAttribute(Qt::WA_Resized, resized);
3964 data->window_state = data->window_state | Qt::WindowMaximized;
3966#if QT_CONFIG(graphicsview)
3968 if (d->extra->proxyWidget)
3969 d->extra->proxyWidget->setMinimumSize(minw, minh);
3972 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
3975bool QWidgetPrivate::setMaximumSize_helper(
int &maxw,
int &maxh)
3979 qWarning(
"QWidget::setMaximumSize: (%s/%s) "
3980 "The largest allowed size is (%d,%d)",
3981 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
3986 if (Q_UNLIKELY(maxw < 0 || maxh < 0)) {
3987 qWarning(
"QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) "
3989 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), maxw, maxh);
3990 maxw = qMax(maxw, 0);
3991 maxh = qMax(maxh, 0);
3994 if (extra->maxw == maxw && extra->maxh == maxh)
3998 extra->explicitMaxSize = (maxw !=
QWIDGETSIZE_MAX ? Qt::Horizontal : 0) |
4004
4005
4006
4007
4008
4009
4010void QWidget::setMaximumSize(
int maxw,
int maxh)
4013 if (!d->setMaximumSize_helper(maxw, maxh))
4017 d->setConstraints_sys();
4018 if (maxw < width() || maxh < height()) {
4019 bool resized = testAttribute(Qt::WA_Resized);
4020 resize(qMin(maxw,width()), qMin(maxh,height()));
4021 setAttribute(Qt::WA_Resized, resized);
4024#if QT_CONFIG(graphicsview)
4026 if (d->extra->proxyWidget)
4027 d->extra->proxyWidget->setMaximumSize(maxw, maxh);
4031 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4035
4036
4037
4038
4039
4040void QWidget::setSizeIncrement(
int w,
int h)
4044 QTLWExtra* x = d->topData();
4045 if (x->incw == w && x->inch == h)
4050 d->setConstraints_sys();
4054
4055
4056
4057
4058
4059void QWidget::setBaseSize(
int basew,
int baseh)
4063 QTLWExtra* x = d->topData();
4064 if (x->basew == basew && x->baseh == baseh)
4069 d->setConstraints_sys();
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4087void QWidget::setFixedSize(
const QSize & s)
4089 setFixedSize(s.width(), s.height());
4094
4095
4096
4097
4098
4100void QWidget::setFixedSize(
int w,
int h)
4103 bool minSizeSet = d->setMinimumSize_helper(w, h);
4104 bool maxSizeSet = d->setMaximumSize_helper(w, h);
4105 if (!minSizeSet && !maxSizeSet)
4109 d->setConstraints_sys();
4111 d->updateGeometry_helper(
true);
4117void QWidget::setMinimumWidth(
int w)
4121 uint expl = d->extra->explicitMinSize | (w ? Qt::Horizontal : 0);
4122 setMinimumSize(w, minimumSize().height());
4123 d->extra->explicitMinSize = expl;
4126void QWidget::setMinimumHeight(
int h)
4130 uint expl = d->extra->explicitMinSize | (h ? Qt::Vertical : 0);
4131 setMinimumSize(minimumSize().width(), h);
4132 d->extra->explicitMinSize = expl;
4135void QWidget::setMaximumWidth(
int w)
4139 uint expl = d->extra->explicitMaxSize | (w ==
QWIDGETSIZE_MAX ? 0 : Qt::Horizontal);
4140 setMaximumSize(w, maximumSize().height());
4141 d->extra->explicitMaxSize = expl;
4144void QWidget::setMaximumHeight(
int h)
4148 uint expl = d->extra->explicitMaxSize | (h ==
QWIDGETSIZE_MAX ? 0 : Qt::Vertical);
4149 setMaximumSize(maximumSize().width(), h);
4150 d->extra->explicitMaxSize = expl;
4154
4155
4156
4157
4158
4160void QWidget::setFixedWidth(
int w)
4164 uint explMin = d->extra->explicitMinSize | Qt::Horizontal;
4165 uint explMax = d->extra->explicitMaxSize | Qt::Horizontal;
4166 setMinimumSize(w, minimumSize().height());
4167 setMaximumSize(w, maximumSize().height());
4168 d->extra->explicitMinSize = explMin;
4169 d->extra->explicitMaxSize = explMax;
4174
4175
4176
4177
4178
4180void QWidget::setFixedHeight(
int h)
4184 uint explMin = d->extra->explicitMinSize | Qt::Vertical;
4185 uint explMax = d->extra->explicitMaxSize | Qt::Vertical;
4186 setMinimumSize(minimumSize().width(), h);
4187 setMaximumSize(maximumSize().width(), h);
4188 d->extra->explicitMinSize = explMin;
4189 d->extra->explicitMaxSize = explMax;
4194
4195
4196
4197
4198
4199
4200
4202QPointF QWidget::mapTo(
const QWidget *parent,
const QPointF &pos)
const
4206 const QWidget * w =
this;
4207 while (w != parent) {
4208 p = w->mapToParent(p);
4209 w = w->parentWidget();
4211 qWarning(
"QWidget::mapTo(): parent must be in parent hierarchy");
4220
4221
4222QPoint QWidget::mapTo(
const QWidget *parent,
const QPoint &pos)
const
4224 return mapTo(parent, QPointF(pos)).toPoint();
4228
4229
4230
4231
4232
4233
4234
4236QPointF QWidget::mapFrom(
const QWidget *parent,
const QPointF &pos)
const
4240 const QWidget * w =
this;
4241 while (w != parent) {
4242 p = w->mapFromParent(p);
4243 w = w->parentWidget();
4245 qWarning(
"QWidget::mapFrom(): parent must be in parent hierarchy");
4254
4255
4256QPoint QWidget::mapFrom(
const QWidget *parent,
const QPoint &pos)
const
4258 return mapFrom(parent, QPointF(pos)).toPoint();
4262
4263
4264
4265
4266
4267
4268
4269
4271QPointF QWidget::mapToParent(
const QPointF &pos)
const
4273 return pos + QPointF(data->crect.topLeft());
4277
4278
4279QPoint QWidget::mapToParent(
const QPoint &pos)
const
4281 return pos + data->crect.topLeft();
4285
4286
4287
4288
4289
4290
4291
4292
4294QPointF QWidget::mapFromParent(
const QPointF &pos)
const
4296 return pos - QPointF(data->crect.topLeft());
4300
4301
4302QPoint QWidget::mapFromParent(
const QPoint &pos)
const
4304 return pos - data->crect.topLeft();
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4321QWidget *QWidget::window()
const
4323 QWidget *w =
const_cast<QWidget *>(
this);
4324 QWidget *p = w->parentWidget();
4325 while (!w->isWindow() && p) {
4327 p = p->parentWidget();
4333
4334
4335
4336
4337
4338
4339
4340
4341QWidget *QWidget::nativeParentWidget()
const
4343 QWidget *parent = parentWidget();
4344 while (parent && !parent->internalWinId())
4345 parent = parent->parentWidget();
4350
4351
4352
4353
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368QPalette::ColorRole QWidget::backgroundRole()
const
4371 const QWidget *w =
this;
4373 QPalette::ColorRole role = w->d_func()->bg_role;
4374 if (role != QPalette::NoRole)
4376 if (w->isWindow() || w->windowType() == Qt::SubWindow)
4378 w = w->parentWidget();
4380 return QPalette::Window;
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4399void QWidget::setBackgroundRole(QPalette::ColorRole role)
4403 d->updateSystemBackground();
4404 d->propagatePaletteChange();
4405 d->updateIsOpaque();
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419QPalette::ColorRole QWidget::foregroundRole()
const
4422 QPalette::ColorRole rl = QPalette::ColorRole(d->fg_role);
4423 if (rl != QPalette::NoRole)
4425 QPalette::ColorRole role = QPalette::WindowText;
4426 switch (backgroundRole()) {
4427 case QPalette::Button:
4428 role = QPalette::ButtonText;
4430 case QPalette::Base:
4431 role = QPalette::Text;
4433 case QPalette::Dark:
4434 case QPalette::Shadow:
4435 role = QPalette::Light;
4437 case QPalette::Highlight:
4438 role = QPalette::HighlightedText;
4440 case QPalette::ToolTipBase:
4441 role = QPalette::ToolTipText;
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464void QWidget::setForegroundRole(QPalette::ColorRole role)
4468 d->updateSystemBackground();
4469 d->propagatePaletteChange();
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522const QPalette &QWidget::palette()
const
4525 data->pal.setCurrentColorGroup(QPalette::Disabled);
4526 }
else if ((!isVisible() || isActiveWindow())
4527#if defined(Q_OS_WIN)
4528 && !QApplicationPrivate::isBlockedByModal(
const_cast<QWidget *>(
this))
4531 data->pal.setCurrentColorGroup(QPalette::Active);
4533 data->pal.setCurrentColorGroup(QPalette::Inactive);
4538void QWidget::setPalette(
const QPalette &palette)
4541 setAttribute(Qt::WA_SetPalette, palette.resolveMask() != 0);
4547 QPalette naturalPalette = d->naturalWidgetPalette(d->inheritedPaletteResolveMask);
4548 QPalette resolvedPalette = palette.resolve(naturalPalette);
4549 d->setPalette_helper(resolvedPalette);
4553
4554
4555
4556
4557
4558
4559
4560
4561QPalette QWidgetPrivate::naturalWidgetPalette(QPalette::ResolveMask inheritedMask)
const
4565 const bool useStyleSheetPropagationInWidgetStyles =
4566 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4568 QPalette naturalPalette = QApplication::palette(q);
4569 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4570 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4571#if QT_CONFIG(graphicsview)
4572 || (extra && extra->proxyWidget)
4575 if (QWidget *p = q->parentWidget()) {
4576 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4577 if (!naturalPalette.isCopyOf(QGuiApplication::palette())) {
4578 QPalette inheritedPalette = p->palette();
4579 inheritedPalette.setResolveMask(inheritedMask);
4580 naturalPalette = inheritedPalette.resolve(naturalPalette);
4582 naturalPalette = p->palette();
4586#if QT_CONFIG(graphicsview)
4587 else if (extra && extra->proxyWidget) {
4588 QPalette inheritedPalette = extra->proxyWidget->palette();
4589 inheritedPalette.setResolveMask(inheritedMask);
4590 naturalPalette = inheritedPalette.resolve(naturalPalette);
4594 naturalPalette.setResolveMask(0);
4595 return naturalPalette;
4598
4599
4600
4601
4602
4603
4604
4605void QWidgetPrivate::resolvePalette()
4607 QPalette naturalPalette = naturalWidgetPalette(inheritedPaletteResolveMask);
4608 QPalette resolvedPalette = data.pal.resolve(naturalPalette);
4609 setPalette_helper(resolvedPalette);
4612void QWidgetPrivate::setPalette_helper(
const QPalette &palette)
4615 if (data.pal == palette && data.pal.resolveMask() == palette.resolveMask())
4618 updateSystemBackground();
4619 propagatePaletteChange();
4625void QWidgetPrivate::updateSystemBackground()
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4675void QWidget::setFont(
const QFont &font)
4679#ifndef QT_NO_STYLE_STYLESHEET
4680 const QStyleSheetStyle* style;
4681 if (d->extra && (style = qt_styleSheet(d->extra->style)))
4682 style->saveWidgetFont(
this, font);
4685 setAttribute(Qt::WA_SetFont, font.resolveMask() != 0);
4691 QFont naturalFont = d->naturalWidgetFont(d->inheritedFontResolveMask);
4692 QFont resolvedFont = font.resolve(naturalFont);
4693 d->setFont_helper(resolvedFont);
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708QFont QWidgetPrivate::naturalWidgetFont(uint inheritedMask)
const
4712 const bool useStyleSheetPropagationInWidgetStyles =
4713 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4715 QFont naturalFont = QApplication::font(q);
4716 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4717 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4718#if QT_CONFIG(graphicsview)
4719 || (extra && extra->proxyWidget)
4722 if (QWidget *p = q->parentWidget()) {
4723 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4724 if (!naturalFont.isCopyOf(QApplication::font())) {
4725 if (inheritedMask != 0) {
4726 QFont inheritedFont = p->font();
4727 inheritedFont.setResolveMask(inheritedMask);
4728 naturalFont = inheritedFont.resolve(naturalFont);
4731 naturalFont = p->font();
4735#if QT_CONFIG(graphicsview)
4736 else if (extra && extra->proxyWidget) {
4737 if (inheritedMask != 0) {
4738 QFont inheritedFont = extra->proxyWidget->font();
4739 inheritedFont.setResolveMask(inheritedMask);
4740 naturalFont = inheritedFont.resolve(naturalFont);
4745 naturalFont.setResolveMask(0);
4750
4751
4752
4753
4754QFont QWidgetPrivate::localFont()
const
4756 QFont localfont = data.fnt;
4757 localfont.setResolveMask(directFontResolveMask);
4762
4763
4764
4765
4766
4767
4768
4769void QWidgetPrivate::resolveFont()
4771 QFont naturalFont = naturalWidgetFont(inheritedFontResolveMask);
4772 QFont resolvedFont = localFont().resolve(naturalFont);
4773 setFont_helper(resolvedFont);
4777
4778
4779
4780
4781
4782
4783
4784
4785void QWidgetPrivate::updateFont(
const QFont &font)
4788#ifndef QT_NO_STYLE_STYLESHEET
4789 const QStyleSheetStyle* cssStyle;
4790 cssStyle = extra ? qt_styleSheet(extra->style) :
nullptr;
4791 const bool useStyleSheetPropagationInWidgetStyles =
4792 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4795 data.fnt = QFont(font, q);
4798#if QT_CONFIG(graphicsview)
4799 if (!q->parentWidget() && extra && extra->proxyWidget) {
4800 QGraphicsProxyWidget *p = extra->proxyWidget;
4801 inheritedFontResolveMask = p->d_func()->inheritedFontResolveMask | p->font().resolveMask();
4804 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
4805 inheritedFontResolveMask = 0;
4807 uint newMask = data.fnt.resolveMask() | inheritedFontResolveMask;
4811 directFontResolveMask = data.fnt.resolveMask();
4812 data.fnt.setResolveMask(newMask);
4814 for (
int i = 0; i < children.size(); ++i) {
4815 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4818#ifndef QT_NO_STYLE_STYLESHEET
4819 }
else if (!useStyleSheetPropagationInWidgetStyles && w->testAttribute(Qt::WA_StyleSheet)) {
4822 cssStyle->updateStyleSheetFont(w);
4824 }
else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
4826 QWidgetPrivate *wd = w->d_func();
4827 wd->inheritedFontResolveMask = newMask;
4833#ifndef QT_NO_STYLE_STYLESHEET
4834 if (!useStyleSheetPropagationInWidgetStyles && cssStyle) {
4835 cssStyle->updateStyleSheetFont(q);
4839 QEvent e(QEvent::FontChange);
4840 QCoreApplication::sendEvent(q, &e);
4843void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
4847 if ( (direction == Qt::RightToLeft) == q->testAttribute(Qt::WA_RightToLeft))
4849 q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
4850 if (!children.isEmpty()) {
4851 for (
int i = 0; i < children.size(); ++i) {
4852 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4853 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_SetLayoutDirection))
4854 w->d_func()->setLayoutDirection_helper(direction);
4857 QEvent e(QEvent::LayoutDirectionChange);
4858 QCoreApplication::sendEvent(q, &e);
4861void QWidgetPrivate::resolveLayoutDirection()
4864 if (!q->testAttribute(Qt::WA_SetLayoutDirection))
4865 setLayoutDirection_helper(q->isWindow() ? QGuiApplication::layoutDirection() : q->parentWidget()->layoutDirection());
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887void QWidget::setLayoutDirection(Qt::LayoutDirection direction)
4891 if (direction == Qt::LayoutDirectionAuto) {
4892 unsetLayoutDirection();
4896 setAttribute(Qt::WA_SetLayoutDirection);
4897 d->setLayoutDirection_helper(direction);
4900Qt::LayoutDirection QWidget::layoutDirection()
const
4902 return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
4905void QWidget::unsetLayoutDirection()
4908 setAttribute(Qt::WA_SetLayoutDirection,
false);
4909 d->resolveLayoutDirection();
4913
4914
4915
4916
4917
4918
4919
4922
4923
4924
4925
4926
4927
4928
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4956QCursor QWidget::cursor()
const
4959 if (testAttribute(Qt::WA_SetCursor))
4960 return (d->extra && d->extra->curs)
4962 : QCursor(Qt::ArrowCursor);
4963 if (isWindow() || !parentWidget())
4964 return QCursor(Qt::ArrowCursor);
4965 return parentWidget()->cursor();
4968void QWidget::setCursor(
const QCursor &cursor)
4971 if (cursor.shape() != Qt::ArrowCursor
4972 || (d->extra && d->extra->curs))
4975 d->extra->curs = std::make_unique<QCursor>(cursor);
4977 setAttribute(Qt::WA_SetCursor);
4978 d->setCursor_sys(cursor);
4980 QEvent event(QEvent::CursorChange);
4981 QCoreApplication::sendEvent(
this, &event);
4984void QWidgetPrivate::setCursor_sys(
const QCursor &cursor)
4988 qt_qpa_set_cursor(q,
false);
4991void QWidget::unsetCursor()
4995 d->extra->curs.reset();
4997 setAttribute(Qt::WA_SetCursor,
false);
4998 d->unsetCursor_sys();
5000 QEvent event(QEvent::CursorChange);
5001 QCoreApplication::sendEvent(
this, &event);
5004void QWidgetPrivate::unsetCursor_sys()
5007 qt_qpa_set_cursor(q,
false);
5012 if (QWindow *window = w->windowHandle())
5013 window->setCursor(c);
5018 if (QWindow *window = w->windowHandle())
5019 window->unsetCursor();
5024 if (!w->testAttribute(Qt::WA_WState_Created))
5030 }
else if (lastUnderMouse) {
5031 const WId lastWinId = lastUnderMouse->effectiveWinId();
5032 const WId winId = w->effectiveWinId();
5033 if (lastWinId && lastWinId == winId)
5035 }
else if (!w->internalWinId()) {
5039 while (!w->internalWinId() && w->parentWidget() && !w->isWindow()
5040 && !w->testAttribute(Qt::WA_SetCursor))
5041 w = w->parentWidget();
5044 if (!w->internalWinId())
5045 nativeParent = w->nativeParentWidget();
5046 if (!nativeParent || !nativeParent->internalWinId())
5049 if (w->isWindow() || w->testAttribute(Qt::WA_SetCursor)) {
5051 applyCursor(nativeParent, w->cursor());
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100void QWidget::render(QPaintDevice *target,
const QPoint &targetOffset,
5101 const QRegion &sourceRegion, RenderFlags renderFlags)
5104 render(&p, targetOffset, sourceRegion, renderFlags);
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120void QWidget::render(QPainter *painter,
const QPoint &targetOffset,
5121 const QRegion &sourceRegion, RenderFlags renderFlags)
5123 if (Q_UNLIKELY(!painter)) {
5124 qWarning(
"QWidget::render: Null pointer to painter");
5128 if (Q_UNLIKELY(!painter->isActive())) {
5129 qWarning(
"QWidget::render: Cannot render with an inactive painter");
5133 const qreal opacity = painter->opacity();
5134 if (qFuzzyIsNull(opacity))
5138 const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
5139 const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
5141 if (toBePainted.isEmpty())
5146 d->extra->inRenderWithPainter =
true;
5148 QPaintEngine *engine = painter->paintEngine();
5150 QPaintEnginePrivate *enginePriv = engine->d_func();
5151 Q_ASSERT(enginePriv);
5152 QPaintDevice *target = engine->paintDevice();
5156 if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
5157 d->render_helper(painter, targetOffset, toBePainted, renderFlags);
5158 d->extra->inRenderWithPainter = inRenderWithPainter;
5163 QPainter *oldPainter = d->sharedPainter();
5164 d->setSharedPainter(painter);
5167 const QTransform oldTransform = enginePriv->systemTransform;
5168 const QRegion oldSystemClip = enginePriv->systemClip;
5169 const QRegion oldBaseClip = enginePriv->baseSystemClip;
5170 const QRegion oldSystemViewport = enginePriv->systemViewport;
5171 const Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection();
5174 if (painter->hasClipping()) {
5175 const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
5176 enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
5178 enginePriv->setSystemViewport(oldSystemClip);
5180 painter->setLayoutDirection(layoutDirection());
5182 d->render(target, targetOffset, toBePainted, renderFlags);
5185 enginePriv->baseSystemClip = oldBaseClip;
5186 enginePriv->setSystemTransformAndViewport(oldTransform, oldSystemViewport);
5187 enginePriv->systemStateChanged();
5188 painter->setLayoutDirection(oldLayoutDirection);
5191 d->setSharedPainter(oldPainter);
5193 d->extra->inRenderWithPainter = inRenderWithPainter;
5198 QResizeEvent e(target->size(), QSize());
5199 QCoreApplication::sendEvent(target, &e);
5201 const QObjectList children = target->children();
5202 for (
int i = 0; i < children.size(); ++i) {
5203 if (!children.at(i)->isWidgetType())
5206 if (!child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223QPixmap QWidget::grab(
const QRect &rectangle)
5226 if (testAttribute(Qt::WA_PendingResizeEvent) || !testAttribute(Qt::WA_WState_Created))
5227 sendResizeEvents(
this);
5229 const QWidget::RenderFlags renderFlags = QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask;
5231 const bool oldDirtyOpaqueChildren = d->dirtyOpaqueChildren;
5233 if (r.width() < 0 || r.height() < 0) {
5236 r = d->prepareToRender(QRegion(), renderFlags).boundingRect();
5237 r.setTopLeft(rectangle.topLeft());
5240 if (!r.intersects(rect()))
5243 const qreal dpr = devicePixelRatio();
5244 QPixmap res((QSizeF(r.size()) * dpr).toSize());
5245 res.setDevicePixelRatio(dpr);
5247 res.fill(Qt::transparent);
5248 d->render(&res, QPoint(), QRegion(r), renderFlags);
5250 d->dirtyOpaqueChildren = oldDirtyOpaqueChildren;
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264#if QT_CONFIG(graphicseffect)
5265QGraphicsEffect *QWidget::graphicsEffect()
const
5268 return d->graphicsEffect;
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294#if QT_CONFIG(graphicseffect)
5295void QWidget::setGraphicsEffect(QGraphicsEffect *effect)
5298 if (d->graphicsEffect == effect)
5301 if (d->graphicsEffect) {
5302 d->invalidateBackingStore(rect());
5303 delete d->graphicsEffect;
5304 d->graphicsEffect =
nullptr;
5309 QGraphicsEffectSourcePrivate *sourced =
new QWidgetEffectSourcePrivate(
this);
5310 QGraphicsEffectSource *source =
new QGraphicsEffectSource(*sourced);
5311 d->graphicsEffect = effect;
5312 effect->d_func()->setGraphicsEffectSource(source);
5316 d->updateIsOpaque();
5320bool QWidgetPrivate::isAboutToShow()
const
5330 QWidget *parent = q->parentWidget();
5331 return parent ? parent->d_func()->isAboutToShow() :
false;
5334QRegion QWidgetPrivate::prepareToRender(
const QRegion ®ion, QWidget::RenderFlags renderFlags)
5337 const bool isVisible = q->isVisible();
5340 if (!isVisible && !isAboutToShow()) {
5341 QWidget *topLevel = q->window();
5342 (
void)topLevel->d_func()->topData();
5343 topLevel->ensurePolished();
5347 QWidget *widget = q;
5348 QWidgetList hiddenWidgets;
5350 if (widget->isHidden()) {
5351 widget->setAttribute(Qt::WA_WState_Hidden,
false);
5352 hiddenWidgets.append(widget);
5353 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5354 widget->d_func()->updateGeometry_helper(
true);
5356 widget = widget->parentWidget();
5360 if (topLevel->d_func()->layout)
5361 topLevel->d_func()->layout->activate();
5364 QTLWExtra *topLevelExtra = topLevel->d_func()->maybeTopData();
5365 if (topLevelExtra && !topLevelExtra->sizeAdjusted
5366 && !topLevel->testAttribute(Qt::WA_Resized)) {
5367 topLevel->adjustSize();
5368 topLevel->setAttribute(Qt::WA_Resized,
false);
5372 topLevel->d_func()->activateChildLayoutsRecursively();
5375 for (
int i = 0; i < hiddenWidgets.size(); ++i) {
5376 QWidget *widget = hiddenWidgets.at(i);
5377 widget->setAttribute(Qt::WA_WState_Hidden);
5378 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5379 widget->parentWidget()->d_func()->layout->invalidate();
5381 }
else if (isVisible) {
5382 q->window()->d_func()->sendPendingMoveAndResizeEvents(
true,
true);
5386 QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
5387 if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
5388 toBePainted &= extra->mask;
5392void QWidgetPrivate::render_helper(QPainter *painter,
const QPoint &targetOffset,
const QRegion &toBePainted,
5393 QWidget::RenderFlags renderFlags)
5396 Q_ASSERT(!toBePainted.isEmpty());
5399 const QTransform originalTransform = painter->worldTransform();
5400 const bool useDeviceCoordinates = originalTransform.isScaling();
5401 if (!useDeviceCoordinates) {
5403 const QRect rect = toBePainted.boundingRect();
5404 const QSize size = rect.size();
5408 const qreal pixmapDevicePixelRatio = painter->device()->devicePixelRatio();
5409 QPixmap pixmap(size * pixmapDevicePixelRatio);
5410 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio);
5412 if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
5413 pixmap.fill(Qt::transparent);
5414 q->render(&pixmap, QPoint(), toBePainted, renderFlags);
5416 const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
5417 painter->setRenderHints(QPainter::SmoothPixmapTransform,
true);
5419 painter->drawPixmap(targetOffset, pixmap);
5422 painter->setRenderHints(QPainter::SmoothPixmapTransform,
false);
5426 QTransform transform = originalTransform;
5427 transform.translate(targetOffset.x(), targetOffset.y());
5429 QPaintDevice *device = painter->device();
5433 const QRectF rect(toBePainted.boundingRect());
5434 QRect deviceRect = transform.mapRect(QRectF(0, 0, rect.width(), rect.height())).toAlignedRect();
5435 deviceRect &= QRect(0, 0, device->width(), device->height());
5437 QPixmap pixmap(deviceRect.size());
5438 pixmap.fill(Qt::transparent);
5441 QPainter pixmapPainter(&pixmap);
5442 pixmapPainter.setRenderHints(painter->renderHints());
5443 transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
5444 pixmapPainter.setTransform(transform);
5446 q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
5447 pixmapPainter.end();
5450 painter->setTransform(QTransform());
5451 painter->drawPixmap(deviceRect.topLeft(), pixmap);
5452 painter->setTransform(originalTransform);
5456void QWidgetPrivate::drawWidget(QPaintDevice *pdev,
const QRegion &rgn,
const QPoint &offset, DrawWidgetFlags flags,
5457 QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5464 qCInfo(lcWidgetPainting) <<
"Drawing" << rgn <<
"of" << q <<
"at" << offset
5465 <<
"into paint device" << pdev <<
"with" << flags;
5467 const bool asRoot = flags & DrawAsRoot;
5468 bool onScreen = shouldPaintOnScreen();
5470#if QT_CONFIG(graphicseffect)
5471 if (graphicsEffect && graphicsEffect->isEnabled()) {
5472 QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
5473 QWidgetEffectSourcePrivate *sourced =
static_cast<QWidgetEffectSourcePrivate *>
5475 if (!sourced->context) {
5476 const QRegion effectRgn((flags & UseEffectRegionBounds) ? rgn.boundingRect() : rgn);
5477 QWidgetPaintContext context(pdev, effectRgn, offset, flags, sharedPainter, repaintManager);
5478 sourced->context = &context;
5479 if (!sharedPainter) {
5480 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), effectRgn.translated(offset));
5482 p.translate(offset);
5483 context.painter = &p;
5484 graphicsEffect->draw(&p);
5485 setSystemClip(pdev->paintEngine(), 1, QRegion());
5487 context.painter = sharedPainter;
5488 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
5489 sourced->invalidateCache();
5490 sourced->lastEffectTransform = sharedPainter->worldTransform();
5492 sharedPainter->save();
5493 sharedPainter->translate(offset);
5494 setSystemClip(sharedPainter->paintEngine(), sharedPainter->device()->devicePixelRatio(), effectRgn.translated(offset));
5495 graphicsEffect->draw(sharedPainter);
5496 setSystemClip(sharedPainter->paintEngine(), 1, QRegion());
5497 sharedPainter->restore();
5499 sourced->context =
nullptr;
5502 repaintManager->markNeedsFlush(q, effectRgn, offset);
5508 flags = flags & ~UseEffectRegionBounds;
5510 const bool alsoOnScreen = flags & DrawPaintOnScreen;
5511 const bool recursive = flags & DrawRecursive;
5512 const bool alsoInvisible = flags & DrawInvisible;
5514 Q_ASSERT(sharedPainter ? sharedPainter->isActive() :
true);
5516 QRegion toBePainted(rgn);
5517 if (asRoot && !alsoInvisible)
5518 toBePainted &= clipRect();
5519 if (!(flags & DontSubtractOpaqueChildren))
5520 subtractOpaqueChildren(toBePainted, q->rect());
5522 if (!toBePainted.isEmpty()) {
5523 if (!onScreen || alsoOnScreen) {
5525 if (Q_UNLIKELY(q->testAttribute(Qt::WA_WState_InPaintEvent)))
5526 qWarning(
"QWidget::repaint: Recursive repaint detected");
5527 q->setAttribute(Qt::WA_WState_InPaintEvent);
5530 QPaintEngine *paintEngine = pdev->paintEngine();
5532 setRedirected(pdev, -offset);
5535 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted);
5537 paintEngine->d_func()->systemRect = q->data->crect;
5540 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
5541 && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
5542 beginBackingStorePainting();
5544 p.setRenderHint(QPainter::SmoothPixmapTransform);
5545 paintBackground(&p, toBePainted, (asRoot || onScreen) ? (flags | DrawAsRoot) : DrawWidgetFlags());
5546 endBackingStorePainting();
5550 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted.translated(offset));
5552 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
5553 beginBackingStorePainting();
5555 QColor tint = q->palette().window().color();
5556 tint.setAlphaF(.6f);
5557 p.fillRect(toBePainted.boundingRect(), tint);
5558 endBackingStorePainting();
5563 qDebug() <<
"painting" << q <<
"opaque ==" << isOpaque();
5564 qDebug() <<
"clipping to" << toBePainted <<
"location == " << offset
5565 <<
"geometry ==" << QRect(q->mapTo(q->window(), QPoint(0, 0)), q->size());
5568 bool skipPaintEvent =
false;
5569 if (renderToTexture) {
5572 beginBackingStorePainting();
5573 if (!q->testAttribute(Qt::WA_AlwaysStackOnTop) && repaintManager) {
5575 p.setCompositionMode(QPainter::CompositionMode_Source);
5576 p.fillRect(q->rect(), Qt::transparent);
5577 }
else if (!repaintManager) {
5579 QImage img = grabFramebuffer();
5582 if (img.format() == QImage::Format_RGB32)
5583 img.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied);
5585 p.drawImage(q->rect(), img);
5586 skipPaintEvent =
true;
5588 endBackingStorePainting();
5589 if (renderToTextureReallyDirty)
5590 renderToTextureReallyDirty = 0;
5592 skipPaintEvent =
true;
5595 if (!skipPaintEvent) {
5597 sendPaintEvent(toBePainted);
5601 repaintManager->markNeedsFlush(q, toBePainted, offset);
5605 restoreRedirected();
5607 paintEngine->d_func()->systemRect = QRect();
5609 paintEngine->d_func()->currentClipDevice =
nullptr;
5611 setSystemClip(pdev->paintEngine(), 1, QRegion());
5613 q->setAttribute(Qt::WA_WState_InPaintEvent,
false);
5614 if (Q_UNLIKELY(q->paintingActive()))
5615 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
5617 if (paintEngine && paintEngine->autoDestruct()) {
5620 }
else if (q->isWindow()) {
5621 QPaintEngine *engine = pdev->paintEngine();
5624 p.setClipRegion(toBePainted);
5625 const QBrush bg = q->palette().brush(QPalette::Window);
5626 if (bg.style() == Qt::TexturePattern)
5627 p.drawTiledPixmap(q->rect(), bg.texture());
5629 p.fillRect(q->rect(), bg);
5631 if (engine->autoDestruct())
5637 if (recursive && !children.isEmpty()) {
5638 paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot,
5639 sharedPainter, repaintManager);
5643void QWidgetPrivate::sendPaintEvent(
const QRegion &toBePainted)
5646 QPaintEvent e(toBePainted);
5647 QCoreApplication::sendSpontaneousEvent(q, &e);
5649 if (renderToTexture)
5653void QWidgetPrivate::render(QPaintDevice *target,
const QPoint &targetOffset,
5654 const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
5656 if (Q_UNLIKELY(!target)) {
5657 qWarning(
"QWidget::render: null pointer to paint device");
5661 const bool inRenderWithPainter = extra && extra->inRenderWithPainter;
5662 QRegion paintRegion = !inRenderWithPainter
5663 ? prepareToRender(sourceRegion, renderFlags)
5665 if (paintRegion.isEmpty())
5668 QPainter *oldSharedPainter = inRenderWithPainter ? sharedPainter() :
nullptr;
5672 if (target->devType() == QInternal::Widget) {
5673 QWidgetPrivate *targetPrivate =
static_cast<QWidget *>(target)->d_func();
5674 if (targetPrivate->extra && targetPrivate->extra->inRenderWithPainter) {
5675 QPainter *targetPainter = targetPrivate->sharedPainter();
5676 if (targetPainter && targetPainter->isActive())
5677 setSharedPainter(targetPainter);
5684 QPoint offset = targetOffset;
5685 offset -= paintRegion.boundingRect().topLeft();
5686 QPoint redirectionOffset;
5687 QPaintDevice *redirected =
nullptr;
5689 if (target->devType() == QInternal::Widget)
5690 redirected =
static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
5693 target = redirected;
5694 offset -= redirectionOffset;
5697 if (!inRenderWithPainter) {
5698 if (QPaintEngine *targetEngine = target->paintEngine()) {
5699 const QRegion targetSystemClip = targetEngine->systemClip();
5700 if (!targetSystemClip.isEmpty())
5701 paintRegion &= targetSystemClip.translated(-offset);
5706 DrawWidgetFlags flags = DrawPaintOnScreen | DrawInvisible;
5707 if (renderFlags & QWidget::DrawWindowBackground)
5708 flags |= DrawAsRoot;
5710 if (renderFlags & QWidget::DrawChildren)
5711 flags |= DrawRecursive;
5713 flags |= DontSubtractOpaqueChildren;
5715 flags |= DontSetCompositionMode;
5718 drawWidget(target, paintRegion, offset, flags, sharedPainter());
5721 if (oldSharedPainter)
5722 setSharedPainter(oldSharedPainter);
5725void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev,
const QObjectList& siblings,
int index,
const QRegion &rgn,
5726 const QPoint &offset, DrawWidgetFlags flags
5727 , QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5729 QWidget *w =
nullptr;
5731 bool dirtyBoundingRect =
true;
5732 const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
5733 const bool excludeNativeChildren = (flags & DontDrawNativeChildren);
5736 QWidget *x = qobject_cast<QWidget*>(siblings.at(index));
5737 if (x && !(exludeOpaqueChildren && x->d_func()->isOpaque) && !x->isHidden() && !x->isWindow()
5738 && !(excludeNativeChildren && x->internalWinId())) {
5739 if (dirtyBoundingRect) {
5740 boundingRect = rgn.boundingRect();
5741 dirtyBoundingRect =
false;
5744 if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
5750 }
while (index >= 0);
5755 QWidgetPrivate *wd = w->d_func();
5756 const QPoint widgetPos(w->data->crect.topLeft());
5757 const bool hasMask = wd->extra && wd->extra->hasMask && !wd->graphicsEffect;
5761 wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
5762 paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags,
5763 sharedPainter, repaintManager);
5766 if (w->updatesEnabled()
5767#if QT_CONFIG(graphicsview)
5768 && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
5771 QRegion wRegion(rgn);
5772 wRegion &= wd->effectiveRectFor(w->data->crect);
5773 wRegion.translate(-widgetPos);
5775 wRegion &= wd->extra->mask;
5776 wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, repaintManager);
5780#if QT_CONFIG(graphicseffect)
5781QRectF QWidgetEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system)
const
5783 if (system != Qt::DeviceCoordinates)
5784 return m_widget->rect();
5786 if (Q_UNLIKELY(!context)) {
5788 qWarning(
"QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
5792 return context->painter->worldTransform().mapRect(m_widget->rect());
5795void QWidgetEffectSourcePrivate::draw(QPainter *painter)
5797 if (!context || context->painter != painter) {
5798 m_widget->render(painter);
5804 QRegion toBePainted = context->rgn;
5805 toBePainted &= m_widget->rect();
5806 QWidgetPrivate *wd = qt_widget_private(m_widget);
5807 if (wd->extra && wd->extra->hasMask)
5808 toBePainted &= wd->extra->mask;
5810 wd->drawWidget(context->pdev, toBePainted, context->offset, context->flags,
5811 context->sharedPainter, context->repaintManager);
5814QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
5815 QGraphicsEffect::PixmapPadMode mode)
const
5817 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
5818 if (Q_UNLIKELY(!context && deviceCoordinates)) {
5820 qWarning(
"QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
5824 QPoint pixmapOffset;
5825 QRectF sourceRect = m_widget->rect();
5827 if (deviceCoordinates) {
5828 const QTransform &painterTransform = context->painter->worldTransform();
5829 sourceRect = painterTransform.mapRect(sourceRect);
5830 pixmapOffset = painterTransform.map(pixmapOffset);
5835 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect)
5836 effectRect = m_widget->graphicsEffect()->boundingRectFor(sourceRect).toAlignedRect();
5837 else if (mode == QGraphicsEffect::PadToTransparentBorder)
5838 effectRect = sourceRect.adjusted(-1, -1, 1, 1).toAlignedRect();
5840 effectRect = sourceRect.toAlignedRect();
5843 *offset = effectRect.topLeft();
5845 pixmapOffset -= effectRect.topLeft();
5848 if (
const auto *paintDevice = context->painter->device())
5849 dpr = paintDevice->devicePixelRatio();
5851 qWarning(
"QWidgetEffectSourcePrivate::pixmap: Painter not active");
5852 QPixmap pixmap(effectRect.size() * dpr);
5853 pixmap.setDevicePixelRatio(dpr);
5855 pixmap.fill(Qt::transparent);
5856 m_widget->render(&pixmap, pixmapOffset, QRegion(), QWidget::DrawChildren);
5861#if QT_CONFIG(graphicsview)
5863
5864
5865
5866
5867
5868
5869
5870QGraphicsProxyWidget *QWidgetPrivate::nearestGraphicsProxyWidget(
const QWidget *origin)
5873 const auto &extra = origin->d_func()->extra;
5874 if (extra && extra->proxyWidget)
5875 return extra->proxyWidget;
5876 return nearestGraphicsProxyWidget(origin->parentWidget());
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5897void QWidgetPrivate::setLocale_helper(
const QLocale &loc,
bool forceUpdate)
5900 if (locale == loc && !forceUpdate)
5905 if (!children.isEmpty()) {
5906 for (
int i = 0; i < children.size(); ++i) {
5907 QWidget *w = qobject_cast<QWidget*>(children.at(i));
5910 if (w->testAttribute(Qt::WA_SetLocale))
5912 if (w->isWindow() && !w->testAttribute(Qt::WA_WindowPropagation))
5914 w->d_func()->setLocale_helper(loc, forceUpdate);
5917 QEvent e(QEvent::LocaleChange);
5918 QCoreApplication::sendEvent(q, &e);
5921void QWidget::setLocale(
const QLocale &locale)
5925 setAttribute(Qt::WA_SetLocale);
5926 d->setLocale_helper(locale);
5929QLocale QWidget::locale()
const
5936void QWidgetPrivate::resolveLocale()
5940 if (!q->testAttribute(Qt::WA_SetLocale)) {
5941 QWidget *parent = q->parentWidget();
5942 setLocale_helper(!parent || (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
5943 ? QLocale() : parent->locale());
5947void QWidget::unsetLocale()
5950 setAttribute(Qt::WA_SetLocale,
false);
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977QString QWidget::windowTitle()
const
5980 if (d->extra && d->extra->topextra) {
5981 if (!d->extra->topextra->caption.isEmpty())
5982 return d->extra->topextra->caption;
5983 if (!d->extra->topextra->filePath.isEmpty())
5984 return QFileInfo(d->extra->topextra->filePath).fileName() +
"[*]"_L1;
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6004 QString cap = title;
6008 const auto placeHolder =
"[*]"_L1;
6009 int index = cap.indexOf(placeHolder);
6012 while (index != -1) {
6013 index += placeHolder.size();
6015 while (cap.indexOf(placeHolder, index) == index) {
6017 index += placeHolder.size();
6021 int lastIndex = cap.lastIndexOf(placeHolder, index - 1);
6022 if (widget->isWindowModified()
6023 && widget->style()->styleHint(QStyle::SH_TitleBar_ModifyNotification,
nullptr, widget))
6024 cap.replace(lastIndex, 3, QWidget::tr(
"*"));
6026 cap.remove(lastIndex, 3);
6029 index = cap.indexOf(placeHolder, index);
6032 cap.replace(
"[*][*]"_L1, placeHolder);
6037void QWidgetPrivate::setWindowTitle_helper(
const QString &title)
6040 if (q->testAttribute(Qt::WA_WState_Created))
6041 setWindowTitle_sys(qt_setWindowTitle_helperHelper(title, q));
6044void QWidgetPrivate::setWindowTitle_sys(
const QString &caption)
6050 if (QWindow *window = q->windowHandle())
6052#if QT_CONFIG(accessibility)
6053 QString oldAccessibleName;
6054 const QAccessibleInterface *accessible = QAccessible::isActive()
6055 ? QAccessible::queryAccessibleInterface(q)
6058 oldAccessibleName = accessible->text(QAccessible::Name);
6061 window->setTitle(caption);
6063#if QT_CONFIG(accessibility)
6064 if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) {
6065 QAccessibleEvent event(q, QAccessible::NameChanged);
6066 QAccessible::updateAccessibility(&event);
6072void QWidgetPrivate::setWindowIconText_helper(
const QString &title)
6075 if (q->testAttribute(Qt::WA_WState_Created))
6076 setWindowIconText_sys(qt_setWindowTitle_helperHelper(title, q));
6079void QWidgetPrivate::setWindowIconText_sys(
const QString &iconText)
6085 if (QWindow *window = q->windowHandle()) {
6086 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(window->handle()))
6087 xcbWindow->setWindowIconText(iconText);
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6106void QWidget::setWindowIconText(
const QString &iconText)
6108 if (QWidget::windowIconText() == iconText)
6112 d->topData()->iconText = iconText;
6113 d->setWindowIconText_helper(iconText);
6115 QEvent e(QEvent::IconTextChange);
6116 QCoreApplication::sendEvent(
this, &e);
6118 emit windowIconTextChanged(iconText);
6122
6123
6124
6125
6126
6127
6128
6130void QWidget::setWindowTitle(
const QString &title)
6132 if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
6136 d->topData()->caption = title;
6137 d->setWindowTitle_helper(title);
6139 QEvent e(QEvent::WindowTitleChange);
6140 QCoreApplication::sendEvent(
this, &e);
6142 emit windowTitleChanged(title);
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160QIcon QWidget::windowIcon()
const
6162 const QWidget *w =
this;
6164 const QWidgetPrivate *d = w->d_func();
6165 if (d->extra && d->extra->topextra && d->extra->topextra->icon)
6166 return *d->extra->topextra->icon;
6167 w = w->parentWidget();
6169 return QApplication::windowIcon();
6172void QWidgetPrivate::setWindowIcon_helper()
6175 QEvent e(QEvent::WindowIconChange);
6181 if (!q->windowHandle())
6182 QCoreApplication::sendEvent(q, &e);
6183 for (
int i = 0; i < children.size(); ++i) {
6184 QWidget *w = qobject_cast<QWidget *>(children.at(i));
6185 if (w && !w->isWindow())
6186 QCoreApplication::sendEvent(w, &e);
6191
6192
6193
6194
6195
6196
6197
6199void QWidget::setWindowIcon(
const QIcon &icon)
6203 setAttribute(Qt::WA_SetWindowIcon, !icon.isNull());
6206 if (!d->extra->topextra->icon)
6207 d->extra->topextra->icon = std::make_unique<QIcon>(icon);
6209 *d->extra->topextra->icon = icon;
6211 d->setWindowIcon_sys();
6212 d->setWindowIcon_helper();
6214 emit windowIconChanged(icon);
6217void QWidgetPrivate::setWindowIcon_sys()
6220 if (QWindow *window = q->windowHandle())
6221 window->setIcon(q->windowIcon());
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6239QString QWidget::windowIconText()
const
6242 return (d->extra && d->extra->topextra) ? d->extra->topextra->iconText : QString();
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6269QString QWidget::windowFilePath()
const
6272 return (d->extra && d->extra->topextra) ? d->extra->topextra->filePath : QString();
6275void QWidget::setWindowFilePath(
const QString &filePath)
6277 if (filePath == windowFilePath())
6283 d->extra->topextra->filePath = filePath;
6284 d->setWindowFilePath_helper(filePath);
6287void QWidgetPrivate::setWindowFilePath_helper(
const QString &filePath)
6289 if (extra->topextra && extra->topextra->caption.isEmpty()) {
6291 setWindowTitle_helper(QFileInfo(filePath).fileName());
6295 setWindowTitle_helper(q->windowTitle());
6299 setWindowFilePath_sys(filePath);
6303void QWidgetPrivate::setWindowFilePath_sys(
const QString &filePath)
6309 if (QWindow *window = q->windowHandle())
6310 window->setFilePath(filePath);
6314
6315
6316
6317
6319QString QWidget::windowRole()
const
6322 return (d->extra && d->extra->topextra) ? d->extra->topextra->role : QString();
6326
6327
6328
6329void QWidget::setWindowRole(
const QString &role)
6334 d->topData()->role = role;
6335 if (windowHandle()) {
6336 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(windowHandle()->handle()))
6337 xcbWindow->setWindowRole(role);
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6393void QWidget::setFocusProxy(QWidget * w)
6396 if (!w && !d->extra)
6399 for (QWidget* fp = w; fp; fp = fp->focusProxy()) {
6400 if (Q_UNLIKELY(fp ==
this)) {
6401 qWarning(
"QWidget: %s (%s) already in focus proxy chain", metaObject()->className(), objectName().toLocal8Bit().constData());
6406 const bool moveFocusToProxy = (QApplicationPrivate::focus_widget ==
this);
6409 d->extra->focus_proxy = w;
6411 if (w && isAncestorOf(w)) {
6417 const QWidget *parentOfW = w->parentWidget();
6418 Q_ASSERT(parentOfW);
6419 QWidget *firstChild =
nullptr;
6420 const auto childList = children();
6421 for (QObject *child : childList) {
6422 if ((firstChild = qobject_cast<QWidget *>(child)))
6425 Q_ASSERT(firstChild);
6426 d->insertIntoFocusChainBefore(firstChild);
6427 }
else if (w && w->isAncestorOf(
this)) {
6432 QWidget *parentsNext = w->nextInFocusChain();
6433 if (parentsNext ==
this) {
6435 Q_ASSERT(previousInFocusChain() == w);
6437 d->QWidgetPrivate::insertIntoFocusChainAfter(w);
6441 if (moveFocusToProxy)
6442 setFocus(Qt::OtherFocusReason);
6447
6448
6449
6450
6452QWidget *QWidget::focusProxy()
const
6455 return d->extra ? d->extra->focus_proxy.data() :
nullptr;
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471bool QWidget::hasFocus()
const
6473 const QWidget* w =
this;
6474 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
6475 w = w->d_func()->extra->focus_proxy;
6476#if QT_CONFIG(graphicsview)
6477 if (QWidget *window = w->window()) {
6478 const auto &e = window->d_func()->extra;
6479 if (e && e->proxyWidget && e->proxyWidget->hasFocus() && window->focusWidget() == w)
6483 return (QApplication::focusWidget() == w);
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6521void QWidget::setFocus(Qt::FocusReason reason)
6526 QWidget *f = d_func()->deepestFocusProxy();
6530 if (QApplication::focusWidget() == f)
6533#if QT_CONFIG(graphicsview)
6534 QWidget *previousProxyFocus =
nullptr;
6535 if (
const auto &topData = window()->d_func()->extra) {
6536 if (topData->proxyWidget && topData->proxyWidget->hasFocus()) {
6537 previousProxyFocus = topData->proxyWidget->widget()->focusWidget();
6538 if (previousProxyFocus && previousProxyFocus->focusProxy())
6539 previousProxyFocus = previousProxyFocus->focusProxy();
6540 if (previousProxyFocus == f && !topData->proxyWidget->d_func()->proxyIsGivingFocus)
6546#if QT_CONFIG(graphicsview)
6548 if (
const auto &topData = window()->d_func()->extra) {
6549 if (topData->proxyWidget && !topData->proxyWidget->hasFocus()) {
6550 f->d_func()->updateFocusChild();
6551 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 1;
6552 topData->proxyWidget->setFocus(reason);
6553 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 0;
6558 if (f->isActiveWindow()) {
6559 QWidget *prev = QApplicationPrivate::focus_widget;
6561 if (reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
6562 && prev->testAttribute(Qt::WA_InputMethodEnabled)) {
6563 QGuiApplication::inputMethod()->commit();
6566 if (reason != Qt::NoFocusReason) {
6567 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason);
6568 QCoreApplication::sendEvent(prev, &focusAboutToChange);
6572 f->d_func()->updateFocusChild();
6574 QApplicationPrivate::setFocusWidget(f, reason);
6575#if QT_CONFIG(accessibility)
6577 if (!(f->inherits(
"QMenuBar") || f->inherits(
"QMenu") || f->inherits(
"QMenuItem")))
6579 QAccessibleEvent event(f, QAccessible::Focus);
6580 QAccessible::updateAccessibility(&event);
6583#if QT_CONFIG(graphicsview)
6584 if (
const auto &topData = window()->d_func()->extra) {
6585 if (topData->proxyWidget) {
6586 if (previousProxyFocus && previousProxyFocus != f) {
6588 QFocusEvent event(QEvent::FocusOut, reason);
6589 QPointer<QWidget> that = previousProxyFocus;
6590 QCoreApplication::sendEvent(previousProxyFocus, &event);
6592 QCoreApplication::sendEvent(that->style(), &event);
6595#if QT_CONFIG(graphicsview)
6597 if (
const auto &topData = window()->d_func()->extra)
6598 if (topData->proxyWidget && topData->proxyWidget->hasFocus())
6599 topData->proxyWidget->d_func()->updateProxyInputMethodAcceptanceFromWidget();
6602 QFocusEvent event(QEvent::FocusIn, reason);
6603 QPointer<QWidget> that = f;
6604 QCoreApplication::sendEvent(f, &event);
6606 QCoreApplication::sendEvent(that->style(), &event);
6612 f->d_func()->updateFocusChild();
6618
6619
6620
6621
6622
6623QWidget *QWidgetPrivate::deepestFocusProxy()
const
6627 QWidget *focusProxy = q->focusProxy();
6631 while (QWidget *nextFocusProxy = focusProxy->focusProxy())
6632 focusProxy = nextFocusProxy;
6639 const auto platformWindow = w->handle();
6640 return platformWindow && platformWindow->isEmbedded();
6643void QWidgetPrivate::setFocus_sys()
6648 if (extra && extra->hasWindowContainer)
6652 if (QWindow *nativeWindow = q->testAttribute(Qt::WA_WState_Created) ? q->window()->windowHandle() :
nullptr) {
6653 if (nativeWindow->type() != Qt::Popup && nativeWindow != QGuiApplication::focusWindow()
6654 && (QGuiApplication::applicationState() == Qt::ApplicationActive
6655 || QCoreApplication::testAttribute(Qt::AA_PluginApplication)
6656 || isEmbedded(nativeWindow))) {
6657 nativeWindow->requestActivate();
6663void QWidgetPrivate::updateFocusChild()
6668 if (q->isHidden()) {
6669 while (w && w->isHidden()) {
6670 w->d_func()->focus_child = q;
6671 w = w->isWindow() ?
nullptr : w->parentWidget();
6675 w->d_func()->focus_child = q;
6676 w = w->isWindow() ?
nullptr : w->parentWidget();
6680 if (QTLWExtra *extra = q->window()->d_func()->maybeTopData()) {
6682 emit extra->window->focusObjectChanged(q);
6687
6688
6689
6690
6691
6692
6693
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6708void QWidget::clearFocus()
6711 if (testAttribute(Qt::WA_InputMethodEnabled))
6712 QGuiApplication::inputMethod()->commit();
6714 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange);
6715 QCoreApplication::sendEvent(
this, &focusAboutToChange);
6718 QTLWExtra *extra = window()->d_func()->maybeTopData();
6719 QObject *originalFocusObject =
nullptr;
6720 if (extra && extra->window) {
6721 originalFocusObject = extra->window->focusObject();
6724 if (!originalFocusObject)
6725 originalFocusObject = focusWidget();
6731 if (w->d_func()->focus_child ==
this)
6732 w->d_func()->focus_child =
nullptr;
6733 w = w->parentWidget();
6740 if (originalFocusObject && originalFocusObject != extra->window->focusObject())
6741 emit extra->window->focusObjectChanged(extra->window->focusObject());
6743#if QT_CONFIG(graphicsview)
6744 const auto &topData = d_func()->extra;
6745 if (topData && topData->proxyWidget)
6746 topData->proxyWidget->clearFocus();
6751 QApplicationPrivate::setFocusWidget(
nullptr, Qt::OtherFocusReason);
6752#if QT_CONFIG(accessibility)
6753 QAccessibleEvent event(
this, QAccessible::Focus);
6754 QAccessible::updateAccessibility(&event);
6761
6762
6763
6764
6765
6766
6767
6768
6771
6772
6773
6774
6775
6776
6777
6778
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6802bool QWidget::focusNextPrevChild(
bool next)
6804 QWidget* p = parentWidget();
6805 bool isSubWindow = (windowType() == Qt::SubWindow);
6806 if (!isWindow() && !isSubWindow && p)
6807 return p->focusNextPrevChild(next);
6808#if QT_CONFIG(graphicsview)
6810 if (d->extra && d->extra->proxyWidget)
6811 return d->extra->proxyWidget->focusNextPrevChild(next);
6814 bool wrappingOccurred =
false;
6815 QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(
this, next,
6817 if (!w)
return false;
6819 Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
6822
6823
6824
6825
6826 if (wrappingOccurred) {
6827 QWindow *window = windowHandle();
6828 if (window !=
nullptr) {
6829 QWindowPrivate *winp = qt_window_private(window);
6831 if (winp->platformWindow !=
nullptr) {
6832 QFocusEvent event(QEvent::FocusIn, reason);
6834 winp->platformWindow->windowEvent(&event);
6835 if (event.isAccepted())
return true;
6840 w->setFocus(reason);
6845
6846
6847
6848
6849
6850
6851
6853QWidget *QWidget::focusWidget()
const
6855 return const_cast<QWidget *>(d_func()->focus_child);
6858QObject *QWidgetPrivate::focusObject()
6861 QWidget *proxy = deepestFocusProxy();
6862 return proxy ? proxy : q;
6866
6867
6868
6869
6870QWidget *QWidget::nextInFocusChain()
const
6873 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Next);
6877
6878
6879
6880
6881
6882
6883
6884QWidget *QWidget::previousInFocusChain()
const
6887 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Previous);
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905bool QWidget::isActiveWindow()
const
6907 QWidget *tlw = window();
6908 if (tlw == QApplication::activeWindow() || (isVisible() && (tlw->windowType() == Qt::Popup)))
6911#if QT_CONFIG(graphicsview)
6912 if (
const auto &tlwExtra = tlw->d_func()->extra) {
6913 if (isVisible() && tlwExtra->proxyWidget)
6914 return tlwExtra->proxyWidget->isActiveWindow();
6918 if (style()->styleHint(QStyle::SH_Widget_ShareActivation,
nullptr,
this)) {
6919 if (tlw->windowType() == Qt::Tool &&
6921 (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow()))
6923 QWidget *w = QApplication::activeWindow();
6924 while(w && tlw->windowType() == Qt::Tool &&
6925 !w->isModal() && w->parentWidget()) {
6926 w = w->parentWidget()->window();
6933 if (QWindow *ww = QGuiApplication::focusWindow()) {
6935 QWidgetWindow *qww = qobject_cast<QWidgetWindow *>(ww);
6936 QWindowContainer *qwc = qww ? qobject_cast<QWindowContainer *>(qww->widget()) : 0;
6937 if (qwc && qwc->topLevelWidget() == tlw)
6948 if (
const QWindow *w = tlw->windowHandle()) {
6950 return w->handle()->isActive();
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006void QWidget::setTabOrder(QWidget* first, QWidget *second)
7008 if (!first || !second || first == second
7009 || first->focusPolicy() == Qt::NoFocus
7010 || second->focusPolicy() == Qt::NoFocus)
7013 if (Q_UNLIKELY(first->window() != second->window())) {
7014 qWarning(
"QWidget::setTabOrder: 'first' and 'second' must be in the same window");
7018 const auto determineLastFocusChild = [](QWidget *target, QWidget *noFurtherThan)
7024 QWidget *lastFocusChild = target;
7026 QWidget *focusProxy = target->d_func()->deepestFocusProxy();
7033 for (
auto *object : target->children()) {
7034 QWidget *w = qobject_cast<QWidget*>(object);
7035 if (w && w->focusProxy() == target) {
7040 }
else if (target->isAncestorOf(focusProxy)) {
7041 lastFocusChild = focusProxy;
7042 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
7043 focusNext != focusProxy && target->isAncestorOf(focusNext) && focusNext->window() == focusProxy->window();
7044 focusNext = focusNext->nextInFocusChain()) {
7045 if (focusNext == noFurtherThan)
7047 if (focusNext->focusPolicy() != Qt::NoFocus)
7048 lastFocusChild = focusNext;
7051 return lastFocusChild;
7054 QWidget *lastFocusChildOfFirst = determineLastFocusChild(first, second);
7055 if (lastFocusChildOfFirst == second)
7056 lastFocusChildOfFirst = first;
7057 QWidget *lastFocusChildOfSecond = determineLastFocusChild(second, first);
7058 if (lastFocusChildOfSecond == first)
7059 lastFocusChildOfSecond = second;
7062 QWidget *oldPrev = second->previousInFocusChain();
7063 QWidget *prevWithFocus = oldPrev;
7064 while (prevWithFocus->focusPolicy() == Qt::NoFocus)
7065 prevWithFocus = prevWithFocus->previousInFocusChain();
7066 if (prevWithFocus == first)
7068 const QWidgetList chain = QWidgetPrivate::takeFromFocusChain(second, lastFocusChildOfSecond);
7069 QWidgetPrivate::insertIntoFocusChain(chain, QWidgetPrivate::FocusDirection::Next, lastFocusChildOfFirst);
7072void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets)
7074 QWidget *prev =
nullptr;
7075 for (
const auto &widget : widgets) {
7079 QWidget::setTabOrder(prev, widget);
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7098void QWidgetPrivate::reparentFocusWidgets(QWidget * oldtlw)
7101 if (oldtlw == q->window())
7105 focus_child->clearFocus();
7107 reparentFocusChildren(QWidgetPrivate::FocusDirection::Next);
7111
7112
7113
7114
7115
7116
7117QSize QWidget::frameSize()
const
7120 if (isWindow() && !(windowType() == Qt::Popup)) {
7121 QRect fs = d->frameStrut();
7122 return QSize(data->crect.width() + fs.left() + fs.right(),
7123 data->crect.height() + fs.top() + fs.bottom());
7125 return data->crect.size();
7129
7130
7131
7132
7133
7135void QWidget::move(
const QPoint &p)
7138 setAttribute(Qt::WA_Moved);
7139 if (testAttribute(Qt::WA_WState_Created)) {
7141 d->topData()->posIncludesFrame =
false;
7142 d->setGeometry_sys(p.x() + geometry().x() - QWidget::x(),
7143 p.y() + geometry().y() - QWidget::y(),
7144 width(), height(),
true);
7145 d->setDirtyOpaqueRegion();
7149 d->topData()->posIncludesFrame =
true;
7150 data->crect.moveTopLeft(p);
7151 setAttribute(Qt::WA_PendingMoveEvent);
7154 if (d->extra && d->extra->hasWindowContainer)
7155 QWindowContainer::parentWasMoved(
this);
7167void QWidgetPrivate::fixPosIncludesFrame()
7170 if (QTLWExtra *te = maybeTopData()) {
7171 if (te->posIncludesFrame) {
7174 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
7175 te->posIncludesFrame = 0;
7177 if (q->windowHandle() && q->windowHandle()->handle()) {
7179 if (!q->data->fstrut_dirty) {
7180 data.crect.translate(te->frameStrut.x(), te->frameStrut.y());
7181 te->posIncludesFrame = 0;
7190
7191
7192
7193
7195void QWidget::resize(
const QSize &s)
7198 setAttribute(Qt::WA_Resized);
7199 if (testAttribute(Qt::WA_WState_Created)) {
7200 d->fixPosIncludesFrame();
7201 d->setGeometry_sys(geometry().x(), geometry().y(), s.width(), s.height(),
false);
7202 d->setDirtyOpaqueRegion();
7204 const auto oldRect = data->crect;
7205 data->crect.setSize(s.boundedTo(maximumSize()).expandedTo(minimumSize()));
7206 if (oldRect != data->crect)
7207 setAttribute(Qt::WA_PendingResizeEvent);
7211void QWidget::setGeometry(
const QRect &r)
7214 setAttribute(Qt::WA_Resized);
7215 setAttribute(Qt::WA_Moved);
7217 d->topData()->posIncludesFrame = 0;
7218 if (testAttribute(Qt::WA_WState_Created)) {
7219 d->setGeometry_sys(r.x(), r.y(), r.width(), r.height(),
true);
7220 d->setDirtyOpaqueRegion();
7222 const auto oldRect = data->crect;
7223 data->crect.setTopLeft(r.topLeft());
7224 data->crect.setSize(r.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
7225 if (oldRect != data->crect) {
7226 setAttribute(Qt::WA_PendingMoveEvent);
7227 setAttribute(Qt::WA_PendingResizeEvent);
7231 if (d->extra && d->extra->hasWindowContainer)
7232 QWindowContainer::parentWasMoved(
this);
7235void QWidgetPrivate::setGeometry_sys(
int x,
int y,
int w,
int h,
bool isMove)
7239 w = qMin(w,extra->maxw);
7240 h = qMin(h,extra->maxh);
7241 w = qMax(w,extra->minw);
7242 h = qMax(h,extra->minh);
7245 if (q->isWindow() && q->windowHandle()) {
7246 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
7247 if (!integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) {
7250 w = q->windowHandle()->width();
7251 h = q->windowHandle()->height();
7255 QPoint oldp = q->geometry().topLeft();
7256 QSize olds = q->size();
7257 QRect r(x, y, w, h);
7259 bool isResize = olds != r.size();
7261 isMove = oldp != r.topLeft();
7266 if (r.size() == olds && oldp == r.topLeft())
7269 if (!data.in_set_window_state) {
7270 q->data->window_state &= ~Qt::WindowMaximized;
7271 q->data->window_state &= ~Qt::WindowFullScreen;
7273 topData()->normalGeometry = QRect(0, 0, -1, -1);
7276 QPoint oldPos = q->pos();
7279 bool needsShow =
false;
7281 if (q->isWindow() || q->windowHandle()) {
7282 if (!(data.window_state & Qt::WindowFullScreen) && (w == 0 || h == 0)) {
7283 q->setAttribute(Qt::WA_OutsideWSRange,
true);
7286 data.crect = QRect(x, y, w, h);
7287 }
else if (q->testAttribute(Qt::WA_OutsideWSRange)) {
7288 q->setAttribute(Qt::WA_OutsideWSRange,
false);
7293 if (q->isVisible()) {
7294 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && !q->testAttribute(Qt::WA_OutsideWSRange)) {
7295 if (QWindow *win = q->windowHandle()) {
7296 if (q->isWindow()) {
7297 if (isResize && !isMove)
7299 else if (isMove && !isResize)
7300 win->setPosition(x, y);
7302 win->setGeometry(q->geometry());
7304 QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());
7305 win->setGeometry(QRect(posInNativeParent,r.size()));
7312 if (!q->isWindow()) {
7313 if (renderToTexture) {
7314 QRegion updateRegion(q->geometry());
7315 updateRegion += QRect(oldPos, olds);
7316 q->parentWidget()->d_func()->invalidateBackingStore(updateRegion);
7317 }
else if (isMove && !isResize) {
7318 moveRect(QRect(oldPos, olds), x - oldPos.x(), y - oldPos.y());
7320 invalidateBackingStore_resizeHelper(oldPos, olds);
7326 QMoveEvent e(q->pos(), oldPos);
7327 QCoreApplication::sendEvent(q, &e);
7330 QResizeEvent e(r.size(), olds);
7331 QCoreApplication::sendEvent(q, &e);
7332 if (q->windowHandle())
7336 if (isMove && q->pos() != oldPos)
7337 q->setAttribute(Qt::WA_PendingMoveEvent,
true);
7339 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361QByteArray QWidget::saveGeometry()
const
7364 QDataStream stream(&array, QIODevice::WriteOnly);
7365 stream.setVersion(QDataStream::Qt_4_0);
7366 const quint32 magicNumber = 0x1D9D0CB;
7371 quint16 majorVersion = 3;
7372 quint16 minorVersion = 0;
7373 const int screenNumber = QGuiApplication::screens().indexOf(screen());
7374 stream << magicNumber
7379 << qint32(screenNumber)
7380 << quint8(windowState() & Qt::WindowMaximized)
7381 << quint8(windowState() & Qt::WindowFullScreen)
7382 << qint32(screen()->geometry().width())
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410void QWidgetPrivate::checkRestoredGeometry(
const QRect &availableGeometry, QRect *restoredGeometry,
7414 const int height = restoredGeometry->height() + frameHeight;
7418 if (availableGeometry.height() <= height)
7419 restoredGeometry->setHeight(availableGeometry.height() - 2 - frameHeight);
7420 if (availableGeometry.width() <= restoredGeometry->width())
7421 restoredGeometry->setWidth(availableGeometry.width() - 2);
7425 const QRect restored = restoredGeometry->adjusted(0, -frameHeight, 0, 0);
7428 if (availableGeometry.contains(restored))
7434 if (restored.top() <= availableGeometry.top()) {
7435 restoredGeometry->moveTop(availableGeometry.top() + 1 + frameHeight);
7436 }
else if (restored.bottom() >= availableGeometry.bottom()) {
7438 restoredGeometry->moveBottom(availableGeometry.bottom() - 1);
7442 if (restored.left() <= availableGeometry.left()) {
7443 restoredGeometry->moveLeft(availableGeometry.left() + 1);
7444 }
else if (restored.right() >= availableGeometry.right()) {
7446 restoredGeometry->moveRight(availableGeometry.right() - 1);
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473bool QWidget::restoreGeometry(
const QByteArray &geometry)
7475 if (geometry.size() < 4)
7477 QDataStream stream(geometry);
7478 stream.setVersion(QDataStream::Qt_4_0);
7480 const quint32 magicNumber = 0x1D9D0CB;
7481 quint32 storedMagicNumber;
7482 stream >> storedMagicNumber;
7483 if (storedMagicNumber != magicNumber)
7486 const quint16 currentMajorVersion = 3;
7487 quint16 majorVersion = 0;
7488 quint16 minorVersion = 0;
7490 stream >> majorVersion >> minorVersion;
7492 if (majorVersion > currentMajorVersion)
7496 QRect restoredFrameGeometry;
7497 QRect restoredGeometry;
7498 QRect restoredNormalGeometry;
7499 qint32 restoredScreenNumber;
7502 qint32 restoredScreenWidth = 0;
7504 stream >> restoredFrameGeometry
7505 >> restoredNormalGeometry
7506 >> restoredScreenNumber
7510 if (majorVersion > 1)
7511 stream >> restoredScreenWidth;
7512 if (majorVersion > 2)
7513 stream >> restoredGeometry;
7517 if (restoredScreenNumber >= qMax(QGuiApplication::screens().size(), 1))
7518 restoredScreenNumber = 0;
7519 const QScreen *restoredScreen = QGuiApplication::screens().value(restoredScreenNumber,
nullptr);
7520 const qreal screenWidthF = restoredScreen ? qreal(restoredScreen->geometry().width()) : 0;
7523 if (restoredScreenWidth) {
7524 const qreal factor = qreal(restoredScreenWidth) / screenWidthF;
7525 if (factor < 0.8 || factor > 1.25)
7530 if (!maximized && !fullScreen && qreal(restoredFrameGeometry.width()) / screenWidthF > 1.5)
7534 const int frameHeight = QApplication::style()
7535 ? QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight,
nullptr,
this)
7538 if (!restoredNormalGeometry.isValid())
7539 restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
7540 if (!restoredNormalGeometry.isValid()) {
7542 restoredNormalGeometry.setSize(restoredNormalGeometry
7544 .expandedTo(d_func()->adjustedSize()));
7547 const QRect availableGeometry = restoredScreen ? restoredScreen->availableGeometry()
7555 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredGeometry, frameHeight);
7556 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredNormalGeometry, frameHeight);
7558 if (maximized || fullScreen) {
7561 Qt::WindowStates ws = windowState();
7563 setGeometry(restoredNormalGeometry);
7565 if (ws & Qt::WindowFullScreen) {
7567 move(availableGeometry.topLeft());
7568 }
else if (ws & Qt::WindowMaximized) {
7572 if (restoredScreen != screen()) {
7573 setWindowState(Qt::WindowNoState);
7574 setGeometry(restoredNormalGeometry);
7577 setGeometry(restoredNormalGeometry);
7581 ws |= Qt::WindowMaximized;
7583 ws |= Qt::WindowFullScreen;
7585 d_func()->topData()->normalGeometry = restoredNormalGeometry;
7587 setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
7590 if (majorVersion > 2)
7591 setGeometry(restoredGeometry);
7593 setGeometry(restoredNormalGeometry);
7599
7600
7601
7602
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614void QWidget::setContentsMargins(
int left,
int top,
int right,
int bottom)
7617 if (left == d->leftmargin && top == d->topmargin
7618 && right == d->rightmargin && bottom == d->bottommargin)
7620 d->leftmargin = left;
7622 d->rightmargin = right;
7623 d->bottommargin = bottom;
7625 d->updateContentsRect();
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644void QWidget::setContentsMargins(
const QMargins &margins)
7646 setContentsMargins(margins.left(), margins.top(),
7647 margins.right(), margins.bottom());
7650void QWidgetPrivate::updateContentsRect()
7657 q->updateGeometry();
7659 if (q->isVisible()) {
7661 QResizeEvent e(q->data->crect.size(), q->data->crect.size());
7662 QCoreApplication::sendEvent(q, &e);
7664 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7667 QEvent e(QEvent::ContentsRectChange);
7668 QCoreApplication::sendEvent(q, &e);
7672
7673
7674
7675
7676
7677
7678QMargins QWidget::contentsMargins()
const
7681 QMargins userMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
7682 return testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea) ?
7683 userMargins | d->safeAreaMargins() : userMargins;
7687
7688
7689
7690
7691QRect QWidget::contentsRect()
const
7693 return rect() - contentsMargins();
7696QMargins QWidgetPrivate::safeAreaMargins()
const
7699 QWidget *nativeWidget = q->window();
7700 if (!nativeWidget->windowHandle())
7703 QPlatformWindow *platformWindow = nativeWidget->windowHandle()->handle();
7704 if (!platformWindow)
7707 QMargins safeAreaMargins = platformWindow->safeAreaMargins();
7709 if (!q->isWindow()) {
7717 if (safeAreaMargins.isNull())
7724 const QWidget *assumedSafeWidget =
nullptr;
7725 for (
const QWidget *w = q; w != nativeWidget; w = w->parentWidget()) {
7726 QWidget *parentWidget = w->parentWidget();
7727 if (!parentWidget->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
7730 if (parentWidget->testAttribute(Qt::WA_LayoutOnEntireRect))
7733 QLayout *layout = parentWidget->layout();
7737 if (layout->geometry().isNull())
7740 if (layout->indexOf(w) < 0)
7743 assumedSafeWidget = w;
7747#if !defined(QT_DEBUG)
7748 if (assumedSafeWidget) {
7759 QPoint topLeftMargins = q->mapFrom(nativeWidget, QPoint(safeAreaMargins.left(), safeAreaMargins.top()));
7760 QRect widgetRect = q->isVisible() ? q->visibleRegion().boundingRect() : q->rect();
7761 QPoint bottomRightMargins = widgetRect.bottomRight() - q->mapFrom(nativeWidget,
7762 nativeWidget->rect().bottomRight() - QPoint(safeAreaMargins.right(), safeAreaMargins.bottom()));
7765 safeAreaMargins = QMargins(qMax(0, topLeftMargins.x()), qMax(0, topLeftMargins.y()),
7766 qMax(0, bottomRightMargins.x()), qMax(0, bottomRightMargins.y()));
7768 if (!safeAreaMargins.isNull() && assumedSafeWidget) {
7769 QLayout *layout = assumedSafeWidget->parentWidget()->layout();
7770 qWarning() << layout <<
"is laying out" << assumedSafeWidget
7771 <<
"outside of the contents rect of" << layout->parentWidget();
7776 return safeAreaMargins;
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7809Qt::ContextMenuPolicy QWidget::contextMenuPolicy()
const
7811 return (Qt::ContextMenuPolicy)data->context_menu_policy;
7814void QWidget::setContextMenuPolicy(Qt::ContextMenuPolicy policy)
7816 data->context_menu_policy = (uint) policy;
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7841Qt::FocusPolicy QWidget::focusPolicy()
const
7843 return (Qt::FocusPolicy)data->focus_policy;
7846void QWidget::setFocusPolicy(Qt::FocusPolicy policy)
7848 data->focus_policy = (uint) policy;
7850 if (d->extra && d->extra->focus_proxy)
7851 d->extra->focus_proxy->setFocusPolicy(policy);
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882void QWidget::setUpdatesEnabled(
bool enable)
7885 setAttribute(Qt::WA_ForceUpdatesDisabled, !enable);
7886 d->setUpdatesEnabled_helper(enable);
7890
7891
7892
7893
7894
7895
7896
7897
7898
7906 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
7907 Qt::WindowState defaultState = platformIntegration->defaultWindowState(data->window_flags);
7908 if (defaultState == Qt::WindowFullScreen)
7910 else if (defaultState == Qt::WindowMaximized)
7918
7919
7920
7921
7922void QWidgetPrivate::show_recursive()
7927 if (!q->testAttribute(Qt::WA_WState_Created))
7928 createRecursively();
7929 q->ensurePolished();
7931 if (!q->isWindow() && q->parentWidget()->d_func()->layout && !q->parentWidget()->data->in_show)
7932 q->parentWidget()->d_func()->layout->activate();
7940void QWidgetPrivate::sendPendingMoveAndResizeEvents(
bool recursive,
bool disableUpdates)
7944 disableUpdates = disableUpdates && q->updatesEnabled();
7946 q->setAttribute(Qt::WA_UpdatesDisabled);
7948 if (q->testAttribute(Qt::WA_PendingMoveEvent)) {
7949 QMoveEvent e(data.crect.topLeft(), data.crect.topLeft());
7950 QCoreApplication::sendEvent(q, &e);
7951 q->setAttribute(Qt::WA_PendingMoveEvent,
false);
7954 if (q->testAttribute(Qt::WA_PendingResizeEvent)) {
7955 QResizeEvent e(data.crect.size(), QSize());
7956 QCoreApplication::sendEvent(q, &e);
7957 q->setAttribute(Qt::WA_PendingResizeEvent,
false);
7961 q->setAttribute(Qt::WA_UpdatesDisabled,
false);
7966 for (
int i = 0; i < children.size(); ++i) {
7967 if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
7968 child->d_func()->sendPendingMoveAndResizeEvents(recursive, disableUpdates);
7972void QWidgetPrivate::activateChildLayoutsRecursively()
7974 sendPendingMoveAndResizeEvents(
false,
true);
7976 for (
int i = 0; i < children.size(); ++i) {
7977 QWidget *child = qobject_cast<QWidget *>(children.at(i));
7978 if (!child || child->isHidden() || child->isWindow())
7981 child->ensurePolished();
7984 QWidgetPrivate *childPrivate = child->d_func();
7985 if (childPrivate->layout)
7986 childPrivate->layout->activate();
7989 const bool wasVisible = child->isVisible();
7991 child->setAttribute(Qt::WA_WState_Visible);
7994 childPrivate->activateChildLayoutsRecursively();
7998 child->setAttribute(Qt::WA_WState_Visible,
false);
8002void QWidgetPrivate::show_helper()
8005 data.in_show =
true;
8007 sendPendingMoveAndResizeEvents();
8010 q->setAttribute(Qt::WA_WState_Visible);
8013 showChildren(
false);
8017 const bool isWindow = q->isWindow();
8018#if QT_CONFIG(graphicsview)
8019 bool isEmbedded = isWindow && q->graphicsProxyWidget() !=
nullptr;
8021 bool isEmbedded =
false;
8027 if (isWindow && !isEmbedded) {
8028 if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
8030 if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
8031 q->setAttribute(Qt::WA_KeyboardFocusChange);
8033 while (QApplication::activePopupWidget()) {
8034 if (!QApplication::activePopupWidget()->close())
8042#if QT_CONFIG(graphicsview)
8044 if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
8045 QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
8046 if (ancestorProxy) {
8048 ancestorProxy->d_func()->embedSubWindow(q);
8053 Q_UNUSED(isEmbedded);
8057 QShowEvent showEvent;
8058 QCoreApplication::sendEvent(q, &showEvent);
8062 if (!isEmbedded && q->windowType() == Qt::Popup)
8063 qApp->d_func()->openPopup(q);
8065#if QT_CONFIG(accessibility)
8066 if (q->windowType() != Qt::ToolTip) {
8067 QAccessibleEvent event(q, QAccessible::ObjectShow);
8068 QAccessible::updateAccessibility(&event);
8072 if (QApplicationPrivate::hidden_focus_widget == q) {
8073 QApplicationPrivate::hidden_focus_widget =
nullptr;
8074 q->setFocus(Qt::OtherFocusReason);
8080 if (!
qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
8081 QCoreApplication::processEvents();
8083 data.in_show =
false;
8086void QWidgetPrivate::show_sys()
8090 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8092 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8093 invalidateBackingStore(q->rect());
8094 q->setAttribute(Qt::WA_Mapped);
8096 if (window && q->isWindow()
8097#if QT_CONFIG(graphicsview)
8098 && (!extra || !extra->proxyWidget)
8100 && q->windowModality() != Qt::NonModal) {
8101 QGuiApplicationPrivate::showModalWindow(window);
8106 if (renderToTexture && !q->isWindow())
8107 QCoreApplication::postEvent(q->parentWidget(),
new QUpdateLaterEvent(q->geometry()));
8109 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(q->rect()));
8111 if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow))
8112 || q->testAttribute(Qt::WA_OutsideWSRange)) {
8118 fixPosIncludesFrame();
8119 QRect geomRect = q->geometry();
8120 if (!q->isWindow()) {
8121 QPoint topLeftOfWindow = q->mapTo(q->nativeParentWidget(),QPoint());
8122 geomRect.moveTopLeft(topLeftOfWindow);
8124 const QRect windowRect = window->geometry();
8125 if (windowRect != geomRect) {
8126 if (q->testAttribute(Qt::WA_Moved)
8127 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
8128 window->setGeometry(geomRect);
8130 window->resize(geomRect.size());
8134 qt_qpa_set_cursor(q,
false);
8136 invalidateBackingStore(q->rect());
8137 window->setNativeWindowVisibility(
true);
8139 if (window->isTopLevel()) {
8140 const QPoint crectTopLeft = q->data->crect.topLeft();
8141 const QPoint windowTopLeft = window->geometry().topLeft();
8142 if (crectTopLeft == QPoint(0, 0) && windowTopLeft != crectTopLeft)
8143 q->data->crect.moveTopLeft(windowTopLeft);
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8165
8166void QWidgetPrivate::hide_helper()
8170 bool isEmbedded =
false;
8171#if QT_CONFIG(graphicsview)
8172 isEmbedded = q->isWindow() && !bypassGraphicsProxyWidget(q) && nearestGraphicsProxyWidget(q->parentWidget()) !=
nullptr;
8174 Q_UNUSED(isEmbedded);
8177 if (!isEmbedded && (q->windowType() == Qt::Popup))
8178 qApp->d_func()->closePopup(q);
8180 q->setAttribute(Qt::WA_Mapped,
false);
8183 bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
8186 q->setAttribute(Qt::WA_WState_Visible,
false);
8190 QHideEvent hideEvent;
8191 QCoreApplication::sendEvent(q, &hideEvent);
8192 hideChildren(
false);
8197 qApp->d_func()->sendSyntheticEnterLeave(q);
8198 QWidget *fw = QApplication::focusWidget();
8199 while (fw && !fw->isWindow()) {
8201 q->focusNextPrevChild(
true);
8204 fw = fw->parentWidget();
8208 if (QWidgetRepaintManager *repaintManager = maybeRepaintManager())
8209 repaintManager->removeDirtyWidget(q);
8211#if QT_CONFIG(accessibility)
8213 QAccessibleEvent event(q, QAccessible::ObjectHide);
8214 QAccessible::updateAccessibility(&event);
8219void QWidgetPrivate::hide_sys()
8223 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8225 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8226 q->setAttribute(Qt::WA_Mapped,
false);
8228 if (window && q->isWindow()
8229#if QT_CONFIG(graphicsview)
8230 && (!extra || !extra->proxyWidget)
8232 && q->windowModality() != Qt::NonModal) {
8233 QGuiApplicationPrivate::hideModalWindow(window);
8238 deactivateWidgetCleanup();
8240 if (!q->isWindow()) {
8241 QWidget *p = q->parentWidget();
8242 if (p &&p->isVisible()) {
8243 if (renderToTexture)
8244 p->d_func()->invalidateBackingStore(q->geometry());
8246 invalidateBackingStore(q->rect());
8249 invalidateBackingStore(q->rect());
8253 window->setNativeWindowVisibility(
false);
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8279void QWidget::setVisible(
bool visible)
8282 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" <<
this
8283 <<
"with attributes" << WidgetAttributes{
this}
8284 <<
"to" << visible <<
"via QWidget";
8286 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) == !visible)
8289 if (d->dontSetExplicitShowHide) {
8290 d->dontSetExplicitShowHide =
false;
8293 setAttribute(Qt::WA_WState_ExplicitShowHide);
8296 d->setVisible(visible);
8302void QWidgetPrivate::setVisible(
bool visible)
8305 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" << q
8306 <<
"with attributes" << WidgetAttributes{q}
8307 <<
"to" << visible <<
"via QWidgetPrivate";
8311 if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
8312 && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
8313 q->parentWidget()->window()->d_func()->createRecursively();
8316 QWidget *pw = q->parentWidget();
8317 if (!q->testAttribute(Qt::WA_WState_Created)
8318 && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
8322 bool wasResized = q->testAttribute(Qt::WA_Resized);
8323 Qt::WindowStates initialWindowState = q->windowState();
8326 q->ensurePolished();
8329 bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
8331 q->setAttribute(Qt::WA_WState_Hidden,
false);
8333 if (needUpdateGeometry)
8334 updateGeometry_helper(
true);
8340 if (!q->isWindow()) {
8341 QWidget *parent = q->parentWidget();
8342 while (parent && parent->isVisible() && parent->d_func()->layout && !parent->data->in_show) {
8343 parent->d_func()->layout->activate();
8344 if (parent->isWindow())
8346 parent = parent->parentWidget();
8349 parent->d_func()->setDirtyOpaqueRegion();
8354 && (q->isWindow() || !q->parentWidget()->d_func()->layout)) {
8355 if (q->isWindow()) {
8357 if (q->windowState() != initialWindowState)
8358 q->setWindowState(initialWindowState);
8362 q->setAttribute(Qt::WA_Resized,
false);
8365 q->setAttribute(Qt::WA_KeyboardFocusChange,
false);
8367 if (q->isWindow() || q->parentWidget()->isVisible()) {
8370 qApp->d_func()->sendSyntheticEnterLeave(q);
8373 QEvent showToParentEvent(QEvent::ShowToParent);
8374 QCoreApplication::sendEvent(q, &showToParentEvent);
8376 if (QApplicationPrivate::hidden_focus_widget == q)
8377 QApplicationPrivate::hidden_focus_widget =
nullptr;
8383 if (!q->isWindow() && q->parentWidget())
8384 q->parentWidget()->d_func()->setDirtyOpaqueRegion();
8386 if (!q->testAttribute(Qt::WA_WState_Hidden)) {
8387 q->setAttribute(Qt::WA_WState_Hidden);
8392 if (!q->isWindow() && q->parentWidget()) {
8393 if (q->parentWidget()->d_func()->layout)
8394 q->parentWidget()->d_func()->layout->invalidate();
8395 else if (q->parentWidget()->isVisible())
8396 QCoreApplication::postEvent(q->parentWidget(),
new QEvent(QEvent::LayoutRequest));
8399 QEvent hideToParentEvent(QEvent::HideToParent);
8400 QCoreApplication::sendEvent(q, &hideToParentEvent);
8405
8406
8407void QWidget::setHidden(
bool hidden)
8409 setVisible(!hidden);
8412bool QWidgetPrivate::isExplicitlyHidden()
const
8415 return q->isHidden() && q->testAttribute(Qt::WA_WState_ExplicitShowHide);
8418void QWidgetPrivate::_q_showIfNotHidden()
8421 if (!isExplicitlyHidden())
8422 q->setVisible(
true);
8425void QWidgetPrivate::showChildren(
bool spontaneous)
8428 qCDebug(lcWidgetShowHide) <<
"Showing children of" << q
8429 <<
"spontaneously" << spontaneous;
8431 QList<QObject*> childList = children;
8432 for (
int i = 0; i < childList.size(); ++i) {
8433 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8436 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8437 <<
"with attributes" << WidgetAttributes{widget};
8438 if (widget->windowHandle() && !widget->testAttribute(Qt::WA_WState_ExplicitShowHide))
8439 widget->setAttribute(Qt::WA_WState_Hidden,
false);
8440 if (widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden))
8443 widget->setAttribute(Qt::WA_Mapped);
8444 widget->d_func()->showChildren(
true);
8446 QApplication::sendSpontaneousEvent(widget, &e);
8448 if (widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) {
8449 widget->d_func()->show_recursive();
8455 widget->d_func()->dontSetExplicitShowHide =
true;
8456 widget->setVisible(
true);
8457 widget->d_func()->dontSetExplicitShowHide =
false;
8463void QWidgetPrivate::hideChildren(
bool spontaneous)
8466 qCDebug(lcWidgetShowHide) <<
"Hiding children of" << q
8467 <<
"spontaneously" << spontaneous;
8469 QList<QObject*> childList = children;
8470 for (
int i = 0; i < childList.size(); ++i) {
8471 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8474 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8475 <<
"with attributes" << WidgetAttributes{widget};
8476 if (widget->isWindow() || !widget->isVisible())
8480 widget->setAttribute(Qt::WA_Mapped,
false);
8482 widget->setAttribute(Qt::WA_WState_Visible,
false);
8483 widget->d_func()->hideChildren(spontaneous);
8486 QApplication::sendSpontaneousEvent(widget, &e);
8488 QCoreApplication::sendEvent(widget, &e);
8489 if (widget->internalWinId()
8490 && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) {
8493 widget->d_func()->hide_sys();
8496 qApp->d_func()->sendSyntheticEnterLeave(widget);
8497#if QT_CONFIG(accessibility)
8499 QAccessibleEvent event(widget, QAccessible::ObjectHide);
8500 QAccessible::updateAccessibility(&event);
8508 if (QWidget* widgetWindow = q->window();
8509 widgetWindow && widgetWindow->data->is_closing) {
8510 q->setAttribute(Qt::WA_UnderMouse,
false);
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532bool QWidgetPrivate::handleClose(CloseMode mode)
8535 qCDebug(lcWidgetShowHide) <<
"Handling close event for" << q;
8537 if (data.is_closing)
8541 data.is_closing =
true;
8543 QPointer<QWidget> that = q;
8545 if (data.in_destructor)
8546 mode = CloseNoEvent;
8548 if (mode != CloseNoEvent) {
8550 if (mode == CloseWithSpontaneousEvent)
8551 QApplication::sendSpontaneousEvent(q, &e);
8553 QCoreApplication::sendEvent(q, &e);
8554 if (!that.isNull() && !e.isAccepted()) {
8555 data.is_closing =
false;
8561 if (!that.isNull() && !q->isHidden())
8564 if (!that.isNull()) {
8565 data.is_closing =
false;
8566 if (q->testAttribute(Qt::WA_DeleteOnClose)) {
8567 q->setAttribute(Qt::WA_DeleteOnClose,
false);
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8597bool QWidget::close()
8599 return d_func()->close();
8602bool QWidgetPrivate::close()
8612 if (QWindow *widgetWindow = windowHandle()) {
8613 if (widgetWindow->isTopLevel())
8614 return widgetWindow->close();
8617 return handleClose(QWidgetPrivate::CloseWithEvent);
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8678bool QWidget::isVisibleTo(
const QWidget *ancestor)
const
8682 const QWidget * w =
this;
8683 while (!w->isHidden()
8685 && w->parentWidget()
8686 && w->parentWidget() != ancestor)
8687 w = w->parentWidget();
8688 return !w->isHidden();
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702QRegion QWidget::visibleRegion()
const
8706 QRect clipRect = d->clipRect();
8707 if (clipRect.isEmpty())
8709 QRegion r(clipRect);
8710 d->subtractOpaqueChildren(r, clipRect);
8711 d->subtractOpaqueSiblings(r);
8716QSize QWidgetPrivate::adjustedSize()
const
8720 QSize s = q->sizeHint();
8722 if (q->isWindow()) {
8723 Qt::Orientations exp;
8725 if (layout->hasHeightForWidth())
8726 s.setHeight(layout->totalHeightForWidth(s.width()));
8727 exp = layout->expandingDirections();
8730 if (q->sizePolicy().hasHeightForWidth())
8731 s.setHeight(q->heightForWidth(s.width()));
8732 exp = q->sizePolicy().expandingDirections();
8734 if (exp & Qt::Horizontal)
8735 s.setWidth(qMax(s.width(), 200));
8736 if (exp & Qt::Vertical)
8737 s.setHeight(qMax(s.height(), 100));
8740 if (
const QScreen *screenAtPoint = QGuiApplication::screenAt(q->pos()))
8741 screen = screenAtPoint->geometry();
8743 screen = QGuiApplication::primaryScreen()->geometry();
8745 s.setWidth(qMin(s.width(), screen.width()*2/3));
8746 s.setHeight(qMin(s.height(), screen.height()*2/3));
8748 if (QTLWExtra *extra = maybeTopData())
8749 extra->sizeAdjusted =
true;
8753 QRect r = q->childrenRect();
8756 s = r.size() + QSize(2 * r.x(), 2 * r.y());
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8778void QWidget::adjustSize()
8782 QSize s = d->adjustedSize();
8785 d->layout->activate();
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8807QSize QWidget::sizeHint()
const
8811 return d->layout->totalSizeHint();
8812 return QSize(-1, -1);
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834QSize QWidget::minimumSizeHint()
const
8838 return d->layout->totalMinimumSize();
8839 return QSize(-1, -1);
8844
8845
8846
8847
8848
8852
8853
8854
8855
8857bool QWidget::isAncestorOf(
const QWidget *child)
const
8862 if (child->isWindow())
8864 child = child->parentWidget();
8870
8871
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8901bool QWidget::event(QEvent *event)
8907 switch(event->type()) {
8908 case QEvent::TabletPress:
8909 case QEvent::TabletRelease:
8910 case QEvent::TabletMove:
8911 case QEvent::MouseButtonPress:
8912 case QEvent::MouseButtonRelease:
8913 case QEvent::MouseButtonDblClick:
8914 case QEvent::MouseMove:
8915 case QEvent::TouchBegin:
8916 case QEvent::TouchUpdate:
8917 case QEvent::TouchEnd:
8918 case QEvent::TouchCancel:
8919 case QEvent::ContextMenu:
8920 case QEvent::KeyPress:
8921 case QEvent::KeyRelease:
8922#if QT_CONFIG(wheelevent)
8930 switch (event->type()) {
8931 case QEvent::PlatformSurface: {
8933 switch (
static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
8934 case QPlatformSurfaceEvent::SurfaceCreated:
8935 if (!testAttribute(Qt::WA_WState_Created))
8938 case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
8939 if (testAttribute(Qt::WA_WState_Created)) {
8942 destroy(
false,
false);
8948 case QEvent::MouseMove:
8949 mouseMoveEvent((QMouseEvent*)event);
8952 case QEvent::MouseButtonPress:
8953 mousePressEvent((QMouseEvent*)event);
8956 case QEvent::MouseButtonRelease:
8957 mouseReleaseEvent((QMouseEvent*)event);
8960 case QEvent::MouseButtonDblClick:
8961 mouseDoubleClickEvent((QMouseEvent*)event);
8963#if QT_CONFIG(wheelevent)
8965 wheelEvent((QWheelEvent*)event);
8968#if QT_CONFIG(tabletevent)
8969 case QEvent::TabletMove:
8970 if (
static_cast<QTabletEvent *>(event)->buttons() == Qt::NoButton && !testAttribute(Qt::WA_TabletTracking))
8973 case QEvent::TabletPress:
8974 case QEvent::TabletRelease:
8975 tabletEvent((QTabletEvent*)event);
8978 case QEvent::KeyPress: {
8979 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
8981 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
8982 if (k->key() == Qt::Key_Backtab
8983 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
8984 res = focusNextPrevChild(
false);
8985 else if (k->key() == Qt::Key_Tab)
8986 res = focusNextPrevChild(
true);
8991#ifdef QT_KEYPAD_NAVIGATION
8992 if (!k->isAccepted() && QApplication::keypadNavigationEnabled()
8993 && !(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::ShiftModifier))) {
8994 if (QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder) {
8995 if (k->key() == Qt::Key_Up)
8996 res = focusNextPrevChild(
false);
8997 else if (k->key() == Qt::Key_Down)
8998 res = focusNextPrevChild(
true);
8999 }
else if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
9000 if (k->key() == Qt::Key_Up)
9001 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionNorth);
9002 else if (k->key() == Qt::Key_Right)
9003 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionEast);
9004 else if (k->key() == Qt::Key_Down)
9005 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionSouth);
9006 else if (k->key() == Qt::Key_Left)
9007 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionWest);
9015#if QT_CONFIG(whatsthis)
9016 if (!k->isAccepted()
9017 && k->modifiers() & Qt::ShiftModifier && k->key() == Qt::Key_F1
9018 && d->whatsThis.size()) {
9019 QWhatsThis::showText(mapToGlobal(inputMethodQuery(Qt::ImCursorRectangle).toRect().center()), d->whatsThis,
this);
9026 case QEvent::KeyRelease:
9027 keyReleaseEvent((QKeyEvent*)event);
9029 case QEvent::ShortcutOverride:
9032 case QEvent::InputMethod:
9033 inputMethodEvent((QInputMethodEvent *) event);
9036 case QEvent::InputMethodQuery: {
9037 QInputMethodQueryEvent *query =
static_cast<QInputMethodQueryEvent *>(event);
9038 Qt::InputMethodQueries queries = query->queries();
9039 for (uint i = 0; i < 32; ++i) {
9040 Qt::InputMethodQuery q = (Qt::InputMethodQuery)(
int)(queries & (1<<i));
9042 QVariant v = inputMethodQuery(q);
9043 if (q == Qt::ImEnabled && !v.isValid() && isEnabled()) {
9050 v = QVariant(testAttribute(Qt::WA_InputMethodEnabled));
9052 query->setValue(q, v);
9059 case QEvent::PolishRequest:
9063 case QEvent::Polish: {
9064 style()->polish(
this);
9065 setAttribute(Qt::WA_WState_Polished);
9066 if (!QApplication::font(
this).isCopyOf(QApplication::font()))
9068 if (!QApplication::palette(
this).isCopyOf(QGuiApplication::palette()))
9069 d->resolvePalette();
9073 case QEvent::ApplicationWindowIconChange:
9074 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
9075 d->setWindowIcon_sys();
9076 d->setWindowIcon_helper();
9079 case QEvent::FocusIn:
9080 focusInEvent((QFocusEvent*)event);
9081 d->updateWidgetTransform(event);
9084 case QEvent::FocusOut:
9085 focusOutEvent((QFocusEvent*)event);
9089#if QT_CONFIG(statustip)
9090 if (d->statusTip.size()) {
9091 QStatusTipEvent tip(d->statusTip);
9092 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9095 enterEvent(
static_cast<QEnterEvent*>(event));
9099#if QT_CONFIG(statustip)
9100 if (d->statusTip.size()) {
9102 QStatusTipEvent tip(empty);
9103 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9109 case QEvent::HoverEnter:
9110 case QEvent::HoverLeave:
9118 paintEvent((QPaintEvent*)event);
9122 moveEvent((QMoveEvent*)event);
9123 d->updateWidgetTransform(event);
9126 case QEvent::Resize:
9127 resizeEvent((QResizeEvent*)event);
9128 d->updateWidgetTransform(event);
9132 closeEvent((QCloseEvent *)event);
9135#ifndef QT_NO_CONTEXTMENU
9136 case QEvent::ContextMenu:
9137 switch (data->context_menu_policy) {
9138 case Qt::PreventContextMenu:
9140 case Qt::DefaultContextMenu:
9141 contextMenuEvent(
static_cast<QContextMenuEvent *>(event));
9143 case Qt::CustomContextMenu:
9144 emit customContextMenuRequested(
static_cast<QContextMenuEvent *>(event)->pos());
9147 case Qt::ActionsContextMenu:
9148 if (d->actions.size()) {
9149 QMenu::exec(d->actions,
static_cast<QContextMenuEvent *>(event)->globalPos(),
9162#if QT_CONFIG(draganddrop)
9164 dropEvent((QDropEvent*) event);
9167 case QEvent::DragEnter:
9168 dragEnterEvent((QDragEnterEvent*) event);
9171 case QEvent::DragMove:
9172 dragMoveEvent((QDragMoveEvent*) event);
9175 case QEvent::DragLeave:
9176 dragLeaveEvent((QDragLeaveEvent*) event);
9181 showEvent((QShowEvent*) event);
9185 hideEvent((QHideEvent*) event);
9188 case QEvent::ShowWindowRequest:
9193 case QEvent::ApplicationFontChange:
9196 case QEvent::ApplicationPaletteChange:
9197 if (!(windowType() == Qt::Desktop))
9198 d->resolvePalette();
9201 case QEvent::ToolBarChange:
9202 case QEvent::ActivationChange:
9203 case QEvent::EnabledChange:
9204 case QEvent::FontChange:
9205 case QEvent::StyleChange:
9206 case QEvent::PaletteChange:
9207 case QEvent::WindowTitleChange:
9208 case QEvent::IconTextChange:
9209 case QEvent::ModifiedChange:
9210 case QEvent::MouseTrackingChange:
9211 case QEvent::TabletTrackingChange:
9212 case QEvent::ParentChange:
9213 case QEvent::LocaleChange:
9214 case QEvent::MacSizeChange:
9215 case QEvent::ContentsRectChange:
9216 case QEvent::ThemeChange:
9217 case QEvent::ReadOnlyChange:
9221 case QEvent::WindowStateChange: {
9222 const bool wasMinimized =
static_cast<
const QWindowStateChangeEvent *>(event)->oldState() & Qt::WindowMinimized;
9223 if (wasMinimized != isMinimized()) {
9224 QWidget *widget =
const_cast<QWidget *>(
this);
9227 if (!d->childrenShownByExpose) {
9229 d->showChildren(
true);
9230 QShowEvent showEvent;
9231 QCoreApplication::sendSpontaneousEvent(widget, &showEvent);
9233 d->childrenHiddenByWState =
false;
9235 QHideEvent hideEvent;
9236 QCoreApplication::sendSpontaneousEvent(widget, &hideEvent);
9237 d->hideChildren(
true);
9238 d->childrenHiddenByWState =
true;
9240 d->childrenShownByExpose =
false;
9246 case QEvent::WindowActivate:
9247 case QEvent::WindowDeactivate: {
9248 if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
9250 QList<QObject*> childList = d->children;
9251 for (
int i = 0; i < childList.size(); ++i) {
9252 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9253 if (w && w->isVisible() && !w->isWindow())
9254 QCoreApplication::sendEvent(w, event);
9258 case QEvent::LanguageChange:
9261 QList<QObject*> childList = d->children;
9262 for (
int i = 0; i < childList.size(); ++i) {
9263 QObject *o = childList.at(i);
9265 QCoreApplication::sendEvent(o, event);
9271 case QEvent::ApplicationLayoutDirectionChange:
9272 d->resolveLayoutDirection();
9275 case QEvent::LayoutDirectionChange:
9277 d->layout->invalidate();
9281 case QEvent::UpdateRequest:
9282 d->syncBackingStore();
9284 case QEvent::UpdateLater:
9285 update(
static_cast<QUpdateLaterEvent*>(event)->region());
9287 case QEvent::StyleAnimationUpdate:
9288 if (isVisible() && !window()->isMinimized()) {
9294 case QEvent::WindowBlocked:
9295 case QEvent::WindowUnblocked:
9296 if (!d->children.isEmpty()) {
9297 QWidget *modalWidget = QApplication::activeModalWidget();
9298 for (
int i = 0; i < d->children.size(); ++i) {
9299 QObject *o = d->children.at(i);
9300 if (o && o != modalWidget && o->isWidgetType()) {
9301 QWidget *w =
static_cast<QWidget *>(o);
9304 QCoreApplication::sendEvent(w, event);
9309#if QT_CONFIG(tooltip)
9310 case QEvent::ToolTip:
9311 if (!d->toolTip.isEmpty())
9312 QToolTip::showText(
static_cast<QHelpEvent*>(event)->globalPos(), d->toolTip,
this, QRect(), d->toolTipDuration);
9317#if QT_CONFIG(whatsthis)
9318 case QEvent::WhatsThis:
9319 if (d->whatsThis.size())
9320 QWhatsThis::showText(
static_cast<QHelpEvent *>(event)->globalPos(), d->whatsThis,
this);
9324 case QEvent::QueryWhatsThis:
9325 if (d->whatsThis.isEmpty())
9329 case QEvent::EmbeddingControl:
9330 d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
9331 data->fstrut_dirty =
false;
9334 case QEvent::ActionAdded:
9335 case QEvent::ActionRemoved:
9336 case QEvent::ActionChanged:
9337 actionEvent((QActionEvent*)event);
9341 case QEvent::KeyboardLayoutChange:
9346 QList<QObject*> childList = d->children;
9347 for (
int i = 0; i < childList.size(); ++i) {
9348 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9349 if (w && w->isVisible() && !w->isWindow())
9350 QCoreApplication::sendEvent(w, event);
9354 case QEvent::TouchBegin:
9355 case QEvent::TouchUpdate:
9356 case QEvent::TouchEnd:
9357 case QEvent::TouchCancel:
9362#ifndef QT_NO_GESTURES
9363 case QEvent::Gesture:
9367 case QEvent::ScreenChangeInternal:
9368 if (
const QTLWExtra *te = d->maybeTopData()) {
9369 const QWindow *win = te->window;
9370 d->setWinId((win && win->handle()) ? win->handle()->winId() : 0);
9373 case QEvent::DevicePixelRatioChange:
9374 if (d->data.fnt.d->dpi != logicalDpiY())
9375 d->updateFont(d->data.fnt);
9376 d->renderToTextureReallyDirty = 1;
9378 case QEvent::DynamicPropertyChange: {
9379 const QByteArray &propName =
static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
9380 if (propName.size() == 13 && !qstrncmp(propName,
"_q_customDpi", 12)) {
9381 uint value = property(propName.constData()).toUInt();
9384 const char axis = propName.at(12);
9386 d->extra->customDpiX = value;
9387 else if (axis ==
'Y')
9388 d->extra->customDpiY = value;
9389 d->updateFont(d->data.fnt);
9391 if (windowHandle() && !qstrncmp(propName,
"_q_platform_", 12))
9392 windowHandle()->setProperty(propName, property(propName));
9396 return QObject::event(event);
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417void QWidget::changeEvent(QEvent * event)
9419 switch(event->type()) {
9420 case QEvent::EnabledChange: {
9422#if QT_CONFIG(accessibility)
9423 QAccessible::State s;
9425 QAccessibleStateChangeEvent event(
this, s);
9426 QAccessible::updateAccessibility(&event);
9431 case QEvent::FontChange:
9432 case QEvent::StyleChange: {
9437 d->layout->invalidate();
9441 case QEvent::PaletteChange:
9445 case QEvent::ThemeChange:
9446 if (QGuiApplication::desktopSettingsAware() && windowType() != Qt::Desktop
9447 &&
qApp && !QCoreApplication::closingDown()) {
9448 if (testAttribute(Qt::WA_WState_Polished))
9449 QApplication::style()->unpolish(
this);
9450 if (testAttribute(Qt::WA_WState_Polished))
9451 QApplication::style()->polish(
this);
9452 QEvent styleChangedEvent(QEvent::StyleChange);
9453 QCoreApplication::sendEvent(
this, &styleChangedEvent);
9460 case QEvent::MacSizeChange:
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9496void QWidget::mouseMoveEvent(QMouseEvent *event)
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9518void QWidget::mousePressEvent(QMouseEvent *event)
9521 if ((windowType() == Qt::Popup)) {
9524 while ((w = QApplication::activePopupWidget()) && w !=
this){
9526 if (QApplication::activePopupWidget() == w)
9529 if (!rect().contains(event->position().toPoint())){
9536
9537
9538
9539
9540
9541
9543void QWidget::mouseReleaseEvent(QMouseEvent *event)
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9565void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
9567 mousePressEvent(event);
9570#if QT_CONFIG(wheelevent)
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9585void QWidget::wheelEvent(QWheelEvent *event)
9591#if QT_CONFIG(tabletevent)
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9612void QWidget::tabletEvent(QTabletEvent *event)
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9641void QWidget::keyPressEvent(QKeyEvent *event)
9643#ifndef QT_NO_SHORTCUT
9644 if ((windowType() == Qt::Popup) && event->matches(QKeySequence::Cancel)) {
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9676void QWidget::keyReleaseEvent(QKeyEvent *event)
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9700void QWidget::focusInEvent(QFocusEvent *)
9702 if (focusPolicy() != Qt::NoFocus || !isWindow()) {
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9726void QWidget::focusOutEvent(QFocusEvent *)
9728 if (focusPolicy() != Qt::NoFocus || !isWindow())
9731#if !defined(QT_PLATFORM_UIKIT)
9733 if (
qApp->autoSipEnabled() && testAttribute(Qt::WA_InputMethodEnabled))
9734 QGuiApplication::inputMethod()->hide();
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9750void QWidget::enterEvent(QEnterEvent *)
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9766void QWidget::leaveEvent(QEvent *)
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9818void QWidget::paintEvent(QPaintEvent *)
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9836void QWidget::moveEvent(QMoveEvent *)
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9857void QWidget::resizeEvent(QResizeEvent * )
9863
9864
9865
9866
9867
9868
9869
9870void QWidget::actionEvent(QActionEvent *)
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9891void QWidget::closeEvent(QCloseEvent *event)
9896#ifndef QT_NO_CONTEXTMENU
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9910void QWidget::contextMenuEvent(QContextMenuEvent *event)
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933void QWidget::inputMethodEvent(QInputMethodEvent *event)
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948QVariant QWidget::inputMethodQuery(Qt::InputMethodQuery query)
const
9951 case Qt::ImCursorRectangle:
9952 return QRect(width()/2, 0, 1, height());
9955 case Qt::ImAnchorPosition:
9957 return inputMethodQuery(Qt::ImCursorPosition);
9959 return (
int)inputMethodHints();
9960 case Qt::ImInputItemClipRectangle:
9961 return d_func()->clipRect();
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992Qt::InputMethodHints QWidget::inputMethodHints()
const
9995 const QWidgetPrivate *priv = d_func();
9996 while (priv->inheritsInputMethodHints) {
9997 priv = priv->q_func()->parentWidget()->d_func();
10000 return priv->imHints;
10002 return Qt::ImhNone;
10006void QWidget::setInputMethodHints(Qt::InputMethodHints hints)
10010 if (d->imHints == hints)
10012 d->imHints = hints;
10013 if (
this == QGuiApplication::focusObject())
10014 QGuiApplication::inputMethod()->update(Qt::ImHints);
10021#if QT_CONFIG(draganddrop)
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037void QWidget::dragEnterEvent(QDragEnterEvent *)
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055void QWidget::dragMoveEvent(QDragMoveEvent *)
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071void QWidget::dragLeaveEvent(QDragLeaveEvent *)
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086void QWidget::dropEvent(QDropEvent *)
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111void QWidget::showEvent(QShowEvent *)
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133void QWidget::hideEvent(QHideEvent *)
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10164bool QWidget::nativeEvent(
const QByteArray &eventType,
void *message, qintptr *result)
10166 Q_UNUSED(eventType);
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195void QWidget::ensurePolished()
const
10197 Q_D(
const QWidget);
10199 const QMetaObject *m = metaObject();
10200 if (m == d->polished)
10204 QEvent e(QEvent::Polish);
10205 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &e);
10208 QList<QObject*> children = d->children;
10209 for (
int i = 0; i < children.size(); ++i) {
10210 QObject *o = children.at(i);
10211 if (!o->isWidgetType())
10213 if (QWidget *w = qobject_cast<QWidget *>(o))
10214 w->ensurePolished();
10217 if (d->parent && d->sendChildEvents) {
10218 QChildEvent e(QEvent::ChildPolished,
const_cast<QWidget *>(
this));
10219 QCoreApplication::sendEvent(d->parent, &e);
10224
10225
10226
10227
10228
10229QRegion QWidget::mask()
const
10231 Q_D(
const QWidget);
10232 return d->extra ? d->extra->mask : QRegion();
10236
10237
10238
10239
10240
10241
10242
10243
10244QLayout *QWidget::layout()
const
10246 return d_func()->layout;
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10275void QWidget::setLayout(QLayout *l)
10277 if (Q_UNLIKELY(!l)) {
10278 qWarning(
"QWidget::setLayout: Cannot set layout to 0");
10282 if (Q_UNLIKELY(layout() != l))
10283 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
10284 " layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
10285 objectName().toLocal8Bit().data());
10289 QObject *oldParent = l->parent();
10290 if (oldParent && oldParent !=
this) {
10291 if (oldParent->isWidgetType()) {
10294 QWidget *oldParentWidget =
static_cast<QWidget *>(oldParent);
10295 oldParentWidget->takeLayout();
10297 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
10298 l->objectName().toLocal8Bit().data(), metaObject()->className(),
10299 objectName().toLocal8Bit().data());
10305 l->d_func()->topLevel =
true;
10307 if (oldParent !=
this) {
10308 l->setParent(
this);
10309 l->d_func()->reparentChildWidgets(
this);
10313 if (isWindow() && d->maybeTopData())
10314 d->topData()->sizeAdjusted =
false;
10318
10319
10320
10321
10322
10324QLayout *QWidget::takeLayout()
10327 QLayout *l = layout();
10330 d->layout =
nullptr;
10331 l->setParent(
nullptr);
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360QSizePolicy QWidget::sizePolicy()
const
10362 Q_D(
const QWidget);
10363 return d->size_policy;
10366void QWidget::setSizePolicy(QSizePolicy policy)
10369 setAttribute(Qt::WA_WState_OwnSizePolicy);
10370 if (policy == d->size_policy)
10373 if (d->size_policy.retainSizeWhenHidden() != policy.retainSizeWhenHidden())
10374 d->retainSizeWhenHiddenChanged = 1;
10376 d->size_policy = policy;
10378#if QT_CONFIG(graphicsview)
10379 if (
const auto &extra = d->extra) {
10380 if (extra->proxyWidget)
10381 extra->proxyWidget->setSizePolicy(policy);
10386 d->retainSizeWhenHiddenChanged = 0;
10388 if (isWindow() && d->maybeTopData())
10389 d->topData()->sizeAdjusted =
false;
10393
10394
10395
10396
10397
10398
10399
10400
10403
10404
10405
10406
10407
10408
10409
10411int QWidget::heightForWidth(
int w)
const
10413 if (layout() && layout()->hasHeightForWidth())
10414 return layout()->totalHeightForWidth(w);
10420
10421
10422
10423
10424bool QWidget::hasHeightForWidth()
const
10426 Q_D(
const QWidget);
10427 return d->layout ? d->layout->hasHeightForWidth() : d->size_policy.hasHeightForWidth();
10431
10432
10433
10434
10435
10436
10439
10440
10441
10442
10443
10445QWidget *QWidget::childAt(
const QPoint &p)
const
10447 return d_func()->childAt_helper(QPointF(p),
false);
10451
10452
10453
10454
10455
10456
10458QWidget *QWidget::childAt(
const QPointF &p)
const
10460 return d_func()->childAt_helper(p,
false);
10463QWidget *QWidgetPrivate::childAt_helper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10465 if (children.isEmpty())
10468 if (!pointInsideRectAndMask(p))
10470 return childAtRecursiveHelper(p, ignoreChildrenInDestructor);
10473QWidget *QWidgetPrivate::childAtRecursiveHelper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10475 for (
int i = children.size() - 1; i >= 0; --i) {
10476 QWidget *child = qobject_cast<QWidget *>(children.at(i));
10477 if (!child || child->isWindow() || child->isHidden() || child->testAttribute(Qt::WA_TransparentForMouseEvents)
10478 || (ignoreChildrenInDestructor && child->data->in_destructor)) {
10483 QPointF childPoint = p;
10484 childPoint -= child->data->crect.topLeft();
10487 if (!child->d_func()->pointInsideRectAndMask(childPoint))
10491 if (QWidget *w = child->d_func()->childAtRecursiveHelper(childPoint, ignoreChildrenInDestructor))
10500void QWidgetPrivate::updateGeometry_helper(
bool forceUpdate)
10504 widgetItem->invalidateSizeCache();
10506 if (forceUpdate || !extra || extra->minw != extra->maxw || extra->minh != extra->maxh) {
10507 const int isHidden = q->isHidden() && !size_policy.retainSizeWhenHidden() && !retainSizeWhenHiddenChanged;
10509 if (!q->isWindow() && !isHidden && (parent = q->parentWidget())) {
10510 if (parent->d_func()->layout)
10511 parent->d_func()->layout->invalidate();
10512 else if (parent->isVisible())
10513 QCoreApplication::postEvent(parent,
new QEvent(QEvent::LayoutRequest));
10519
10520
10521
10522
10523
10524
10525
10526
10528void QWidget::updateGeometry()
10531 d->updateGeometry_helper(
false);
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552void QWidget::setWindowFlags(Qt::WindowFlags flags)
10555 d->setWindowFlags(flags);
10559
10560
10561
10562
10563
10564
10565
10566void QWidget::setWindowFlag(Qt::WindowType flag,
bool on)
10570 d->setWindowFlags(data->window_flags | flag);
10572 d->setWindowFlags(data->window_flags & ~flag);
10576
10577
10578
10579void QWidgetPrivate::setWindowFlags(Qt::WindowFlags flags)
10582 if (q->data->window_flags == flags)
10585 if ((q->data->window_flags | flags) & Qt::Window) {
10587 QPoint oldPos = q->pos();
10588 bool visible = q->isVisible();
10589 const bool windowFlagChanged = (q->data->window_flags ^ flags) & Qt::Window;
10590 q->setParent(q->parentWidget(), flags);
10594 if (!windowFlagChanged && (visible || q->testAttribute(Qt::WA_Moved)))
10597 adjustQuitOnCloseAttribute();
10599 q->data->window_flags = flags;
10604
10605
10606
10607
10608
10609
10610
10611
10612void QWidget::overrideWindowFlags(Qt::WindowFlags flags)
10614 data->window_flags = flags;
10618
10619
10620
10621
10622
10623
10624
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654void QWidget::setParent(QWidget *parent)
10656 if (parent == parentWidget())
10658 setParent((QWidget*)parent, windowFlags() & ~Qt::WindowType_Mask);
10664 if (d->renderToTexture) {
10665 QEvent e(eventType);
10666 QCoreApplication::sendEvent(widget, &e);
10669 for (
int i = 0; i < d->children.size(); ++i) {
10671 if (w && !w->isWindow())
10672 qSendWindowChangeToTextureChildrenRecursively(w, eventType);
10676 if (
auto *window = d->windowHandle(QWidgetPrivate::WindowHandleMode::Direct)) {
10677 QEvent e(eventType);
10678 QCoreApplication::sendEvent(window, &e);
10683
10684
10685
10686
10688void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
10691 Q_ASSERT_X(
this != parent, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
10693 const auto checkForParentChildLoops = qScopeGuard([&](){
10695 auto p = parentWidget();
10697 if (++depth == QObjectPrivate::CheckForParentChildLoopsWarnDepth) {
10698 qWarning(
"QWidget %p (class: '%s', object name: '%s') may have a loop in its parent-child chain; "
10699 "this is undefined behavior",
10700 this, metaObject()->className(), qPrintable(objectName()));
10702 p = p->parentWidget();
10707 const bool resized = testAttribute(Qt::WA_Resized);
10708 const bool wasCreated = testAttribute(Qt::WA_WState_Created);
10709 QWidget *oldtlw = window();
10711 QWidget *oldParentWithWindow = d->closestParentWidgetWithWindowHandle();
10713 if (f & Qt::Window)
10714 d->data.fstrut_dirty =
true;
10716 QWidget *desktopWidget =
nullptr;
10717 if (parent && parent->windowType() == Qt::Desktop)
10718 desktopWidget = parent;
10719 bool newParent = (parent != parentWidget()) || desktopWidget;
10721 if (newParent && parent && !desktopWidget) {
10722 if (testAttribute(Qt::WA_NativeWindow) && !QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings))
10723 parent->d_func()->enforceNativeChildren();
10724 else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen))
10725 setAttribute(Qt::WA_NativeWindow);
10729 if (!testAttribute(Qt::WA_WState_Hidden)) {
10744 setAttribute(Qt::WA_WState_ExplicitShowHide,
false);
10747 QEvent e(QEvent::ParentAboutToChange);
10748 QCoreApplication::sendEvent(
this, &e);
10754 const bool oldWidgetUsesRhiFlush = oldParentWithWindow ? oldParentWithWindow->d_func()->usesRhiFlush
10755 : oldtlw->d_func()->usesRhiFlush;
10756 if (oldWidgetUsesRhiFlush && ((!parent && parentWidget()) || (parent && parent->window() != oldtlw)))
10757 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowAboutToChangeInternal);
10761 if (newParent && isAncestorOf(focusWidget()) && !(f & Qt::Window))
10762 focusWidget()->clearFocus();
10764 d->setParent_sys(parent, f);
10769 if (d->textureChildSeen && parent) {
10771 QWidgetPrivate::get(parent)->setTextureChildSeen();
10774 if (QWidgetRepaintManager *oldPaintManager = oldtlw->d_func()->maybeRepaintManager()) {
10776 oldPaintManager->removeDirtyWidget(
this);
10779 oldPaintManager->moveStaticWidgets(
this);
10782 d->reparentFocusWidgets(oldtlw);
10783 setAttribute(Qt::WA_Resized, resized);
10785 const bool useStyleSheetPropagationInWidgetStyles =
10786 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
10788 if (!useStyleSheetPropagationInWidgetStyles && !testAttribute(Qt::WA_StyleSheet)
10789 && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
10792 const auto pd = parent->d_func();
10793 d->inheritedFontResolveMask = pd->directFontResolveMask | pd->inheritedFontResolveMask;
10794 d->inheritedPaletteResolveMask = pd->directPaletteResolveMask | pd->inheritedPaletteResolveMask;
10797 d->resolvePalette();
10799 d->resolveLayoutDirection();
10800 d->resolveLocale();
10806 if (newParent || !wasCreated
10807#if QT_CONFIG(opengles2)
10808 || (f & Qt::MSWindowsOwnDC)
10813 if (!testAttribute(Qt::WA_ForceDisabled))
10814 d->setEnabled_helper(parent ? parent->isEnabled() :
true);
10815 if (!testAttribute(Qt::WA_ForceUpdatesDisabled))
10816 d->setUpdatesEnabled_helper(parent ? parent->updatesEnabled() :
true);
10821 if (parent && d->sendChildEvents) {
10822 QChildEvent e(QEvent::ChildAdded,
this);
10823 QCoreApplication::sendEvent(parent, &e);
10826 if (parent && d->sendChildEvents && d->polished) {
10827 QChildEvent e(QEvent::ChildPolished,
this);
10828 QCoreApplication::sendEvent(parent, &e);
10831 QEvent e(QEvent::ParentChange);
10832 QCoreApplication::sendEvent(
this, &e);
10837 if (oldWidgetUsesRhiFlush && oldtlw != window())
10838 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowChangeInternal);
10841 if (isWindow() || parentWidget()->isVisible())
10842 setAttribute(Qt::WA_WState_Hidden,
true);
10843 else if (!testAttribute(Qt::WA_WState_ExplicitShowHide))
10844 setAttribute(Qt::WA_WState_Hidden,
false);
10847 d->updateIsOpaque();
10849#if QT_CONFIG(graphicsview)
10852 if (oldtlw->graphicsProxyWidget()) {
10853 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
10854 ancestorProxy->d_func()->unembedSubWindow(
this);
10856 if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(
this)) {
10857 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
10858 ancestorProxy->d_func()->embedSubWindow(
this);
10862 if (d->extra && d->extra->hasWindowContainer)
10863 QWindowContainer::parentWasChanged(
this);
10865 QWidget *newParentWithWindow = d->closestParentWidgetWithWindowHandle();
10866 if (newParentWithWindow && newParentWithWindow != oldParentWithWindow) {
10868 qCDebug(lcWidgetPainting) <<
"Evaluating whether reparenting of" <<
this
10869 <<
"into" << parent <<
"requires RHI enablement for" << newParentWithWindow;
10871 QPlatformBackingStoreRhiConfig rhiConfig;
10872 QSurface::SurfaceType surfaceType = QSurface::RasterSurface;
10878 if (q_evaluateRhiConfig(
this, &rhiConfig, &surfaceType)) {
10882 if (q_evaluateRhiConfig(newParentWithWindow,
nullptr,
nullptr)) {
10885 auto *existingWindow = newParentWithWindow->windowHandle();
10886 auto existingSurfaceType = existingWindow->surfaceType();
10887 if (existingSurfaceType != surfaceType) {
10888 qCDebug(lcWidgetPainting)
10889 <<
"Recreating" << existingWindow
10890 <<
"with current type" << existingSurfaceType
10891 <<
"to support" << surfaceType;
10892 const auto windowStateBeforeDestroy = newParentWithWindow->windowState();
10893 const auto visibilityBeforeDestroy = newParentWithWindow->isVisible();
10894 const auto positionBeforeDestroy = newParentWithWindow->pos();
10895 newParentWithWindow->destroy();
10896 newParentWithWindow->create();
10897 Q_ASSERT(newParentWithWindow->windowHandle());
10898 newParentWithWindow->windowHandle()->setWindowStates(windowStateBeforeDestroy);
10899 newParentWithWindow->move(positionBeforeDestroy);
10900 QWidgetPrivate::get(newParentWithWindow)->setVisible(visibilityBeforeDestroy);
10901 }
else if (
auto *backingStore = newParentWithWindow->backingStore()) {
10904 backingStore->handle()->createRhi(existingWindow, rhiConfig);
10906 QWidgetPrivate::get(newParentWithWindow)->usesRhiFlush =
true;
10913void QWidgetPrivate::setParent_sys(QWidget *newparent, Qt::WindowFlags f)
10917 Qt::WindowFlags oldFlags = data.window_flags;
10918 bool wasCreated = q->testAttribute(Qt::WA_WState_Created);
10920 QScreen *targetScreen =
nullptr;
10922 if (newparent && newparent->windowType() == Qt::Desktop) {
10925 targetScreen = newparent->screen();
10926 newparent =
nullptr;
10934 targetScreen = q->parentWidget()->window()->screen();
10937 const bool destroyWindow = (
10939 (oldFlags & Qt::Window) && !(f & Qt::Window)
10941 && wasCreated && !q->testAttribute(Qt::WA_NativeWindow)
10944 if (parent != newparent) {
10946 QObjectPrivate::setParent_helper(newparent);
10948 if (q->windowHandle())
10949 q->windowHandle()->setFlags(f);
10953 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
10956 if (destroyWindow) {
10957 reparentWidgetWindowChildren(parentWithWindow);
10967 reparentWidgetWindows(parentWithWindow, f);
10971 bool explicitlyHidden = isExplicitlyHidden();
10973 if (destroyWindow) {
10974 if (extra && extra->hasWindowContainer)
10975 QWindowContainer::toplevelAboutToBeDestroyed(q);
10979 if (!q->windowHandle()->children().isEmpty()) {
10980 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
10981 QWindow *newParentWindow = parentWithWindow ? parentWithWindow->windowHandle() :
nullptr;
10982 for (QObject *child : q->windowHandle()->children()) {
10983 if (QWindow *childWindow = qobject_cast<QWindow *>(child)) {
10984 qCWarning(lcWidgetWindow) <<
"Reparenting" << childWindow
10985 <<
"before destroying" <<
this;
10986 childWindow->setParent(newParentWindow);
10994 q->destroy(
true,
false);
10998 data.window_flags = f;
10999 q->setAttribute(Qt::WA_WState_Created,
false);
11000 q->setAttribute(Qt::WA_WState_Visible,
false);
11001 q->setAttribute(Qt::WA_WState_Hidden,
false);
11003 if (newparent && wasCreated && (q->testAttribute(Qt::WA_NativeWindow) || (f & Qt::Window)))
11006 if (q->isWindow() || (!newparent || newparent->isVisible()) || explicitlyHidden)
11007 q->setAttribute(Qt::WA_WState_Hidden);
11008 q->setAttribute(Qt::WA_WState_ExplicitShowHide, explicitlyHidden);
11011 if (!newparent && targetScreen) {
11013 if (q->testAttribute(Qt::WA_WState_Created))
11014 q->windowHandle()->setScreen(targetScreen);
11016 topData()->initialScreen = targetScreen;
11020void QWidgetPrivate::reparentWidgetWindows(QWidget *parentWithWindow, Qt::WindowFlags windowFlags)
11022 if (QWindow *window = windowHandle()) {
11024 if (parentWithWindow) {
11025 if (windowFlags & Qt::Window) {
11028 QWidget *topLevel = parentWithWindow->window();
11029 auto *transientParent = topLevel->windowHandle();
11030 Q_ASSERT(transientParent);
11031 qCDebug(lcWidgetWindow) <<
"Setting" << window <<
"transient parent to" << transientParent;
11032 window->setTransientParent(transientParent);
11033 window->setParent(
nullptr);
11035 auto *parentWindow = parentWithWindow->windowHandle();
11036 qCDebug(lcWidgetWindow) <<
"Reparenting" << window <<
"into" << parentWindow;
11037 window->setTransientParent(
nullptr);
11038 window->setParent(parentWindow);
11041 qCDebug(lcWidgetWindow) <<
"Making" << window <<
"top level window";
11042 window->setTransientParent(
nullptr);
11043 window->setParent(
nullptr);
11046 reparentWidgetWindowChildren(parentWithWindow);
11050void QWidgetPrivate::reparentWidgetWindowChildren(QWidget *parentWithWindow)
11052 for (
auto *child : std::as_const(children)) {
11053 if (
auto *childWidget = qobject_cast<QWidget*>(child)) {
11054 auto *childPrivate = QWidgetPrivate::get(childWidget);
11057 childPrivate->reparentWidgetWindows(parentWithWindow, childWidget->windowFlags());
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11083void QWidget::scroll(
int dx,
int dy)
11085 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11087 if (dx == 0 && dy == 0)
11090#if QT_CONFIG(graphicsview)
11091 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11095 for (
const QRect &rect : d->dirty)
11096 proxy->update(rect.translated(dx, dy));
11097 proxy->scroll(dx, dy, proxy->subWidgetRect(
this));
11101 d->setDirtyOpaqueRegion();
11102 d->scroll_sys(dx, dy);
11105void QWidgetPrivate::scroll_sys(
int dx,
int dy)
11108 scrollChildren(dx, dy);
11109 scrollRect(q->rect(), dx, dy);
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122void QWidget::scroll(
int dx,
int dy,
const QRect &r)
11125 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11127 if (dx == 0 && dy == 0)
11130#if QT_CONFIG(graphicsview)
11131 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11135 if (!d->dirty.isEmpty()) {
11136 for (
const QRect &rect : d->dirty.translated(dx, dy) & r)
11137 proxy->update(rect);
11139 proxy->scroll(dx, dy, r.translated(proxy->subWidgetRect(
this).topLeft().toPoint()));
11143 d->scroll_sys(dx, dy, r);
11146void QWidgetPrivate::scroll_sys(
int dx,
int dy,
const QRect &r)
11148 scrollRect(r, dx, dy);
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11167void QWidget::repaint()
11173
11174
11175
11176
11177
11178
11179
11180void QWidget::repaint(
int x,
int y,
int w,
int h)
11182 if (x > data->crect.width() || y > data->crect.height())
11186 w = data->crect.width() - x;
11188 h = data->crect.height() - y;
11190 repaint(QRect(x, y, w, h));
11194
11195
11196
11197void QWidget::repaint(
const QRect &rect)
11204
11205
11206
11207
11208void QWidget::repaint(
const QRegion &rgn)
11214template <
typename T>
11215void QWidgetPrivate::repaint(T r)
11219 if (!q->isVisible() || !q->updatesEnabled() || r.isEmpty())
11222 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11223 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11224 tlwExtra->repaintManager->markDirty(r, q, QWidgetRepaintManager::UpdateNow);
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245void QWidget::update()
11251
11252
11253
11254
11255
11258
11259
11260
11261
11262void QWidget::update(
const QRect &rect)
11269
11270
11271
11272
11273void QWidget::update(
const QRegion &rgn)
11279template <
typename T>
11280void QWidgetPrivate::update(T r)
11284 if (renderToTexture && !q->isVisible()) {
11285 renderToTextureReallyDirty = 1;
11289 if (!q->isVisible() || !q->updatesEnabled())
11292 T clipped = r & q->rect();
11294 if (clipped.isEmpty())
11297 if (q->testAttribute(Qt::WA_WState_InPaintEvent)) {
11298 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(clipped));
11302 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11303 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11304 tlwExtra->repaintManager->markDirty(clipped, q);
11308
11309
11310
11311
11315 if (attribute <
int(8*
sizeof(uint))) {
11317 data->widget_attributes |= (1<<attribute);
11319 data->widget_attributes &= ~(1<<attribute);
11321 const int x = attribute - 8*
sizeof(uint);
11322 const int int_off = x / (8*
sizeof(uint));
11324 d->high_attributes[int_off] |= (1<<(x-(int_off*8*
sizeof(uint))));
11326 d->high_attributes[int_off] &= ~(1<<(x-(int_off*8*
sizeof(uint))));
11331void QWidgetPrivate::macUpdateSizeAttribute()
11334 QEvent event(QEvent::MacSizeChange);
11335 QCoreApplication::sendEvent(q, &event);
11336 for (
int i = 0; i < children.size(); ++i) {
11337 QWidget *w = qobject_cast<QWidget *>(children.at(i));
11338 if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
11339 && !w->testAttribute(Qt::WA_MacMiniSize)
11340 && !w->testAttribute(Qt::WA_MacSmallSize)
11341 && !w->testAttribute(Qt::WA_MacNormalSize))
11342 w->d_func()->macUpdateSizeAttribute();
11349
11350
11351
11352
11353
11354void QWidget::setAttribute(Qt::WidgetAttribute attribute,
bool on)
11356 if (testAttribute(attribute) == on)
11360 static_assert(
sizeof(d->high_attributes)*8 >= (Qt::WA_AttributeCount -
sizeof(uint)*8),
11361 "QWidget::setAttribute(WidgetAttribute, bool): "
11362 "QWidgetPrivate::high_attributes[] too small to contain all attributes in WidgetAttribute");
11365 if (attribute == Qt::WA_PaintOnScreen && on && windowType() != Qt::Desktop && !inherits(
"QGLWidget")) {
11368 if (d->noPaintOnScreen)
11374 if (attribute == Qt::WA_NativeWindow && !d->mustHaveWindowHandle) {
11375 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
11376 if (!platformIntegration->hasCapability(QPlatformIntegration::NativeWidgets))
11380 setAttribute_internal(attribute, on, data, d);
11382 switch (attribute) {
11384#if QT_CONFIG(draganddrop)
11385 case Qt::WA_AcceptDrops: {
11386 if (on && !testAttribute(Qt::WA_DropSiteRegistered))
11387 setAttribute(Qt::WA_DropSiteRegistered,
true);
11388 else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)))
11389 setAttribute(Qt::WA_DropSiteRegistered,
false);
11390 QEvent e(QEvent::AcceptDropsChange);
11391 QCoreApplication::sendEvent(
this, &e);
11394 case Qt::WA_DropSiteRegistered: {
11395 for (
int i = 0; i < d->children.size(); ++i) {
11396 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
11397 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_AcceptDrops) && w->testAttribute(Qt::WA_DropSiteRegistered) != on)
11398 w->setAttribute(Qt::WA_DropSiteRegistered, on);
11404 case Qt::WA_NoChildEventsForParent:
11405 d->sendChildEvents = !on;
11407 case Qt::WA_NoChildEventsFromChildren:
11408 d->receiveChildEvents = !on;
11410 case Qt::WA_MacNormalSize:
11411 case Qt::WA_MacSmallSize:
11412 case Qt::WA_MacMiniSize:
11416 const Qt::WidgetAttribute MacSizes[] = { Qt::WA_MacNormalSize, Qt::WA_MacSmallSize,
11417 Qt::WA_MacMiniSize };
11418 for (
int i = 0; i < 3; ++i) {
11419 if (MacSizes[i] != attribute)
11420 setAttribute_internal(MacSizes[i],
false, data, d);
11422 d->macUpdateSizeAttribute();
11426 case Qt::WA_ShowModal:
11429 data->window_modality = Qt::NonModal;
11430 }
else if (data->window_modality == Qt::NonModal) {
11433 data->window_modality = Qt::ApplicationModal;
11438 if (testAttribute(Qt::WA_WState_Created)) {
11443 case Qt::WA_MouseTracking: {
11444 QEvent e(QEvent::MouseTrackingChange);
11445 QCoreApplication::sendEvent(
this, &e);
11447 case Qt::WA_TabletTracking: {
11448 QEvent e(QEvent::TabletTrackingChange);
11449 QCoreApplication::sendEvent(
this, &e);
11451 case Qt::WA_NativeWindow: {
11452 d->createTLExtra();
11454 d->createTLSysExtra();
11456 QWidget *focusWidget = d->effectiveFocusWidget();
11457 if (on && !internalWinId() &&
this == QGuiApplication::focusObject()
11458 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11459 QGuiApplication::inputMethod()->commit();
11460 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11462 if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget())
11463 parentWidget()->d_func()->enforceNativeChildren();
11464 if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created))
11466 if (isEnabled() && focusWidget->isEnabled() &&
this == QGuiApplication::focusObject()
11467 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11468 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11473 case Qt::WA_PaintOnScreen:
11474 d->updateIsOpaque();
11476 case Qt::WA_OpaquePaintEvent:
11477 d->updateIsOpaque();
11479 case Qt::WA_NoSystemBackground:
11480 d->updateIsOpaque();
11482 case Qt::WA_UpdatesDisabled:
11483 d->updateSystemBackground();
11485 case Qt::WA_TransparentForMouseEvents:
11487 case Qt::WA_InputMethodEnabled: {
11489 if (QGuiApplication::focusObject() ==
this) {
11491 QGuiApplication::inputMethod()->commit();
11492 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11497 case Qt::WA_WindowPropagation:
11498 d->resolvePalette();
11500 d->resolveLocale();
11502 case Qt::WA_DontShowOnScreen: {
11503 if (on && isVisible()) {
11513 case Qt::WA_X11NetWmWindowTypeDesktop:
11514 case Qt::WA_X11NetWmWindowTypeDock:
11515 case Qt::WA_X11NetWmWindowTypeToolBar:
11516 case Qt::WA_X11NetWmWindowTypeMenu:
11517 case Qt::WA_X11NetWmWindowTypeUtility:
11518 case Qt::WA_X11NetWmWindowTypeSplash:
11519 case Qt::WA_X11NetWmWindowTypeDialog:
11520 case Qt::WA_X11NetWmWindowTypeDropDownMenu:
11521 case Qt::WA_X11NetWmWindowTypePopupMenu:
11522 case Qt::WA_X11NetWmWindowTypeToolTip:
11523 case Qt::WA_X11NetWmWindowTypeNotification:
11524 case Qt::WA_X11NetWmWindowTypeCombo:
11525 case Qt::WA_X11NetWmWindowTypeDND:
11526 d->setNetWmWindowTypes();
11529 case Qt::WA_StaticContents:
11530 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
11532 repaintManager->addStaticWidget(
this);
11534 repaintManager->removeStaticWidget(
this);
11537 case Qt::WA_TranslucentBackground:
11539 setAttribute(Qt::WA_NoSystemBackground);
11540 d->updateIsTranslucent();
11543 case Qt::WA_AcceptTouchEvents:
11551
11552
11553
11554
11555
11556
11557bool QWidget::testAttribute_helper(Qt::WidgetAttribute attribute)
const
11559 Q_D(
const QWidget);
11560 const int x = attribute - 8*
sizeof(uint);
11561 const int int_off = x / (8*
sizeof(uint));
11562 return (d->high_attributes[int_off] & (1<<(x-(int_off*8*
sizeof(uint)))));
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590qreal QWidget::windowOpacity()
const
11592 Q_D(
const QWidget);
11593 return (isWindow() && d->maybeTopData()) ? d->maybeTopData()->opacity / 255. : 1.0;
11596void QWidget::setWindowOpacity(qreal opacity)
11602 opacity = qBound(qreal(0.0), opacity, qreal(1.0));
11603 QTLWExtra *extra = d->topData();
11604 extra->opacity = uint(opacity * 255);
11605 setAttribute(Qt::WA_WState_WindowOpacitySet);
11606 d->setWindowOpacity_sys(opacity);
11608 if (!testAttribute(Qt::WA_WState_Created))
11611#if QT_CONFIG(graphicsview)
11612 if (QGraphicsProxyWidget *proxy = graphicsProxyWidget()) {
11614 if (proxy->cacheMode() == QGraphicsItem::NoCache)
11616 else if (QGraphicsScene *scene = proxy->scene())
11617 scene->update(proxy->sceneBoundingRect());
11623void QWidgetPrivate::setWindowOpacity_sys(qreal level)
11626 if (q->windowHandle())
11627 q->windowHandle()->setOpacity(level);
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654bool QWidget::isWindowModified()
const
11656 return testAttribute(Qt::WA_WindowModified);
11659void QWidget::setWindowModified(
bool mod)
11662 setAttribute(Qt::WA_WindowModified, mod);
11664 d->setWindowModified_helper();
11666 QEvent e(QEvent::ModifiedChange);
11667 QCoreApplication::sendEvent(
this, &e);
11670void QWidgetPrivate::setWindowModified_helper()
11673 QWindow *window = q->windowHandle();
11676 QPlatformWindow *platformWindow = window->handle();
11677 if (!platformWindow)
11679 bool on = q->testAttribute(Qt::WA_WindowModified);
11680 if (!platformWindow->setWindowModified(on)) {
11681 if (Q_UNLIKELY(on && !q->windowTitle().contains(
"[*]"_L1)))
11682 qWarning(
"QWidget::setWindowModified: The window title does not contain a '[*]' placeholder");
11683 setWindowTitle_helper(q->windowTitle());
11684 setWindowIconText_helper(q->windowIconText());
11688#if QT_CONFIG(tooltip)
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707void QWidget::setToolTip(
const QString &s)
11712 QEvent event(QEvent::ToolTipChange);
11713 QCoreApplication::sendEvent(
this, &event);
11716QString QWidget::toolTip()
const
11718 Q_D(
const QWidget);
11723
11724
11725
11726
11727
11728
11729
11730
11731
11733void QWidget::setToolTipDuration(
int msec)
11736 d->toolTipDuration = msec;
11739int QWidget::toolTipDuration()
const
11741 Q_D(
const QWidget);
11742 return d->toolTipDuration;
11748#if QT_CONFIG(statustip)
11750
11751
11752
11753
11754
11755
11756
11757void QWidget::setStatusTip(
const QString &s)
11763QString QWidget::statusTip()
const
11765 Q_D(
const QWidget);
11766 return d->statusTip;
11770#if QT_CONFIG(whatsthis)
11772
11773
11774
11775
11776
11777
11778
11779
11780void QWidget::setWhatsThis(
const QString &s)
11786QString QWidget::whatsThis()
const
11788 Q_D(
const QWidget);
11789 return d->whatsThis;
11793#if QT_CONFIG(accessibility)
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815void QWidget::setAccessibleName(
const QString &name)
11818 if (d->accessibleName == name)
11821 d->accessibleName = name;
11822 QAccessibleEvent event(
this, QAccessible::NameChanged);
11823 QAccessible::updateAccessibility(&event);
11826QString QWidget::accessibleName()
const
11828 Q_D(
const QWidget);
11829 return d->accessibleName;
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848void QWidget::setAccessibleDescription(
const QString &description)
11851 if (d->accessibleDescription == description)
11854 d->accessibleDescription = description;
11855 QAccessibleEvent event(
this, QAccessible::DescriptionChanged);
11856 QAccessible::updateAccessibility(&event);
11859QString QWidget::accessibleDescription()
const
11861 Q_D(
const QWidget);
11862 return d->accessibleDescription;
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876void QWidget::setAccessibleIdentifier(
const QString &identifier)
11879 if (d->accessibleIdentifier == identifier)
11882 d->accessibleIdentifier = identifier;
11883 QAccessibleEvent event(
this, QAccessible::IdentifierChanged);
11884 QAccessible::updateAccessibility(&event);
11887QString QWidget::accessibleIdentifier()
const
11889 Q_D(
const QWidget);
11890 return d->accessibleIdentifier;
11895#ifndef QT_NO_SHORTCUT
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919int QWidget::grabShortcut(
const QKeySequence &key, Qt::ShortcutContext context)
11924 setAttribute(Qt::WA_GrabbedShortcut);
11925 return QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(
this, key, context, qWidgetShortcutContextMatcher);
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943void QWidget::releaseShortcut(
int id)
11947 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id,
this, 0);
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962void QWidget::setShortcutEnabled(
int id,
bool enable)
11966 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id,
this, 0);
11970
11971
11972
11973
11974
11975
11976
11977void QWidget::setShortcutAutoRepeat(
int id,
bool enable)
11981 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(enable, id,
this, 0);
11986
11987
11988
11989void QWidget::updateMicroFocus(Qt::InputMethodQuery query)
11991 if (
this == QGuiApplication::focusObject())
11992 QGuiApplication::inputMethod()->update(query);
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12007void QWidget::raise()
12011 QWidget *p = parentWidget();
12012 const int parentChildCount = p->d_func()->children.size();
12013 if (parentChildCount < 2)
12015 const int from = p->d_func()->children.indexOf(
this);
12016 Q_ASSERT(from >= 0);
12018 if (from != parentChildCount -1)
12019 p->d_func()->children.move(from, parentChildCount - 1);
12020 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12022 else if (from == parentChildCount - 1)
12025 QRegion region(rect());
12026 d->subtractOpaqueSiblings(region);
12027 d->invalidateBackingStore(region);
12029 if (testAttribute(Qt::WA_WState_Created))
12032 if (d->extra && d->extra->hasWindowContainer)
12033 QWindowContainer::parentWasRaised(
this);
12035 QEvent e(QEvent::ZOrderChange);
12036 QCoreApplication::sendEvent(
this, &e);
12039void QWidgetPrivate::raise_sys()
12042 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12043 q->windowHandle()->raise();
12044 }
else if (renderToTexture) {
12045 if (QWidget *p = q->parentWidget()) {
12046 setDirtyOpaqueRegion();
12047 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12053
12054
12055
12056
12057
12058
12059
12061void QWidget::lower()
12065 QWidget *p = parentWidget();
12066 const int parentChildCount = p->d_func()->children.size();
12067 if (parentChildCount < 2)
12069 const int from = p->d_func()->children.indexOf(
this);
12070 Q_ASSERT(from >= 0);
12073 p->d_func()->children.move(from, 0);
12074 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12076 else if (from == 0)
12079 if (testAttribute(Qt::WA_WState_Created))
12082 if (d->extra && d->extra->hasWindowContainer)
12083 QWindowContainer::parentWasLowered(
this);
12085 QEvent e(QEvent::ZOrderChange);
12086 QCoreApplication::sendEvent(
this, &e);
12089void QWidgetPrivate::lower_sys()
12092 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12093 Q_ASSERT(q->testAttribute(Qt::WA_WState_Created));
12094 q->windowHandle()->lower();
12095 }
else if (QWidget *p = q->parentWidget()) {
12096 setDirtyOpaqueRegion();
12097 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12102
12103
12104
12105
12106
12107
12108void QWidget::stackUnder(QWidget* w)
12111 QWidget *p = parentWidget();
12112 if (!w || isWindow() || p != w->parentWidget() ||
this == w)
12115 int from = p->d_func()->children.indexOf(
this);
12116 int to = p->d_func()->children.indexOf(w);
12117 Q_ASSERT(from >= 0);
12123 p->d_func()->children.move(from, to);
12124 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12126 else if (from == to)
12129 if (testAttribute(Qt::WA_WState_Created))
12130 d->stackUnder_sys(w);
12132 QEvent e(QEvent::ZOrderChange);
12133 QCoreApplication::sendEvent(
this, &e);
12136void QWidgetPrivate::stackUnder_sys(QWidget*)
12139 if (QWidget *p = q->parentWidget()) {
12140 setDirtyOpaqueRegion();
12141 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12146
12147
12148
12149
12150
12153
12154
12155
12158
12159
12160
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12176
12177
12178
12179
12180
12181
12182
12183
12185QRect QWidgetPrivate::frameStrut()
const
12187 Q_Q(
const QWidget);
12188 if (!q->isWindow() || (q->windowType() == Qt::Desktop) || q->testAttribute(Qt::WA_DontShowOnScreen)) {
12190 return QRect(0, 0, 1, 1);
12193 if (data.fstrut_dirty
12196 && q->testAttribute(Qt::WA_WState_Created))
12197 const_cast<QWidgetPrivate *>(
this)->updateFrameStrut();
12199 return maybeTopData() ? maybeTopData()->frameStrut : QRect();
12202void QWidgetPrivate::updateFrameStrut()
12205 if (q->data->fstrut_dirty) {
12206 if (QTLWExtra *te = maybeTopData()) {
12207 if (te->window && te->window->handle()) {
12208 const QMargins margins = te->window->frameMargins();
12209 if (!margins.isNull()) {
12210 te->frameStrut.setCoords(margins.left(), margins.top(), margins.right(), margins.bottom());
12211 q->data->fstrut_dirty =
false;
12218#ifdef QT_KEYPAD_NAVIGATION
12220
12221
12222
12223
12224
12225
12226
12227bool QWidgetPrivate::navigateToDirection(Direction direction)
12229 QWidget *targetWidget = widgetInNavigationDirection(direction);
12231 targetWidget->setFocus();
12232 return (targetWidget != 0);
12236
12237
12238
12239
12240
12241
12242
12243
12244QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
12246 const QWidget *sourceWidget = QApplication::focusWidget();
12249 const QRect sourceRect = sourceWidget->rect().translated(sourceWidget->mapToGlobal(QPoint()));
12250 const int sourceX =
12251 (direction == DirectionNorth || direction == DirectionSouth) ?
12252 (sourceRect.left() + (sourceRect.right() - sourceRect.left()) / 2)
12253 :(direction == DirectionEast ? sourceRect.right() : sourceRect.left());
12254 const int sourceY =
12255 (direction == DirectionEast || direction == DirectionWest) ?
12256 (sourceRect.top() + (sourceRect.bottom() - sourceRect.top()) / 2)
12257 :(direction == DirectionSouth ? sourceRect.bottom() : sourceRect.top());
12258 const QPoint sourcePoint(sourceX, sourceY);
12259 const QPoint sourceCenter = sourceRect.center();
12260 const QWidget *sourceWindow = sourceWidget->window();
12262 QWidget *targetWidget =
nullptr;
12263 int shortestDistance = INT_MAX;
12265 const auto targetCandidates = QApplication::allWidgets();
12266 for (QWidget *targetCandidate : targetCandidates) {
12268 const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));
12273 if (targetCandidate->focusProxy() || targetCandidateRect.isEmpty())
12277 if ( targetCandidate != sourceWidget
12279 && targetCandidate->focusPolicy() & Qt::TabFocus
12281 && !(direction == DirectionNorth && targetCandidateRect.bottom() > sourceRect.top())
12283 && !(direction == DirectionEast && targetCandidateRect.left() < sourceRect.right())
12285 && !(direction == DirectionSouth && targetCandidateRect.top() < sourceRect.bottom())
12287 && !(direction == DirectionWest && targetCandidateRect.right() > sourceRect.left())
12289 && targetCandidate->isEnabled()
12291 && targetCandidate->isVisible()
12293 && targetCandidate->window() == sourceWindow) {
12294 const int targetCandidateDistance = [](
const QPoint &sourcePoint,
12295 const QRect &targetCandidateRect) {
12298 if (p.x() < r.left())
12299 dx = r.left() - p.x();
12300 else if (p.x() > r.right())
12301 dx = p.x() - r.right();
12302 if (p.y() < r.top())
12303 dy = r.top() - p.y();
12304 else if (p.y() > r.bottom())
12305 dy = p.y() - r.bottom();
12308 if (targetCandidateDistance < shortestDistance) {
12309 shortestDistance = targetCandidateDistance;
12310 targetWidget = targetCandidate;
12314 return targetWidget;
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328bool QWidgetPrivate::canKeypadNavigate(Qt::Orientation orientation)
12330 return orientation == Qt::Horizontal?
12331 (QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionEast)
12332 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionWest))
12333 :(QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionNorth)
12334 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionSouth));
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348bool QWidgetPrivate::inTabWidget(QWidget *widget)
12350 for (QWidget *tabWidget = widget; tabWidget; tabWidget = tabWidget->parentWidget())
12351 if (qobject_cast<
const QTabWidget*>(tabWidget))
12358
12359
12360
12361
12362
12363
12364void QWidget::setBackingStore(QBackingStore *store)
12373 QTLWExtra *topData = d->topData();
12374 if (topData->backingStore == store)
12377 QBackingStore *oldStore = topData->backingStore;
12378 delete topData->backingStore;
12379 topData->backingStore = store;
12381 QWidgetRepaintManager *repaintManager = d->maybeRepaintManager();
12382 if (!repaintManager)
12386 if (repaintManager->backingStore() != oldStore && repaintManager->backingStore() != store)
12387 delete repaintManager->backingStore();
12388 repaintManager->setBackingStore(store);
12393
12394
12395
12396
12397QBackingStore *QWidget::backingStore()
const
12399 Q_D(
const QWidget);
12400 QTLWExtra *extra = d->maybeTopData();
12401 if (extra && extra->backingStore)
12402 return extra->backingStore;
12405 return window()->backingStore();
12410void QWidgetPrivate::getLayoutItemMargins(
int *left,
int *top,
int *right,
int *bottom)
const
12413 *left = (
int)leftLayoutItemMargin;
12415 *top = (
int)topLayoutItemMargin;
12417 *right = (
int)rightLayoutItemMargin;
12419 *bottom = (
int)bottomLayoutItemMargin;
12422void QWidgetPrivate::setLayoutItemMargins(
int left,
int top,
int right,
int bottom)
12424 if (leftLayoutItemMargin == left
12425 && topLayoutItemMargin == top
12426 && rightLayoutItemMargin == right
12427 && bottomLayoutItemMargin == bottom)
12431 leftLayoutItemMargin = (
signed char)left;
12432 topLayoutItemMargin = (
signed char)top;
12433 rightLayoutItemMargin = (
signed char)right;
12434 bottomLayoutItemMargin = (
signed char)bottom;
12435 q->updateGeometry();
12438void QWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element,
const QStyleOption *opt)
12441 QStyleOption myOpt;
12444 myOpt.rect.setRect(0, 0, 32768, 32768);
12448 QRect liRect = q->style()->subElementRect(element, opt, q);
12449 if (liRect.isValid()) {
12450 leftLayoutItemMargin = (
signed char)(opt->rect.left() - liRect.left());
12451 topLayoutItemMargin = (
signed char)(opt->rect.top() - liRect.top());
12452 rightLayoutItemMargin = (
signed char)(liRect.right() - opt->rect.right());
12453 bottomLayoutItemMargin = (
signed char)(liRect.bottom() - opt->rect.bottom());
12455 leftLayoutItemMargin = 0;
12456 topLayoutItemMargin = 0;
12457 rightLayoutItemMargin = 0;
12458 bottomLayoutItemMargin = 0;
12462void QWidgetPrivate::adjustQuitOnCloseAttribute()
12466 if (!q->parentWidget()) {
12467 Qt::WindowType type = q->windowType();
12468 if (type == Qt::Widget || type == Qt::SubWindow)
12470 if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
12471 q->setAttribute(Qt::WA_QuitOnClose,
false);
12475void QWidgetPrivate::sendComposeStatus(QWidget *w,
bool end)
12477 QWidgetPrivate *wd = QWidgetPrivate::get(w);
12478 if (!wd->textureChildSeen)
12483 wd->beginCompose();
12484 for (
int i = 0; i < wd->children.size(); ++i) {
12485 w = qobject_cast<QWidget *>(wd->children.at(i));
12486 if (w && !w->isWindow() && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen)
12487 sendComposeStatus(w, end);
12491Q_WIDGETS_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget)
12493 return widget->data;
12498 return widget->d_func();
12502#if QT_CONFIG(graphicsview)
12504
12505
12506
12507
12508
12509
12510
12511
12512QGraphicsProxyWidget *QWidget::graphicsProxyWidget()
const
12514 Q_D(
const QWidget);
12516 return d->extra->proxyWidget;
12522#ifndef QT_NO_GESTURES
12524
12525
12526
12527
12528
12529void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
12532 d->gestureContext.insert(gesture, flags);
12533 (
void)QGestureManager::instance();
12537
12538
12539
12540
12541
12542void QWidget::ungrabGesture(Qt::GestureType gesture)
12546 if (d->gestureContext.remove(gesture)) {
12547 if (QGestureManager *manager = QGestureManager::instance())
12548 manager->cleanupCachedGestures(
this, gesture);
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566void QWidget::destroy(
bool destroyWindow,
bool destroySubWindows)
12570 d->aboutToDestroy();
12571 if (!isWindow() && parentWidget())
12572 parentWidget()->d_func()->invalidateBackingStore(d->effectiveRectFor(geometry()));
12573 d->deactivateWidgetCleanup();
12575 if ((windowType() == Qt::Popup) &&
qApp)
12576 qApp->d_func()->closePopup(
this);
12578 if (
this ==
qApp->activeWindow())
12579 QApplicationPrivate::setActiveWindow(
nullptr);
12580 if (QWidget::mouseGrabber() ==
this)
12582 if (QWidget::keyboardGrabber() ==
this)
12585 setAttribute(Qt::WA_WState_Created,
false);
12587 if (windowType() != Qt::Desktop) {
12588 if (destroySubWindows) {
12589 QObjectList childList(children());
12590 for (
int i = 0; i < childList.size(); i++) {
12591 QWidget *widget = qobject_cast<QWidget *>(childList.at(i));
12592 if (widget && widget->testAttribute(Qt::WA_NativeWindow)) {
12593 if (widget->windowHandle()) {
12599 if (destroyWindow) {
12600 d->deleteTLSysExtra();
12602 if (parentWidget() && parentWidget()->testAttribute(Qt::WA_WState_Created)) {
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621QPaintEngine *QWidget::paintEngine()
const
12623 qWarning(
"QWidget::paintEngine: Should no longer be called");
12640 const_cast<QWidgetPrivate *>(d_func())->noPaintOnScreen = 1;
12649 return window->handle() && !qt_window_private(window)->resizeEventPending;
12652#if QT_CONFIG(graphicsview)
12653static inline QGraphicsProxyWidget *graphicsProxyWidget(
const QWidget *w)
12655 QGraphicsProxyWidget *result =
nullptr;
12656 const QWidgetPrivate *d = qt_widget_private(
const_cast<QWidget *>(w));
12658 result = d->extra->proxyWidget;
12672 for ( ; w ; w = w->parentWidget()) {
12673#if QT_CONFIG(graphicsview)
12674 if (QGraphicsProxyWidget *qgpw = graphicsProxyWidget(w)) {
12675 if (
const QGraphicsScene *scene = qgpw->scene()) {
12676 const QList <QGraphicsView *> views = scene->views();
12677 if (!views.isEmpty()) {
12678 auto *viewP =
static_cast<QGraphicsViewPrivate *>(qt_widget_private(views.constFirst()));
12679 result.transform *= viewP->mapToViewTransform(qgpw);
12680 w = views.first()->viewport();
12685 QWindow *window = w->windowHandle();
12691 const auto &geometry = w->geometry();
12692 result.transform *= QTransform::fromTranslate(geometry.x(), geometry.y());
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709QPointF QWidget::mapToGlobal(
const QPointF &pos)
const
12711 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12712 const QPointF g = t.transform.map(pos);
12713 return t.window ? t.window->mapToGlobal(g) : g;
12717
12718
12719QPoint QWidget::mapToGlobal(
const QPoint &pos)
const
12721 return mapToGlobal(QPointF(pos)).toPoint();
12725
12726
12727
12728
12729
12730
12731
12732
12733QPointF QWidget::mapFromGlobal(
const QPointF &pos)
const
12735 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12736 const QPointF windowLocal = t.window ? t.window->mapFromGlobal(pos) : pos;
12737 return t.transform.inverted().map(windowLocal);
12741
12742
12743QPoint QWidget::mapFromGlobal(
const QPoint &pos)
const
12745 return mapFromGlobal(QPointF(pos)).toPoint();
12754 QWindow *window = w->windowHandle();
12756 if (
const QWidget *nativeParent = w->nativeParentWidget())
12757 window = nativeParent->windowHandle();
12761#ifndef QT_NO_CURSOR
12772#ifndef QT_NO_CURSOR
12775 QGuiApplication::setOverrideCursor(*cursor);
12778 window->setMouseGrabEnabled(
true);
12788#ifndef QT_NO_CURSOR
12790 QGuiApplication::restoreOverrideCursor();
12794 window->setMouseGrabEnabled(
false);
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828void QWidget::grabMouse()
12830 grabMouseForWidget(
this);
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849#ifndef QT_NO_CURSOR
12850void QWidget::grabMouse(
const QCursor &cursor)
12852 grabMouseForWidget(
this, &cursor);
12856bool QWidgetPrivate::stealMouseGrab(
bool grab)
12861 QWindow *window = grabberWindow(q);
12862 return window ? window->setMouseGrabEnabled(grab) :
false;
12866
12867
12868
12869
12870
12871
12872void QWidget::releaseMouse()
12874 releaseMouseGrabOfWidget(
this);
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896void QWidget::grabKeyboard()
12899 keyboardGrb->releaseKeyboard();
12900 if (QWindow *window = grabberWindow(
this))
12901 window->setKeyboardGrabEnabled(
true);
12902 keyboardGrb =
this;
12905bool QWidgetPrivate::stealKeyboardGrab(
bool grab)
12910 QWindow *window = grabberWindow(q);
12911 return window ? window->setKeyboardGrabEnabled(grab) :
false;
12915
12916
12917
12918
12919
12920
12921void QWidget::releaseKeyboard()
12923 if (keyboardGrb ==
this) {
12924 if (QWindow *window = grabberWindow(
this))
12925 window->setKeyboardGrabEnabled(
false);
12926 keyboardGrb =
nullptr;
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940QWidget *QWidget::mouseGrabber()
12942 return qt_mouseGrb;
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955QWidget *QWidget::keyboardGrabber()
12957 return keyboardGrb;
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984void QWidget::activateWindow()
12986 QWindow *
const wnd = window()->windowHandle();
12989 wnd->requestActivate();
12993
12994
12995
12996
12997
12998
12999int QWidget::metric(PaintDeviceMetric m)
const
13001 QScreen *screen =
this->screen();
13004 if (m == PdmDpiX || m == PdmDpiY)
13006 return QPaintDevice::metric(m);
13009 auto resolveDevicePixelRatio = [
this, screen]() -> qreal {
13012 static bool downscale = qEnvironmentVariableIntValue(
"QT_WIDGETS_HIGHDPI_DOWNSCALE") > 0;
13013 QWindow *window =
this->window()->windowHandle();
13015 return downscale ? std::ceil(window->devicePixelRatio()) : window->devicePixelRatio();
13016 return screen->devicePixelRatio();
13021 return data->crect.width();
13023 return data->crect.width() * screen->physicalSize().width() / screen->geometry().width();
13025 return data->crect.height();
13027 return data->crect.height() * screen->physicalSize().height() / screen->geometry().height();
13029 return screen->depth();
13031 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13032 if (p->d_func()->extra && p->d_func()->extra->customDpiX)
13033 return p->d_func()->extra->customDpiX;
13035 return qRound(screen->logicalDotsPerInchX());
13037 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13038 if (p->d_func()->extra && p->d_func()->extra->customDpiY)
13039 return p->d_func()->extra->customDpiY;
13041 return qRound(screen->logicalDotsPerInchY());
13042 case PdmPhysicalDpiX:
13043 return qRound(screen->physicalDotsPerInchX());
13044 case PdmPhysicalDpiY:
13045 return qRound(screen->physicalDotsPerInchY());
13046 case PdmDevicePixelRatio:
13047 return resolveDevicePixelRatio();
13048 case PdmDevicePixelRatioScaled:
13049 return QPaintDevice::devicePixelRatioFScale() * resolveDevicePixelRatio();
13050 case PdmDevicePixelRatioF_EncodedA:
13052 case PdmDevicePixelRatioF_EncodedB:
13053 return QPaintDevice::encodeMetricF(m, resolveDevicePixelRatio());
13057 return QPaintDevice::metric(m);
13061
13062
13063
13064
13065void QWidget::initPainter(QPainter *painter)
const
13067 const QPalette &pal = palette();
13068 painter->d_func()->state->pen = QPen(pal.brush(foregroundRole()), 1);
13069 painter->d_func()->state->bgBrush = pal.brush(backgroundRole());
13070 QFont f(font(),
const_cast<QWidget *>(
this));
13071 painter->d_func()->state->deviceFont = f;
13072 painter->d_func()->state->font = f;
13076
13077
13078
13079
13080QPaintDevice *QWidget::redirected(QPoint *offset)
const
13082 return d_func()->redirected(offset);
13086
13087
13088
13089
13090QPainter *QWidget::sharedPainter()
const
13093 if (!d_func()->redirectDev)
13096 QPainter *sp = d_func()->sharedPainter();
13097 if (!sp || !sp->isActive())
13100 if (sp->paintEngine()->paintDevice() != d_func()->redirectDev)
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128void QWidget::setMask(
const QRegion &newMask)
13133 if (newMask == d->extra->mask)
13136#ifndef QT_NO_BACKINGSTORE
13137 const QRegion oldMask(d->extra->mask);
13140 d->extra->mask = newMask;
13141 d->extra->hasMask = !newMask.isEmpty();
13143 if (!testAttribute(Qt::WA_WState_Created))
13146 d->setMask_sys(newMask);
13148#ifndef QT_NO_BACKINGSTORE
13152 if (!d->extra->hasMask) {
13154 QRegion expose(rect());
13156 if (!expose.isEmpty()) {
13157 d->setDirtyOpaqueRegion();
13165 QRegion parentExpose(rect());
13166 parentExpose -= newMask;
13167 if (!parentExpose.isEmpty()) {
13168 d->setDirtyOpaqueRegion();
13169 parentExpose.translate(data->crect.topLeft());
13170 parentWidget()->update(parentExpose);
13174 if (!oldMask.isEmpty())
13175 update(newMask - oldMask);
13180void QWidgetPrivate::setMask_sys(
const QRegion ®ion)
13183 if (QWindow *window = q->windowHandle())
13184 window->setMask(region);
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212void QWidget::setMask(
const QBitmap &bitmap)
13214 setMask(QRegion(bitmap));
13218
13219
13220
13221
13222
13223
13224void QWidget::clearMask()
13227 if (!d->extra || !d->extra->hasMask)
13229 setMask(QRegion());
13232void QWidgetPrivate::setWidgetParentHelper(QObject *widgetAsObject, QObject *newParent)
13234 Q_ASSERT(widgetAsObject->isWidgetType());
13235 Q_ASSERT(!newParent || newParent->isWidgetType());
13236 QWidget *widget =
static_cast<QWidget*>(widgetAsObject);
13237 widget->setParent(
static_cast<QWidget*>(newParent));
13240std::string QWidgetPrivate::flagsForDumping()
const
13242 Q_Q(
const QWidget);
13243 std::string flags = QObjectPrivate::flagsForDumping();
13244 if (QApplication::focusWidget() == q)
13246 if (q->isVisible()) {
13247 std::stringstream s;
13249 << q->width() <<
'x' << q->height()
13250 << std::showpos << q->x() << q->y()
13259void QWidgetPrivate::setNetWmWindowTypes(
bool skipIfMissing)
13264 if (!q->windowHandle())
13267 QXcbWindow::WindowTypes wmWindowType = QXcbWindow::None;
13268 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDesktop))
13269 wmWindowType |= QXcbWindow::Desktop;
13270 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDock))
13271 wmWindowType |= QXcbWindow::Dock;
13272 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolBar))
13273 wmWindowType |= QXcbWindow::Toolbar;
13274 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeMenu))
13275 wmWindowType |= QXcbWindow::Menu;
13276 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeUtility))
13277 wmWindowType |= QXcbWindow::Utility;
13278 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeSplash))
13279 wmWindowType |= QXcbWindow::Splash;
13280 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDialog))
13281 wmWindowType |= QXcbWindow::Dialog;
13282 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu))
13283 wmWindowType |= QXcbWindow::DropDownMenu;
13284 if (q->testAttribute(Qt::WA_X11NetWmWindowTypePopupMenu))
13285 wmWindowType |= QXcbWindow::PopupMenu;
13286 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip))
13287 wmWindowType |= QXcbWindow::Tooltip;
13288 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeNotification))
13289 wmWindowType |= QXcbWindow::Notification;
13290 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeCombo))
13291 wmWindowType |= QXcbWindow::Combo;
13292 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDND))
13293 wmWindowType |= QXcbWindow::Dnd;
13295 if (wmWindowType == QXcbWindow::None && skipIfMissing)
13298 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(q->windowHandle()->handle()))
13299 xcbWindow->setWindowType(wmWindowType);
13301 Q_UNUSED(skipIfMissing);
13306
13307
13308
13309
13310bool QWidgetPrivate::hasChildWithFocusPolicy(Qt::FocusPolicy policy,
const QWidget *excludeChildrenOf)
const
13312 Q_Q(
const QWidget);
13313 const QWidgetList &children = q->findChildren<QWidget *>(Qt::FindChildrenRecursively);
13314 for (
const auto *child : children) {
13315 if (child->focusPolicy() == policy && child->isEnabled()
13316 && (!excludeChildrenOf || !excludeChildrenOf->isAncestorOf(child))) {
13323#ifndef QT_NO_DEBUG_STREAM
13326QDebug operator<<(QDebug debug,
const WidgetAttributes &attributes)
13328 const QDebugStateSaver saver(debug);
13331 if (
const QWidget *widget = attributes.widget) {
13332 const QMetaObject *qtMo = qt_getEnumMetaObject(Qt::WA_AttributeCount);
13333 const QMetaEnum me = qtMo->enumerator(qtMo->indexOfEnumerator(
"WidgetAttribute"));
13335 for (
int a = 0; a < Qt::WA_AttributeCount; ++a) {
13336 if (widget->testAttribute(
static_cast<Qt::WidgetAttribute>(a))) {
13339 debug << me.valueToKey(a);
13350 const QDebugStateSaver saver(debug);
13353 debug << widget->metaObject()->className() <<
'(' << (
const void *)widget;
13354 if (!widget->objectName().isEmpty())
13355 debug <<
", name=" << widget->objectName();
13356 if (debug.verbosity() > 2) {
13357 const QRect geometry = widget->geometry();
13358 const QRect frameGeometry = widget->frameGeometry();
13359 if (widget->isVisible())
13360 debug <<
", visible";
13361 if (!widget->isEnabled())
13362 debug <<
", disabled";
13363 debug <<
", states=" << widget->windowState()
13364 <<
", type=" << widget->windowType() <<
", flags=" << widget->windowFlags();
13365 debug <<
", attributes=" << WidgetAttributes{widget};
13366 if (widget->isWindow())
13367 debug <<
", window";
13368 debug <<
", " << geometry.width() <<
'x' << geometry.height()
13369 << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
13370 if (frameGeometry != geometry) {
13371 const QMargins margins(geometry.x() - frameGeometry.x(),
13372 geometry.y() - frameGeometry.y(),
13373 frameGeometry.right() - geometry.right(),
13374 frameGeometry.bottom() - geometry.bottom());
13375 debug <<
", margins=" << margins;
13377 debug <<
", devicePixelRatio=" << widget->devicePixelRatio();
13378 if (
const WId wid = widget->internalWinId())
13379 debug <<
", winId=0x" << Qt::hex << wid << Qt::dec;
13383 debug <<
"QWidget(0x0)";
13392#define FOCUS_NEXT(w) w->d_func()->focus_next
13393#define FOCUS_PREV(w) w->d_func()->focus_prev
13396
13397
13398
13399
13400QWidget *QWidgetPrivate::nextPrevElementInFocusChain(FocusDirection direction)
const
13402 Q_Q(
const QWidget);
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417bool QWidgetPrivate::removeFromFocusChain(FocusChainRemovalRules rules, FocusDirection direction)
13420 if (!isFocusChainConsistent()) {
13422 if (rules.testFlag(FocusChainRemovalRule::AssertConsistency))
13423 qFatal() << q <<
"has inconsistent focus chain.";
13425 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because of inconsistent focus chain.";
13429 if (!isInFocusChain()) {
13430 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because it is not part of a focus chain.";
13434 if (rules.testFlag(FocusChainRemovalRule::EnsureFocusOut))
13435 q->focusNextPrevChild(direction == FocusDirection::Next);
13440 qCDebug(lcWidgetFocus) << q <<
"removed from focus chain.";
13445
13446
13447
13448void QWidgetPrivate::initFocusChain()
13451 qCDebug(lcWidgetFocus) <<
"Initializing focus chain of" << q;
13457
13458
13459
13460
13461
13462
13463void QWidgetPrivate::reparentFocusChildren(FocusDirection direction)
13468 QWidget *firstOld =
nullptr;
13469 QWidget *lastOld =
nullptr;
13470 QWidget *lastNew = q;
13471 bool prevWasNew =
true;
13472 QWidget *widget = nextPrevElementInFocusChain(direction);
13477 while (widget != q) {
13478 bool currentIsNew = q->isAncestorOf(widget);
13479 if (currentIsNew) {
13499 widget = widget->d_func()->nextPrevElementInFocusChain(direction);
13500 prevWasNew = currentIsNew;
13509 if (!q->isWindow()) {
13510 QWidget *topLevel = q->window();
13525
13526
13527
13528
13529
13530bool QWidgetPrivate::insertIntoFocusChain(FocusDirection direction, QWidget *position)
13533 Q_ASSERT(position);
13537 switch (direction) {
13538 case FocusDirection::Next:
13539 if (previous == position) {
13540 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already before" << position;
13544 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13550 qCDebug(lcWidgetFocus) << q <<
"inserted after" << position;
13553 case FocusDirection::Previous:
13554 if (next == position) {
13555 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already after" << position;
13559 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13565 qCDebug(lcWidgetFocus) << q <<
"inserted before" << position;
13569 Q_ASSERT(isFocusChainConsistent());
13574
13575
13576
13577
13578
13579
13580
13581bool QWidgetPrivate::insertIntoFocusChain(
const QWidgetList &toBeInserted,
13582 FocusDirection direction, QWidget *position)
13584 if (toBeInserted.isEmpty()) {
13585 qCDebug(lcWidgetFocus) <<
"No-op insertion of an empty list";
13589 Q_ASSERT_X(!toBeInserted.contains(position),
13591 "Coding error: toBeInserted contains position");
13593 QWidget *first = toBeInserted.constFirst();
13594 QWidget *last = toBeInserted.constLast();
13597 if (toBeInserted.count() == 1)
13598 return first->d_func()->insertIntoFocusChain(direction, position);
13600 Q_ASSERT(first != last);
13601 switch (direction) {
13602 case FocusDirection::Previous:
13604 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already before"
13612 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted before" << position;
13614 case FocusDirection::Next:
13616 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already after"
13624 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted after" << position;
13628 Q_ASSERT(position->d_func()->isFocusChainConsistent());
13633
13634
13635
13636
13639 QWidgetList path({from});
13645 switch (direction) {
13646 case QWidgetPrivate::FocusDirection::Previous:
13647 current = current->previousInFocusChain();
13649 case QWidgetPrivate::FocusDirection::Next:
13650 current = current->nextInFocusChain();
13653 if (path.contains(current))
13654 return QWidgetList();
13656 }
while (current != to);
13662
13663
13664
13665
13666
13667QWidgetList QWidgetPrivate::takeFromFocusChain(QWidget *from,
13669 FocusDirection direction)
13672 const QWidgetList path = focusPath(from, to , direction);
13673 if (path.isEmpty()) {
13674 qCDebug(lcWidgetFocus) <<
"No-op removal. Focus chain from" << from <<
"doesn't lead to " << to;
13675 return QWidgetList();
13678 QWidget *first = path.constFirst();
13679 QWidget *last = path.constLast();
13680 if (first == last) {
13681 first->d_func()->removeFromFocusChain();
13682 return QWidgetList({first});
13689 qCDebug(lcWidgetFocus) << path <<
"removed from focus chain";
13694
13695
13696
13697
13698QWidget *QWidgetPrivate::determineLastFocusChild(QWidget *noFurtherThan)
13705 QWidget *lastFocusChild = q;
13707 QWidget *focusProxy = deepestFocusProxy();
13714 for (
auto *object : std::as_const(q->children())) {
13715 QWidget *w = qobject_cast<QWidget *>(object);
13716 if (w && w->focusProxy() == q) {
13717 lastFocusChild = w;
13721 }
else if (q->isAncestorOf(focusProxy)) {
13722 lastFocusChild = focusProxy;
13723 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
13724 focusNext != focusProxy && q->isAncestorOf(focusNext)
13725 && focusNext->window() == focusProxy->window();
13726 focusNext = focusNext->nextInFocusChain()) {
13727 if (focusNext == noFurtherThan)
13729 if (focusNext->focusPolicy() != Qt::NoFocus)
13730 lastFocusChild = focusNext;
13733 return lastFocusChild;
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748bool QWidgetPrivate::isInFocusChain()
const
13750 Q_Q(
const QWidget);
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776bool QWidgetPrivate::isFocusChainConsistent()
const
13778 Q_Q(
const QWidget);
13779 const bool skip = !QLoggingCategory(
"qt.widgets.focus").isDebugEnabled();
13783 if (!isInFocusChain())
13786 const QWidget *position = q;
13788 for (
int i = 0; i < QApplication::allWidgets().count(); ++i) {
13790 qCDebug(lcWidgetFocus) <<
"Nullptr found at:" << position
13791 <<
"Previous pointing to" <<
FOCUS_PREV(position)
13792 <<
"Next pointing to" <<
FOCUS_NEXT(position);
13797 qCDebug(lcWidgetFocus) <<
"Inconsistent focus chain at:" << position
13808 qCDebug(lcWidgetFocus) <<
"Focus chain leading from" << q <<
"to" << position <<
"is not closed.";
13818#include "moc_qwidget.cpp"
13819#include "moc_qwidget_p.cpp"