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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
795QWidgetMapper *QWidgetPrivate::mapper =
nullptr;
796QWidgetSet *QWidgetPrivate::allWidgets =
nullptr;
800
801
804
805
806
807
808
809
810
811
812
813
814
815
816
817
824#ifdef QT_NO_EXCEPTIONS
828 QWidgetPrivate::allWidgets->remove(that);
829 d->removeFromFocusChain();
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
860 : QWidget(*
new QWidgetPrivate, parent, f)
866
867QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
868 : QObject(dd,
nullptr), QPaintDevice()
875 QWidgetExceptionCleaner::cleanup(
this, d_func());
881
882
883int QWidget::devType()
const
885 return QInternal::Widget;
890void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
892 bool customize = (flags & (Qt::CustomizeWindowHint
893 | Qt::FramelessWindowHint
894 | Qt::WindowTitleHint
895 | Qt::WindowSystemMenuHint
896 | Qt::WindowMinimizeButtonHint
897 | Qt::WindowMaximizeButtonHint
898 | Qt::WindowCloseButtonHint
899 | Qt::WindowContextHelpButtonHint));
901 uint type = (flags & Qt::WindowType_Mask);
903 if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
908 if (flags & Qt::CustomizeWindowHint) {
913 if ((flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint))
915 && type != Qt::Dialog
918 flags |= Qt::WindowSystemMenuHint;
919 flags |= Qt::WindowTitleHint;
920 flags &= ~Qt::FramelessWindowHint;
922 }
else if (customize && !(flags & Qt::FramelessWindowHint)) {
926 flags |= Qt::WindowSystemMenuHint;
927 flags |= Qt::WindowTitleHint;
930 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
931 if (type != Qt::Dialog && type != Qt::Sheet && type != Qt::Tool)
932 flags |= Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowFullscreenButtonHint;
934 if (w->testAttribute(Qt::WA_TransparentForMouseEvents))
935 flags |= Qt::WindowTransparentForInput;
938void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
944 QT_IGNORE_DEPRECATIONS(
945 Q_ASSERT_X(!f.testFlag(Qt::WindowType::Desktop), Q_FUNC_INFO,
"Qt::WindowType::Desktop is not allowed.");
946 if (f.testFlag(Qt::WindowType::Desktop))
947 f.setFlag(Qt::WindowType::Desktop,
false);
950 Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
952 if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
953 qFatal(
"QWidget: Cannot create a QWidget without QApplication");
955 Q_ASSERT(allWidgets);
957 allWidgets->insert(q);
961 Q_ASSERT_X(QThread::isMainThread(),
"QWidget",
962 "Widgets must be created in the GUI thread.");
966 data.fstrut_dirty =
true;
969 data.widget_attributes = 0;
970 data.window_flags = f;
971 data.window_state = 0;
972 data.focus_policy = 0;
973 data.context_menu_policy = Qt::DefaultContextMenu;
974 data.window_modality = Qt::NonModal;
976 data.sizehint_forced = 0;
977 data.is_closing =
false;
979 data.in_set_window_state = 0;
980 data.in_destructor =
false;
983 if (f & Qt::MSWindowsOwnDC) {
984 mustHaveWindowHandle = 1;
985 q->setAttribute(Qt::WA_NativeWindow);
988 q->setAttribute(Qt::WA_QuitOnClose);
989 adjustQuitOnCloseAttribute();
991 q->setAttribute(Qt::WA_WState_Hidden);
994 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
998 q->setParent(parentWidget, data.window_flags);
1000 adjustFlags(data.window_flags, q);
1001 resolveLayoutDirection();
1003 const QBrush &background = q->palette().brush(QPalette::Window);
1004 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
1006 data.fnt = QFont(data.fnt, q);
1008 q->setAttribute(Qt::WA_PendingMoveEvent);
1009 q->setAttribute(Qt::WA_PendingResizeEvent);
1011 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
1012 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
1014 QEvent e(QEvent::Create);
1015 QCoreApplication::sendEvent(q, &e);
1016 QCoreApplication::postEvent(q,
new QEvent(QEvent::PolishRequest));
1018 extraPaintEngine =
nullptr;
1021void QWidgetPrivate::createRecursively()
1024 q->create(0,
true,
true);
1025 for (
int i = 0; i < children.size(); ++i) {
1026 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1027 if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
1028 child->d_func()->createRecursively();
1032QRhi *QWidgetPrivate::rhi()
const
1035 if (
auto *backingStore = q->backingStore()) {
1036 auto *window = windowHandle(WindowHandleMode::Closest);
1037 return backingStore->handle()->rhi(window);
1044
1045
1046
1047
1048
1049
1050
1051QWidget *QWidgetPrivate::closestParentWidgetWithWindowHandle()
const
1054 QWidget *parent = q->parentWidget();
1055 while (parent && !parent->windowHandle())
1056 parent = parent->parentWidget();
1060QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode)
const
1062 if (mode == WindowHandleMode::Direct || mode == WindowHandleMode::Closest) {
1063 if (QTLWExtra *x = maybeTopData()) {
1064 if (x->window !=
nullptr || mode == WindowHandleMode::Direct)
1068 if (mode == WindowHandleMode::Closest) {
1070 if (
auto nativeParent = q_func()->nativeParentWidget()) {
1071 if (
auto window = nativeParent->windowHandle())
1075 if (mode == WindowHandleMode::TopLevel || mode == WindowHandleMode::Closest) {
1076 if (
auto topLevel = q_func()->topLevelWidget()) {
1077 if (
auto window = topLevel ->windowHandle())
1085
1086
1087
1088
1089
1090QWindow *QWidgetPrivate::_q_closestWindowHandle()
const
1092 return windowHandle(QWidgetPrivate::WindowHandleMode::Closest);
1095QScreen *QWidgetPrivate::associatedScreen()
const
1097#if QT_CONFIG(graphicsview)
1099 if (nearestGraphicsProxyWidget(q_func()))
1102 if (
auto window = windowHandle(WindowHandleMode::Closest))
1103 return window->screen();
1110 QPlatformBackingStoreRhiConfig config = QWidgetPrivate::get(w)->rhiConfig();
1111 if (config.isEnabled()) {
1113 *outConfig = config;
1115 *outType = QBackingStoreRhiSupport::surfaceTypeForConfig(config);
1118 for (
const QObject *child : w->children()) {
1119 if (
const QWidget *childWidget = qobject_cast<
const QWidget *>(child)) {
1120 if (q_evaluateRhiConfigRecursive(childWidget, outConfig, outType)) {
1121 static bool optOut = qEnvironmentVariableIsSet(
"QT_WIDGETS_NO_CHILD_RHI");
1124 if (!optOut && childWidget->testAttribute(Qt::WA_NativeWindow))
1140 if (QBackingStoreRhiSupport::checkForceRhi(outConfig, outType)) {
1141 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluated to forced flushing with QRhi";
1147 if (q_evaluateRhiConfigRecursive(w, outConfig, outType)) {
1148 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluates to flushing with QRhi";
1158
1159
1160
1161
1162
1163
1164
1165
1166
1168void QWidget::create(WId window,
bool initializeWindow,
bool destroyOldWindow)
1170 Q_UNUSED(initializeWindow);
1171 Q_UNUSED(destroyOldWindow);
1174 if (Q_UNLIKELY(window))
1175 qWarning(
"QWidget::create(): Parameter 'window' does not have any effect.");
1176 if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
1179 if (d->data.in_destructor)
1182 Qt::WindowType type = windowType();
1183 Qt::WindowFlags &flags = data->window_flags;
1185 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1187 flags |= Qt::Window;
1190 if (QWidget *parent = parentWidget()) {
1191 if (type & Qt::Window) {
1192 if (!parent->testAttribute(Qt::WA_WState_Created))
1193 parent->createWinId();
1194 }
else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
1195 && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
1201 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1202 Q_ASSERT(internalWinId());
1208 static const bool paintOnScreenEnv = qEnvironmentVariableIntValue(
"QT_ONSCREEN_PAINT") > 0;
1209 if (paintOnScreenEnv)
1210 setAttribute(Qt::WA_PaintOnScreen);
1212 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1213 setAttribute(Qt::WA_NativeWindow);
1216#if QT_CONFIG(graphicsview)
1217 && !graphicsProxyWidget()
1221 auto *topExtra = d->maybeTopData();
1222 if (!topExtra || !topExtra->explicitContentsMarginsRespectsSafeArea) {
1223 setAttribute_internal(Qt::WA_ContentsMarginsRespectsSafeArea,
1228 d->updateIsOpaque();
1230 setAttribute(Qt::WA_WState_Created);
1235 d->topData()->repaintManager.reset(
new QWidgetRepaintManager(
this));
1239 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1240 setAttribute(Qt::WA_DropSiteRegistered,
true);
1243 if (testAttribute(Qt::WA_SetWindowIcon))
1244 d->setWindowIcon_sys();
1246 if (isWindow() && !d->topData()->iconText.isEmpty())
1247 d->setWindowIconText_helper(d->topData()->iconText);
1248 if (isWindow() && !d->topData()->caption.isEmpty())
1249 d->setWindowTitle_helper(d->topData()->caption);
1250 if (isWindow() && !d->topData()->filePath.isEmpty())
1251 d->setWindowFilePath_helper(d->topData()->filePath);
1252 d->updateSystemBackground();
1254 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1255 d->setWindowIcon_sys();
1262 d->updateFrameStrut();
1267 QObjectList children = parentWidget->children();
1268 for (
int i = 0; i < children.size(); i++) {
1269 if (children.at(i)->isWidgetType()) {
1270 const QWidget *childWidget = qobject_cast<
const QWidget *>(children.at(i));
1272 if (childWidget->testAttribute(Qt::WA_NativeWindow)) {
1273 if (!childWidget->internalWinId())
1274 childWidget->winId();
1275 if (childWidget->windowHandle()) {
1276 if (childWidget->isWindow()) {
1277 childWidget->windowHandle()->setTransientParent(parentWidget->window()->windowHandle());
1279 childWidget->windowHandle()->setParent(childWidget->nativeParentWidget()->windowHandle());
1291void QWidgetPrivate::create()
1295 if (!q->testAttribute(Qt::WA_NativeWindow) && !q->isWindow())
1298 QWidgetWindow *win = topData()->window;
1303 Q_ASSERT(topData()->window);
1304 win = topData()->window;
1307 const auto dynamicPropertyNames = q->dynamicPropertyNames();
1308 for (
const QByteArray &propertyName : dynamicPropertyNames) {
1309 if (!qstrncmp(propertyName,
"_q_platform_", 12))
1310 win->setProperty(propertyName, q->property(propertyName));
1313 Qt::WindowFlags &flags = data.window_flags;
1315#if defined(QT_PLATFORM_UIKIT)
1316 if (q->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
1317 flags |= Qt::ExpandedClientAreaHint;
1320 if (q->testAttribute(Qt::WA_ShowWithoutActivating))
1321 win->setProperty(
"_q_showWithoutActivating", QVariant(
true));
1322 if (q->testAttribute(Qt::WA_MacAlwaysShowToolWindow))
1323 win->setProperty(
"_q_macAlwaysShowToolWindow", QVariant(
true));
1324 win->setFlags(flags);
1325 fixPosIncludesFrame();
1326 if (q->testAttribute(Qt::WA_Moved)
1327 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
1328 win->setGeometry(q->geometry());
1330 win->resize(q->size());
1331 if (win->isTopLevel()) {
1332 QScreen *targetScreen = topData()->initialScreen;
1333 topData()->initialScreen =
nullptr;
1335 targetScreen = q->screen();
1336 win->setScreen(targetScreen);
1339 QSurfaceFormat format = win->requestedFormat();
1340 if ((flags & Qt::Window) && win->surfaceType() != QSurface::OpenGLSurface
1341 && q->testAttribute(Qt::WA_TranslucentBackground)) {
1342 format.setAlphaBufferSize(8);
1344 win->setFormat(format);
1346 if (QWidget *nativeParent = q->nativeParentWidget()) {
1347 if (nativeParent->windowHandle()) {
1348 if (flags & Qt::Window) {
1349 win->setTransientParent(nativeParent->window()->windowHandle());
1350 win->setParent(
nullptr);
1352 win->setTransientParent(
nullptr);
1353 win->setParent(nativeParent->windowHandle());
1358 qt_window_private(win)->positionPolicy = topData()->posIncludesFrame ?
1359 QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
1363 if (QPlatformWindow *platformWindow = win->handle())
1364 platformWindow->setFrameStrutEventsEnabled(
true);
1366 data.window_flags = win->flags();
1369 if (!topData()->role.isNull()) {
1370 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(win->handle()))
1371 xcbWindow->setWindowRole(topData()->role);
1374#if QT_CONFIG(wayland)
1375 if (!topData()->role.isNull()) {
1376 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(win->handle()))
1377 waylandWindow->setSessionRestoreId(topData()->role);
1381 QBackingStore *store = q->backingStore();
1382 usesRhiFlush =
false;
1384 if (!store && q->isWindow())
1385 q->setBackingStore(
new QBackingStore(win));
1387 QPlatformBackingStoreRhiConfig rhiConfig;
1388 usesRhiFlush = q_evaluateRhiConfig(q, &rhiConfig,
nullptr);
1389 if (usesRhiFlush && q->backingStore()) {
1392 q->backingStore()->handle()->createRhi(win, rhiConfig);
1395 setWindowModified_helper();
1397 if (win->handle()) {
1398 WId id = win->winId();
1400 Q_ASSERT(id != WId(0));
1403 setNetWmWindowTypes(
true);
1406 q_createNativeChildrenAndSetParent(q);
1408 if (extra && !extra->mask.isEmpty())
1409 setMask_sys(extra->mask);
1411 if (data.crect.width() == 0 || data.crect.height() == 0) {
1412 q->setAttribute(Qt::WA_OutsideWSRange,
true);
1414 q->setAttribute(Qt::WA_OutsideWSRange,
false);
1415 if (q->isVisible()) {
1417 win->setNativeWindowVisibility(
true);
1423static const char activeXNativeParentHandleProperty[] =
"_q_embedded_native_parent_handle";
1426void QWidgetPrivate::createTLSysExtra()
1429 if (!extra->topextra->window && (q->testAttribute(Qt::WA_NativeWindow) || q->isWindow())) {
1430 extra->topextra->window =
new QWidgetWindow(q);
1431 if (extra->minw || extra->minh)
1432 extra->topextra->window->setMinimumSize(QSize(extra->minw, extra->minh));
1434 extra->topextra->window->setMaximumSize(QSize(extra->maxw, extra->maxh));
1435 if (extra->topextra->opacity != 255 && q->isWindow())
1436 extra->topextra->window->setOpacity(qreal(extra->topextra->opacity) / qreal(255));
1438 const bool isTipLabel = q->inherits(
"QTipLabel");
1439 const bool isAlphaWidget = !isTipLabel && q->inherits(
"QAlphaWidget");
1442 const QVariant activeXNativeParentHandle = q->property(activeXNativeParentHandleProperty);
1443 if (activeXNativeParentHandle.isValid())
1444 extra->topextra->window->setProperty(activeXNativeParentHandleProperty, activeXNativeParentHandle);
1445 if (isTipLabel || isAlphaWidget)
1446 extra->topextra->window->setProperty(
"_q_windowsDropShadow", QVariant(
true));
1448 if (isTipLabel || isAlphaWidget || q->inherits(
"QRollEffect"))
1449 qt_window_private(extra->topextra->window)->setAutomaticPositionAndResizeEnabled(
false);
1451 updateIsTranslucent();
1457
1458
1459
1460
1461
1466 d->data.in_destructor =
true;
1468#if QT_CONFIG(accessibility)
1469 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing && QAccessible::isActive())
1470 QAccessibleCache::instance()->sendObjectDestroyedEvent(
this);
1473#if defined (QT_CHECK_STATE)
1474 if (Q_UNLIKELY(paintingActive()))
1475 qWarning(
"QWidget: %s (%s) deleted while being painted", className(), name());
1478#ifndef QT_NO_GESTURES
1479 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1481 for (
auto it = d->gestureContext.keyBegin(), end = d->gestureContext.keyEnd(); it != end; ++it)
1482 manager->cleanupCachedGestures(
this, *it);
1484 d->gestureContext.clear();
1489 for (
auto action : std::as_const(d->actions)) {
1490 QActionPrivate *apriv = action->d_func();
1491 apriv->associatedObjects.removeAll(
this);
1496#ifndef QT_NO_SHORTCUT
1499 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1500 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0,
this, QKeySequence());
1505 d->layout =
nullptr;
1508 d->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency);
1511#if QT_CONFIG(graphicsview)
1512 const QWidget* w =
this;
1513 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
1514 w = w->d_func()->extra->focus_proxy;
1515 QWidget *window = w->window();
1516 QWExtra *e = window ? window->d_func()->extra.get() :
nullptr ;
1517 if (!e || !e->proxyWidget || (w->parentWidget() && w->parentWidget()->d_func()->focus_child ==
this))
1524 d->setDirtyOpaqueRegion();
1526 if (isWindow() && isVisible() && internalWinId()) {
1537 }
else if (isVisible()) {
1538 qApp->d_func()->sendSyntheticEnterLeave(
this);
1541 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
1542 repaintManager->removeDirtyWidget(
this);
1543 if (testAttribute(Qt::WA_StaticContents))
1544 repaintManager->removeStaticWidget(
this);
1547 delete d->needsFlush;
1548 d->needsFlush =
nullptr;
1552 bool blocked = d->blockSig;
1555 if (d->isSignalConnected(0)) {
1557 emit destroyed(
this);
1561 qWarning(
"Detected an unexpected exception in ~QWidget while emitting destroyed().");
1566 if (d->declarativeData) {
1567 d->wasDeleted =
true;
1568 if (QAbstractDeclarativeData::destroyed)
1569 QAbstractDeclarativeData::destroyed(d->declarativeData,
this);
1570 d->declarativeData =
nullptr;
1571 d->wasDeleted =
false;
1574 d->blockSig = blocked;
1576 if (!d->children.isEmpty())
1577 d->deleteChildren();
1579 QCoreApplication::removePostedEvents(
this);
1586 --QWidgetPrivate::instanceCounter;
1588 if (QWidgetPrivate::allWidgets)
1589 QWidgetPrivate::allWidgets->remove(
this);
1592 QEvent e(QEvent::Destroy);
1593 QCoreApplication::sendEvent(
this, &e);
1594 } QT_CATCH(
const std::exception&) {
1598#if QT_CONFIG(graphicseffect)
1599 delete d->graphicsEffect;
1603 d->isWidget =
false;
1606int QWidgetPrivate::instanceCounter = 0;
1607int QWidgetPrivate::maxInstances = 0;
1609void QWidgetPrivate::setWinId(WId id)
1612 if (mapper && data.winid) {
1613 mapper->remove(data.winid);
1616 const WId oldWinId = data.winid;
1620 mapper->insert(data.winid, q);
1623 if (oldWinId != id) {
1624 QEvent e(QEvent::WinIdChange);
1625 QCoreApplication::sendEvent(q, &e);
1629void QWidgetPrivate::createTLExtra()
1633 if (!extra->topextra) {
1634 extra->topextra = std::make_unique<QTLWExtra>();
1635 QTLWExtra* x = extra->topextra.get();
1636 x->backingStore =
nullptr;
1637 x->sharedPainter =
nullptr;
1638 x->incw = x->inch = 0;
1639 x->basew = x->baseh = 0;
1640 x->frameStrut.setCoords(0, 0, 0, 0);
1641 x->normalGeometry = QRect(0,0,-1,-1);
1642 x->savedFlags = { };
1644 x->posIncludesFrame = 0;
1645 x->sizeAdjusted =
false;
1647 x->explicitContentsMarginsRespectsSafeArea = 0;
1648 x->window =
nullptr;
1649 x->initialScreen =
nullptr;
1651#ifdef QWIDGET_EXTRA_DEBUG
1652 static int count = 0;
1653 qDebug() <<
"tlextra" << ++count;
1659
1660
1661
1663void QWidgetPrivate::createExtra()
1666 extra = std::make_unique<QWExtra>();
1667 extra->glContext =
nullptr;
1668#if QT_CONFIG(graphicsview)
1669 extra->proxyWidget =
nullptr;
1675 extra->customDpiX = 0;
1676 extra->customDpiY = 0;
1677 extra->explicitMinSize = 0;
1678 extra->explicitMaxSize = 0;
1679 extra->autoFillBackground = 0;
1680 extra->nativeChildrenForced = 0;
1681 extra->inRenderWithPainter = 0;
1682 extra->hasWindowContainer =
false;
1685#ifdef QWIDGET_EXTRA_DEBUG
1686 static int count = 0;
1687 qDebug() <<
"extra" << ++count;
1692void QWidgetPrivate::createSysExtra()
1697
1698
1699
1701void QWidgetPrivate::deleteExtra()
1705#if QT_CONFIG(style_stylesheet)
1707 if (QStyleSheetStyle *proxy = qt_styleSheet(extra->style))
1710 if (extra->topextra)
1718void QWidgetPrivate::deleteSysExtra()
1722void QWidgetPrivate::deleteTLSysExtra()
1725 if (extra && extra->topextra) {
1726 if (extra->hasWindowContainer)
1727 QWindowContainer::toplevelAboutToBeDestroyed(q);
1729 delete extra->topextra->window;
1730 extra->topextra->window =
nullptr;
1735
1736
1737
1739QRegion QWidgetPrivate::overlappedRegion(
const QRect &rect,
bool breakAfterFirst)
const
1743 const QWidget *w = q;
1750 QWidgetPrivate *pd = w->parentWidget()->d_func();
1752 for (
int i = 0; i < pd->children.size(); ++i) {
1753 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
1754 if (!sibling || !sibling->isVisible() || sibling->isWindow())
1757 above = (sibling == w);
1761 const QRect siblingRect = sibling->d_func()->effectiveRectFor(sibling->data->crect);
1762 if (qRectIntersects(siblingRect, r)) {
1763 const auto &siblingExtra = sibling->d_func()->extra;
1764 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
1765 && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
1768 region += siblingRect.translated(-p);
1769 if (breakAfterFirst)
1773 w = w->parentWidget();
1774 r.translate(pd->data.crect.topLeft());
1775 p += pd->data.crect.topLeft();
1780void QWidgetPrivate::syncBackingStore()
1782 if (shouldPaintOnScreen()) {
1783 paintOnScreen(dirty);
1785 }
else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1786 repaintManager->sync();
1790void QWidgetPrivate::syncBackingStore(
const QRegion ®ion)
1792 if (shouldPaintOnScreen())
1793 paintOnScreen(region);
1794 else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1795 repaintManager->sync(q_func(), region);
1799void QWidgetPrivate::paintOnScreen(
const QRegion &rgn)
1801 if (data.in_destructor)
1804 if (shouldDiscardSyncRequest())
1808 if (q->testAttribute(Qt::WA_StaticContents)) {
1811 extra->staticContentsSize = data.crect.size();
1814 QPaintEngine *engine = q->paintEngine();
1819 const bool noPartialUpdateSupport = (engine && (engine->type() == QPaintEngine::OpenGL
1820 || engine->type() == QPaintEngine::OpenGL2))
1821 && (usesDoubleBufferedGLContext || q->autoFillBackground());
1822 QRegion toBePainted(noPartialUpdateSupport ? q->rect() : rgn);
1824 toBePainted &= clipRect();
1825 clipToEffectiveMask(toBePainted);
1826 if (toBePainted.isEmpty())
1829 drawWidget(q, toBePainted, QPoint(), QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen,
nullptr);
1831 if (Q_UNLIKELY(q->paintingActive()))
1832 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
1835void QWidgetPrivate::setUpdatesEnabled_helper(
bool enable)
1839 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
1842 if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
1845 q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
1849 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
1850 for (
int i = 0; i < children.size(); ++i) {
1851 QWidget *w = qobject_cast<QWidget *>(children.at(i));
1852 if (w && !w->isWindow() && !w->testAttribute(attribute))
1853 w->d_func()->setUpdatesEnabled_helper(enable);
1858
1859
1860
1861
1862
1863
1864void QWidgetPrivate::propagatePaletteChange()
1868#if QT_CONFIG(graphicsview)
1869 if (!q->parentWidget() && extra && extra->proxyWidget) {
1870 QGraphicsProxyWidget *p = extra->proxyWidget;
1871 inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolveMask();
1874 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
1875 inheritedPaletteResolveMask = 0;
1878 directPaletteResolveMask = data.pal.resolveMask();
1879 auto mask = directPaletteResolveMask | inheritedPaletteResolveMask;
1881 const bool useStyleSheetPropagationInWidgetStyles =
1882 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
1884 QEvent pc(QEvent::PaletteChange);
1885 QCoreApplication::sendEvent(q, &pc);
1886 for (
int i = 0; i < children.size(); ++i) {
1887 QWidget *w = qobject_cast<QWidget*>(children.at(i));
1888 if (w && (!w->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
1889 && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
1890 QWidgetPrivate *wd = w->d_func();
1891 wd->inheritedPaletteResolveMask = mask;
1892 wd->resolvePalette();
1898
1899
1900QRect QWidgetPrivate::clipRect()
const
1903 const QWidget * w = q;
1904 if (!w->isVisible())
1906 QRect r = effectiveRectFor(q->rect());
1912 && w->parentWidget()) {
1915 w = w->parentWidget();
1916 r &= QRect(ox, oy, w->width(), w->height());
1922
1923
1924QRegion QWidgetPrivate::clipRegion()
const
1927 if (!q->isVisible())
1929 QRegion r(q->rect());
1930 const QWidget * w = q;
1931 const QWidget *ignoreUpTo;
1937 && w->parentWidget()) {
1941 w = w->parentWidget();
1942 r &= QRegion(ox, oy, w->width(), w->height());
1945 while(w->d_func()->children.at(i++) !=
static_cast<
const QObject *>(ignoreUpTo))
1947 for ( ; i < w->d_func()->children.size(); ++i) {
1948 if (QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
1949 if (sibling->isVisible() && !sibling->isWindow()) {
1950 QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
1951 sibling->width(), sibling->height());
1952 if (qRectIntersects(siblingRect, q->rect()))
1953 r -= QRegion(siblingRect);
1961void QWidgetPrivate::setSystemClip(QPaintEngine *paintEngine, qreal devicePixelRatio,
const QRegion ®ion)
1964 QTransform scaleTransform;
1965 scaleTransform.scale(devicePixelRatio, devicePixelRatio);
1967 paintEngine->d_func()->baseSystemClip = region;
1968 paintEngine->d_func()->setSystemTransform(scaleTransform);
1972#if QT_CONFIG(graphicseffect)
1973void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
1978 if (w->graphicsEffect()) {
1979 QWidgetEffectSourcePrivate *sourced =
1980 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
1981 if (!sourced->updateDueToGraphicsEffect)
1982 w->graphicsEffect()->source()->d_func()->invalidateCache();
1984 w = w->parentWidget();
1989void QWidgetPrivate::setDirtyOpaqueRegion()
1993 dirtyOpaqueChildren =
true;
1995#if QT_CONFIG(graphicseffect)
1996 invalidateGraphicsEffectsRecursively();
2002 QWidget *parent = q->parentWidget();
2007 QWidgetPrivate *pd = parent->d_func();
2008 if (!pd->dirtyOpaqueChildren)
2009 pd->setDirtyOpaqueRegion();
2012const QRegion &QWidgetPrivate::getOpaqueChildren()
const
2014 if (!dirtyOpaqueChildren)
2015 return opaqueChildren;
2017 QWidgetPrivate *that =
const_cast<QWidgetPrivate*>(
this);
2018 that->opaqueChildren = QRegion();
2020 for (
int i = 0; i < children.size(); ++i) {
2021 QWidget *child = qobject_cast<QWidget *>(children.at(i));
2022 if (!child || !child->isVisible() || child->isWindow())
2025 const QPoint offset = child->geometry().topLeft();
2026 QWidgetPrivate *childd = child->d_func();
2027 QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
2028 if (childd->extra && childd->extra->hasMask)
2029 r &= childd->extra->mask;
2032 r.translate(offset);
2033 that->opaqueChildren += r;
2036 that->opaqueChildren &= q_func()->rect();
2037 that->dirtyOpaqueChildren =
false;
2039 return that->opaqueChildren;
2042void QWidgetPrivate::subtractOpaqueChildren(QRegion &source,
const QRect &clipRect)
const
2044 if (children.isEmpty() || clipRect.isEmpty())
2047 const QRegion &r = getOpaqueChildren();
2049 source -= (r & clipRect);
2053void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion,
bool *hasDirtySiblingsAbove,
2054 bool alsoNonOpaque)
const
2057 static int disableSubtractOpaqueSiblings = qEnvironmentVariableIntValue(
"QT_NO_SUBTRACTOPAQUESIBLINGS");
2058 if (disableSubtractOpaqueSiblings || q->isWindow())
2061 QRect clipBoundingRect;
2062 bool dirtyClipBoundingRect =
true;
2065 bool dirtyParentClip =
true;
2067 QPoint parentOffset = data.crect.topLeft();
2069 const QWidget *w = q;
2074 QWidgetPrivate *pd = w->parentWidget()->d_func();
2075 const int myIndex = pd->children.indexOf(
const_cast<QWidget *>(w));
2076 const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
2077 for (
int i = myIndex + 1; i < pd->children.size(); ++i) {
2078 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
2079 if (!sibling || !sibling->isVisible() || sibling->isWindow())
2082 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2083 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2086 if (dirtyClipBoundingRect) {
2087 clipBoundingRect = sourceRegion.boundingRect();
2088 dirtyClipBoundingRect =
false;
2091 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2094 if (dirtyParentClip) {
2095 parentClip = sourceRegion.translated(parentOffset);
2096 dirtyParentClip =
false;
2099 const QPoint siblingPos(sibling->data->crect.topLeft());
2100 const QRect siblingClipRect(sibling->d_func()->clipRect());
2101 QRegion siblingDirty(parentClip);
2102 siblingDirty &= (siblingClipRect.translated(siblingPos));
2103 const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
2104 && !sibling->d_func()->graphicsEffect;
2106 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2107 if (siblingDirty.isEmpty())
2110 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2112 siblingDirty.translate(-parentOffset);
2113 sourceRegion -= siblingDirty;
2115 sourceRegion -= siblingGeometry.translated(-parentOffset);
2118 if (hasDirtySiblingsAbove)
2119 *hasDirtySiblingsAbove =
true;
2120 if (sibling->d_func()->children.isEmpty())
2122 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2123 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2124 sourceRegion -= opaqueSiblingChildren;
2126 if (sourceRegion.isEmpty())
2129 dirtyClipBoundingRect =
true;
2130 dirtyParentClip =
true;
2133 w = w->parentWidget();
2134 parentOffset += pd->data.crect.topLeft();
2135 dirtyParentClip =
true;
2139void QWidgetPrivate::clipToEffectiveMask(QRegion ®ion)
const
2143 const QWidget *w = q;
2146#if QT_CONFIG(graphicseffect)
2147 if (graphicsEffect && !w->isWindow()) {
2148 w = q->parentWidget();
2149 offset -= data.crect.topLeft();
2154 const QWidgetPrivate *wd = w->d_func();
2155 if (wd->extra && wd->extra->hasMask)
2156 region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
2159 offset -= wd->data.crect.topLeft();
2160 w = w->parentWidget();
2164bool QWidgetPrivate::shouldPaintOnScreen()
const
2166#if defined(QT_NO_BACKINGSTORE)
2170 if (q->testAttribute(Qt::WA_PaintOnScreen)
2171 || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
2179void QWidgetPrivate::updateIsOpaque()
2182 setDirtyOpaqueRegion();
2184#if QT_CONFIG(graphicseffect)
2185 if (graphicsEffect) {
2193 if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
2198 const QPalette &pal = q->palette();
2200 if (q->autoFillBackground()) {
2201 const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
2202 if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
2208 if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
2209 const QBrush &windowBrush = q->palette().brush(QPalette::Window);
2210 if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
2218void QWidgetPrivate::setOpaque(
bool opaque)
2220 if (isOpaque != opaque) {
2222 updateIsTranslucent();
2226void QWidgetPrivate::updateIsTranslucent()
2229 if (QWindow *window = q->windowHandle()) {
2230 QSurfaceFormat format = window->format();
2231 const int oldAlpha = format.alphaBufferSize();
2232 const int newAlpha = q->testAttribute(Qt::WA_TranslucentBackground) ? 8 : -1;
2233 if (oldAlpha != newAlpha) {
2250 if (!window->handle()) {
2251 format.setAlphaBufferSize(newAlpha);
2252 window->setFormat(format);
2258static inline void fillRegion(QPainter *painter,
const QRegion &rgn,
const QBrush &brush)
2262 if (brush.style() == Qt::TexturePattern) {
2263 const QRect rect(rgn.boundingRect());
2264 painter->setClipRegion(rgn);
2265 painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
2266 }
else if (brush.gradient()
2267 && (brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode
2268 || brush.gradient()->coordinateMode() == QGradient::ObjectMode)) {
2270 painter->setClipRegion(rgn);
2271 painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
2274 for (
const QRect &rect : rgn)
2275 painter->fillRect(rect, brush);
2279bool QWidgetPrivate::updateBrushOrigin(QPainter *painter,
const QBrush &brush)
const
2281#if QT_CONFIG(scrollarea)
2284 if (brush.style() == Qt::NoBrush || brush.style() == Qt::SolidPattern)
2286 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
2287 if (scrollArea && scrollArea->viewport() == q) {
2288 QObjectData *scrollPrivate =
static_cast<QWidget *>(scrollArea)->d_ptr.data();
2289 QAbstractScrollAreaPrivate *priv =
static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
2290 painter->setBrushOrigin(-priv->contentsOffset());
2296void QWidgetPrivate::paintBackground(QPainter *painter,
const QRegion &rgn, DrawWidgetFlags flags)
const
2300 bool brushOriginSet =
false;
2301 const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
2303 if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
2304 const QBrush bg = q->palette().brush(QPalette::Window);
2305 if (!brushOriginSet)
2306 brushOriginSet = updateBrushOrigin(painter, bg);
2307 if (!(flags & DontSetCompositionMode)) {
2309 QPainter::CompositionMode oldMode = painter->compositionMode();
2310 painter->setCompositionMode(QPainter::CompositionMode_Source);
2311 fillRegion(painter, rgn, bg);
2312 painter->setCompositionMode(oldMode);
2314 fillRegion(painter, rgn, bg);
2318 if (q->autoFillBackground()) {
2319 if (!brushOriginSet)
2320 brushOriginSet = updateBrushOrigin(painter, autoFillBrush);
2321 fillRegion(painter, rgn, autoFillBrush);
2324 if (q->testAttribute(Qt::WA_StyledBackground)) {
2325 painter->setClipRegion(rgn);
2328 q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
2333
2334
2335
2336
2337
2341void QWidgetPrivate::deactivateWidgetCleanup()
2345 if (QApplication::activeWindow() == q)
2346 QApplicationPrivate::setActiveWindow(
nullptr);
2348 if (q == qt_button_down)
2349 qt_button_down =
nullptr;
2354
2355
2356
2357
2358
2359
2360
2362QWidget *QWidget::find(WId id)
2364 return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) :
nullptr;
2370
2371
2372
2373
2374
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392WId QWidget::winId()
const
2394 if (!data->in_destructor
2395 && (!testAttribute(Qt::WA_WState_Created) || !internalWinId()))
2397 QWidget *that =
const_cast<QWidget*>(
this);
2398 that->setAttribute(Qt::WA_NativeWindow);
2399 that->d_func()->createWinId();
2400 return that->data->winid;
2405void QWidgetPrivate::createWinId()
2409 const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
2410 if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
2411 if (!q->isWindow()) {
2412 QWidget *parent = q->parentWidget();
2413 QWidgetPrivate *pd = parent->d_func();
2414 if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
2415 parent->setAttribute(Qt::WA_NativeWindow);
2416 if (!parent->internalWinId()) {
2420 for (
int i = 0; i < pd->children.size(); ++i) {
2421 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
2422 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
2423 || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
2434
2435
2436
2437
2438
2440bool QWidgetPrivate::setScreenForPoint(
const QPoint &pos)
2446 return setScreen(QGuiApplication::screenAt(pos));
2450
2451
2452
2453
2455bool QWidgetPrivate::setScreen(QScreen *screen)
2458 if (!screen || !q->isWindow())
2460 const QScreen *currentScreen = windowHandle() ? windowHandle()->screen() :
nullptr;
2461 if (currentScreen != screen) {
2462 topData()->initialScreen = screen;
2464 windowHandle()->setScreen(screen);
2471
2472
2473
2474
2476void QWidget::createWinId()
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497WId QWidget::effectiveWinId()
const
2499 const WId id = internalWinId();
2500 if (id || !testAttribute(Qt::WA_WState_Created))
2502 if (
const QWidget *realParent = nativeParentWidget())
2503 return realParent->internalWinId();
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518QWindow *QWidget::windowHandle()
const
2521 return d->windowHandle();
2525
2526
2527
2528
2529
2530
2531QScreen *QWidget::screen()
const
2534 if (
auto associatedScreen = d->associatedScreen())
2535 return associatedScreen;
2536 if (
auto topLevel = window()) {
2537 if (
auto topData = qt_widget_private(topLevel)->topData()) {
2538 if (topData->initialScreen)
2539 return topData->initialScreen;
2541 if (
auto screenByPos = QGuiApplication::screenAt(topLevel->geometry().center()))
2544 return QGuiApplication::primaryScreen();
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559void QWidget::setScreen(QScreen *screen)
2562 d->setScreen(screen);
2565#if QT_CONFIG(style_stylesheet)
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582QString QWidget::styleSheet()
const
2587 return d->extra->styleSheet;
2590void QWidget::setStyleSheet(
const QString& styleSheet)
2593 if (data->in_destructor)
2597 QStyleSheetStyle *proxy = qt_styleSheet(d->extra->style);
2598 d->extra->styleSheet = styleSheet;
2599 if (styleSheet.isEmpty()) {
2608 bool repolish = d->polished;
2610 const auto childWidgets = findChildren<QWidget*>();
2611 for (
auto child : childWidgets) {
2612 repolish = child->d_func()->polished;
2618 proxy->repolish(
this);
2622 if (testAttribute(Qt::WA_SetStyle)) {
2623 d->setStyle_helper(
new QStyleSheetStyle(d->extra->style),
true);
2625 d->setStyle_helper(
new QStyleSheetStyle(
nullptr),
true);
2632
2633
2635QStyle *QWidget::style()
const
2639 if (d->extra && d->extra->style)
2640 return d->extra->style;
2641 return QApplication::style();
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2665void QWidget::setStyle(QStyle *style)
2668 setAttribute(Qt::WA_SetStyle, style !=
nullptr);
2670#if QT_CONFIG(style_stylesheet)
2671 if (QStyleSheetStyle *styleSheetStyle = qt_styleSheet(style)) {
2674 styleSheetStyle->ref();
2675 d->setStyle_helper(style,
false);
2676 }
else if (qt_styleSheet(d->extra->style) || !qApp->styleSheet().isEmpty()) {
2678 d->setStyle_helper(
new QStyleSheetStyle(style),
true);
2681 d->setStyle_helper(style,
false);
2684void QWidgetPrivate::setStyle_helper(QStyle *newStyle,
bool propagate)
2687 QStyle *oldStyle = q->style();
2691#if QT_CONFIG(style_stylesheet)
2692 QPointer<QStyle> origStyle = extra->style;
2694 extra->style = newStyle;
2698 oldStyle->unpolish(q);
2699 q->style()->polish(q);
2704 const QObjectList childrenList = children;
2705 for (
int i = 0; i < childrenList.size(); ++i) {
2706 QWidget *c = qobject_cast<QWidget*>(childrenList.at(i));
2708 c->d_func()->inheritStyle();
2712#if QT_CONFIG(style_stylesheet)
2713 if (!qt_styleSheet(newStyle)) {
2714 if (
const QStyleSheetStyle* cssStyle = qt_styleSheet(origStyle)) {
2715 cssStyle->clearWidgetFont(q);
2720 QEvent e(QEvent::StyleChange);
2721 QCoreApplication::sendEvent(q, &e);
2723#if QT_CONFIG(style_stylesheet)
2725 if (QStyleSheetStyle *proxy = qt_styleSheet(origStyle))
2731void QWidgetPrivate::inheritStyle()
2733#if QT_CONFIG(style_stylesheet)
2736 QStyle *extraStyle = extra ? (QStyle*)extra->style :
nullptr;
2738 QStyleSheetStyle *proxy = qt_styleSheet(extraStyle);
2740 if (!q->styleSheet().isEmpty()) {
2745 if (inheritStyleRecursionGuard)
2747 inheritStyleRecursionGuard =
true;
2748 const auto resetGuard = qScopeGuard([&]() {
2749 inheritStyleRecursionGuard =
false;
2752 QStyle *origStyle = proxy ? proxy->base : extraStyle;
2753 QWidget *parent = q->parentWidget();
2754 QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style :
nullptr;
2757 if (!qApp->styleSheet().isEmpty() || qt_styleSheet(parentStyle)) {
2758 QStyle *newStyle = parentStyle;
2759 if (q->testAttribute(Qt::WA_SetStyle) && qt_styleSheet(origStyle) ==
nullptr)
2760 newStyle =
new QStyleSheetStyle(origStyle);
2761 else if (
auto *styleSheetStyle = qt_styleSheet(origStyle))
2762 newStyle = styleSheetStyle;
2763 else if (QStyleSheetStyle *newProxy = qt_styleSheet(parentStyle))
2766 setStyle_helper(newStyle,
true);
2772 if (origStyle == extraStyle)
2778 if (!q->testAttribute(Qt::WA_SetStyle))
2779 origStyle =
nullptr;
2781 setStyle_helper(origStyle,
true);
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2837Qt::WindowModality QWidget::windowModality()
const
2839 return static_cast<Qt::WindowModality>(data->window_modality);
2842void QWidget::setWindowModality(Qt::WindowModality windowModality)
2844 data->window_modality = windowModality;
2846 setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
2847 setAttribute(Qt::WA_SetWindowModality,
true);
2850void QWidgetPrivate::setModal_sys()
2853 if (q->windowHandle())
2854 q->windowHandle()->setModality(q->windowModality());
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879bool QWidget::isMinimized()
const
2880{
return data->window_state & Qt::WindowMinimized; }
2883
2884
2885
2886
2887
2888
2889
2890void QWidget::showMinimized()
2892 bool isMin = isMinimized();
2893 if (isMin && isVisible())
2899 setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919bool QWidget::isMaximized()
const
2920{
return data->window_state & Qt::WindowMaximized; }
2925
2926
2927
2928
2929
2930
2931Qt::WindowStates QWidget::windowState()
const
2933 return Qt::WindowStates(data->window_state);
2937
2938
2939
2940
2941
2942
2943void QWidget::overrideWindowState(Qt::WindowStates newstate)
2945 QWindowStateChangeEvent e(Qt::WindowStates(data->window_state),
true);
2946 data->window_state = newstate;
2947 QCoreApplication::sendEvent(
this, &e);
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980void QWidget::setWindowState(Qt::WindowStates newstate)
2983 Qt::WindowStates oldstate = windowState();
2984 if (newstate.testFlag(Qt::WindowMinimized))
2985 newstate.setFlag(Qt::WindowActive,
false);
2986 if (oldstate == newstate)
2988 if (isWindow() && !testAttribute(Qt::WA_WState_Created))
2991 data->window_state = newstate;
2992 data->in_set_window_state = 1;
2995 if (!testAttribute(Qt::WA_Resized) && !isVisible())
2999 if (!(oldstate & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)))
3000 d->topData()->normalGeometry = geometry();
3002 Q_ASSERT(windowHandle());
3003 windowHandle()->setWindowStates(newstate & ~Qt::WindowActive);
3005 data->in_set_window_state = 0;
3007 if (newstate & Qt::WindowActive)
3010 QWindowStateChangeEvent e(oldstate);
3011 QCoreApplication::sendEvent(
this, &e);
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025bool QWidget::isFullScreen()
const
3026{
return data->window_state & Qt::WindowFullScreen; }
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064void QWidget::showFullScreen()
3068 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
3069 | Qt::WindowFullScreen);
3071#if !defined Q_OS_QNX
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087void QWidget::showMaximized()
3091 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
3092 | Qt::WindowMaximized);
3097
3098
3099
3100
3101
3102
3103void QWidget::showNormal()
3107 setWindowState(windowState() & ~(Qt::WindowMinimized
3108 | Qt::WindowMaximized
3109 | Qt::WindowFullScreen));
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3133bool QWidget::isEnabledTo(
const QWidget *ancestor)
const
3135 const QWidget * w =
this;
3136 while (!w->testAttribute(Qt::WA_ForceDisabled)
3138 && w->parentWidget()
3139 && w->parentWidget() != ancestor)
3140 w = w->parentWidget();
3141 return !w->testAttribute(Qt::WA_ForceDisabled);
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160void QWidget::addAction(QAction *action)
3162 insertAction(
nullptr, action);
3166
3167
3168
3169
3170void QWidget::addActions(
const QList<QAction *> &actions)
3172 for(
int i = 0; i < actions.size(); i++)
3173 insertAction(
nullptr, actions.at(i));
3177
3178
3179
3180
3181
3182
3183
3184
3185void QWidget::insertAction(QAction *before, QAction *action)
3187 if (Q_UNLIKELY(!action)) {
3188 qWarning(
"QWidget::insertAction: Attempt to insert null action");
3193 if (d->actions.contains(action))
3194 removeAction(action);
3196 int pos = d->actions.indexOf(before);
3199 pos = d->actions.size();
3201 d->actions.insert(pos, action);
3203 QActionPrivate *apriv = action->d_func();
3204 apriv->associatedObjects.append(
this);
3206 QActionEvent e(QEvent::ActionAdded, action, before);
3207 QCoreApplication::sendEvent(
this, &e);
3211
3212
3213
3214
3215
3216
3217
3218
3219void QWidget::insertActions(QAction *before,
const QList<QAction*> &actions)
3221 for(
int i = 0; i < actions.size(); ++i)
3222 insertAction(before, actions.at(i));
3226
3227
3228
3229void QWidget::removeAction(QAction *action)
3236 QActionPrivate *apriv = action->d_func();
3237 apriv->associatedObjects.removeAll(
this);
3239 if (d->actions.removeAll(action)) {
3240 QActionEvent e(QEvent::ActionRemoved, action);
3241 QCoreApplication::sendEvent(
this, &e);
3246
3247
3248
3249
3250QList<QAction*> QWidget::actions()
const
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272QAction *QWidget::addAction(
const QString &text)
3274 QAction *ret =
new QAction(text,
this);
3279QAction *QWidget::addAction(
const QIcon &icon,
const QString &text)
3281 QAction *ret =
new QAction(icon, text,
this);
3286#if QT_CONFIG(shortcut)
3287QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut)
3289 QAction *ret = addAction(text);
3290 ret->setShortcut(shortcut);
3294QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut)
3296 QAction *ret = addAction(icon, text);
3297 ret->setShortcut(shortcut);
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320QAction *QWidget::addAction(
const QString &text,
const QObject *receiver,
const char* member,
3321 Qt::ConnectionType type)
3323 QAction *action = addAction(text);
3324 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3328QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
3329 const QObject *receiver,
const char* member,
3330 Qt::ConnectionType type)
3332 QAction *action = addAction(icon, text);
3333 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3337#if QT_CONFIG(shortcut)
3338QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut,
3339 const QObject *receiver,
const char* member,
3340 Qt::ConnectionType type)
3342 QAction *action = addAction(text, receiver, member, type);
3343 action->setShortcut(shortcut);
3347QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut,
3348 const QObject *receiver,
const char* member,
3349 Qt::ConnectionType type)
3351 QAction *action = addAction(icon, text, receiver, member, type);
3352 action->setShortcut(shortcut);
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401void QWidget::setEnabled(
bool enable)
3405#if QT_CONFIG(accessibility)
3406 const bool wasEnabled = !testAttribute(Qt::WA_ForceDisabled);
3409 setAttribute(Qt::WA_ForceDisabled, !enable);
3410 d->setEnabled_helper(enable);
3412#if QT_CONFIG(accessibility)
3416 if (QAccessible::isActive() && wasEnabled != enable) {
3417 QAccessible::State states;
3418 states.disabled = 1;
3419 QAccessibleStateChangeEvent scEvent(
this, states);
3420 QAccessible::updateAccessibility(&scEvent);
3425void QWidgetPrivate::setEnabled_helper(
bool enable)
3429 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
3432 if (enable != q->testAttribute(Qt::WA_Disabled))
3435 q->setAttribute(Qt::WA_Disabled, !enable);
3436 updateSystemBackground();
3438 if (!enable && q->window()->focusWidget() == q) {
3439 bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
3440 if (!parentIsEnabled || !q->focusNextChild())
3444 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
3445 for (
int i = 0; i < children.size(); ++i) {
3446 QWidget *w = qobject_cast<QWidget *>(children.at(i));
3447 if (w && !w->testAttribute(attribute))
3448 w->d_func()->setEnabled_helper(enable);
3451 if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
3454 qt_qpa_set_cursor(q,
false);
3458 if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
3459 QWidget *focusWidget = effectiveFocusWidget();
3462 if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
3463 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3465 QGuiApplication::inputMethod()->commit();
3466 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3470 QEvent e(QEvent::EnabledChange);
3471 QCoreApplication::sendEvent(q, &e);
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487bool QWidget::acceptDrops()
const
3489 return testAttribute(Qt::WA_AcceptDrops);
3492void QWidget::setAcceptDrops(
bool on)
3494 setAttribute(Qt::WA_AcceptDrops, on);
3499
3500
3501
3502
3503
3504
3505
3506void QWidget::setDisabled(
bool disable)
3508 setEnabled(!disable);
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524QRect QWidget::frameGeometry()
const
3527 if (isWindow() && ! (windowType() == Qt::Popup)) {
3528 QRect fs = d->frameStrut();
3529 return QRect(data->crect.x() - fs.left(),
3530 data->crect.y() - fs.top(),
3531 data->crect.width() + fs.left() + fs.right(),
3532 data->crect.height() + fs.top() + fs.bottom());
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550int QWidget::x()
const
3553 if (isWindow() && ! (windowType() == Qt::Popup))
3554 return data->crect.x() - d->frameStrut().left();
3555 return data->crect.x();
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570int QWidget::y()
const
3573 if (isWindow() && ! (windowType() == Qt::Popup))
3574 return data->crect.y() - d->frameStrut().top();
3575 return data->crect.y();
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605QPoint QWidget::pos()
const
3608 QPoint result = data->crect.topLeft();
3609 if (isWindow() && ! (windowType() == Qt::Popup))
3610 if (!d->maybeTopData() || !d->maybeTopData()->posIncludesFrame)
3611 result -= d->frameStrut().topLeft();
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724QRect QWidget::normalGeometry()
const
3730 if (!isMaximized() && !isFullScreen())
3733 return d->topData()->normalGeometry;
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3749QRect QWidget::childrenRect()
const
3752 QRect r(0, 0, 0, 0);
3753 for (
int i = 0; i < d->children.size(); ++i) {
3754 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3755 if (w && !w->isWindow() && !w->isHidden())
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3773QRegion QWidget::childrenRegion()
const
3777 for (
int i = 0; i < d->children.size(); ++i) {
3778 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3779 if (w && !w->isWindow() && !w->isHidden()) {
3780 QRegion mask = w->mask();
3784 r |= mask.translated(w->pos());
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3808QSize QWidget::minimumSize()
const
3811 return d->extra ? QSize(d->extra->minw, d->extra->minh) : QSize(0, 0);
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3830QSize QWidget::maximumSize()
const
3833 return d->extra ? QSize(d->extra->maxw, d->extra->maxh)
3839
3840
3841
3842
3843
3844
3845
3846
3847
3850
3851
3852
3853
3854
3855
3856
3857
3858
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909QSize QWidget::sizeIncrement()
const
3912 return (d->extra && d->extra->topextra)
3913 ? QSize(d->extra->topextra->incw, d->extra->topextra->inch)
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3930QSize QWidget::baseSize()
const
3933 return (d->extra && d->extra->topextra)
3934 ? QSize(d->extra->topextra->basew, d->extra->topextra->baseh)
3938bool QWidgetPrivate::setMinimumSize_helper(
int &minw,
int &minh)
3942 int mw = minw, mh = minh;
3948 qWarning(
"QWidget::setMinimumSize: (%s/%s) "
3949 "The largest allowed size is (%d,%d)",
3950 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
3955 if (Q_UNLIKELY(minw < 0 || minh < 0)) {
3956 qWarning(
"QWidget::setMinimumSize: (%s/%s) Negative sizes (%d,%d) "
3958 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), minw, minh);
3959 minw = mw = qMax(minw, 0);
3960 minh = mh = qMax(minh, 0);
3963 if (extra->minw == mw && extra->minh == mh)
3967 extra->explicitMinSize = (mw ? Qt::Horizontal : 0) | (mh ? Qt::Vertical : 0);
3971void QWidgetPrivate::setConstraints_sys()
3974 if (extra && q->windowHandle()) {
3975 QWindow *win = q->windowHandle();
3976 QWindowPrivate *winp = qt_window_private(win);
3978 winp->minimumSize = QSize(extra->minw, extra->minh);
3979 winp->maximumSize = QSize(extra->maxw, extra->maxh);
3981 if (extra->topextra) {
3982 winp->baseSize = QSize(extra->topextra->basew, extra->topextra->baseh);
3983 winp->sizeIncrement = QSize(extra->topextra->incw, extra->topextra->inch);
3986 if (winp->platformWindow) {
3987 fixPosIncludesFrame();
3988 winp->platformWindow->propagateSizeHints();
3994
3995
3996
3997
3998
3999
4001void QWidget::setMinimumSize(
int minw,
int minh)
4004 if (!d->setMinimumSize_helper(minw, minh))
4008 d->setConstraints_sys();
4009 if (minw > width() || minh > height()) {
4010 bool resized = testAttribute(Qt::WA_Resized);
4011 bool maximized = isMaximized();
4012 resize(qMax(minw,width()), qMax(minh,height()));
4013 setAttribute(Qt::WA_Resized, resized);
4015 data->window_state = data->window_state | Qt::WindowMaximized;
4017#if QT_CONFIG(graphicsview)
4019 if (d->extra->proxyWidget)
4020 d->extra->proxyWidget->setMinimumSize(minw, minh);
4023 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4026bool QWidgetPrivate::setMaximumSize_helper(
int &maxw,
int &maxh)
4030 qWarning(
"QWidget::setMaximumSize: (%s/%s) "
4031 "The largest allowed size is (%d,%d)",
4032 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
4037 if (Q_UNLIKELY(maxw < 0 || maxh < 0)) {
4038 qWarning(
"QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) "
4040 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), maxw, maxh);
4041 maxw = qMax(maxw, 0);
4042 maxh = qMax(maxh, 0);
4045 if (extra->maxw == maxw && extra->maxh == maxh)
4049 extra->explicitMaxSize = (maxw !=
QWIDGETSIZE_MAX ? Qt::Horizontal : 0) |
4055
4056
4057
4058
4059
4060
4061void QWidget::setMaximumSize(
int maxw,
int maxh)
4064 if (!d->setMaximumSize_helper(maxw, maxh))
4068 d->setConstraints_sys();
4069 if (maxw < width() || maxh < height()) {
4070 bool resized = testAttribute(Qt::WA_Resized);
4071 resize(qMin(maxw,width()), qMin(maxh,height()));
4072 setAttribute(Qt::WA_Resized, resized);
4075#if QT_CONFIG(graphicsview)
4077 if (d->extra->proxyWidget)
4078 d->extra->proxyWidget->setMaximumSize(maxw, maxh);
4082 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4086
4087
4088
4089
4090
4091void QWidget::setSizeIncrement(
int w,
int h)
4095 QTLWExtra* x = d->topData();
4096 if (x->incw == w && x->inch == h)
4101 d->setConstraints_sys();
4105
4106
4107
4108
4109
4110void QWidget::setBaseSize(
int basew,
int baseh)
4114 QTLWExtra* x = d->topData();
4115 if (x->basew == basew && x->baseh == baseh)
4120 d->setConstraints_sys();
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4138void QWidget::setFixedSize(
const QSize & s)
4140 setFixedSize(s.width(), s.height());
4145
4146
4147
4148
4149
4151void QWidget::setFixedSize(
int w,
int h)
4154 bool minSizeSet = d->setMinimumSize_helper(w, h);
4155 bool maxSizeSet = d->setMaximumSize_helper(w, h);
4156 if (!minSizeSet && !maxSizeSet)
4160 d->setConstraints_sys();
4162 d->updateGeometry_helper(
true);
4168void QWidget::setMinimumWidth(
int w)
4172 uint expl = d->extra->explicitMinSize | (w ? Qt::Horizontal : 0);
4173 setMinimumSize(w, minimumSize().height());
4174 d->extra->explicitMinSize = expl;
4177void QWidget::setMinimumHeight(
int h)
4181 uint expl = d->extra->explicitMinSize | (h ? Qt::Vertical : 0);
4182 setMinimumSize(minimumSize().width(), h);
4183 d->extra->explicitMinSize = expl;
4186void QWidget::setMaximumWidth(
int w)
4190 uint expl = d->extra->explicitMaxSize | (w ==
QWIDGETSIZE_MAX ? 0 : Qt::Horizontal);
4191 setMaximumSize(w, maximumSize().height());
4192 d->extra->explicitMaxSize = expl;
4195void QWidget::setMaximumHeight(
int h)
4199 uint expl = d->extra->explicitMaxSize | (h ==
QWIDGETSIZE_MAX ? 0 : Qt::Vertical);
4200 setMaximumSize(maximumSize().width(), h);
4201 d->extra->explicitMaxSize = expl;
4205
4206
4207
4208
4209
4211void QWidget::setFixedWidth(
int w)
4215 uint explMin = d->extra->explicitMinSize | Qt::Horizontal;
4216 uint explMax = d->extra->explicitMaxSize | Qt::Horizontal;
4217 setMinimumSize(w, minimumSize().height());
4218 setMaximumSize(w, maximumSize().height());
4219 d->extra->explicitMinSize = explMin;
4220 d->extra->explicitMaxSize = explMax;
4225
4226
4227
4228
4229
4231void QWidget::setFixedHeight(
int h)
4235 uint explMin = d->extra->explicitMinSize | Qt::Vertical;
4236 uint explMax = d->extra->explicitMaxSize | Qt::Vertical;
4237 setMinimumSize(minimumSize().width(), h);
4238 setMaximumSize(maximumSize().width(), h);
4239 d->extra->explicitMinSize = explMin;
4240 d->extra->explicitMaxSize = explMax;
4245
4246
4247
4248
4249
4250
4251
4253QPointF QWidget::mapTo(
const QWidget *parent,
const QPointF &pos)
const
4257 const QWidget * w =
this;
4258 while (w != parent) {
4259 p = w->mapToParent(p);
4260 w = w->parentWidget();
4262 qWarning(
"QWidget::mapTo(): parent must be in parent hierarchy");
4271
4272
4273QPoint QWidget::mapTo(
const QWidget *parent,
const QPoint &pos)
const
4275 return mapTo(parent, QPointF(pos)).toPoint();
4279
4280
4281
4282
4283
4284
4285
4287QPointF QWidget::mapFrom(
const QWidget *parent,
const QPointF &pos)
const
4291 const QWidget * w =
this;
4292 while (w != parent) {
4293 p = w->mapFromParent(p);
4294 w = w->parentWidget();
4296 qWarning(
"QWidget::mapFrom(): parent must be in parent hierarchy");
4305
4306
4307QPoint QWidget::mapFrom(
const QWidget *parent,
const QPoint &pos)
const
4309 return mapFrom(parent, QPointF(pos)).toPoint();
4313
4314
4315
4316
4317
4318
4319
4320
4322QPointF QWidget::mapToParent(
const QPointF &pos)
const
4324 return pos + QPointF(data->crect.topLeft());
4328
4329
4330QPoint QWidget::mapToParent(
const QPoint &pos)
const
4332 return pos + data->crect.topLeft();
4336
4337
4338
4339
4340
4341
4342
4343
4345QPointF QWidget::mapFromParent(
const QPointF &pos)
const
4347 return pos - QPointF(data->crect.topLeft());
4351
4352
4353QPoint QWidget::mapFromParent(
const QPoint &pos)
const
4355 return pos - data->crect.topLeft();
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4372QWidget *QWidget::window()
const
4374 QWidget *w =
const_cast<QWidget *>(
this);
4375 QWidget *p = w->parentWidget();
4376 while (!w->isWindow() && p) {
4378 p = p->parentWidget();
4384
4385
4386
4387
4388
4389
4390
4391
4392QWidget *QWidget::nativeParentWidget()
const
4394 QWidget *parent = parentWidget();
4395 while (parent && !parent->internalWinId())
4396 parent = parent->parentWidget();
4401
4402
4403
4404
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419QPalette::ColorRole QWidget::backgroundRole()
const
4422 const QWidget *w =
this;
4424 QPalette::ColorRole role = w->d_func()->bg_role;
4425 if (role != QPalette::NoRole)
4427 if (w->isWindow() || w->windowType() == Qt::SubWindow)
4429 w = w->parentWidget();
4431 return QPalette::Window;
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4450void QWidget::setBackgroundRole(QPalette::ColorRole role)
4454 d->updateSystemBackground();
4455 d->propagatePaletteChange();
4456 d->updateIsOpaque();
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470QPalette::ColorRole QWidget::foregroundRole()
const
4473 QPalette::ColorRole rl = QPalette::ColorRole(d->fg_role);
4474 if (rl != QPalette::NoRole)
4476 QPalette::ColorRole role = QPalette::WindowText;
4477 switch (backgroundRole()) {
4478 case QPalette::Button:
4479 role = QPalette::ButtonText;
4481 case QPalette::Base:
4482 role = QPalette::Text;
4484 case QPalette::Dark:
4485 case QPalette::Shadow:
4486 role = QPalette::Light;
4488 case QPalette::Highlight:
4489 role = QPalette::HighlightedText;
4491 case QPalette::ToolTipBase:
4492 role = QPalette::ToolTipText;
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515void QWidget::setForegroundRole(QPalette::ColorRole role)
4519 d->updateSystemBackground();
4520 d->propagatePaletteChange();
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573const QPalette &QWidget::palette()
const
4576 data->pal.setCurrentColorGroup(QPalette::Disabled);
4577 }
else if ((!isVisible() || isActiveWindow())
4578#if defined(Q_OS_WIN)
4579 && !QApplicationPrivate::isBlockedByModal(
const_cast<QWidget *>(
this))
4582 data->pal.setCurrentColorGroup(QPalette::Active);
4584 data->pal.setCurrentColorGroup(QPalette::Inactive);
4589void QWidget::setPalette(
const QPalette &palette)
4592 setAttribute(Qt::WA_SetPalette, palette.resolveMask() != 0);
4598 QPalette naturalPalette = d->naturalWidgetPalette(d->inheritedPaletteResolveMask);
4599 QPalette resolvedPalette = palette.resolve(naturalPalette);
4600 d->setPalette_helper(resolvedPalette);
4604
4605
4606
4607
4608
4609
4610
4611
4612QPalette QWidgetPrivate::naturalWidgetPalette(QPalette::ResolveMask inheritedMask)
const
4616 const bool useStyleSheetPropagationInWidgetStyles =
4617 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4619 QPalette naturalPalette = QApplication::palette(q);
4620 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4621 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4622#if QT_CONFIG(graphicsview)
4623 || (extra && extra->proxyWidget)
4626 if (QWidget *p = q->parentWidget()) {
4627 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4628 if (!naturalPalette.isCopyOf(QGuiApplication::palette())) {
4629 QPalette inheritedPalette = p->palette();
4630 inheritedPalette.setResolveMask(inheritedMask);
4631 naturalPalette = inheritedPalette.resolve(naturalPalette);
4633 naturalPalette = p->palette();
4637#if QT_CONFIG(graphicsview)
4638 else if (extra && extra->proxyWidget) {
4639 QPalette inheritedPalette = extra->proxyWidget->palette();
4640 inheritedPalette.setResolveMask(inheritedMask);
4641 naturalPalette = inheritedPalette.resolve(naturalPalette);
4645 naturalPalette.setResolveMask(0);
4646 return naturalPalette;
4649
4650
4651
4652
4653
4654
4655
4656void QWidgetPrivate::resolvePalette()
4658 QPalette naturalPalette = naturalWidgetPalette(inheritedPaletteResolveMask);
4659 QPalette resolvedPalette = data.pal.resolve(naturalPalette);
4660 setPalette_helper(resolvedPalette);
4663void QWidgetPrivate::setPalette_helper(
const QPalette &palette)
4666 if (data.pal == palette && data.pal.resolveMask() == palette.resolveMask())
4669 updateSystemBackground();
4670 propagatePaletteChange();
4676void QWidgetPrivate::updateSystemBackground()
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4726void QWidget::setFont(
const QFont &font)
4730#if QT_CONFIG(style_stylesheet)
4731 const QStyleSheetStyle* style;
4732 if (d->extra && (style = qt_styleSheet(d->extra->style)))
4733 style->saveWidgetFont(
this, font);
4736 setAttribute(Qt::WA_SetFont, font.resolveMask() != 0);
4742 QFont naturalFont = d->naturalWidgetFont(d->inheritedFontResolveMask);
4743 QFont resolvedFont = font.resolve(naturalFont);
4744 d->setFont_helper(resolvedFont);
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759QFont QWidgetPrivate::naturalWidgetFont(uint inheritedMask)
const
4763 const bool useStyleSheetPropagationInWidgetStyles =
4764 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4766 QFont naturalFont = QApplication::font(q);
4767 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4768 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4769#if QT_CONFIG(graphicsview)
4770 || (extra && extra->proxyWidget)
4773 if (QWidget *p = q->parentWidget()) {
4774 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4775 if (!naturalFont.isCopyOf(QApplication::font())) {
4776 if (inheritedMask != 0) {
4777 QFont inheritedFont = p->font();
4778 inheritedFont.setResolveMask(inheritedMask);
4779 naturalFont = inheritedFont.resolve(naturalFont);
4782 naturalFont = p->font();
4786#if QT_CONFIG(graphicsview)
4787 else if (extra && extra->proxyWidget) {
4788 if (inheritedMask != 0) {
4789 QFont inheritedFont = extra->proxyWidget->font();
4790 inheritedFont.setResolveMask(inheritedMask);
4791 naturalFont = inheritedFont.resolve(naturalFont);
4796 naturalFont.setResolveMask(0);
4801
4802
4803
4804
4805QFont QWidgetPrivate::localFont()
const
4807 QFont localfont = data.fnt;
4808 localfont.setResolveMask(directFontResolveMask);
4813
4814
4815
4816
4817
4818
4819
4820void QWidgetPrivate::resolveFont()
4822 QFont naturalFont = naturalWidgetFont(inheritedFontResolveMask);
4823 QFont resolvedFont = localFont().resolve(naturalFont);
4824 setFont_helper(resolvedFont);
4828
4829
4830
4831
4832
4833
4834
4835
4836void QWidgetPrivate::updateFont(
const QFont &font)
4839#if QT_CONFIG(style_stylesheet)
4840 const QStyleSheetStyle* cssStyle;
4841 cssStyle = extra ? qt_styleSheet(extra->style) :
nullptr;
4842 const bool useStyleSheetPropagationInWidgetStyles =
4843 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4846 data.fnt = QFont(font, q);
4849#if QT_CONFIG(graphicsview)
4850 if (!q->parentWidget() && extra && extra->proxyWidget) {
4851 QGraphicsProxyWidget *p = extra->proxyWidget;
4852 inheritedFontResolveMask = p->d_func()->inheritedFontResolveMask | p->font().resolveMask();
4855 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
4856 inheritedFontResolveMask = 0;
4858 uint newMask = data.fnt.resolveMask() | inheritedFontResolveMask;
4862 directFontResolveMask = data.fnt.resolveMask();
4863 data.fnt.setResolveMask(newMask);
4865 for (
int i = 0; i < children.size(); ++i) {
4866 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4869#if QT_CONFIG(style_stylesheet)
4870 }
else if (!useStyleSheetPropagationInWidgetStyles && w->testAttribute(Qt::WA_StyleSheet)) {
4873 cssStyle->updateStyleSheetFont(w);
4875 }
else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
4877 QWidgetPrivate *wd = w->d_func();
4878 wd->inheritedFontResolveMask = newMask;
4884#if QT_CONFIG(style_stylesheet)
4885 if (!useStyleSheetPropagationInWidgetStyles && cssStyle) {
4886 cssStyle->updateStyleSheetFont(q);
4890 QEvent e(QEvent::FontChange);
4891 QCoreApplication::sendEvent(q, &e);
4894void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
4898 if ( (direction == Qt::RightToLeft) == q->testAttribute(Qt::WA_RightToLeft))
4900 q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
4901 if (!children.isEmpty()) {
4902 for (
int i = 0; i < children.size(); ++i) {
4903 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4904 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_SetLayoutDirection))
4905 w->d_func()->setLayoutDirection_helper(direction);
4908 QEvent e(QEvent::LayoutDirectionChange);
4909 QCoreApplication::sendEvent(q, &e);
4912void QWidgetPrivate::resolveLayoutDirection()
4915 if (!q->testAttribute(Qt::WA_SetLayoutDirection))
4916 setLayoutDirection_helper(q->isWindow() ? QGuiApplication::layoutDirection() : q->parentWidget()->layoutDirection());
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938void QWidget::setLayoutDirection(Qt::LayoutDirection direction)
4942 if (direction == Qt::LayoutDirectionAuto) {
4943 unsetLayoutDirection();
4947 setAttribute(Qt::WA_SetLayoutDirection);
4948 d->setLayoutDirection_helper(direction);
4951Qt::LayoutDirection QWidget::layoutDirection()
const
4953 return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
4956void QWidget::unsetLayoutDirection()
4959 setAttribute(Qt::WA_SetLayoutDirection,
false);
4960 d->resolveLayoutDirection();
4964
4965
4966
4967
4968
4969
4970
4973
4974
4975
4976
4977
4978
4979
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5007QCursor QWidget::cursor()
const
5010 if (testAttribute(Qt::WA_SetCursor))
5011 return (d->extra && d->extra->curs)
5013 : QCursor(Qt::ArrowCursor);
5014 if (isWindow() || !parentWidget())
5015 return QCursor(Qt::ArrowCursor);
5016 return parentWidget()->cursor();
5019void QWidget::setCursor(
const QCursor &cursor)
5022 if (cursor.shape() != Qt::ArrowCursor
5023 || (d->extra && d->extra->curs))
5026 d->extra->curs = std::make_unique<QCursor>(cursor);
5028 setAttribute(Qt::WA_SetCursor);
5029 d->setCursor_sys(cursor);
5031 QEvent event(QEvent::CursorChange);
5032 QCoreApplication::sendEvent(
this, &event);
5035void QWidgetPrivate::setCursor_sys(
const QCursor &cursor)
5039 qt_qpa_set_cursor(q,
false);
5042void QWidget::unsetCursor()
5046 d->extra->curs.reset();
5048 setAttribute(Qt::WA_SetCursor,
false);
5049 d->unsetCursor_sys();
5051 QEvent event(QEvent::CursorChange);
5052 QCoreApplication::sendEvent(
this, &event);
5055void QWidgetPrivate::unsetCursor_sys()
5058 qt_qpa_set_cursor(q,
false);
5063 if (QWindow *window = w->windowHandle())
5064 window->setCursor(c);
5069 if (QWindow *window = w->windowHandle())
5070 window->unsetCursor();
5075 if (!w->testAttribute(Qt::WA_WState_Created))
5081 }
else if (lastUnderMouse) {
5082 const WId lastWinId = lastUnderMouse->effectiveWinId();
5083 const WId winId = w->effectiveWinId();
5084 if (lastWinId && lastWinId == winId)
5086 }
else if (!w->internalWinId()) {
5090 while (!w->internalWinId() && w->parentWidget() && !w->isWindow()
5091 && !w->testAttribute(Qt::WA_SetCursor))
5092 w = w->parentWidget();
5095 if (!w->internalWinId())
5096 nativeParent = w->nativeParentWidget();
5097 if (!nativeParent || !nativeParent->internalWinId())
5100 if (w->isWindow() || w->testAttribute(Qt::WA_SetCursor)) {
5102 applyCursor(nativeParent, w->cursor());
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151void QWidget::render(QPaintDevice *target,
const QPoint &targetOffset,
5152 const QRegion &sourceRegion, RenderFlags renderFlags)
5155 render(&p, targetOffset, sourceRegion, renderFlags);
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171void QWidget::render(QPainter *painter,
const QPoint &targetOffset,
5172 const QRegion &sourceRegion, RenderFlags renderFlags)
5174 if (Q_UNLIKELY(!painter)) {
5175 qWarning(
"QWidget::render: Null pointer to painter");
5179 if (Q_UNLIKELY(!painter->isActive())) {
5180 qWarning(
"QWidget::render: Cannot render with an inactive painter");
5184 const qreal opacity = painter->opacity();
5185 if (qFuzzyIsNull(opacity))
5189 const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
5190 const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
5192 if (toBePainted.isEmpty())
5197 d->extra->inRenderWithPainter =
true;
5199 QPaintEngine *engine = painter->paintEngine();
5201 QPaintEnginePrivate *enginePriv = engine->d_func();
5202 Q_ASSERT(enginePriv);
5203 QPaintDevice *target = engine->paintDevice();
5207 if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
5208 d->render_helper(painter, targetOffset, toBePainted, renderFlags);
5209 d->extra->inRenderWithPainter = inRenderWithPainter;
5214 QPainter *oldPainter = d->sharedPainter();
5215 d->setSharedPainter(painter);
5218 const QTransform oldTransform = enginePriv->systemTransform;
5219 const QRegion oldSystemClip = enginePriv->systemClip;
5220 const QRegion oldBaseClip = enginePriv->baseSystemClip;
5221 const QRegion oldSystemViewport = enginePriv->systemViewport;
5222 const Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection();
5225 if (painter->hasClipping()) {
5226 const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
5227 enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
5229 enginePriv->setSystemViewport(oldSystemClip);
5231 painter->setLayoutDirection(layoutDirection());
5233 d->render(target, targetOffset, toBePainted, renderFlags);
5236 enginePriv->baseSystemClip = oldBaseClip;
5237 enginePriv->setSystemTransformAndViewport(oldTransform, oldSystemViewport);
5238 enginePriv->systemStateChanged();
5239 painter->setLayoutDirection(oldLayoutDirection);
5242 d->setSharedPainter(oldPainter);
5244 d->extra->inRenderWithPainter = inRenderWithPainter;
5249 QResizeEvent e(target->size(), QSize());
5250 QCoreApplication::sendEvent(target, &e);
5252 const QObjectList children = target->children();
5253 for (
int i = 0; i < children.size(); ++i) {
5254 if (!children.at(i)->isWidgetType())
5257 if (!child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274QPixmap QWidget::grab(
const QRect &rectangle)
5277 if (testAttribute(Qt::WA_PendingResizeEvent) || !testAttribute(Qt::WA_WState_Created))
5278 sendResizeEvents(
this);
5280 const QWidget::RenderFlags renderFlags = QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask;
5282 const bool oldDirtyOpaqueChildren = d->dirtyOpaqueChildren;
5284 if (r.width() < 0 || r.height() < 0) {
5287 r = d->prepareToRender(QRegion(), renderFlags).boundingRect();
5288 r.setTopLeft(rectangle.topLeft());
5291 if (!r.intersects(rect()))
5294 const qreal dpr = devicePixelRatio();
5295 QPixmap res((QSizeF(r.size()) * dpr).toSize());
5296 res.setDevicePixelRatio(dpr);
5298 res.fill(Qt::transparent);
5299 d->render(&res, QPoint(), QRegion(r), renderFlags);
5301 d->dirtyOpaqueChildren = oldDirtyOpaqueChildren;
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315#if QT_CONFIG(graphicseffect)
5316QGraphicsEffect *QWidget::graphicsEffect()
const
5319 return d->graphicsEffect;
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345#if QT_CONFIG(graphicseffect)
5346void QWidget::setGraphicsEffect(QGraphicsEffect *effect)
5349 if (d->graphicsEffect == effect)
5352 if (d->graphicsEffect) {
5353 d->invalidateBackingStore(rect());
5354 delete d->graphicsEffect;
5355 d->graphicsEffect =
nullptr;
5360 QGraphicsEffectSourcePrivate *sourced =
new QWidgetEffectSourcePrivate(
this);
5361 QGraphicsEffectSource *source =
new QGraphicsEffectSource(*sourced);
5362 d->graphicsEffect = effect;
5363 effect->d_func()->setGraphicsEffectSource(source);
5367 d->updateIsOpaque();
5371bool QWidgetPrivate::isAboutToShow()
const
5381 QWidget *parent = q->parentWidget();
5382 return parent ? parent->d_func()->isAboutToShow() :
false;
5385QRegion QWidgetPrivate::prepareToRender(
const QRegion ®ion, QWidget::RenderFlags renderFlags)
5388 const bool isVisible = q->isVisible();
5391 if (!isVisible && !isAboutToShow()) {
5392 QWidget *topLevel = q->window();
5393 (
void)topLevel->d_func()->topData();
5394 topLevel->ensurePolished();
5398 QWidget *widget = q;
5399 QWidgetList hiddenWidgets;
5401 if (widget->isHidden()) {
5402 widget->setAttribute(Qt::WA_WState_Hidden,
false);
5403 hiddenWidgets.append(widget);
5404 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5405 widget->d_func()->updateGeometry_helper(
true);
5407 widget = widget->parentWidget();
5411 if (topLevel->d_func()->layout)
5412 topLevel->d_func()->layout->activate();
5415 QTLWExtra *topLevelExtra = topLevel->d_func()->maybeTopData();
5416 if (topLevelExtra && !topLevelExtra->sizeAdjusted
5417 && !topLevel->testAttribute(Qt::WA_Resized)) {
5418 topLevel->adjustSize();
5419 topLevel->setAttribute(Qt::WA_Resized,
false);
5423 topLevel->d_func()->activateChildLayoutsRecursively();
5426 for (
int i = 0; i < hiddenWidgets.size(); ++i) {
5427 QWidget *widget = hiddenWidgets.at(i);
5428 widget->setAttribute(Qt::WA_WState_Hidden);
5429 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5430 widget->parentWidget()->d_func()->layout->invalidate();
5432 }
else if (isVisible) {
5433 q->window()->d_func()->sendPendingMoveAndResizeEvents(
true,
true);
5437 QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
5438 if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
5439 toBePainted &= extra->mask;
5443void QWidgetPrivate::render_helper(QPainter *painter,
const QPoint &targetOffset,
const QRegion &toBePainted,
5444 QWidget::RenderFlags renderFlags)
5447 Q_ASSERT(!toBePainted.isEmpty());
5450 const QTransform originalTransform = painter->worldTransform();
5451 const bool useDeviceCoordinates = originalTransform.isScaling();
5452 if (!useDeviceCoordinates) {
5454 const QRect rect = toBePainted.boundingRect();
5455 const QSize size = rect.size();
5459 const qreal pixmapDevicePixelRatio = painter->device()->devicePixelRatio();
5460 QPixmap pixmap(size * pixmapDevicePixelRatio);
5461 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio);
5463 if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
5464 pixmap.fill(Qt::transparent);
5465 q->render(&pixmap, QPoint(), toBePainted, renderFlags);
5467 const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
5468 painter->setRenderHints(QPainter::SmoothPixmapTransform,
true);
5470 painter->drawPixmap(targetOffset, pixmap);
5473 painter->setRenderHints(QPainter::SmoothPixmapTransform,
false);
5477 QTransform transform = originalTransform;
5478 transform.translate(targetOffset.x(), targetOffset.y());
5480 QPaintDevice *device = painter->device();
5484 const QRectF rect(toBePainted.boundingRect());
5485 QRect deviceRect = transform.mapRect(QRectF(0, 0, rect.width(), rect.height())).toAlignedRect();
5486 deviceRect &= QRect(0, 0, device->width(), device->height());
5488 QPixmap pixmap(deviceRect.size());
5489 pixmap.fill(Qt::transparent);
5492 QPainter pixmapPainter(&pixmap);
5493 pixmapPainter.setRenderHints(painter->renderHints());
5494 transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
5495 pixmapPainter.setTransform(transform);
5497 q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
5498 pixmapPainter.end();
5501 painter->setTransform(QTransform());
5502 painter->drawPixmap(deviceRect.topLeft(), pixmap);
5503 painter->setTransform(originalTransform);
5507void QWidgetPrivate::drawWidget(QPaintDevice *pdev,
const QRegion &rgn,
const QPoint &offset, DrawWidgetFlags flags,
5508 QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5515 qCInfo(lcWidgetPainting) <<
"Drawing" << rgn <<
"of" << q <<
"at" << offset
5516 <<
"into paint device" << pdev <<
"with" << flags;
5518 const bool asRoot = flags & DrawAsRoot;
5519 bool onScreen = shouldPaintOnScreen();
5521#if QT_CONFIG(graphicseffect)
5522 if (graphicsEffect && graphicsEffect->isEnabled()) {
5523 QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
5524 QWidgetEffectSourcePrivate *sourced =
static_cast<QWidgetEffectSourcePrivate *>
5526 if (!sourced->context) {
5527 const QRegion effectRgn((flags & UseEffectRegionBounds) ? rgn.boundingRect() : rgn);
5528 QWidgetPaintContext context(pdev, effectRgn, offset, flags, sharedPainter, repaintManager);
5529 sourced->context = &context;
5530 if (!sharedPainter) {
5531 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), effectRgn.translated(offset));
5533 p.translate(offset);
5534 context.painter = &p;
5535 graphicsEffect->draw(&p);
5536 setSystemClip(pdev->paintEngine(), 1, QRegion());
5538 context.painter = sharedPainter;
5539 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
5540 sourced->invalidateCache();
5541 sourced->lastEffectTransform = sharedPainter->worldTransform();
5543 sharedPainter->save();
5544 sharedPainter->translate(offset);
5545 setSystemClip(sharedPainter->paintEngine(), sharedPainter->device()->devicePixelRatio(), effectRgn.translated(offset));
5546 graphicsEffect->draw(sharedPainter);
5547 setSystemClip(sharedPainter->paintEngine(), 1, QRegion());
5548 sharedPainter->restore();
5550 sourced->context =
nullptr;
5553 repaintManager->markNeedsFlush(q, effectRgn, offset);
5559 flags = flags & ~UseEffectRegionBounds;
5561 const bool alsoOnScreen = flags & DrawPaintOnScreen;
5562 const bool recursive = flags & DrawRecursive;
5563 const bool alsoInvisible = flags & DrawInvisible;
5565 Q_ASSERT(sharedPainter ? sharedPainter->isActive() :
true);
5567 QRegion toBePainted(rgn);
5568 if (asRoot && !alsoInvisible)
5569 toBePainted &= clipRect();
5570 if (!(flags & DontSubtractOpaqueChildren))
5571 subtractOpaqueChildren(toBePainted, q->rect());
5573 if (!toBePainted.isEmpty()) {
5574 if (!onScreen || alsoOnScreen) {
5576 if (Q_UNLIKELY(q->testAttribute(Qt::WA_WState_InPaintEvent)))
5577 qWarning(
"QWidget::repaint: Recursive repaint detected");
5578 q->setAttribute(Qt::WA_WState_InPaintEvent);
5581 QPaintEngine *paintEngine = pdev->paintEngine();
5583 setRedirected(pdev, -offset);
5586 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted);
5588 paintEngine->d_func()->systemRect = q->data->crect;
5591 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
5592 && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
5593 beginBackingStorePainting();
5595 p.setRenderHint(QPainter::SmoothPixmapTransform);
5596 paintBackground(&p, toBePainted, (asRoot || onScreen) ? (flags | DrawAsRoot) : DrawWidgetFlags());
5597 endBackingStorePainting();
5601 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted.translated(offset));
5603 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
5604 beginBackingStorePainting();
5606 QColor tint = q->palette().window().color();
5607 tint.setAlphaF(.6f);
5608 p.fillRect(toBePainted.boundingRect(), tint);
5609 endBackingStorePainting();
5614 qDebug() <<
"painting" << q <<
"opaque ==" << isOpaque();
5615 qDebug() <<
"clipping to" << toBePainted <<
"location == " << offset
5616 <<
"geometry ==" << QRect(q->mapTo(q->window(), QPoint(0, 0)), q->size());
5619 bool skipPaintEvent =
false;
5620 if (renderToTexture) {
5623 beginBackingStorePainting();
5624 if (!q->testAttribute(Qt::WA_AlwaysStackOnTop) && repaintManager) {
5626 p.setCompositionMode(QPainter::CompositionMode_Source);
5627 p.fillRect(q->rect(), Qt::transparent);
5628 }
else if (!repaintManager) {
5630 QImage img = grabFramebuffer();
5633 if (img.format() == QImage::Format_RGB32)
5634 img.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied);
5636 p.drawImage(q->rect(), img);
5637 skipPaintEvent =
true;
5639 endBackingStorePainting();
5640 if (renderToTextureReallyDirty)
5641 renderToTextureReallyDirty = 0;
5643 skipPaintEvent =
true;
5646 if (!skipPaintEvent) {
5648 sendPaintEvent(toBePainted);
5652 repaintManager->markNeedsFlush(q, toBePainted, offset);
5656 restoreRedirected();
5658 paintEngine->d_func()->systemRect = QRect();
5660 paintEngine->d_func()->currentClipDevice =
nullptr;
5662 setSystemClip(pdev->paintEngine(), 1, QRegion());
5664 q->setAttribute(Qt::WA_WState_InPaintEvent,
false);
5665 if (Q_UNLIKELY(q->paintingActive()))
5666 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
5668 if (paintEngine && paintEngine->autoDestruct()) {
5671 }
else if (q->isWindow()) {
5672 QPaintEngine *engine = pdev->paintEngine();
5675 p.setClipRegion(toBePainted);
5676 const QBrush bg = q->palette().brush(QPalette::Window);
5677 if (bg.style() == Qt::TexturePattern)
5678 p.drawTiledPixmap(q->rect(), bg.texture());
5680 p.fillRect(q->rect(), bg);
5682 if (engine->autoDestruct())
5688 if (recursive && !children.isEmpty()) {
5689 paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot,
5690 sharedPainter, repaintManager);
5694void QWidgetPrivate::sendPaintEvent(
const QRegion &toBePainted)
5697 QPaintEvent e(toBePainted);
5698 QCoreApplication::sendSpontaneousEvent(q, &e);
5700 if (renderToTexture)
5704void QWidgetPrivate::render(QPaintDevice *target,
const QPoint &targetOffset,
5705 const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
5707 if (Q_UNLIKELY(!target)) {
5708 qWarning(
"QWidget::render: null pointer to paint device");
5712 const bool inRenderWithPainter = extra && extra->inRenderWithPainter;
5713 QRegion paintRegion = !inRenderWithPainter
5714 ? prepareToRender(sourceRegion, renderFlags)
5716 if (paintRegion.isEmpty())
5719 QPainter *oldSharedPainter = inRenderWithPainter ? sharedPainter() :
nullptr;
5723 if (target->devType() == QInternal::Widget) {
5724 QWidgetPrivate *targetPrivate =
static_cast<QWidget *>(target)->d_func();
5725 if (targetPrivate->extra && targetPrivate->extra->inRenderWithPainter) {
5726 QPainter *targetPainter = targetPrivate->sharedPainter();
5727 if (targetPainter && targetPainter->isActive())
5728 setSharedPainter(targetPainter);
5735 QPoint offset = targetOffset;
5736 offset -= paintRegion.boundingRect().topLeft();
5737 QPoint redirectionOffset;
5738 QPaintDevice *redirected =
nullptr;
5740 if (target->devType() == QInternal::Widget)
5741 redirected =
static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
5744 target = redirected;
5745 offset -= redirectionOffset;
5748 if (!inRenderWithPainter) {
5749 if (QPaintEngine *targetEngine = target->paintEngine()) {
5750 const QRegion targetSystemClip = targetEngine->systemClip();
5751 if (!targetSystemClip.isEmpty())
5752 paintRegion &= targetSystemClip.translated(-offset);
5757 DrawWidgetFlags flags = DrawPaintOnScreen | DrawInvisible;
5758 if (renderFlags & QWidget::DrawWindowBackground)
5759 flags |= DrawAsRoot;
5761 if (renderFlags & QWidget::DrawChildren)
5762 flags |= DrawRecursive;
5764 flags |= DontSubtractOpaqueChildren;
5766 flags |= DontSetCompositionMode;
5769 drawWidget(target, paintRegion, offset, flags, sharedPainter());
5772 if (oldSharedPainter)
5773 setSharedPainter(oldSharedPainter);
5776void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev,
const QObjectList& siblings,
int index,
const QRegion &rgn,
5777 const QPoint &offset, DrawWidgetFlags flags
5778 , QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5780 QWidget *w =
nullptr;
5782 bool dirtyBoundingRect =
true;
5783 const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
5784 const bool excludeNativeChildren = (flags & DontDrawNativeChildren);
5787 QWidget *x = qobject_cast<QWidget*>(siblings.at(index));
5788 if (x && !(exludeOpaqueChildren && x->d_func()->isOpaque) && !x->isHidden() && !x->isWindow()
5789 && !(excludeNativeChildren && x->internalWinId())) {
5790 if (dirtyBoundingRect) {
5791 boundingRect = rgn.boundingRect();
5792 dirtyBoundingRect =
false;
5795 if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
5801 }
while (index >= 0);
5806 QWidgetPrivate *wd = w->d_func();
5807 const QPoint widgetPos(w->data->crect.topLeft());
5808 const bool hasMask = wd->extra && wd->extra->hasMask && !wd->graphicsEffect;
5812 wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
5813 paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags,
5814 sharedPainter, repaintManager);
5817 if (w->updatesEnabled()
5818#if QT_CONFIG(graphicsview)
5819 && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
5822 QRegion wRegion(rgn);
5823 wRegion &= wd->effectiveRectFor(w->data->crect);
5824 wRegion.translate(-widgetPos);
5826 wRegion &= wd->extra->mask;
5827 wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, repaintManager);
5831#if QT_CONFIG(graphicseffect)
5832QRectF QWidgetEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system)
const
5834 if (system != Qt::DeviceCoordinates)
5835 return m_widget->rect();
5837 if (Q_UNLIKELY(!context)) {
5839 qWarning(
"QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
5843 return context->painter->worldTransform().mapRect(m_widget->rect());
5846void QWidgetEffectSourcePrivate::draw(QPainter *painter)
5848 if (!context || context->painter != painter) {
5849 m_widget->render(painter);
5855 QRegion toBePainted = context->rgn;
5856 toBePainted &= m_widget->rect();
5857 QWidgetPrivate *wd = qt_widget_private(m_widget);
5858 if (wd->extra && wd->extra->hasMask)
5859 toBePainted &= wd->extra->mask;
5861 wd->drawWidget(context->pdev, toBePainted, context->offset, context->flags,
5862 context->sharedPainter, context->repaintManager);
5865QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
5866 QGraphicsEffect::PixmapPadMode mode)
const
5868 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
5869 if (Q_UNLIKELY(!context && deviceCoordinates)) {
5871 qWarning(
"QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
5875 QPoint pixmapOffset;
5876 QRectF sourceRect = m_widget->rect();
5878 if (deviceCoordinates) {
5879 const QTransform &painterTransform = context->painter->worldTransform();
5880 sourceRect = painterTransform.mapRect(sourceRect);
5881 pixmapOffset = painterTransform.map(pixmapOffset);
5886 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect)
5887 effectRect = m_widget->graphicsEffect()->boundingRectFor(sourceRect).toAlignedRect();
5888 else if (mode == QGraphicsEffect::PadToTransparentBorder)
5889 effectRect = sourceRect.adjusted(-1, -1, 1, 1).toAlignedRect();
5891 effectRect = sourceRect.toAlignedRect();
5894 *offset = effectRect.topLeft();
5896 pixmapOffset -= effectRect.topLeft();
5899 if (
const auto *paintDevice = context->painter->device())
5900 dpr = paintDevice->devicePixelRatio();
5902 qWarning(
"QWidgetEffectSourcePrivate::pixmap: Painter not active");
5903 QPixmap pixmap(effectRect.size() * dpr);
5904 pixmap.setDevicePixelRatio(dpr);
5906 pixmap.fill(Qt::transparent);
5907 m_widget->render(&pixmap, pixmapOffset, QRegion(), QWidget::DrawChildren);
5912#if QT_CONFIG(graphicsview)
5914
5915
5916
5917
5918
5919
5920
5921QGraphicsProxyWidget *QWidgetPrivate::nearestGraphicsProxyWidget(
const QWidget *origin)
5924 const auto &extra = origin->d_func()->extra;
5925 if (extra && extra->proxyWidget)
5926 return extra->proxyWidget;
5927 return nearestGraphicsProxyWidget(origin->parentWidget());
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5948void QWidgetPrivate::setLocale_helper(
const QLocale &loc,
bool forceUpdate)
5951 if (locale == loc && !forceUpdate)
5956 if (!children.isEmpty()) {
5957 for (
int i = 0; i < children.size(); ++i) {
5958 QWidget *w = qobject_cast<QWidget*>(children.at(i));
5961 if (w->testAttribute(Qt::WA_SetLocale))
5963 if (w->isWindow() && !w->testAttribute(Qt::WA_WindowPropagation))
5965 w->d_func()->setLocale_helper(loc, forceUpdate);
5968 QEvent e(QEvent::LocaleChange);
5969 QCoreApplication::sendEvent(q, &e);
5972void QWidget::setLocale(
const QLocale &locale)
5976 setAttribute(Qt::WA_SetLocale);
5977 d->setLocale_helper(locale);
5980QLocale QWidget::locale()
const
5987void QWidgetPrivate::resolveLocale()
5991 if (!q->testAttribute(Qt::WA_SetLocale)) {
5992 QWidget *parent = q->parentWidget();
5993 setLocale_helper(!parent || (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
5994 ? QLocale() : parent->locale());
5998void QWidget::unsetLocale()
6001 setAttribute(Qt::WA_SetLocale,
false);
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028QString QWidget::windowTitle()
const
6031 if (d->extra && d->extra->topextra) {
6032 if (!d->extra->topextra->caption.isEmpty())
6033 return d->extra->topextra->caption;
6034 if (!d->extra->topextra->filePath.isEmpty())
6035 return QFileInfo(d->extra->topextra->filePath).fileName() +
"[*]"_L1;
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6055 QString cap = title;
6059 const auto placeHolder =
"[*]"_L1;
6060 int index = cap.indexOf(placeHolder);
6063 while (index != -1) {
6064 index += placeHolder.size();
6066 while (cap.indexOf(placeHolder, index) == index) {
6068 index += placeHolder.size();
6072 int lastIndex = cap.lastIndexOf(placeHolder, index - 1);
6073 if (widget->isWindowModified()
6074 && widget->style()->styleHint(QStyle::SH_TitleBar_ModifyNotification,
nullptr, widget))
6075 cap.replace(lastIndex, 3, QWidget::tr(
"*"));
6077 cap.remove(lastIndex, 3);
6080 index = cap.indexOf(placeHolder, index);
6083 cap.replace(
"[*][*]"_L1, placeHolder);
6088void QWidgetPrivate::setWindowTitle_helper(
const QString &title)
6091 if (q->testAttribute(Qt::WA_WState_Created))
6092 setWindowTitle_sys(qt_setWindowTitle_helperHelper(title, q));
6095void QWidgetPrivate::setWindowTitle_sys(
const QString &caption)
6101 if (QWindow *window = q->windowHandle())
6103#if QT_CONFIG(accessibility)
6104 QString oldAccessibleName;
6105 const QAccessibleInterface *accessible = QAccessible::isActive()
6106 ? QAccessible::queryAccessibleInterface(q)
6109 oldAccessibleName = accessible->text(QAccessible::Name);
6112 window->setTitle(caption);
6114#if QT_CONFIG(accessibility)
6115 if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) {
6116 QAccessibleEvent event(q, QAccessible::NameChanged);
6117 QAccessible::updateAccessibility(&event);
6123void QWidgetPrivate::setWindowIconText_helper(
const QString &title)
6126 if (q->testAttribute(Qt::WA_WState_Created))
6127 setWindowIconText_sys(qt_setWindowTitle_helperHelper(title, q));
6130void QWidgetPrivate::setWindowIconText_sys(
const QString &iconText)
6136 if (QWindow *window = q->windowHandle()) {
6137 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(window->handle()))
6138 xcbWindow->setWindowIconText(iconText);
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6157void QWidget::setWindowIconText(
const QString &iconText)
6159 if (QWidget::windowIconText() == iconText)
6163 d->topData()->iconText = iconText;
6164 d->setWindowIconText_helper(iconText);
6166 QEvent e(QEvent::IconTextChange);
6167 QCoreApplication::sendEvent(
this, &e);
6169 emit windowIconTextChanged(iconText);
6173
6174
6175
6176
6177
6178
6179
6181void QWidget::setWindowTitle(
const QString &title)
6183 if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
6187 d->topData()->caption = title;
6188 d->setWindowTitle_helper(title);
6190 QEvent e(QEvent::WindowTitleChange);
6191 QCoreApplication::sendEvent(
this, &e);
6193 emit windowTitleChanged(title);
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211QIcon QWidget::windowIcon()
const
6213 const QWidget *w =
this;
6215 const QWidgetPrivate *d = w->d_func();
6216 if (d->extra && d->extra->topextra && d->extra->topextra->icon)
6217 return *d->extra->topextra->icon;
6218 w = w->parentWidget();
6220 return QApplication::windowIcon();
6223void QWidgetPrivate::setWindowIcon_helper()
6226 QEvent e(QEvent::WindowIconChange);
6232 if (!q->windowHandle())
6233 QCoreApplication::sendEvent(q, &e);
6234 for (
int i = 0; i < children.size(); ++i) {
6235 QWidget *w = qobject_cast<QWidget *>(children.at(i));
6236 if (w && !w->isWindow())
6237 QCoreApplication::sendEvent(w, &e);
6242
6243
6244
6245
6246
6247
6248
6250void QWidget::setWindowIcon(
const QIcon &icon)
6254 setAttribute(Qt::WA_SetWindowIcon, !icon.isNull());
6257 if (!d->extra->topextra->icon)
6258 d->extra->topextra->icon = std::make_unique<QIcon>(icon);
6260 *d->extra->topextra->icon = icon;
6262 d->setWindowIcon_sys();
6263 d->setWindowIcon_helper();
6265 emit windowIconChanged(icon);
6268void QWidgetPrivate::setWindowIcon_sys()
6271 if (QWindow *window = q->windowHandle())
6272 window->setIcon(q->windowIcon());
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6290QString QWidget::windowIconText()
const
6293 return (d->extra && d->extra->topextra) ? d->extra->topextra->iconText : QString();
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6320QString QWidget::windowFilePath()
const
6323 return (d->extra && d->extra->topextra) ? d->extra->topextra->filePath : QString();
6326void QWidget::setWindowFilePath(
const QString &filePath)
6328 if (filePath == windowFilePath())
6334 d->extra->topextra->filePath = filePath;
6335 d->setWindowFilePath_helper(filePath);
6338void QWidgetPrivate::setWindowFilePath_helper(
const QString &filePath)
6340 if (extra->topextra && extra->topextra->caption.isEmpty()) {
6342 setWindowTitle_helper(QFileInfo(filePath).fileName());
6346 setWindowTitle_helper(q->windowTitle());
6350 setWindowFilePath_sys(filePath);
6354void QWidgetPrivate::setWindowFilePath_sys(
const QString &filePath)
6360 if (QWindow *window = q->windowHandle())
6361 window->setFilePath(filePath);
6365
6366
6367
6368
6370QString QWidget::windowRole()
const
6373 return (d->extra && d->extra->topextra) ? d->extra->topextra->role : QString();
6377
6378
6379
6380void QWidget::setWindowRole(
const QString &role)
6382#if QT_CONFIG(xcb) || QT_CONFIG(wayland)
6385 d->topData()->role = role;
6390 if (windowHandle()) {
6392 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(windowHandle()->handle()))
6393 xcbWindow->setWindowRole(role);
6395#if QT_CONFIG(wayland)
6396 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(windowHandle()->handle()))
6397 waylandWindow->setSessionRestoreId(role);
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6451void QWidget::setFocusProxy(QWidget * w)
6454 if (!w && !d->extra)
6457 for (QWidget* fp = w; fp; fp = fp->focusProxy()) {
6458 if (Q_UNLIKELY(fp ==
this)) {
6459 qWarning(
"QWidget: %s (%s) already in focus proxy chain", metaObject()->className(), objectName().toLocal8Bit().constData());
6464 const bool moveFocusToProxy = (QApplicationPrivate::focus_widget ==
this);
6467 d->extra->focus_proxy = w;
6469 if (w && isAncestorOf(w)) {
6475 const QWidget *parentOfW = w->parentWidget();
6476 Q_ASSERT(parentOfW);
6477 QWidget *firstChild =
nullptr;
6478 const auto childList = children();
6479 for (QObject *child : childList) {
6480 if ((firstChild = qobject_cast<QWidget *>(child)))
6483 Q_ASSERT(firstChild);
6484 d->insertIntoFocusChainBefore(firstChild);
6485 }
else if (w && w->isAncestorOf(
this)) {
6490 QWidget *parentsNext = w->nextInFocusChain();
6491 if (parentsNext ==
this) {
6493 Q_ASSERT(previousInFocusChain() == w);
6495 d->QWidgetPrivate::insertIntoFocusChainAfter(w);
6499 if (moveFocusToProxy)
6500 setFocus(Qt::OtherFocusReason);
6505
6506
6507
6508
6510QWidget *QWidget::focusProxy()
const
6513 return d->extra ? d->extra->focus_proxy.data() :
nullptr;
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529bool QWidget::hasFocus()
const
6531 const QWidget* w =
this;
6532 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
6533 w = w->d_func()->extra->focus_proxy;
6534#if QT_CONFIG(graphicsview)
6535 if (QWidget *window = w->window()) {
6536 const auto &e = window->d_func()->extra;
6537 if (e && e->proxyWidget && e->proxyWidget->hasFocus() && window->focusWidget() == w)
6541 return (QApplication::focusWidget() == w);
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6579void QWidget::setFocus(Qt::FocusReason reason)
6584 QWidget *f = d_func()->deepestFocusProxy();
6588 if (QApplication::focusWidget() == f)
6591#if QT_CONFIG(graphicsview)
6592 QWidget *previousProxyFocus =
nullptr;
6593 if (
const auto &topData = window()->d_func()->extra) {
6594 if (topData->proxyWidget && topData->proxyWidget->hasFocus()) {
6595 previousProxyFocus = topData->proxyWidget->widget()->focusWidget();
6596 if (previousProxyFocus && previousProxyFocus->focusProxy())
6597 previousProxyFocus = previousProxyFocus->focusProxy();
6598 if (previousProxyFocus == f && !topData->proxyWidget->d_func()->proxyIsGivingFocus)
6604#if QT_CONFIG(graphicsview)
6606 if (
const auto &topData = window()->d_func()->extra) {
6607 if (topData->proxyWidget && !topData->proxyWidget->hasFocus()) {
6608 f->d_func()->updateFocusChild();
6609 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 1;
6610 topData->proxyWidget->setFocus(reason);
6611 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 0;
6616 if (f->isActiveWindow()) {
6617 QWidget *prev = QApplicationPrivate::focus_widget;
6619 if (reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
6620 && prev->testAttribute(Qt::WA_InputMethodEnabled)) {
6621 QGuiApplication::inputMethod()->commit();
6624 if (reason != Qt::NoFocusReason) {
6625 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason);
6626 QCoreApplication::sendEvent(prev, &focusAboutToChange);
6630 f->d_func()->updateFocusChild();
6632 QApplicationPrivate::setFocusWidget(f, reason);
6633#if QT_CONFIG(accessibility)
6635 if (!(f->inherits(
"QMenuBar") || f->inherits(
"QMenu") || f->inherits(
"QMenuItem")))
6637 QAccessibleEvent event(f, QAccessible::Focus);
6638 QAccessible::updateAccessibility(&event);
6641#if QT_CONFIG(graphicsview)
6642 if (
const auto &topData = window()->d_func()->extra) {
6643 if (topData->proxyWidget) {
6644 if (previousProxyFocus && previousProxyFocus != f) {
6646 QFocusEvent event(QEvent::FocusOut, reason);
6647 QPointer<QWidget> that = previousProxyFocus;
6648 QCoreApplication::sendEvent(previousProxyFocus, &event);
6650 QCoreApplication::sendEvent(that->style(), &event);
6653#if QT_CONFIG(graphicsview)
6655 if (
const auto &topData = window()->d_func()->extra)
6656 if (topData->proxyWidget && topData->proxyWidget->hasFocus())
6657 topData->proxyWidget->d_func()->updateProxyInputMethodAcceptanceFromWidget();
6660 QFocusEvent event(QEvent::FocusIn, reason);
6661 QPointer<QWidget> that = f;
6662 QCoreApplication::sendEvent(f, &event);
6664 QCoreApplication::sendEvent(that->style(), &event);
6670 f->d_func()->updateFocusChild();
6676
6677
6678
6679
6680
6681QWidget *QWidgetPrivate::deepestFocusProxy()
const
6685 QWidget *focusProxy = q->focusProxy();
6689 while (QWidget *nextFocusProxy = focusProxy->focusProxy())
6690 focusProxy = nextFocusProxy;
6697 const auto platformWindow = w->handle();
6698 return platformWindow && platformWindow->isEmbedded();
6701void QWidgetPrivate::setFocus_sys()
6706 if (extra && extra->hasWindowContainer)
6710 if (QWindow *nativeWindow = q->testAttribute(Qt::WA_WState_Created) ? q->window()->windowHandle() :
nullptr) {
6711 if (nativeWindow->type() != Qt::Popup && nativeWindow != QGuiApplication::focusWindow()
6712 && (QGuiApplication::applicationState() == Qt::ApplicationActive
6713 || QCoreApplication::testAttribute(Qt::AA_PluginApplication)
6714 || isEmbedded(nativeWindow))) {
6715 nativeWindow->requestActivate();
6721void QWidgetPrivate::updateFocusChild()
6726 if (q->isHidden()) {
6727 while (w && w->isHidden()) {
6728 w->d_func()->focus_child = q;
6729 w = w->isWindow() ?
nullptr : w->parentWidget();
6733 w->d_func()->focus_child = q;
6734 w = w->isWindow() ?
nullptr : w->parentWidget();
6738 if (QTLWExtra *extra = q->window()->d_func()->maybeTopData()) {
6740 emit extra->window->focusObjectChanged(q);
6745
6746
6747
6748
6749
6750
6751
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6766void QWidget::clearFocus()
6769 if (testAttribute(Qt::WA_InputMethodEnabled))
6770 QGuiApplication::inputMethod()->commit();
6772 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange);
6773 QCoreApplication::sendEvent(
this, &focusAboutToChange);
6776 QTLWExtra *extra = window()->d_func()->maybeTopData();
6777 QObject *originalFocusObject =
nullptr;
6778 if (extra && extra->window) {
6779 originalFocusObject = extra->window->focusObject();
6782 if (!originalFocusObject)
6783 originalFocusObject = focusWidget();
6789 if (w->d_func()->focus_child ==
this)
6790 w->d_func()->focus_child =
nullptr;
6791 w = w->parentWidget();
6798 if (originalFocusObject && originalFocusObject != extra->window->focusObject())
6799 emit extra->window->focusObjectChanged(extra->window->focusObject());
6801#if QT_CONFIG(graphicsview)
6802 const auto &topData = d_func()->extra;
6803 if (topData && topData->proxyWidget)
6804 topData->proxyWidget->clearFocus();
6809 QApplicationPrivate::setFocusWidget(
nullptr, Qt::OtherFocusReason);
6810#if QT_CONFIG(accessibility)
6811 QAccessibleEvent event(
this, QAccessible::Focus);
6812 QAccessible::updateAccessibility(&event);
6819
6820
6821
6822
6823
6824
6825
6826
6829
6830
6831
6832
6833
6834
6835
6836
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6860bool QWidget::focusNextPrevChild(
bool next)
6862 QWidget* p = parentWidget();
6863 bool isSubWindow = (windowType() == Qt::SubWindow);
6864 if (!isWindow() && !isSubWindow && p)
6865 return p->focusNextPrevChild(next);
6866#if QT_CONFIG(graphicsview)
6868 if (d->extra && d->extra->proxyWidget)
6869 return d->extra->proxyWidget->focusNextPrevChild(next);
6872 bool wrappingOccurred =
false;
6873 QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(
this, next,
6875 if (!w)
return false;
6877 Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
6880
6881
6882
6883
6884 if (wrappingOccurred) {
6885 QWindow *window = windowHandle();
6886 if (window !=
nullptr) {
6887 QWindowPrivate *winp = qt_window_private(window);
6889 if (winp->platformWindow !=
nullptr) {
6890 QFocusEvent event(QEvent::FocusIn, reason);
6892 winp->platformWindow->windowEvent(&event);
6893 if (event.isAccepted())
return true;
6898 w->setFocus(reason);
6903
6904
6905
6906
6907
6908
6909
6911QWidget *QWidget::focusWidget()
const
6913 return const_cast<QWidget *>(d_func()->focus_child);
6916QObject *QWidgetPrivate::focusObject()
6919 QWidget *proxy = deepestFocusProxy();
6920 return proxy ? proxy : q;
6924
6925
6926
6927
6928QWidget *QWidget::nextInFocusChain()
const
6931 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Next);
6935
6936
6937
6938
6939
6940
6941
6942QWidget *QWidget::previousInFocusChain()
const
6945 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Previous);
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963bool QWidget::isActiveWindow()
const
6965 QWidget *tlw = window();
6966 if (tlw == QApplication::activeWindow() || (isVisible() && (tlw->windowType() == Qt::Popup)))
6969#if QT_CONFIG(graphicsview)
6970 if (
const auto &tlwExtra = tlw->d_func()->extra) {
6971 if (isVisible() && tlwExtra->proxyWidget)
6972 return tlwExtra->proxyWidget->isActiveWindow();
6976 if (style()->styleHint(QStyle::SH_Widget_ShareActivation,
nullptr,
this)) {
6977 if (tlw->windowType() == Qt::Tool &&
6979 (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow()))
6981 QWidget *w = QApplication::activeWindow();
6982 while(w && tlw->windowType() == Qt::Tool &&
6983 !w->isModal() && w->parentWidget()) {
6984 w = w->parentWidget()->window();
6991 if (QWindow *ww = QGuiApplication::focusWindow()) {
6993 QWidgetWindow *qww = qobject_cast<QWidgetWindow *>(ww);
6994 QWindowContainer *qwc = qww ? qobject_cast<QWindowContainer *>(qww->widget()) : 0;
6995 if (qwc && qwc->topLevelWidget() == tlw)
7006 if (
const QWindow *w = tlw->windowHandle()) {
7008 return w->handle()->isActive();
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064void QWidget::setTabOrder(QWidget* first, QWidget *second)
7066 if (!first || !second || first == second
7067 || first->focusPolicy() == Qt::NoFocus
7068 || second->focusPolicy() == Qt::NoFocus)
7071 if (Q_UNLIKELY(first->window() != second->window())) {
7072 qWarning(
"QWidget::setTabOrder: 'first' and 'second' must be in the same window");
7076 const auto determineLastFocusChild = [](QWidget *target, QWidget *noFurtherThan)
7082 QWidget *lastFocusChild = target;
7084 QWidget *focusProxy = target->d_func()->deepestFocusProxy();
7091 for (
auto *object : target->children()) {
7092 QWidget *w = qobject_cast<QWidget*>(object);
7093 if (w && w->focusProxy() == target) {
7098 }
else if (target->isAncestorOf(focusProxy)) {
7099 lastFocusChild = focusProxy;
7100 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
7101 focusNext != focusProxy && target->isAncestorOf(focusNext) && focusNext->window() == focusProxy->window();
7102 focusNext = focusNext->nextInFocusChain()) {
7103 if (focusNext == noFurtherThan)
7105 if (focusNext->focusPolicy() != Qt::NoFocus)
7106 lastFocusChild = focusNext;
7109 return lastFocusChild;
7112 QWidget *lastFocusChildOfFirst = determineLastFocusChild(first, second);
7113 if (lastFocusChildOfFirst == second)
7114 lastFocusChildOfFirst = first;
7115 QWidget *lastFocusChildOfSecond = determineLastFocusChild(second, first);
7116 if (lastFocusChildOfSecond == first)
7117 lastFocusChildOfSecond = second;
7120 QWidget *oldPrev = second->previousInFocusChain();
7121 QWidget *prevWithFocus = oldPrev;
7122 while (prevWithFocus->focusPolicy() == Qt::NoFocus)
7123 prevWithFocus = prevWithFocus->previousInFocusChain();
7124 if (prevWithFocus == first)
7126 const QWidgetList chain = QWidgetPrivate::takeFromFocusChain(second, lastFocusChildOfSecond);
7127 QWidgetPrivate::insertIntoFocusChain(chain, QWidgetPrivate::FocusDirection::Next, lastFocusChildOfFirst);
7130void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets)
7132 QWidget *prev =
nullptr;
7133 for (
const auto &widget : widgets) {
7137 QWidget::setTabOrder(prev, widget);
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7156void QWidgetPrivate::reparentFocusWidgets(QWidget * oldtlw)
7159 if (oldtlw == q->window())
7163 focus_child->clearFocus();
7165 reparentFocusChildren(QWidgetPrivate::FocusDirection::Next);
7169
7170
7171
7172
7173
7174
7175QSize QWidget::frameSize()
const
7178 if (isWindow() && !(windowType() == Qt::Popup)) {
7179 QRect fs = d->frameStrut();
7180 return QSize(data->crect.width() + fs.left() + fs.right(),
7181 data->crect.height() + fs.top() + fs.bottom());
7183 return data->crect.size();
7187
7188
7189
7190
7191
7193void QWidget::move(
const QPoint &p)
7196 setAttribute(Qt::WA_Moved);
7197 if (testAttribute(Qt::WA_WState_Created)) {
7199 d->topData()->posIncludesFrame =
false;
7200 d->setGeometry_sys(p.x() + geometry().x() - QWidget::x(),
7201 p.y() + geometry().y() - QWidget::y(),
7202 width(), height(),
true);
7203 d->setDirtyOpaqueRegion();
7207 d->topData()->posIncludesFrame =
true;
7208 data->crect.moveTopLeft(p);
7209 setAttribute(Qt::WA_PendingMoveEvent);
7212 if (d->extra && d->extra->hasWindowContainer)
7213 QWindowContainer::parentWasMoved(
this);
7225void QWidgetPrivate::fixPosIncludesFrame()
7228 if (QTLWExtra *te = maybeTopData()) {
7229 if (te->posIncludesFrame) {
7232 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
7233 te->posIncludesFrame = 0;
7235 if (q->windowHandle() && q->windowHandle()->handle()) {
7237 if (!q->data->fstrut_dirty) {
7238 data.crect.translate(te->frameStrut.x(), te->frameStrut.y());
7239 te->posIncludesFrame = 0;
7248
7249
7250
7251
7253void QWidget::resize(
const QSize &s)
7256 setAttribute(Qt::WA_Resized);
7257 if (testAttribute(Qt::WA_WState_Created)) {
7258 d->fixPosIncludesFrame();
7259 d->setGeometry_sys(geometry().x(), geometry().y(), s.width(), s.height(),
false);
7260 d->setDirtyOpaqueRegion();
7262 const auto oldRect = data->crect;
7263 data->crect.setSize(s.boundedTo(maximumSize()).expandedTo(minimumSize()));
7264 if (oldRect != data->crect)
7265 setAttribute(Qt::WA_PendingResizeEvent);
7269void QWidget::setGeometry(
const QRect &r)
7272 setAttribute(Qt::WA_Resized);
7273 setAttribute(Qt::WA_Moved);
7275 d->topData()->posIncludesFrame = 0;
7276 if (testAttribute(Qt::WA_WState_Created)) {
7277 d->setGeometry_sys(r.x(), r.y(), r.width(), r.height(),
true);
7278 d->setDirtyOpaqueRegion();
7280 const auto oldRect = data->crect;
7281 data->crect.setTopLeft(r.topLeft());
7282 data->crect.setSize(r.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
7283 if (oldRect != data->crect) {
7284 setAttribute(Qt::WA_PendingMoveEvent);
7285 setAttribute(Qt::WA_PendingResizeEvent);
7289 if (d->extra && d->extra->hasWindowContainer)
7290 QWindowContainer::parentWasMoved(
this);
7293void QWidgetPrivate::setGeometry_sys(
int x,
int y,
int w,
int h,
bool isMove)
7297 w = qMin(w,extra->maxw);
7298 h = qMin(h,extra->maxh);
7299 w = qMax(w,extra->minw);
7300 h = qMax(h,extra->minh);
7303 if (q->isWindow() && q->windowHandle()) {
7304 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
7305 if (!integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) {
7308 w = q->windowHandle()->width();
7309 h = q->windowHandle()->height();
7313 QPoint oldp = q->geometry().topLeft();
7314 QSize olds = q->size();
7315 QRect r(x, y, w, h);
7317 bool isResize = olds != r.size();
7319 isMove = oldp != r.topLeft();
7324 if (r.size() == olds && oldp == r.topLeft())
7327 if (!data.in_set_window_state) {
7328 q->data->window_state &= ~Qt::WindowMaximized;
7329 q->data->window_state &= ~Qt::WindowFullScreen;
7331 topData()->normalGeometry = QRect(0, 0, -1, -1);
7334 QPoint oldPos = q->pos();
7337 bool needsShow =
false;
7339 if (q->isWindow() || q->windowHandle()) {
7340 if (!(data.window_state & Qt::WindowFullScreen) && (w == 0 || h == 0)) {
7341 q->setAttribute(Qt::WA_OutsideWSRange,
true);
7344 data.crect = QRect(x, y, w, h);
7345 }
else if (q->testAttribute(Qt::WA_OutsideWSRange)) {
7346 q->setAttribute(Qt::WA_OutsideWSRange,
false);
7351 if (q->isVisible()) {
7352 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && !q->testAttribute(Qt::WA_OutsideWSRange)) {
7353 if (QWindow *win = q->windowHandle()) {
7354 if (q->isWindow()) {
7355 if (isResize && !isMove)
7357 else if (isMove && !isResize)
7358 win->setPosition(x, y);
7360 win->setGeometry(q->geometry());
7362 QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());
7363 win->setGeometry(QRect(posInNativeParent,r.size()));
7370 if (!q->isWindow()) {
7371 if (renderToTexture) {
7372 QRegion updateRegion(q->geometry());
7373 updateRegion += QRect(oldPos, olds);
7374 q->parentWidget()->d_func()->invalidateBackingStore(updateRegion);
7375 }
else if (isMove && !isResize) {
7376 moveRect(QRect(oldPos, olds), x - oldPos.x(), y - oldPos.y());
7378 invalidateBackingStore_resizeHelper(oldPos, olds);
7384 QMoveEvent e(q->pos(), oldPos);
7385 QCoreApplication::sendEvent(q, &e);
7388 QResizeEvent e(r.size(), olds);
7389 QCoreApplication::sendEvent(q, &e);
7390 if (q->windowHandle())
7394 if (isMove && q->pos() != oldPos)
7395 q->setAttribute(Qt::WA_PendingMoveEvent,
true);
7397 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7400#if QT_CONFIG(accessibility)
7401 if (QAccessible::isActive() && q->isVisible()) {
7402 QAccessibleEvent event(q, QAccessible::LocationChanged);
7403 QAccessible::updateAccessibility(&event);
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425QByteArray QWidget::saveGeometry()
const
7428 QDataStream stream(&array, QIODevice::WriteOnly);
7429 stream.setVersion(QDataStream::Qt_4_0);
7430 const quint32 magicNumber = 0x1D9D0CB;
7435 quint16 majorVersion = 3;
7436 quint16 minorVersion = 0;
7437 const int screenNumber = QGuiApplication::screens().indexOf(screen());
7438 stream << magicNumber
7443 << qint32(screenNumber)
7444 << quint8(windowState() & Qt::WindowMaximized)
7445 << quint8(windowState() & Qt::WindowFullScreen)
7446 << qint32(screen()->geometry().width())
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474void QWidgetPrivate::checkRestoredGeometry(
const QRect &availableGeometry, QRect *restoredGeometry,
7478 const int height = restoredGeometry->height() + frameHeight;
7482 if (availableGeometry.height() <= height)
7483 restoredGeometry->setHeight(availableGeometry.height() - 2 - frameHeight);
7484 if (availableGeometry.width() <= restoredGeometry->width())
7485 restoredGeometry->setWidth(availableGeometry.width() - 2);
7489 const QRect restored = restoredGeometry->adjusted(0, -frameHeight, 0, 0);
7492 if (availableGeometry.contains(restored))
7498 if (restored.top() <= availableGeometry.top()) {
7499 restoredGeometry->moveTop(availableGeometry.top() + 1 + frameHeight);
7500 }
else if (restored.bottom() >= availableGeometry.bottom()) {
7502 restoredGeometry->moveBottom(availableGeometry.bottom() - 1);
7506 if (restored.left() <= availableGeometry.left()) {
7507 restoredGeometry->moveLeft(availableGeometry.left() + 1);
7508 }
else if (restored.right() >= availableGeometry.right()) {
7510 restoredGeometry->moveRight(availableGeometry.right() - 1);
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537bool QWidget::restoreGeometry(
const QByteArray &geometry)
7539 if (geometry.size() < 4)
7541 QDataStream stream(geometry);
7542 stream.setVersion(QDataStream::Qt_4_0);
7544 const quint32 magicNumber = 0x1D9D0CB;
7545 quint32 storedMagicNumber;
7546 stream >> storedMagicNumber;
7547 if (storedMagicNumber != magicNumber)
7550 const quint16 currentMajorVersion = 3;
7551 quint16 majorVersion = 0;
7552 quint16 minorVersion = 0;
7554 stream >> majorVersion >> minorVersion;
7556 if (majorVersion > currentMajorVersion)
7560 QRect restoredFrameGeometry;
7561 QRect restoredGeometry;
7562 QRect restoredNormalGeometry;
7563 qint32 restoredScreenNumber;
7566 qint32 restoredScreenWidth = 0;
7568 stream >> restoredFrameGeometry
7569 >> restoredNormalGeometry
7570 >> restoredScreenNumber
7574 if (majorVersion > 1)
7575 stream >> restoredScreenWidth;
7576 if (majorVersion > 2)
7577 stream >> restoredGeometry;
7581 if (restoredScreenNumber >= qMax(QGuiApplication::screens().size(), 1))
7582 restoredScreenNumber = 0;
7583 const QScreen *restoredScreen = QGuiApplication::screens().value(restoredScreenNumber,
nullptr);
7584 const qreal screenWidthF = restoredScreen ? qreal(restoredScreen->geometry().width()) : 0;
7587 if (restoredScreenWidth) {
7588 const qreal factor = qreal(restoredScreenWidth) / screenWidthF;
7589 if (factor < 0.8 || factor > 1.25)
7594 if (!maximized && !fullScreen && qreal(restoredFrameGeometry.width()) / screenWidthF > 1.5)
7598 const int frameHeight = QApplication::style()
7599 ? QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight,
nullptr,
this)
7602 if (!restoredNormalGeometry.isValid())
7603 restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
7604 if (!restoredNormalGeometry.isValid()) {
7606 restoredNormalGeometry.setSize(restoredNormalGeometry
7608 .expandedTo(d_func()->adjustedSize()));
7611 const QRect availableGeometry = restoredScreen ? restoredScreen->availableGeometry()
7619 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredGeometry, frameHeight);
7620 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredNormalGeometry, frameHeight);
7622 if (maximized || fullScreen) {
7625 Qt::WindowStates ws = windowState();
7627 setGeometry(restoredNormalGeometry);
7629 if (ws & Qt::WindowFullScreen) {
7631 move(availableGeometry.topLeft());
7632 }
else if (ws & Qt::WindowMaximized) {
7636 if (restoredScreen != screen()) {
7637 setWindowState(Qt::WindowNoState);
7638 setGeometry(restoredNormalGeometry);
7641 setGeometry(restoredNormalGeometry);
7645 ws |= Qt::WindowMaximized;
7647 ws |= Qt::WindowFullScreen;
7649 d_func()->topData()->normalGeometry = restoredNormalGeometry;
7651 setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
7654 if (majorVersion > 2)
7655 setGeometry(restoredGeometry);
7657 setGeometry(restoredNormalGeometry);
7663
7664
7665
7666
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678void QWidget::setContentsMargins(
int left,
int top,
int right,
int bottom)
7681 if (left == d->leftmargin && top == d->topmargin
7682 && right == d->rightmargin && bottom == d->bottommargin)
7684 d->leftmargin = left;
7686 d->rightmargin = right;
7687 d->bottommargin = bottom;
7689 d->updateContentsRect();
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708void QWidget::setContentsMargins(
const QMargins &margins)
7710 setContentsMargins(margins.left(), margins.top(),
7711 margins.right(), margins.bottom());
7714void QWidgetPrivate::updateContentsRect()
7721 q->updateGeometry();
7723 if (q->isVisible()) {
7725 QResizeEvent e(q->data->crect.size(), q->data->crect.size());
7726 QCoreApplication::sendEvent(q, &e);
7728 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7731 QEvent e(QEvent::ContentsRectChange);
7732 QCoreApplication::sendEvent(q, &e);
7736
7737
7738
7739
7740
7741
7742QMargins QWidget::contentsMargins()
const
7745 QMargins userMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
7746 return testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea) ?
7747 userMargins | d->safeAreaMargins() : userMargins;
7751
7752
7753
7754
7755QRect QWidget::contentsRect()
const
7757 return rect() - contentsMargins();
7760QMargins QWidgetPrivate::safeAreaMargins()
const
7764#if QT_CONFIG(graphicsview)
7767 if (nearestGraphicsProxyWidget(q))
7771 QWidget *nativeWidget = q->window();
7772 if (!nativeWidget->windowHandle())
7775 QMargins safeAreaMargins = nativeWidget->windowHandle()->safeAreaMargins();
7777 if (!q->isWindow()) {
7785 if (safeAreaMargins.isNull())
7792 const QWidget *assumedSafeWidget =
nullptr;
7793 for (
const QWidget *w = q; w != nativeWidget; w = w->parentWidget()) {
7794 QWidget *parentWidget = w->parentWidget();
7795 if (!parentWidget->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
7798 if (parentWidget->testAttribute(Qt::WA_LayoutOnEntireRect))
7801 QLayout *layout = parentWidget->layout();
7805 if (layout->geometry().isNull())
7808 if (layout->indexOf(w) < 0)
7811 assumedSafeWidget = w;
7815#if !defined(QT_DEBUG)
7816 if (assumedSafeWidget) {
7827 QPoint topLeftMargins = q->mapFrom(nativeWidget, QPoint(safeAreaMargins.left(), safeAreaMargins.top()));
7828 QRect widgetRect = q->isVisible() ? q->visibleRegion().boundingRect() : q->rect();
7829 QPoint bottomRightMargins = widgetRect.bottomRight() - q->mapFrom(nativeWidget,
7830 nativeWidget->rect().bottomRight() - QPoint(safeAreaMargins.right(), safeAreaMargins.bottom()));
7833 safeAreaMargins = QMargins(qMax(0, topLeftMargins.x()), qMax(0, topLeftMargins.y()),
7834 qMax(0, bottomRightMargins.x()), qMax(0, bottomRightMargins.y()));
7836 if (!safeAreaMargins.isNull() && assumedSafeWidget) {
7837 QLayout *layout = assumedSafeWidget->parentWidget()->layout();
7838 qWarning() << layout <<
"is laying out" << assumedSafeWidget
7839 <<
"outside of the contents rect of" << layout->parentWidget();
7844 return safeAreaMargins;
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7877Qt::ContextMenuPolicy QWidget::contextMenuPolicy()
const
7879 return (Qt::ContextMenuPolicy)data->context_menu_policy;
7882void QWidget::setContextMenuPolicy(Qt::ContextMenuPolicy policy)
7884 data->context_menu_policy = (uint) policy;
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7909Qt::FocusPolicy QWidget::focusPolicy()
const
7911 return (Qt::FocusPolicy)data->focus_policy;
7914void QWidget::setFocusPolicy(Qt::FocusPolicy policy)
7916 data->focus_policy = (uint) policy;
7918 if (d->extra && d->extra->focus_proxy)
7919 d->extra->focus_proxy->setFocusPolicy(policy);
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950void QWidget::setUpdatesEnabled(
bool enable)
7953 setAttribute(Qt::WA_ForceUpdatesDisabled, !enable);
7954 d->setUpdatesEnabled_helper(enable);
7958
7959
7960
7961
7962
7963
7964
7965
7966
7974 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
7975 Qt::WindowState defaultState = platformIntegration->defaultWindowState(data->window_flags);
7976 if (defaultState == Qt::WindowFullScreen)
7978 else if (defaultState == Qt::WindowMaximized)
7986
7987
7988
7989
7990void QWidgetPrivate::show_recursive()
7995 if (!q->testAttribute(Qt::WA_WState_Created))
7996 createRecursively();
7997 q->ensurePolished();
7999 if (!q->isWindow() && q->parentWidget()->d_func()->layout && !q->parentWidget()->data->in_show)
8000 q->parentWidget()->d_func()->layout->activate();
8008void QWidgetPrivate::sendPendingMoveAndResizeEvents(
bool recursive,
bool disableUpdates)
8012 disableUpdates = disableUpdates && q->updatesEnabled();
8014 q->setAttribute(Qt::WA_UpdatesDisabled);
8016 if (q->testAttribute(Qt::WA_PendingMoveEvent)) {
8017 QMoveEvent e(data.crect.topLeft(), data.crect.topLeft());
8018 QCoreApplication::sendEvent(q, &e);
8019 q->setAttribute(Qt::WA_PendingMoveEvent,
false);
8022 if (q->testAttribute(Qt::WA_PendingResizeEvent)) {
8023 QResizeEvent e(data.crect.size(), QSize());
8024 QCoreApplication::sendEvent(q, &e);
8025 q->setAttribute(Qt::WA_PendingResizeEvent,
false);
8029 q->setAttribute(Qt::WA_UpdatesDisabled,
false);
8034 for (
int i = 0; i < children.size(); ++i) {
8035 if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
8036 child->d_func()->sendPendingMoveAndResizeEvents(recursive, disableUpdates);
8040void QWidgetPrivate::activateChildLayoutsRecursively()
8042 sendPendingMoveAndResizeEvents(
false,
true);
8044 for (
int i = 0; i < children.size(); ++i) {
8045 QWidget *child = qobject_cast<QWidget *>(children.at(i));
8046 if (!child || child->isHidden() || child->isWindow())
8049 child->ensurePolished();
8052 QWidgetPrivate *childPrivate = child->d_func();
8053 if (childPrivate->layout)
8054 childPrivate->layout->activate();
8057 const bool wasVisible = child->isVisible();
8059 child->setAttribute(Qt::WA_WState_Visible);
8062 childPrivate->activateChildLayoutsRecursively();
8066 child->setAttribute(Qt::WA_WState_Visible,
false);
8070void QWidgetPrivate::show_helper()
8073 data.in_show =
true;
8075 sendPendingMoveAndResizeEvents();
8078 q->setAttribute(Qt::WA_WState_Visible);
8081 showChildren(
false);
8085 const bool isWindow = q->isWindow();
8086#if QT_CONFIG(graphicsview)
8087 bool isEmbedded = isWindow && q->graphicsProxyWidget() !=
nullptr;
8089 bool isEmbedded =
false;
8095 if (isWindow && !isEmbedded) {
8096 if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
8098 if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
8099 q->setAttribute(Qt::WA_KeyboardFocusChange);
8101 while (QApplication::activePopupWidget()) {
8102 if (!QApplication::activePopupWidget()->close())
8110#if QT_CONFIG(graphicsview)
8112 if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
8113 QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
8114 if (ancestorProxy) {
8116 ancestorProxy->d_func()->embedSubWindow(q);
8121 Q_UNUSED(isEmbedded);
8125 QShowEvent showEvent;
8126 QCoreApplication::sendEvent(q, &showEvent);
8130 if (!isEmbedded && q->windowType() == Qt::Popup)
8131 qApp->d_func()->openPopup(q);
8133#if QT_CONFIG(accessibility)
8134 if (q->windowType() != Qt::ToolTip) {
8135 QAccessibleEvent event(q, QAccessible::ObjectShow);
8136 QAccessible::updateAccessibility(&event);
8140 if (QApplicationPrivate::hidden_focus_widget == q) {
8141 QApplicationPrivate::hidden_focus_widget =
nullptr;
8142 q->setFocus(Qt::OtherFocusReason);
8148 if (!
qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
8149 QCoreApplication::processEvents();
8151 data.in_show =
false;
8154void QWidgetPrivate::show_sys()
8158 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8160 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8161 invalidateBackingStore(q->rect());
8162 q->setAttribute(Qt::WA_Mapped);
8164 if (window && q->isWindow()
8165#if QT_CONFIG(graphicsview)
8166 && (!extra || !extra->proxyWidget)
8168 && q->windowModality() != Qt::NonModal) {
8169 QGuiApplicationPrivate::showModalWindow(window);
8174 if (renderToTexture && !q->isWindow())
8175 QCoreApplication::postEvent(q->parentWidget(),
new QUpdateLaterEvent(q->geometry()));
8177 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(q->rect()));
8179 if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow))
8180 || q->testAttribute(Qt::WA_OutsideWSRange)) {
8186 fixPosIncludesFrame();
8187 QRect geomRect = q->geometry();
8188 if (!q->isWindow()) {
8189 QPoint topLeftOfWindow = q->mapTo(q->nativeParentWidget(),QPoint());
8190 geomRect.moveTopLeft(topLeftOfWindow);
8192 const QRect windowRect = window->geometry();
8193 if (windowRect != geomRect) {
8194 if (q->testAttribute(Qt::WA_Moved)
8195 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
8196 window->setGeometry(geomRect);
8198 window->resize(geomRect.size());
8202 qt_qpa_set_cursor(q,
false);
8204 invalidateBackingStore(q->rect());
8205 window->setNativeWindowVisibility(
true);
8207 if (window->isTopLevel()) {
8208 const QPoint crectTopLeft = q->data->crect.topLeft();
8209 const QPoint windowTopLeft = window->geometry().topLeft();
8210 if (crectTopLeft == QPoint(0, 0) && windowTopLeft != crectTopLeft)
8211 q->data->crect.moveTopLeft(windowTopLeft);
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8233
8234void QWidgetPrivate::hide_helper()
8238 bool isEmbedded =
false;
8239#if QT_CONFIG(graphicsview)
8240 isEmbedded = q->isWindow() && !bypassGraphicsProxyWidget(q) && nearestGraphicsProxyWidget(q->parentWidget()) !=
nullptr;
8242 Q_UNUSED(isEmbedded);
8245 if (!isEmbedded && (q->windowType() == Qt::Popup))
8246 qApp->d_func()->closePopup(q);
8248 q->setAttribute(Qt::WA_Mapped,
false);
8251 bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
8254 q->setAttribute(Qt::WA_WState_Visible,
false);
8258 QHideEvent hideEvent;
8259 QCoreApplication::sendEvent(q, &hideEvent);
8260 hideChildren(
false);
8265 qApp->d_func()->sendSyntheticEnterLeave(q);
8266 QWidget *fw = QApplication::focusWidget();
8267 while (fw && !fw->isWindow()) {
8269 q->focusNextPrevChild(
true);
8272 fw = fw->parentWidget();
8276 if (QWidgetRepaintManager *repaintManager = maybeRepaintManager())
8277 repaintManager->removeDirtyWidget(q);
8279#if QT_CONFIG(accessibility)
8281 QAccessibleEvent event(q, QAccessible::ObjectHide);
8282 QAccessible::updateAccessibility(&event);
8287void QWidgetPrivate::hide_sys()
8291 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8293 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8294 q->setAttribute(Qt::WA_Mapped,
false);
8296 if (window && q->isWindow()
8297#if QT_CONFIG(graphicsview)
8298 && (!extra || !extra->proxyWidget)
8300 && q->windowModality() != Qt::NonModal) {
8301 QGuiApplicationPrivate::hideModalWindow(window);
8306 deactivateWidgetCleanup();
8308 if (!q->isWindow()) {
8309 QWidget *p = q->parentWidget();
8310 if (p &&p->isVisible()) {
8311 if (renderToTexture)
8312 p->d_func()->invalidateBackingStore(q->geometry());
8314 invalidateBackingStore(q->rect());
8317 invalidateBackingStore(q->rect());
8321 window->setNativeWindowVisibility(
false);
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8347void QWidget::setVisible(
bool visible)
8350 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" <<
this
8351 <<
"with attributes" << WidgetAttributes{
this}
8352 <<
"to" << visible <<
"via QWidget";
8354 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) == !visible)
8357 if (d->dontSetExplicitShowHide) {
8358 d->dontSetExplicitShowHide =
false;
8361 setAttribute(Qt::WA_WState_ExplicitShowHide);
8364 d->setVisible(visible);
8370void QWidgetPrivate::setVisible(
bool visible)
8373 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" << q
8374 <<
"with attributes" << WidgetAttributes{q}
8375 <<
"to" << visible <<
"via QWidgetPrivate";
8379 if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
8380 && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
8381 q->parentWidget()->window()->d_func()->createRecursively();
8384 QWidget *pw = q->parentWidget();
8385 if (!q->testAttribute(Qt::WA_WState_Created)
8386 && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
8390 bool wasResized = q->testAttribute(Qt::WA_Resized);
8391 Qt::WindowStates initialWindowState = q->windowState();
8394 q->ensurePolished();
8397 bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
8399 q->setAttribute(Qt::WA_WState_Hidden,
false);
8401 if (needUpdateGeometry)
8402 updateGeometry_helper(
true);
8408 if (!q->isWindow()) {
8409 QWidget *parent = q->parentWidget();
8410 while (parent && parent->isVisible() && parent->d_func()->layout && !parent->data->in_show) {
8411 parent->d_func()->layout->activate();
8412 if (parent->isWindow())
8414 parent = parent->parentWidget();
8417 parent->d_func()->setDirtyOpaqueRegion();
8422 && (q->isWindow() || !q->parentWidget()->d_func()->layout)) {
8423 if (q->isWindow()) {
8425 if (q->windowState() != initialWindowState)
8426 q->setWindowState(initialWindowState);
8430 q->setAttribute(Qt::WA_Resized,
false);
8433 q->setAttribute(Qt::WA_KeyboardFocusChange,
false);
8435 if (q->isWindow() || q->parentWidget()->isVisible()) {
8438 qApp->d_func()->sendSyntheticEnterLeave(q);
8441 QEvent showToParentEvent(QEvent::ShowToParent);
8442 QCoreApplication::sendEvent(q, &showToParentEvent);
8444 if (QApplicationPrivate::hidden_focus_widget == q)
8445 QApplicationPrivate::hidden_focus_widget =
nullptr;
8451 if (!q->isWindow() && q->parentWidget())
8452 q->parentWidget()->d_func()->setDirtyOpaqueRegion();
8454 if (!q->testAttribute(Qt::WA_WState_Hidden)) {
8455 q->setAttribute(Qt::WA_WState_Hidden);
8460 if (!q->isWindow() && q->parentWidget()) {
8461 if (q->parentWidget()->d_func()->layout)
8462 q->parentWidget()->d_func()->layout->invalidate();
8463 else if (q->parentWidget()->isVisible())
8464 QCoreApplication::postEvent(q->parentWidget(),
new QEvent(QEvent::LayoutRequest));
8467 QEvent hideToParentEvent(QEvent::HideToParent);
8468 QCoreApplication::sendEvent(q, &hideToParentEvent);
8473
8474
8475void QWidget::setHidden(
bool hidden)
8477 setVisible(!hidden);
8480bool QWidgetPrivate::isExplicitlyHidden()
const
8483 return q->isHidden() && q->testAttribute(Qt::WA_WState_ExplicitShowHide);
8486void QWidgetPrivate::_q_showIfNotHidden()
8489 if (!isExplicitlyHidden())
8490 q->setVisible(
true);
8493void QWidgetPrivate::showChildren(
bool spontaneous)
8496 qCDebug(lcWidgetShowHide) <<
"Showing children of" << q
8497 <<
"spontaneously" << spontaneous;
8499 QList<QObject*> childList = children;
8500 for (
int i = 0; i < childList.size(); ++i) {
8501 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8504 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8505 <<
"with attributes" << WidgetAttributes{widget};
8506 if (widget->windowHandle() && !widget->testAttribute(Qt::WA_WState_ExplicitShowHide))
8507 widget->setAttribute(Qt::WA_WState_Hidden,
false);
8508 if (widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden))
8511 widget->setAttribute(Qt::WA_Mapped);
8512 widget->d_func()->showChildren(
true);
8514 QApplication::sendSpontaneousEvent(widget, &e);
8516 if (widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) {
8517 widget->d_func()->show_recursive();
8523 widget->d_func()->dontSetExplicitShowHide =
true;
8524 widget->setVisible(
true);
8525 widget->d_func()->dontSetExplicitShowHide =
false;
8531void QWidgetPrivate::hideChildren(
bool spontaneous)
8534 qCDebug(lcWidgetShowHide) <<
"Hiding children of" << q
8535 <<
"spontaneously" << spontaneous;
8537 QList<QObject*> childList = children;
8538 for (
int i = 0; i < childList.size(); ++i) {
8539 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8542 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8543 <<
"with attributes" << WidgetAttributes{widget};
8544 if (widget->isWindow() || !widget->isVisible())
8548 widget->setAttribute(Qt::WA_Mapped,
false);
8550 widget->setAttribute(Qt::WA_WState_Visible,
false);
8551 widget->d_func()->hideChildren(spontaneous);
8554 QApplication::sendSpontaneousEvent(widget, &e);
8556 QCoreApplication::sendEvent(widget, &e);
8557 if (widget->internalWinId()
8558 && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) {
8561 widget->d_func()->hide_sys();
8564 qApp->d_func()->sendSyntheticEnterLeave(widget);
8565#if QT_CONFIG(accessibility)
8567 QAccessibleEvent event(widget, QAccessible::ObjectHide);
8568 QAccessible::updateAccessibility(&event);
8576 if (QWidget* widgetWindow = q->window();
8577 widgetWindow && widgetWindow->data->is_closing) {
8578 q->setAttribute(Qt::WA_UnderMouse,
false);
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600bool QWidgetPrivate::handleClose(CloseMode mode)
8603 qCDebug(lcWidgetShowHide) <<
"Handling close event for" << q;
8605 if (data.is_closing)
8609 data.is_closing =
true;
8611 QPointer<QWidget> that = q;
8613 if (data.in_destructor)
8614 mode = CloseNoEvent;
8616 if (mode != CloseNoEvent) {
8618 if (mode == CloseWithSpontaneousEvent)
8619 QApplication::sendSpontaneousEvent(q, &e);
8621 QCoreApplication::sendEvent(q, &e);
8622 if (!that.isNull() && !e.isAccepted()) {
8623 data.is_closing =
false;
8629 if (!that.isNull() && !q->isHidden())
8632 if (!that.isNull()) {
8633 data.is_closing =
false;
8634 if (q->testAttribute(Qt::WA_DeleteOnClose)) {
8635 q->setAttribute(Qt::WA_DeleteOnClose,
false);
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8665bool QWidget::close()
8667 return d_func()->close();
8670bool QWidgetPrivate::close()
8680 if (QWindow *widgetWindow = windowHandle()) {
8681 if (widgetWindow->isTopLevel())
8682 return widgetWindow->close();
8685 return handleClose(QWidgetPrivate::CloseWithEvent);
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8746bool QWidget::isVisibleTo(
const QWidget *ancestor)
const
8750 const QWidget * w =
this;
8751 while (!w->isHidden()
8753 && w->parentWidget()
8754 && w->parentWidget() != ancestor)
8755 w = w->parentWidget();
8756 return !w->isHidden();
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770QRegion QWidget::visibleRegion()
const
8774 QRect clipRect = d->clipRect();
8775 if (clipRect.isEmpty())
8777 QRegion r(clipRect);
8778 d->subtractOpaqueChildren(r, clipRect);
8779 d->subtractOpaqueSiblings(r);
8784QSize QWidgetPrivate::adjustedSize()
const
8788 QSize s = q->sizeHint();
8790 if (q->isWindow()) {
8791 Qt::Orientations exp;
8793 if (layout->hasHeightForWidth())
8794 s.setHeight(layout->totalHeightForWidth(s.width()));
8795 exp = layout->expandingDirections();
8798 if (q->sizePolicy().hasHeightForWidth())
8799 s.setHeight(q->heightForWidth(s.width()));
8800 exp = q->sizePolicy().expandingDirections();
8802 if (exp & Qt::Horizontal)
8803 s.setWidth(qMax(s.width(), 200));
8804 if (exp & Qt::Vertical)
8805 s.setHeight(qMax(s.height(), 100));
8808 if (
const QScreen *screenAtPoint = QGuiApplication::screenAt(q->pos()))
8809 screen = screenAtPoint->geometry();
8811 screen = QGuiApplication::primaryScreen()->geometry();
8813 s.setWidth(qMin(s.width(), screen.width()*2/3));
8814 s.setHeight(qMin(s.height(), screen.height()*2/3));
8816 if (QTLWExtra *extra = maybeTopData())
8817 extra->sizeAdjusted =
true;
8821 QRect r = q->childrenRect();
8824 s = r.size() + QSize(2 * r.x(), 2 * r.y());
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8846void QWidget::adjustSize()
8850 QSize s = d->adjustedSize();
8853 d->layout->activate();
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8875QSize QWidget::sizeHint()
const
8879 return d->layout->totalSizeHint();
8880 return QSize(-1, -1);
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902QSize QWidget::minimumSizeHint()
const
8906 return d->layout->totalMinimumSize();
8907 return QSize(-1, -1);
8912
8913
8914
8915
8916
8920
8921
8922
8923
8925bool QWidget::isAncestorOf(
const QWidget *child)
const
8930 if (child->isWindow())
8932 child = child->parentWidget();
8938
8939
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8969bool QWidget::event(QEvent *event)
8975 switch(event->type()) {
8976 case QEvent::TabletPress:
8977 case QEvent::TabletRelease:
8978 case QEvent::TabletMove:
8979 case QEvent::MouseButtonPress:
8980 case QEvent::MouseButtonRelease:
8981 case QEvent::MouseButtonDblClick:
8982 case QEvent::MouseMove:
8983 case QEvent::TouchBegin:
8984 case QEvent::TouchUpdate:
8985 case QEvent::TouchEnd:
8986 case QEvent::TouchCancel:
8987 case QEvent::ContextMenu:
8988 case QEvent::KeyPress:
8989 case QEvent::KeyRelease:
8990#if QT_CONFIG(wheelevent)
8998 switch (event->type()) {
8999 case QEvent::PlatformSurface: {
9001 switch (
static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
9002 case QPlatformSurfaceEvent::SurfaceCreated:
9003 if (!testAttribute(Qt::WA_WState_Created))
9006 case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
9007 if (testAttribute(Qt::WA_WState_Created)) {
9010 destroy(
false,
false);
9016 case QEvent::MouseMove:
9017 mouseMoveEvent((QMouseEvent*)event);
9020 case QEvent::MouseButtonPress:
9021 mousePressEvent((QMouseEvent*)event);
9024 case QEvent::MouseButtonRelease:
9025 mouseReleaseEvent((QMouseEvent*)event);
9028 case QEvent::MouseButtonDblClick:
9029 mouseDoubleClickEvent((QMouseEvent*)event);
9031#if QT_CONFIG(wheelevent)
9033 wheelEvent((QWheelEvent*)event);
9036#if QT_CONFIG(tabletevent)
9037 case QEvent::TabletMove:
9038 if (
static_cast<QTabletEvent *>(event)->buttons() == Qt::NoButton && !testAttribute(Qt::WA_TabletTracking))
9041 case QEvent::TabletPress:
9042 case QEvent::TabletRelease:
9043 tabletEvent((QTabletEvent*)event);
9046 case QEvent::KeyPress: {
9047 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
9049 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
9050 if (k->key() == Qt::Key_Backtab
9051 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
9052 res = focusNextPrevChild(
false);
9053 else if (k->key() == Qt::Key_Tab)
9054 res = focusNextPrevChild(
true);
9059#ifdef QT_KEYPAD_NAVIGATION
9060 if (!k->isAccepted() && QApplication::keypadNavigationEnabled()
9061 && !(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::ShiftModifier))) {
9062 if (QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder) {
9063 if (k->key() == Qt::Key_Up)
9064 res = focusNextPrevChild(
false);
9065 else if (k->key() == Qt::Key_Down)
9066 res = focusNextPrevChild(
true);
9067 }
else if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
9068 if (k->key() == Qt::Key_Up)
9069 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionNorth);
9070 else if (k->key() == Qt::Key_Right)
9071 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionEast);
9072 else if (k->key() == Qt::Key_Down)
9073 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionSouth);
9074 else if (k->key() == Qt::Key_Left)
9075 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionWest);
9083#if QT_CONFIG(whatsthis)
9084 if (!k->isAccepted()
9085 && k->modifiers() & Qt::ShiftModifier && k->key() == Qt::Key_F1
9086 && d->whatsThis.size()) {
9087 QWhatsThis::showText(mapToGlobal(inputMethodQuery(Qt::ImCursorRectangle).toRect().center()), d->whatsThis,
this);
9094 case QEvent::KeyRelease:
9095 keyReleaseEvent((QKeyEvent*)event);
9097 case QEvent::ShortcutOverride:
9100 case QEvent::InputMethod:
9101 inputMethodEvent((QInputMethodEvent *) event);
9104 case QEvent::InputMethodQuery: {
9105 QInputMethodQueryEvent *query =
static_cast<QInputMethodQueryEvent *>(event);
9106 Qt::InputMethodQueries queries = query->queries();
9107 for (uint i = 0; i < 32; ++i) {
9108 Qt::InputMethodQuery q = (Qt::InputMethodQuery)(
int)(queries & (1<<i));
9110 QVariant v = inputMethodQuery(q);
9111 if (q == Qt::ImEnabled && !v.isValid() && isEnabled()) {
9118 v = QVariant(testAttribute(Qt::WA_InputMethodEnabled));
9120 query->setValue(q, v);
9127 case QEvent::PolishRequest:
9131 case QEvent::Polish: {
9132 style()->polish(
this);
9133 setAttribute(Qt::WA_WState_Polished);
9134 if (!QApplication::font(
this).isCopyOf(QApplication::font()))
9136 if (!QApplication::palette(
this).isCopyOf(QGuiApplication::palette()))
9137 d->resolvePalette();
9141 case QEvent::ApplicationWindowIconChange:
9142 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
9143 d->setWindowIcon_sys();
9144 d->setWindowIcon_helper();
9147 case QEvent::FocusIn:
9148 focusInEvent((QFocusEvent*)event);
9149 d->updateWidgetTransform(event);
9152 case QEvent::FocusOut:
9153 focusOutEvent((QFocusEvent*)event);
9157#if QT_CONFIG(statustip)
9158 if (d->statusTip.size()) {
9159 QStatusTipEvent tip(d->statusTip);
9160 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9163 enterEvent(
static_cast<QEnterEvent*>(event));
9167#if QT_CONFIG(statustip)
9168 if (d->statusTip.size()) {
9170 QStatusTipEvent tip(empty);
9171 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9177 case QEvent::HoverEnter:
9178 case QEvent::HoverLeave:
9186 paintEvent((QPaintEvent*)event);
9190 moveEvent((QMoveEvent*)event);
9191 d->updateWidgetTransform(event);
9194 case QEvent::Resize:
9195 resizeEvent((QResizeEvent*)event);
9196 d->updateWidgetTransform(event);
9200 closeEvent((QCloseEvent *)event);
9203#ifndef QT_NO_CONTEXTMENU
9204 case QEvent::ContextMenu:
9205 switch (data->context_menu_policy) {
9206 case Qt::PreventContextMenu:
9208 case Qt::DefaultContextMenu:
9209 contextMenuEvent(
static_cast<QContextMenuEvent *>(event));
9211 case Qt::CustomContextMenu:
9212 emit customContextMenuRequested(
static_cast<QContextMenuEvent *>(event)->pos());
9215 case Qt::ActionsContextMenu:
9216 if (d->actions.size()) {
9217 QMenu::exec(d->actions,
static_cast<QContextMenuEvent *>(event)->globalPos(),
9230#if QT_CONFIG(draganddrop)
9232 dropEvent((QDropEvent*) event);
9235 case QEvent::DragEnter:
9236 dragEnterEvent((QDragEnterEvent*) event);
9239 case QEvent::DragMove:
9240 dragMoveEvent((QDragMoveEvent*) event);
9243 case QEvent::DragLeave:
9244 dragLeaveEvent((QDragLeaveEvent*) event);
9249 showEvent((QShowEvent*) event);
9253 hideEvent((QHideEvent*) event);
9256 case QEvent::ShowWindowRequest:
9261 case QEvent::ApplicationFontChange:
9264 case QEvent::ApplicationPaletteChange:
9265 d->resolvePalette();
9268 case QEvent::ToolBarChange:
9269 case QEvent::ActivationChange:
9270 case QEvent::EnabledChange:
9271 case QEvent::FontChange:
9272 case QEvent::StyleChange:
9273 case QEvent::PaletteChange:
9274 case QEvent::WindowTitleChange:
9275 case QEvent::IconTextChange:
9276 case QEvent::ModifiedChange:
9277 case QEvent::MouseTrackingChange:
9278 case QEvent::TabletTrackingChange:
9279 case QEvent::ParentChange:
9280 case QEvent::LocaleChange:
9281 case QEvent::MacSizeChange:
9282 case QEvent::ContentsRectChange:
9283 case QEvent::ThemeChange:
9284 case QEvent::ReadOnlyChange:
9288 case QEvent::WindowStateChange: {
9289 const bool wasMinimized =
static_cast<
const QWindowStateChangeEvent *>(event)->oldState() & Qt::WindowMinimized;
9290 if (wasMinimized != isMinimized()) {
9291 QWidget *widget =
const_cast<QWidget *>(
this);
9294 if (!d->childrenShownByExpose) {
9296 d->showChildren(
true);
9297 QShowEvent showEvent;
9298 QCoreApplication::sendSpontaneousEvent(widget, &showEvent);
9300 d->childrenHiddenByWState =
false;
9302 QHideEvent hideEvent;
9303 QCoreApplication::sendSpontaneousEvent(widget, &hideEvent);
9304 d->hideChildren(
true);
9305 d->childrenHiddenByWState =
true;
9307 d->childrenShownByExpose =
false;
9313 case QEvent::WindowActivate:
9314 case QEvent::WindowDeactivate: {
9315 if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
9317 QList<QObject*> childList = d->children;
9318 for (
int i = 0; i < childList.size(); ++i) {
9319 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9320 if (w && w->isVisible() && !w->isWindow())
9321 QCoreApplication::sendEvent(w, event);
9325 case QEvent::LanguageChange:
9328 QList<QObject*> childList = d->children;
9329 for (
int i = 0; i < childList.size(); ++i) {
9330 QObject *o = childList.at(i);
9332 QCoreApplication::sendEvent(o, event);
9338 case QEvent::ApplicationLayoutDirectionChange:
9339 d->resolveLayoutDirection();
9342 case QEvent::LayoutDirectionChange:
9344 d->layout->invalidate();
9348 case QEvent::UpdateRequest:
9349 d->syncBackingStore();
9351 case QEvent::UpdateLater:
9352 update(
static_cast<QUpdateLaterEvent*>(event)->region());
9354 case QEvent::StyleAnimationUpdate:
9355 if (isVisible() && !window()->isMinimized()) {
9361 case QEvent::WindowBlocked:
9362 case QEvent::WindowUnblocked:
9363 if (!d->children.isEmpty()) {
9364 QWidget *modalWidget = QApplication::activeModalWidget();
9365 for (
int i = 0; i < d->children.size(); ++i) {
9366 QObject *o = d->children.at(i);
9367 if (o && o != modalWidget && o->isWidgetType()) {
9368 QWidget *w =
static_cast<QWidget *>(o);
9371 QCoreApplication::sendEvent(w, event);
9376#if QT_CONFIG(tooltip)
9377 case QEvent::ToolTip:
9378 if (!d->toolTip.isEmpty())
9379 QToolTip::showText(
static_cast<QHelpEvent*>(event)->globalPos(), d->toolTip,
this, QRect(), d->toolTipDuration);
9384#if QT_CONFIG(whatsthis)
9385 case QEvent::WhatsThis:
9386 if (d->whatsThis.size())
9387 QWhatsThis::showText(
static_cast<QHelpEvent *>(event)->globalPos(), d->whatsThis,
this);
9391 case QEvent::QueryWhatsThis:
9392 if (d->whatsThis.isEmpty())
9396 case QEvent::EmbeddingControl:
9397 d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
9398 data->fstrut_dirty =
false;
9401 case QEvent::ActionAdded:
9402 case QEvent::ActionRemoved:
9403 case QEvent::ActionChanged:
9404 actionEvent((QActionEvent*)event);
9408 case QEvent::KeyboardLayoutChange:
9413 QList<QObject*> childList = d->children;
9414 for (
int i = 0; i < childList.size(); ++i) {
9415 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9416 if (w && w->isVisible() && !w->isWindow())
9417 QCoreApplication::sendEvent(w, event);
9421 case QEvent::TouchBegin:
9422 case QEvent::TouchUpdate:
9423 case QEvent::TouchEnd:
9424 case QEvent::TouchCancel:
9429#ifndef QT_NO_GESTURES
9430 case QEvent::Gesture:
9434 case QEvent::ScreenChangeInternal:
9435 if (
const QTLWExtra *te = d->maybeTopData()) {
9436 const QWindow *win = te->window;
9437 d->setWinId((win && win->handle()) ? win->handle()->winId() : 0);
9440 case QEvent::DevicePixelRatioChange:
9441 if (d->data.fnt.d->dpi != logicalDpiY())
9442 d->updateFont(d->data.fnt);
9443 d->renderToTextureReallyDirty = 1;
9445 case QEvent::DynamicPropertyChange: {
9446 const QByteArray &propName =
static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
9447 if (propName.size() == 13 && !qstrncmp(propName,
"_q_customDpi", 12)) {
9448 uint value = property(propName.constData()).toUInt();
9451 const char axis = propName.at(12);
9453 d->extra->customDpiX = value;
9454 else if (axis ==
'Y')
9455 d->extra->customDpiY = value;
9456 d->updateFont(d->data.fnt);
9458 if (windowHandle() && !qstrncmp(propName,
"_q_platform_", 12))
9459 windowHandle()->setProperty(propName, property(propName));
9463 return QObject::event(event);
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484void QWidget::changeEvent(QEvent * event)
9486 switch(event->type()) {
9487 case QEvent::EnabledChange: {
9489#if QT_CONFIG(accessibility)
9490 QAccessible::State s;
9492 QAccessibleStateChangeEvent event(
this, s);
9493 QAccessible::updateAccessibility(&event);
9498 case QEvent::FontChange:
9499 case QEvent::StyleChange: {
9504 d->layout->invalidate();
9508 case QEvent::PaletteChange:
9512 case QEvent::ThemeChange:
9513 if (QGuiApplication::desktopSettingsAware()
9514 &&
qApp && !QCoreApplication::closingDown()) {
9515 if (testAttribute(Qt::WA_WState_Polished))
9516 QApplication::style()->unpolish(
this);
9517 if (testAttribute(Qt::WA_WState_Polished))
9518 QApplication::style()->polish(
this);
9519 QEvent styleChangedEvent(QEvent::StyleChange);
9520 QCoreApplication::sendEvent(
this, &styleChangedEvent);
9527 case QEvent::MacSizeChange:
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9563void QWidget::mouseMoveEvent(QMouseEvent *event)
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9585void QWidget::mousePressEvent(QMouseEvent *event)
9588 if ((windowType() == Qt::Popup)) {
9591 while ((w = QApplication::activePopupWidget()) && w !=
this){
9593 if (QApplication::activePopupWidget() == w)
9596 if (!rect().contains(event->position().toPoint())){
9603
9604
9605
9606
9607
9608
9610void QWidget::mouseReleaseEvent(QMouseEvent *event)
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9632void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
9634 mousePressEvent(event);
9637#if QT_CONFIG(wheelevent)
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9652void QWidget::wheelEvent(QWheelEvent *event)
9658#if QT_CONFIG(tabletevent)
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9679void QWidget::tabletEvent(QTabletEvent *event)
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9708void QWidget::keyPressEvent(QKeyEvent *event)
9710#ifndef QT_NO_SHORTCUT
9711 if ((windowType() == Qt::Popup) && event->matches(QKeySequence::Cancel)) {
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9743void QWidget::keyReleaseEvent(QKeyEvent *event)
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9767void QWidget::focusInEvent(QFocusEvent *)
9769 if (focusPolicy() != Qt::NoFocus || !isWindow()) {
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9793void QWidget::focusOutEvent(QFocusEvent *)
9795 if (focusPolicy() != Qt::NoFocus || !isWindow())
9798#if !defined(QT_PLATFORM_UIKIT)
9800 if (
qApp->autoSipEnabled() && testAttribute(Qt::WA_InputMethodEnabled))
9801 QGuiApplication::inputMethod()->hide();
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9817void QWidget::enterEvent(QEnterEvent *)
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9833void QWidget::leaveEvent(QEvent *)
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9885void QWidget::paintEvent(QPaintEvent *)
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9903void QWidget::moveEvent(QMoveEvent *)
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9924void QWidget::resizeEvent(QResizeEvent * )
9930
9931
9932
9933
9934
9935
9936
9937void QWidget::actionEvent(QActionEvent *)
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9958void QWidget::closeEvent(QCloseEvent *event)
9963#ifndef QT_NO_CONTEXTMENU
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9977void QWidget::contextMenuEvent(QContextMenuEvent *event)
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000void QWidget::inputMethodEvent(QInputMethodEvent *event)
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015QVariant QWidget::inputMethodQuery(Qt::InputMethodQuery query)
const
10018 case Qt::ImCursorRectangle:
10019 return QRect(width()/2, 0, 1, height());
10022 case Qt::ImAnchorPosition:
10024 return inputMethodQuery(Qt::ImCursorPosition);
10026 return (
int)inputMethodHints();
10027 case Qt::ImInputItemClipRectangle:
10028 return d_func()->clipRect();
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059Qt::InputMethodHints QWidget::inputMethodHints()
const
10062 const QWidgetPrivate *priv = d_func();
10063 while (priv->inheritsInputMethodHints) {
10064 priv = priv->q_func()->parentWidget()->d_func();
10067 return priv->imHints;
10069 return Qt::ImhNone;
10073void QWidget::setInputMethodHints(Qt::InputMethodHints hints)
10077 if (d->imHints == hints)
10079 d->imHints = hints;
10080 if (
this == QGuiApplication::focusObject())
10081 QGuiApplication::inputMethod()->update(Qt::ImHints);
10088#if QT_CONFIG(draganddrop)
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104void QWidget::dragEnterEvent(QDragEnterEvent *)
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122void QWidget::dragMoveEvent(QDragMoveEvent *)
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138void QWidget::dragLeaveEvent(QDragLeaveEvent *)
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153void QWidget::dropEvent(QDropEvent *)
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178void QWidget::showEvent(QShowEvent *)
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200void QWidget::hideEvent(QHideEvent *)
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10231bool QWidget::nativeEvent(
const QByteArray &eventType,
void *message, qintptr *result)
10233 Q_UNUSED(eventType);
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262void QWidget::ensurePolished()
const
10264 Q_D(
const QWidget);
10266 const QMetaObject *m = metaObject();
10267 if (m == d->polished)
10271 QEvent e(QEvent::Polish);
10272 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &e);
10275 QList<QObject*> children = d->children;
10276 for (
int i = 0; i < children.size(); ++i) {
10277 QObject *o = children.at(i);
10278 if (!o->isWidgetType())
10280 if (QWidget *w = qobject_cast<QWidget *>(o))
10281 w->ensurePolished();
10284 if (d->parent && d->sendChildEvents) {
10285 QChildEvent e(QEvent::ChildPolished,
const_cast<QWidget *>(
this));
10286 QCoreApplication::sendEvent(d->parent, &e);
10291
10292
10293
10294
10295
10296QRegion QWidget::mask()
const
10298 Q_D(
const QWidget);
10299 return d->extra ? d->extra->mask : QRegion();
10303
10304
10305
10306
10307
10308
10309
10310
10311QLayout *QWidget::layout()
const
10313 return d_func()->layout;
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10342void QWidget::setLayout(QLayout *l)
10344 if (Q_UNLIKELY(!l)) {
10345 qWarning(
"QWidget::setLayout: Cannot set layout to 0");
10349 if (Q_UNLIKELY(layout() != l))
10350 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
10351 " layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
10352 objectName().toLocal8Bit().data());
10356 QObject *oldParent = l->parent();
10357 if (oldParent && oldParent !=
this) {
10358 if (oldParent->isWidgetType()) {
10361 QWidget *oldParentWidget =
static_cast<QWidget *>(oldParent);
10362 oldParentWidget->takeLayout();
10364 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
10365 l->objectName().toLocal8Bit().data(), metaObject()->className(),
10366 objectName().toLocal8Bit().data());
10372 l->d_func()->topLevel =
true;
10374 if (oldParent !=
this) {
10375 l->setParent(
this);
10376 l->d_func()->reparentChildWidgets(
this);
10380 if (isWindow() && d->maybeTopData())
10381 d->topData()->sizeAdjusted =
false;
10385
10386
10387
10388
10389
10391QLayout *QWidget::takeLayout()
10394 QLayout *l = layout();
10397 d->layout =
nullptr;
10398 l->setParent(
nullptr);
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427QSizePolicy QWidget::sizePolicy()
const
10429 Q_D(
const QWidget);
10430 return d->size_policy;
10433void QWidget::setSizePolicy(QSizePolicy policy)
10436 setAttribute(Qt::WA_WState_OwnSizePolicy);
10437 if (policy == d->size_policy)
10440 if (d->size_policy.retainSizeWhenHidden() != policy.retainSizeWhenHidden())
10441 d->retainSizeWhenHiddenChanged = 1;
10443 d->size_policy = policy;
10445#if QT_CONFIG(graphicsview)
10446 if (
const auto &extra = d->extra) {
10447 if (extra->proxyWidget)
10448 extra->proxyWidget->setSizePolicy(policy);
10453 d->retainSizeWhenHiddenChanged = 0;
10455 if (isWindow() && d->maybeTopData())
10456 d->topData()->sizeAdjusted =
false;
10460
10461
10462
10463
10464
10465
10466
10467
10470
10471
10472
10473
10474
10475
10476
10478int QWidget::heightForWidth(
int w)
const
10480 if (layout() && layout()->hasHeightForWidth())
10481 return layout()->totalHeightForWidth(w);
10487
10488
10489
10490
10491bool QWidget::hasHeightForWidth()
const
10493 Q_D(
const QWidget);
10494 return d->layout ? d->layout->hasHeightForWidth() : d->size_policy.hasHeightForWidth();
10498
10499
10500
10501
10502
10503
10506
10507
10508
10509
10510
10512QWidget *QWidget::childAt(
const QPoint &p)
const
10514 return d_func()->childAt_helper(QPointF(p),
false);
10518
10519
10520
10521
10522
10523
10525QWidget *QWidget::childAt(
const QPointF &p)
const
10527 return d_func()->childAt_helper(p,
false);
10530QWidget *QWidgetPrivate::childAt_helper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10532 if (children.isEmpty())
10535 if (!pointInsideRectAndMask(p))
10537 return childAtRecursiveHelper(p, ignoreChildrenInDestructor);
10540QWidget *QWidgetPrivate::childAtRecursiveHelper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10542 for (
int i = children.size() - 1; i >= 0; --i) {
10543 QWidget *child = qobject_cast<QWidget *>(children.at(i));
10544 if (!child || child->isWindow() || child->isHidden() || child->testAttribute(Qt::WA_TransparentForMouseEvents)
10545 || (ignoreChildrenInDestructor && child->data->in_destructor)) {
10550 QPointF childPoint = p;
10551 childPoint -= child->data->crect.topLeft();
10554 if (!child->d_func()->pointInsideRectAndMask(childPoint))
10558 if (QWidget *w = child->d_func()->childAtRecursiveHelper(childPoint, ignoreChildrenInDestructor))
10567void QWidgetPrivate::updateGeometry_helper(
bool forceUpdate)
10571 widgetItem->invalidateSizeCache();
10573 if (forceUpdate || !extra || extra->minw != extra->maxw || extra->minh != extra->maxh) {
10574 const int isHidden = q->isHidden() && !size_policy.retainSizeWhenHidden() && !retainSizeWhenHiddenChanged;
10576 if (!q->isWindow() && !isHidden && (parent = q->parentWidget())) {
10577 if (parent->d_func()->layout)
10578 parent->d_func()->layout->invalidate();
10579 else if (parent->isVisible())
10580 QCoreApplication::postEvent(parent,
new QEvent(QEvent::LayoutRequest));
10586
10587
10588
10589
10590
10591
10592
10593
10595void QWidget::updateGeometry()
10598 d->updateGeometry_helper(
false);
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619void QWidget::setWindowFlags(Qt::WindowFlags flags)
10622 d->setWindowFlags(flags);
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637void QWidget::setWindowFlag(Qt::WindowType flag,
bool on)
10641 d->setWindowFlags(data->window_flags | flag);
10643 d->setWindowFlags(data->window_flags & ~flag);
10647
10648
10649
10650void QWidgetPrivate::setWindowFlags(Qt::WindowFlags flags)
10653 QT_IGNORE_DEPRECATIONS(
10654 if (flags.testFlag(Qt::WindowType::Desktop)) {
10655 qWarning() <<
"Qt::WindowType::Desktop has been deprecated in Qt 6. Ignoring.";
10656 flags.setFlag(Qt::WindowType::Desktop,
false);
10660 if (q->data->window_flags == flags)
10663 if ((q->data->window_flags | flags) & Qt::Window) {
10665 QPoint oldPos = q->pos();
10666 bool visible = q->isVisible();
10667 const bool windowFlagChanged = (q->data->window_flags ^ flags) & Qt::Window;
10668 q->setParent(q->parentWidget(), flags);
10672 if (!windowFlagChanged && (visible || q->testAttribute(Qt::WA_Moved)))
10675 adjustQuitOnCloseAttribute();
10677 q->data->window_flags = flags;
10682
10683
10684
10685
10686
10687
10688
10689
10690void QWidget::overrideWindowFlags(Qt::WindowFlags flags)
10692 data->window_flags = flags;
10696
10697
10698
10699
10700
10701
10702
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732void QWidget::setParent(QWidget *parent)
10734 if (parent == parentWidget())
10736 setParent((QWidget*)parent, windowFlags() & ~Qt::WindowType_Mask);
10742 if (d->renderToTexture) {
10743 QEvent e(eventType);
10744 QCoreApplication::sendEvent(widget, &e);
10747 for (
int i = 0; i < d->children.size(); ++i) {
10749 if (w && !w->isWindow())
10750 qSendWindowChangeToTextureChildrenRecursively(w, eventType);
10754 if (
auto *window = d->windowHandle(QWidgetPrivate::WindowHandleMode::Direct)) {
10755 QEvent e(eventType);
10756 QCoreApplication::sendEvent(window, &e);
10761
10762
10763
10764
10766void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
10769 Q_ASSERT_X(
this != parent, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
10771 const auto checkForParentChildLoops = qScopeGuard([&](){
10773 auto p = parentWidget();
10775 if (++depth == QObjectPrivate::CheckForParentChildLoopsWarnDepth) {
10776 qWarning(
"QWidget %p (class: '%s', object name: '%s') may have a loop in its parent-child chain; "
10777 "this is undefined behavior",
10778 this, metaObject()->className(), qPrintable(objectName()));
10780 p = p->parentWidget();
10785 const bool resized = testAttribute(Qt::WA_Resized);
10786 const bool wasCreated = testAttribute(Qt::WA_WState_Created);
10787 QWidget *oldtlw = window();
10789 QWidget *oldParentWithWindow = d->closestParentWidgetWithWindowHandle();
10791 if (f & Qt::Window)
10792 d->data.fstrut_dirty =
true;
10794 bool newParent = (parent != parentWidget());
10796 if (newParent && parent) {
10797 if (testAttribute(Qt::WA_NativeWindow) && !QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings))
10798 parent->d_func()->enforceNativeChildren();
10799 else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen))
10800 setAttribute(Qt::WA_NativeWindow);
10804 if (!testAttribute(Qt::WA_WState_Hidden)) {
10819 setAttribute(Qt::WA_WState_ExplicitShowHide,
false);
10822 QEvent e(QEvent::ParentAboutToChange);
10823 QCoreApplication::sendEvent(
this, &e);
10829 const bool oldWidgetUsesRhiFlush = oldParentWithWindow ? oldParentWithWindow->d_func()->usesRhiFlush
10830 : oldtlw->d_func()->usesRhiFlush;
10831 if (oldWidgetUsesRhiFlush && ((!parent && parentWidget()) || (parent && parent->window() != oldtlw)))
10832 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowAboutToChangeInternal);
10836 if (newParent && isAncestorOf(focusWidget()) && !(f & Qt::Window))
10837 focusWidget()->clearFocus();
10839 d->setParent_sys(parent, f);
10841 if (d->textureChildSeen && parent) {
10843 QWidgetPrivate::get(parent)->setTextureChildSeen();
10846 if (QWidgetRepaintManager *oldPaintManager = oldtlw->d_func()->maybeRepaintManager()) {
10848 oldPaintManager->removeDirtyWidget(
this);
10851 oldPaintManager->moveStaticWidgets(
this);
10854 d->reparentFocusWidgets(oldtlw);
10855 setAttribute(Qt::WA_Resized, resized);
10857 const bool useStyleSheetPropagationInWidgetStyles =
10858 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
10860 if (!useStyleSheetPropagationInWidgetStyles && !testAttribute(Qt::WA_StyleSheet)
10861 && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
10864 const auto pd = parent->d_func();
10865 d->inheritedFontResolveMask = pd->directFontResolveMask | pd->inheritedFontResolveMask;
10866 d->inheritedPaletteResolveMask = pd->directPaletteResolveMask | pd->inheritedPaletteResolveMask;
10869 d->resolvePalette();
10871 d->resolveLayoutDirection();
10872 d->resolveLocale();
10878 if (newParent || !wasCreated
10879#if QT_CONFIG(opengles2)
10880 || (f & Qt::MSWindowsOwnDC)
10885 if (!testAttribute(Qt::WA_ForceDisabled))
10886 d->setEnabled_helper(parent ? parent->isEnabled() :
true);
10887 if (!testAttribute(Qt::WA_ForceUpdatesDisabled))
10888 d->setUpdatesEnabled_helper(parent ? parent->updatesEnabled() :
true);
10893 if (parent && d->sendChildEvents) {
10894 QChildEvent e(QEvent::ChildAdded,
this);
10895 QCoreApplication::sendEvent(parent, &e);
10898 if (parent && d->sendChildEvents && d->polished) {
10899 QChildEvent e(QEvent::ChildPolished,
this);
10900 QCoreApplication::sendEvent(parent, &e);
10903 QEvent e(QEvent::ParentChange);
10904 QCoreApplication::sendEvent(
this, &e);
10909 if (oldWidgetUsesRhiFlush && oldtlw != window())
10910 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowChangeInternal);
10913 if (isWindow() || parentWidget()->isVisible())
10914 setAttribute(Qt::WA_WState_Hidden,
true);
10915 else if (!testAttribute(Qt::WA_WState_ExplicitShowHide))
10916 setAttribute(Qt::WA_WState_Hidden,
false);
10919 d->updateIsOpaque();
10921#if QT_CONFIG(graphicsview)
10924 if (oldtlw->graphicsProxyWidget()) {
10925 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
10926 ancestorProxy->d_func()->unembedSubWindow(
this);
10928 if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(
this)) {
10929 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
10930 ancestorProxy->d_func()->embedSubWindow(
this);
10934 if (d->extra && d->extra->hasWindowContainer)
10935 QWindowContainer::parentWasChanged(
this);
10937 QWidget *newParentWithWindow = d->closestParentWidgetWithWindowHandle();
10938 if (newParentWithWindow && newParentWithWindow != oldParentWithWindow) {
10940 qCDebug(lcWidgetPainting) <<
"Evaluating whether reparenting of" <<
this
10941 <<
"into" << parent <<
"requires RHI enablement for" << newParentWithWindow;
10943 QPlatformBackingStoreRhiConfig rhiConfig;
10944 QSurface::SurfaceType surfaceType = QSurface::RasterSurface;
10950 if (q_evaluateRhiConfig(
this, &rhiConfig, &surfaceType)) {
10954 if (q_evaluateRhiConfig(newParentWithWindow,
nullptr,
nullptr)) {
10957 auto *existingWindow = newParentWithWindow->windowHandle();
10958 auto existingSurfaceType = existingWindow->surfaceType();
10959 if (existingSurfaceType != surfaceType) {
10960 qCDebug(lcWidgetPainting)
10961 <<
"Recreating" << existingWindow
10962 <<
"with current type" << existingSurfaceType
10963 <<
"to support" << surfaceType;
10964 const auto windowStateBeforeDestroy = newParentWithWindow->windowState();
10965 const auto visibilityBeforeDestroy = newParentWithWindow->isVisible();
10966 const auto positionBeforeDestroy = newParentWithWindow->pos();
10967 newParentWithWindow->destroy();
10968 newParentWithWindow->create();
10969 Q_ASSERT(newParentWithWindow->windowHandle());
10970 newParentWithWindow->windowHandle()->setWindowStates(windowStateBeforeDestroy);
10971 newParentWithWindow->move(positionBeforeDestroy);
10972 QWidgetPrivate::get(newParentWithWindow)->setVisible(visibilityBeforeDestroy);
10973 }
else if (
auto *backingStore = newParentWithWindow->backingStore()) {
10976 backingStore->handle()->createRhi(existingWindow, rhiConfig);
10978 QWidgetPrivate::get(newParentWithWindow)->usesRhiFlush =
true;
10984#if QT_CONFIG(accessibility)
10985 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing) {
10986 QAccessibleEvent qaEvent(
this, QAccessible::ParentChanged);
10987 QAccessible::updateAccessibility(&qaEvent);
10993void QWidgetPrivate::setParent_sys(QWidget *newparent, Qt::WindowFlags f)
10997 Qt::WindowFlags oldFlags = data.window_flags;
10998 bool wasCreated = q->testAttribute(Qt::WA_WState_Created);
11000 QScreen *targetScreen =
nullptr;
11006 targetScreen = q->parentWidget()->window()->screen();
11009 const bool destroyWindow = (
11011 (oldFlags & Qt::Window) && !(f & Qt::Window)
11013 && wasCreated && !q->testAttribute(Qt::WA_NativeWindow)
11016 if (parent != newparent) {
11018 QObjectPrivate::setParent_helper(newparent);
11020 if (q->windowHandle())
11021 q->windowHandle()->setFlags(f);
11025 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11028 if (destroyWindow) {
11029 reparentWidgetWindowChildren(parentWithWindow);
11039 reparentWidgetWindows(parentWithWindow, f);
11043 bool explicitlyHidden = isExplicitlyHidden();
11045 if (destroyWindow) {
11046 if (extra && extra->hasWindowContainer)
11047 QWindowContainer::toplevelAboutToBeDestroyed(q);
11051 if (!q->windowHandle()->children().isEmpty()) {
11052 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11053 QWindow *newParentWindow = parentWithWindow ? parentWithWindow->windowHandle() :
nullptr;
11054 for (QObject *child : q->windowHandle()->children()) {
11055 if (QWindow *childWindow = qobject_cast<QWindow *>(child)) {
11056 qCWarning(lcWidgetWindow) <<
"Reparenting" << childWindow
11057 <<
"before destroying" <<
this;
11058 childWindow->setParent(newParentWindow);
11066 q->destroy(
true,
false);
11070 data.window_flags = f;
11071 q->setAttribute(Qt::WA_WState_Created,
false);
11072 q->setAttribute(Qt::WA_WState_Visible,
false);
11073 q->setAttribute(Qt::WA_WState_Hidden,
false);
11075 if (newparent && wasCreated && (q->testAttribute(Qt::WA_NativeWindow) || (f & Qt::Window)))
11078 if (q->isWindow() || (!newparent || newparent->isVisible()) || explicitlyHidden)
11079 q->setAttribute(Qt::WA_WState_Hidden);
11080 q->setAttribute(Qt::WA_WState_ExplicitShowHide, explicitlyHidden);
11083 if (!newparent && targetScreen) {
11085 if (q->testAttribute(Qt::WA_WState_Created))
11086 q->windowHandle()->setScreen(targetScreen);
11088 topData()->initialScreen = targetScreen;
11092void QWidgetPrivate::reparentWidgetWindows(QWidget *parentWithWindow, Qt::WindowFlags windowFlags)
11094 if (QWindow *window = windowHandle()) {
11096 if (parentWithWindow) {
11097 if (windowFlags & Qt::Window) {
11100 QWidget *topLevel = parentWithWindow->window();
11101 auto *transientParent = topLevel->windowHandle();
11102 Q_ASSERT(transientParent);
11103 qCDebug(lcWidgetWindow) <<
"Setting" << window <<
"transient parent to" << transientParent;
11104 window->setTransientParent(transientParent);
11105 window->setParent(
nullptr);
11107 auto *parentWindow = parentWithWindow->windowHandle();
11108 qCDebug(lcWidgetWindow) <<
"Reparenting" << window <<
"into" << parentWindow;
11109 window->setTransientParent(
nullptr);
11110 window->setParent(parentWindow);
11113 qCDebug(lcWidgetWindow) <<
"Making" << window <<
"top level window";
11114 window->setTransientParent(
nullptr);
11115 window->setParent(
nullptr);
11118 reparentWidgetWindowChildren(parentWithWindow);
11122void QWidgetPrivate::reparentWidgetWindowChildren(QWidget *parentWithWindow)
11124 for (
auto *child : std::as_const(children)) {
11125 if (
auto *childWidget = qobject_cast<QWidget*>(child)) {
11126 auto *childPrivate = QWidgetPrivate::get(childWidget);
11129 childPrivate->reparentWidgetWindows(parentWithWindow, childWidget->windowFlags());
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11155void QWidget::scroll(
int dx,
int dy)
11157 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11159 if (dx == 0 && dy == 0)
11162#if QT_CONFIG(graphicsview)
11163 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11167 for (
const QRect &rect : d->dirty)
11168 proxy->update(rect.translated(dx, dy));
11169 proxy->scroll(dx, dy, proxy->subWidgetRect(
this));
11170 d->scrollChildren(dx, dy);
11174 d->setDirtyOpaqueRegion();
11175 d->scroll_sys(dx, dy);
11178void QWidgetPrivate::scroll_sys(
int dx,
int dy)
11181 scrollChildren(dx, dy);
11182 scrollRect(q->rect(), dx, dy);
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195void QWidget::scroll(
int dx,
int dy,
const QRect &r)
11198 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11200 if (dx == 0 && dy == 0)
11203#if QT_CONFIG(graphicsview)
11204 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11208 if (!d->dirty.isEmpty()) {
11209 for (
const QRect &rect : d->dirty.translated(dx, dy) & r)
11210 proxy->update(rect);
11212 proxy->scroll(dx, dy, r.translated(proxy->subWidgetRect(
this).topLeft().toPoint()));
11216 d->scroll_sys(dx, dy, r);
11219void QWidgetPrivate::scroll_sys(
int dx,
int dy,
const QRect &r)
11221 scrollRect(r, dx, dy);
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11240void QWidget::repaint()
11246
11247
11248
11249
11250
11251
11252
11253void QWidget::repaint(
int x,
int y,
int w,
int h)
11255 if (x > data->crect.width() || y > data->crect.height())
11259 w = data->crect.width() - x;
11261 h = data->crect.height() - y;
11263 repaint(QRect(x, y, w, h));
11267
11268
11269
11270void QWidget::repaint(
const QRect &rect)
11277
11278
11279
11280
11281void QWidget::repaint(
const QRegion &rgn)
11287template <
typename T>
11288void QWidgetPrivate::repaint(T r)
11292 if (!q->isVisible() || !q->updatesEnabled() || r.isEmpty())
11295 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11296 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11297 tlwExtra->repaintManager->markDirty(r, q, QWidgetRepaintManager::UpdateNow);
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318void QWidget::update()
11324
11325
11326
11327
11328
11331
11332
11333
11334
11335void QWidget::update(
const QRect &rect)
11342
11343
11344
11345
11346void QWidget::update(
const QRegion &rgn)
11352template <
typename T>
11353void QWidgetPrivate::update(T r)
11357 if (renderToTexture && !q->isVisible()) {
11358 renderToTextureReallyDirty = 1;
11362 if (!q->isVisible() || !q->updatesEnabled())
11365 T clipped = r & q->rect();
11367 if (clipped.isEmpty())
11370 if (q->testAttribute(Qt::WA_WState_InPaintEvent)) {
11371 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(clipped));
11375 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11376 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11377 tlwExtra->repaintManager->markDirty(clipped, q);
11381
11382
11383
11384
11388 if (attribute <
int(8*
sizeof(uint))) {
11390 data->widget_attributes |= (1<<attribute);
11392 data->widget_attributes &= ~(1<<attribute);
11394 const int x = attribute - 8*
sizeof(uint);
11395 const int int_off = x / (8*
sizeof(uint));
11397 d->high_attributes[int_off] |= (1<<(x-(int_off*8*
sizeof(uint))));
11399 d->high_attributes[int_off] &= ~(1<<(x-(int_off*8*
sizeof(uint))));
11404void QWidgetPrivate::macUpdateSizeAttribute()
11407 QEvent event(QEvent::MacSizeChange);
11408 QCoreApplication::sendEvent(q, &event);
11409 for (
int i = 0; i < children.size(); ++i) {
11410 QWidget *w = qobject_cast<QWidget *>(children.at(i));
11411 if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
11412 && !w->testAttribute(Qt::WA_MacMiniSize)
11413 && !w->testAttribute(Qt::WA_MacSmallSize)
11414 && !w->testAttribute(Qt::WA_MacNormalSize))
11415 w->d_func()->macUpdateSizeAttribute();
11422
11423
11424
11425
11426
11427void QWidget::setAttribute(Qt::WidgetAttribute attribute,
bool on)
11431 if (attribute == Qt::WA_ContentsMarginsRespectsSafeArea) {
11433 auto *topExtra = d->topData();
11434 topExtra->explicitContentsMarginsRespectsSafeArea =
true;
11438 if (testAttribute(attribute) == on)
11441 static_assert(
sizeof(d->high_attributes)*8 >= (Qt::WA_AttributeCount -
sizeof(uint)*8),
11442 "QWidget::setAttribute(WidgetAttribute, bool): "
11443 "QWidgetPrivate::high_attributes[] too small to contain all attributes in WidgetAttribute");
11446 if (attribute == Qt::WA_PaintOnScreen && on && !inherits(
"QGLWidget")) {
11449 if (d->noPaintOnScreen)
11455 if (attribute == Qt::WA_NativeWindow && !d->mustHaveWindowHandle) {
11456 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
11457 if (!platformIntegration->hasCapability(QPlatformIntegration::NativeWidgets))
11461 setAttribute_internal(attribute, on, data, d);
11463 switch (attribute) {
11465#if QT_CONFIG(draganddrop)
11466 case Qt::WA_AcceptDrops: {
11467 if (on && !testAttribute(Qt::WA_DropSiteRegistered))
11468 setAttribute(Qt::WA_DropSiteRegistered,
true);
11469 else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)))
11470 setAttribute(Qt::WA_DropSiteRegistered,
false);
11471 QEvent e(QEvent::AcceptDropsChange);
11472 QCoreApplication::sendEvent(
this, &e);
11475 case Qt::WA_DropSiteRegistered: {
11476 for (
int i = 0; i < d->children.size(); ++i) {
11477 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
11478 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_AcceptDrops) && w->testAttribute(Qt::WA_DropSiteRegistered) != on)
11479 w->setAttribute(Qt::WA_DropSiteRegistered, on);
11485 case Qt::WA_NoChildEventsForParent:
11486 d->sendChildEvents = !on;
11488 case Qt::WA_NoChildEventsFromChildren:
11489 d->receiveChildEvents = !on;
11491 case Qt::WA_MacNormalSize:
11492 case Qt::WA_MacSmallSize:
11493 case Qt::WA_MacMiniSize:
11497 const Qt::WidgetAttribute MacSizes[] = { Qt::WA_MacNormalSize, Qt::WA_MacSmallSize,
11498 Qt::WA_MacMiniSize };
11499 for (
int i = 0; i < 3; ++i) {
11500 if (MacSizes[i] != attribute)
11501 setAttribute_internal(MacSizes[i],
false, data, d);
11503 d->macUpdateSizeAttribute();
11507 case Qt::WA_ShowModal:
11510 data->window_modality = Qt::NonModal;
11511 }
else if (data->window_modality == Qt::NonModal) {
11514 data->window_modality = Qt::ApplicationModal;
11519 if (testAttribute(Qt::WA_WState_Created)) {
11524 case Qt::WA_MouseTracking: {
11525 QEvent e(QEvent::MouseTrackingChange);
11526 QCoreApplication::sendEvent(
this, &e);
11528 case Qt::WA_TabletTracking: {
11529 QEvent e(QEvent::TabletTrackingChange);
11530 QCoreApplication::sendEvent(
this, &e);
11532 case Qt::WA_NativeWindow: {
11533 d->createTLExtra();
11535 d->createTLSysExtra();
11537 QWidget *focusWidget = d->effectiveFocusWidget();
11538 if (on && !internalWinId() &&
this == QGuiApplication::focusObject()
11539 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11540 QGuiApplication::inputMethod()->commit();
11541 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11543 if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget())
11544 parentWidget()->d_func()->enforceNativeChildren();
11545 if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created))
11547 if (isEnabled() && focusWidget->isEnabled() &&
this == QGuiApplication::focusObject()
11548 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11549 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11554 case Qt::WA_PaintOnScreen:
11555 d->updateIsOpaque();
11557 case Qt::WA_OpaquePaintEvent:
11558 d->updateIsOpaque();
11560 case Qt::WA_NoSystemBackground:
11561 d->updateIsOpaque();
11563 case Qt::WA_UpdatesDisabled:
11564 d->updateSystemBackground();
11566 case Qt::WA_TransparentForMouseEvents:
11568 case Qt::WA_InputMethodEnabled: {
11570 if (QGuiApplication::focusObject() ==
this) {
11572 QGuiApplication::inputMethod()->commit();
11573 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11578 case Qt::WA_WindowPropagation:
11579 d->resolvePalette();
11581 d->resolveLocale();
11583 case Qt::WA_DontShowOnScreen: {
11584 if (on && isVisible()) {
11594 case Qt::WA_X11NetWmWindowTypeDesktop:
11595 case Qt::WA_X11NetWmWindowTypeDock:
11596 case Qt::WA_X11NetWmWindowTypeToolBar:
11597 case Qt::WA_X11NetWmWindowTypeMenu:
11598 case Qt::WA_X11NetWmWindowTypeUtility:
11599 case Qt::WA_X11NetWmWindowTypeSplash:
11600 case Qt::WA_X11NetWmWindowTypeDialog:
11601 case Qt::WA_X11NetWmWindowTypeDropDownMenu:
11602 case Qt::WA_X11NetWmWindowTypePopupMenu:
11603 case Qt::WA_X11NetWmWindowTypeToolTip:
11604 case Qt::WA_X11NetWmWindowTypeNotification:
11605 case Qt::WA_X11NetWmWindowTypeCombo:
11606 case Qt::WA_X11NetWmWindowTypeDND:
11607 d->setNetWmWindowTypes();
11610 case Qt::WA_StaticContents:
11611 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
11613 repaintManager->addStaticWidget(
this);
11615 repaintManager->removeStaticWidget(
this);
11618 case Qt::WA_TranslucentBackground:
11620 setAttribute(Qt::WA_NoSystemBackground);
11621 d->updateIsTranslucent();
11624 case Qt::WA_AcceptTouchEvents:
11632
11633
11634
11635
11636
11637
11638bool QWidget::testAttribute_helper(Qt::WidgetAttribute attribute)
const
11640 Q_D(
const QWidget);
11641 const int x = attribute - 8*
sizeof(uint);
11642 const int int_off = x / (8*
sizeof(uint));
11643 return (d->high_attributes[int_off] & (1<<(x-(int_off*8*
sizeof(uint)))));
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671qreal QWidget::windowOpacity()
const
11673 Q_D(
const QWidget);
11674 return (isWindow() && d->maybeTopData()) ? d->maybeTopData()->opacity / 255. : 1.0;
11677void QWidget::setWindowOpacity(qreal opacity)
11683 opacity = qBound(qreal(0.0), opacity, qreal(1.0));
11684 QTLWExtra *extra = d->topData();
11685 extra->opacity = uint(opacity * 255);
11686 setAttribute(Qt::WA_WState_WindowOpacitySet);
11687 d->setWindowOpacity_sys(opacity);
11689 if (!testAttribute(Qt::WA_WState_Created))
11692#if QT_CONFIG(graphicsview)
11693 if (QGraphicsProxyWidget *proxy = graphicsProxyWidget()) {
11695 if (proxy->cacheMode() == QGraphicsItem::NoCache)
11697 else if (QGraphicsScene *scene = proxy->scene())
11698 scene->update(proxy->sceneBoundingRect());
11704void QWidgetPrivate::setWindowOpacity_sys(qreal level)
11707 if (q->windowHandle())
11708 q->windowHandle()->setOpacity(level);
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735bool QWidget::isWindowModified()
const
11737 return testAttribute(Qt::WA_WindowModified);
11740void QWidget::setWindowModified(
bool mod)
11743 setAttribute(Qt::WA_WindowModified, mod);
11745 d->setWindowModified_helper();
11747 QEvent e(QEvent::ModifiedChange);
11748 QCoreApplication::sendEvent(
this, &e);
11751void QWidgetPrivate::setWindowModified_helper()
11754 QWindow *window = q->windowHandle();
11757 QPlatformWindow *platformWindow = window->handle();
11758 if (!platformWindow)
11760 bool on = q->testAttribute(Qt::WA_WindowModified);
11761 if (!platformWindow->setWindowModified(on)) {
11762 if (Q_UNLIKELY(on && !q->windowTitle().contains(
"[*]"_L1)))
11763 qWarning(
"QWidget::setWindowModified: The window title does not contain a '[*]' placeholder");
11764 setWindowTitle_helper(q->windowTitle());
11765 setWindowIconText_helper(q->windowIconText());
11769#if QT_CONFIG(tooltip)
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788void QWidget::setToolTip(
const QString &s)
11793 QEvent event(QEvent::ToolTipChange);
11794 QCoreApplication::sendEvent(
this, &event);
11797QString QWidget::toolTip()
const
11799 Q_D(
const QWidget);
11804
11805
11806
11807
11808
11809
11810
11811
11812
11814void QWidget::setToolTipDuration(
int msec)
11817 d->toolTipDuration = msec;
11820int QWidget::toolTipDuration()
const
11822 Q_D(
const QWidget);
11823 return d->toolTipDuration;
11829#if QT_CONFIG(statustip)
11831
11832
11833
11834
11835
11836
11837
11838void QWidget::setStatusTip(
const QString &s)
11844QString QWidget::statusTip()
const
11846 Q_D(
const QWidget);
11847 return d->statusTip;
11851#if QT_CONFIG(whatsthis)
11853
11854
11855
11856
11857
11858
11859
11860
11861void QWidget::setWhatsThis(
const QString &s)
11867QString QWidget::whatsThis()
const
11869 Q_D(
const QWidget);
11870 return d->whatsThis;
11874#if QT_CONFIG(accessibility)
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896void QWidget::setAccessibleName(
const QString &name)
11899 if (d->accessibleName == name)
11902 d->accessibleName = name;
11903 QAccessibleEvent event(
this, QAccessible::NameChanged);
11904 QAccessible::updateAccessibility(&event);
11907QString QWidget::accessibleName()
const
11909 Q_D(
const QWidget);
11910 return d->accessibleName;
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929void QWidget::setAccessibleDescription(
const QString &description)
11932 if (d->accessibleDescription == description)
11935 d->accessibleDescription = description;
11936 QAccessibleEvent event(
this, QAccessible::DescriptionChanged);
11937 QAccessible::updateAccessibility(&event);
11940QString QWidget::accessibleDescription()
const
11942 Q_D(
const QWidget);
11943 return d->accessibleDescription;
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957void QWidget::setAccessibleIdentifier(
const QString &identifier)
11960 if (d->accessibleIdentifier == identifier)
11963 d->accessibleIdentifier = identifier;
11964 QAccessibleEvent event(
this, QAccessible::IdentifierChanged);
11965 QAccessible::updateAccessibility(&event);
11968QString QWidget::accessibleIdentifier()
const
11970 Q_D(
const QWidget);
11971 return d->accessibleIdentifier;
11976#ifndef QT_NO_SHORTCUT
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000int QWidget::grabShortcut(
const QKeySequence &key, Qt::ShortcutContext context)
12005 setAttribute(Qt::WA_GrabbedShortcut);
12006 return QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(
this, key, context, qWidgetShortcutContextMatcher);
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024void QWidget::releaseShortcut(
int id)
12028 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id,
this, 0);
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043void QWidget::setShortcutEnabled(
int id,
bool enable)
12047 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id,
this, 0);
12051
12052
12053
12054
12055
12056
12057
12058void QWidget::setShortcutAutoRepeat(
int id,
bool enable)
12062 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(enable, id,
this, 0);
12067
12068
12069
12070void QWidget::updateMicroFocus(Qt::InputMethodQuery query)
12072 if (
this == QGuiApplication::focusObject())
12073 QGuiApplication::inputMethod()->update(query);
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12088void QWidget::raise()
12092 QWidget *p = parentWidget();
12093 const int parentChildCount = p->d_func()->children.size();
12094 if (parentChildCount < 2)
12096 const int from = p->d_func()->children.indexOf(
this);
12097 Q_ASSERT(from >= 0);
12099 if (from != parentChildCount -1)
12100 p->d_func()->children.move(from, parentChildCount - 1);
12101 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12103 else if (from == parentChildCount - 1)
12106 QRegion region(rect());
12107 d->subtractOpaqueSiblings(region);
12108 d->invalidateBackingStore(region);
12110 if (testAttribute(Qt::WA_WState_Created))
12113 if (d->extra && d->extra->hasWindowContainer)
12114 QWindowContainer::parentWasRaised(
this);
12116 QEvent e(QEvent::ZOrderChange);
12117 QCoreApplication::sendEvent(
this, &e);
12120void QWidgetPrivate::raise_sys()
12123 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12124 q->windowHandle()->raise();
12125 }
else if (renderToTexture) {
12126 if (QWidget *p = q->parentWidget()) {
12127 setDirtyOpaqueRegion();
12128 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12134
12135
12136
12137
12138
12139
12140
12142void QWidget::lower()
12146 QWidget *p = parentWidget();
12147 const int parentChildCount = p->d_func()->children.size();
12148 if (parentChildCount < 2)
12150 const int from = p->d_func()->children.indexOf(
this);
12151 Q_ASSERT(from >= 0);
12154 p->d_func()->children.move(from, 0);
12155 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12157 else if (from == 0)
12160 if (testAttribute(Qt::WA_WState_Created))
12163 if (d->extra && d->extra->hasWindowContainer)
12164 QWindowContainer::parentWasLowered(
this);
12166 QEvent e(QEvent::ZOrderChange);
12167 QCoreApplication::sendEvent(
this, &e);
12170void QWidgetPrivate::lower_sys()
12173 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12174 Q_ASSERT(q->testAttribute(Qt::WA_WState_Created));
12175 q->windowHandle()->lower();
12176 }
else if (QWidget *p = q->parentWidget()) {
12177 setDirtyOpaqueRegion();
12178 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12183
12184
12185
12186
12187
12188
12189void QWidget::stackUnder(QWidget* w)
12192 QWidget *p = parentWidget();
12193 if (!w || isWindow() || p != w->parentWidget() ||
this == w)
12196 int from = p->d_func()->children.indexOf(
this);
12197 int to = p->d_func()->children.indexOf(w);
12198 Q_ASSERT(from >= 0);
12204 p->d_func()->children.move(from, to);
12205 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12207 else if (from == to)
12210 if (testAttribute(Qt::WA_WState_Created))
12211 d->stackUnder_sys(w);
12213 QEvent e(QEvent::ZOrderChange);
12214 QCoreApplication::sendEvent(
this, &e);
12217void QWidgetPrivate::stackUnder_sys(QWidget*)
12220 if (QWidget *p = q->parentWidget()) {
12221 setDirtyOpaqueRegion();
12222 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12227
12228
12229
12230
12231
12234
12235
12236
12239
12240
12241
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12257
12258
12259
12260
12261
12262
12263
12264
12266QRect QWidgetPrivate::frameStrut()
const
12268 Q_Q(
const QWidget);
12269 if (!q->isWindow() || q->testAttribute(Qt::WA_DontShowOnScreen)) {
12271 return QRect(0, 0, 1, 1);
12274 if (data.fstrut_dirty
12277 && q->testAttribute(Qt::WA_WState_Created))
12278 const_cast<QWidgetPrivate *>(
this)->updateFrameStrut();
12280 return maybeTopData() ? maybeTopData()->frameStrut : QRect();
12283void QWidgetPrivate::updateFrameStrut()
12286 if (q->data->fstrut_dirty) {
12287 if (QTLWExtra *te = maybeTopData()) {
12288 if (te->window && te->window->handle()) {
12289 const QMargins margins = te->window->frameMargins();
12290 if (!margins.isNull()) {
12291 te->frameStrut.setCoords(margins.left(), margins.top(), margins.right(), margins.bottom());
12292 q->data->fstrut_dirty =
false;
12299#ifdef QT_KEYPAD_NAVIGATION
12301
12302
12303
12304
12305
12306
12307
12308bool QWidgetPrivate::navigateToDirection(Direction direction)
12310 QWidget *targetWidget = widgetInNavigationDirection(direction);
12312 targetWidget->setFocus();
12313 return (targetWidget != 0);
12317
12318
12319
12320
12321
12322
12323
12324
12325QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
12327 const QWidget *sourceWidget = QApplication::focusWidget();
12330 const QRect sourceRect = sourceWidget->rect().translated(sourceWidget->mapToGlobal(QPoint()));
12331 const int sourceX =
12332 (direction == DirectionNorth || direction == DirectionSouth) ?
12333 (sourceRect.left() + (sourceRect.right() - sourceRect.left()) / 2)
12334 :(direction == DirectionEast ? sourceRect.right() : sourceRect.left());
12335 const int sourceY =
12336 (direction == DirectionEast || direction == DirectionWest) ?
12337 (sourceRect.top() + (sourceRect.bottom() - sourceRect.top()) / 2)
12338 :(direction == DirectionSouth ? sourceRect.bottom() : sourceRect.top());
12339 const QPoint sourcePoint(sourceX, sourceY);
12340 const QPoint sourceCenter = sourceRect.center();
12341 const QWidget *sourceWindow = sourceWidget->window();
12343 QWidget *targetWidget =
nullptr;
12344 int shortestDistance = INT_MAX;
12346 const auto targetCandidates = QApplication::allWidgets();
12347 for (QWidget *targetCandidate : targetCandidates) {
12349 const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));
12354 if (targetCandidate->focusProxy() || targetCandidateRect.isEmpty())
12358 if ( targetCandidate != sourceWidget
12360 && targetCandidate->focusPolicy() & Qt::TabFocus
12362 && !(direction == DirectionNorth && targetCandidateRect.bottom() > sourceRect.top())
12364 && !(direction == DirectionEast && targetCandidateRect.left() < sourceRect.right())
12366 && !(direction == DirectionSouth && targetCandidateRect.top() < sourceRect.bottom())
12368 && !(direction == DirectionWest && targetCandidateRect.right() > sourceRect.left())
12370 && targetCandidate->isEnabled()
12372 && targetCandidate->isVisible()
12374 && targetCandidate->window() == sourceWindow) {
12375 const int targetCandidateDistance = [](
const QPoint &sourcePoint,
12376 const QRect &targetCandidateRect) {
12379 if (p.x() < r.left())
12380 dx = r.left() - p.x();
12381 else if (p.x() > r.right())
12382 dx = p.x() - r.right();
12383 if (p.y() < r.top())
12384 dy = r.top() - p.y();
12385 else if (p.y() > r.bottom())
12386 dy = p.y() - r.bottom();
12389 if (targetCandidateDistance < shortestDistance) {
12390 shortestDistance = targetCandidateDistance;
12391 targetWidget = targetCandidate;
12395 return targetWidget;
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409bool QWidgetPrivate::canKeypadNavigate(Qt::Orientation orientation)
12411 return orientation == Qt::Horizontal?
12412 (QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionEast)
12413 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionWest))
12414 :(QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionNorth)
12415 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionSouth));
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429bool QWidgetPrivate::inTabWidget(QWidget *widget)
12431 for (QWidget *tabWidget = widget; tabWidget; tabWidget = tabWidget->parentWidget())
12432 if (qobject_cast<
const QTabWidget*>(tabWidget))
12439
12440
12441
12442
12443
12444
12445void QWidget::setBackingStore(QBackingStore *store)
12454 QTLWExtra *topData = d->topData();
12455 if (topData->backingStore == store)
12458 QBackingStore *oldStore = topData->backingStore;
12459 delete topData->backingStore;
12460 topData->backingStore = store;
12462 QWidgetRepaintManager *repaintManager = d->maybeRepaintManager();
12463 if (!repaintManager)
12467 if (repaintManager->backingStore() != oldStore && repaintManager->backingStore() != store)
12468 delete repaintManager->backingStore();
12469 repaintManager->setBackingStore(store);
12474
12475
12476
12477
12478QBackingStore *QWidget::backingStore()
const
12480 Q_D(
const QWidget);
12481 QTLWExtra *extra = d->maybeTopData();
12482 if (extra && extra->backingStore)
12483 return extra->backingStore;
12486 return window()->backingStore();
12491void QWidgetPrivate::getLayoutItemMargins(
int *left,
int *top,
int *right,
int *bottom)
const
12494 *left = (
int)leftLayoutItemMargin;
12496 *top = (
int)topLayoutItemMargin;
12498 *right = (
int)rightLayoutItemMargin;
12500 *bottom = (
int)bottomLayoutItemMargin;
12503void QWidgetPrivate::setLayoutItemMargins(
int left,
int top,
int right,
int bottom)
12505 if (leftLayoutItemMargin == left
12506 && topLayoutItemMargin == top
12507 && rightLayoutItemMargin == right
12508 && bottomLayoutItemMargin == bottom)
12512 leftLayoutItemMargin = (
signed char)left;
12513 topLayoutItemMargin = (
signed char)top;
12514 rightLayoutItemMargin = (
signed char)right;
12515 bottomLayoutItemMargin = (
signed char)bottom;
12516 q->updateGeometry();
12519void QWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element,
const QStyleOption *opt)
12522 QStyleOption myOpt;
12525 myOpt.rect.setRect(0, 0, 32768, 32768);
12529 QRect liRect = q->style()->subElementRect(element, opt, q);
12530 if (liRect.isValid()) {
12531 leftLayoutItemMargin = (
signed char)(opt->rect.left() - liRect.left());
12532 topLayoutItemMargin = (
signed char)(opt->rect.top() - liRect.top());
12533 rightLayoutItemMargin = (
signed char)(liRect.right() - opt->rect.right());
12534 bottomLayoutItemMargin = (
signed char)(liRect.bottom() - opt->rect.bottom());
12536 leftLayoutItemMargin = 0;
12537 topLayoutItemMargin = 0;
12538 rightLayoutItemMargin = 0;
12539 bottomLayoutItemMargin = 0;
12543void QWidgetPrivate::adjustQuitOnCloseAttribute()
12547 if (!q->parentWidget()) {
12548 Qt::WindowType type = q->windowType();
12549 if (type == Qt::Widget || type == Qt::SubWindow)
12551 if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
12552 q->setAttribute(Qt::WA_QuitOnClose,
false);
12556void QWidgetPrivate::sendComposeStatus(QWidget *w,
bool end)
12558 QWidgetPrivate *wd = QWidgetPrivate::get(w);
12559 if (!wd->textureChildSeen)
12564 wd->beginCompose();
12565 for (
int i = 0; i < wd->children.size(); ++i) {
12566 w = qobject_cast<QWidget *>(wd->children.at(i));
12567 if (w && !w->isWindow() && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen)
12568 sendComposeStatus(w, end);
12572Q_WIDGETS_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget)
12574 return widget->data;
12579 return widget->d_func();
12583#if QT_CONFIG(graphicsview)
12585
12586
12587
12588
12589
12590
12591
12592
12593QGraphicsProxyWidget *QWidget::graphicsProxyWidget()
const
12595 Q_D(
const QWidget);
12597 return d->extra->proxyWidget;
12603#ifndef QT_NO_GESTURES
12605
12606
12607
12608
12609
12610void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
12613 d->gestureContext.insert(gesture, flags);
12614 (
void)QGestureManager::instance();
12618
12619
12620
12621
12622
12623void QWidget::ungrabGesture(Qt::GestureType gesture)
12627 if (d->gestureContext.remove(gesture)) {
12628 if (QGestureManager *manager = QGestureManager::instance())
12629 manager->cleanupCachedGestures(
this, gesture);
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647void QWidget::destroy(
bool destroyWindow,
bool destroySubWindows)
12651 d->aboutToDestroy();
12652 if (!isWindow() && parentWidget())
12653 parentWidget()->d_func()->invalidateBackingStore(d->effectiveRectFor(geometry()));
12654 d->deactivateWidgetCleanup();
12656 if ((windowType() == Qt::Popup) &&
qApp)
12657 qApp->d_func()->closePopup(
this);
12659 if (
this ==
qApp->activeWindow())
12660 QApplicationPrivate::setActiveWindow(
nullptr);
12661 if (QWidget::mouseGrabber() ==
this)
12663 if (QWidget::keyboardGrabber() ==
this)
12666 setAttribute(Qt::WA_WState_Created,
false);
12668 if (destroySubWindows) {
12669 QObjectList childList(children());
12670 for (
int i = 0; i < childList.size(); i++) {
12671 QWidget *widget = qobject_cast<QWidget *>(childList.at(i));
12672 if (widget && widget->testAttribute(Qt::WA_NativeWindow)) {
12673 if (widget->windowHandle()) {
12679 if (destroyWindow) {
12680 d->deleteTLSysExtra();
12682 if (parentWidget() && parentWidget()->testAttribute(Qt::WA_WState_Created)) {
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700QPaintEngine *QWidget::paintEngine()
const
12702 qWarning(
"QWidget::paintEngine: Should no longer be called");
12719 const_cast<QWidgetPrivate *>(d_func())->noPaintOnScreen = 1;
12728 return window->handle() && !qt_window_private(window)->resizeEventPending;
12731#if QT_CONFIG(graphicsview)
12732static inline QGraphicsProxyWidget *graphicsProxyWidget(
const QWidget *w)
12734 QGraphicsProxyWidget *result =
nullptr;
12735 const QWidgetPrivate *d = qt_widget_private(
const_cast<QWidget *>(w));
12737 result = d->extra->proxyWidget;
12751 for ( ; w ; w = w->parentWidget()) {
12752#if QT_CONFIG(graphicsview)
12753 if (QGraphicsProxyWidget *qgpw = graphicsProxyWidget(w)) {
12754 if (
const QGraphicsScene *scene = qgpw->scene()) {
12755 const QList <QGraphicsView *> views = scene->views();
12756 if (!views.isEmpty()) {
12757 auto *viewP =
static_cast<QGraphicsViewPrivate *>(qt_widget_private(views.constFirst()));
12758 result.transform *= viewP->mapToViewTransform(qgpw);
12759 w = views.first()->viewport();
12764 QWindow *window = w->windowHandle();
12770 const auto &geometry = w->geometry();
12771 result.transform *= QTransform::fromTranslate(geometry.x(), geometry.y());
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788QPointF QWidget::mapToGlobal(
const QPointF &pos)
const
12790 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12791 const QPointF g = t.transform.map(pos);
12792 return t.window ? t.window->mapToGlobal(g) : g;
12796
12797
12798QPoint QWidget::mapToGlobal(
const QPoint &pos)
const
12800 return mapToGlobal(QPointF(pos)).toPoint();
12804
12805
12806
12807
12808
12809
12810
12811
12812QPointF QWidget::mapFromGlobal(
const QPointF &pos)
const
12814 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12815 const QPointF windowLocal = t.window ? t.window->mapFromGlobal(pos) : pos;
12816 return t.transform.inverted().map(windowLocal);
12820
12821
12822QPoint QWidget::mapFromGlobal(
const QPoint &pos)
const
12824 return mapFromGlobal(QPointF(pos)).toPoint();
12833 QWindow *window = w->windowHandle();
12835 if (
const QWidget *nativeParent = w->nativeParentWidget())
12836 window = nativeParent->windowHandle();
12840#ifndef QT_NO_CURSOR
12851#ifndef QT_NO_CURSOR
12854 QGuiApplication::setOverrideCursor(*cursor);
12857 window->setMouseGrabEnabled(
true);
12867#ifndef QT_NO_CURSOR
12869 QGuiApplication::restoreOverrideCursor();
12873 window->setMouseGrabEnabled(
false);
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907void QWidget::grabMouse()
12909 grabMouseForWidget(
this);
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928#ifndef QT_NO_CURSOR
12929void QWidget::grabMouse(
const QCursor &cursor)
12931 grabMouseForWidget(
this, &cursor);
12935bool QWidgetPrivate::stealMouseGrab(
bool grab)
12940 QWindow *window = grabberWindow(q);
12941 return window ? window->setMouseGrabEnabled(grab) :
false;
12945
12946
12947
12948
12949
12950
12951void QWidget::releaseMouse()
12953 releaseMouseGrabOfWidget(
this);
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975void QWidget::grabKeyboard()
12978 keyboardGrb->releaseKeyboard();
12979 if (QWindow *window = grabberWindow(
this))
12980 window->setKeyboardGrabEnabled(
true);
12981 keyboardGrb =
this;
12984bool QWidgetPrivate::stealKeyboardGrab(
bool grab)
12989 QWindow *window = grabberWindow(q);
12990 return window ? window->setKeyboardGrabEnabled(grab) :
false;
12994
12995
12996
12997
12998
12999
13000void QWidget::releaseKeyboard()
13002 if (keyboardGrb ==
this) {
13003 if (QWindow *window = grabberWindow(
this))
13004 window->setKeyboardGrabEnabled(
false);
13005 keyboardGrb =
nullptr;
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019QWidget *QWidget::mouseGrabber()
13021 return qt_mouseGrb;
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034QWidget *QWidget::keyboardGrabber()
13036 return keyboardGrb;
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063void QWidget::activateWindow()
13065 QWindow *
const wnd = window()->windowHandle();
13068 wnd->requestActivate();
13072
13073
13074
13075
13076
13077
13078int QWidget::metric(PaintDeviceMetric m)
const
13080 QScreen *screen =
this->screen();
13083 if (m == PdmDpiX || m == PdmDpiY)
13085 return QPaintDevice::metric(m);
13088 auto resolveDevicePixelRatio = [
this, screen]() -> qreal {
13091 static bool downscale = qEnvironmentVariableIntValue(
"QT_WIDGETS_HIGHDPI_DOWNSCALE") > 0;
13092 QWindow *window =
this->window()->windowHandle();
13094 return downscale ? std::ceil(window->devicePixelRatio()) : window->devicePixelRatio();
13095 return screen->devicePixelRatio();
13100 return data->crect.width();
13102 return data->crect.width() * screen->physicalSize().width() / screen->geometry().width();
13104 return data->crect.height();
13106 return data->crect.height() * screen->physicalSize().height() / screen->geometry().height();
13108 return screen->depth();
13110 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13111 if (p->d_func()->extra && p->d_func()->extra->customDpiX)
13112 return p->d_func()->extra->customDpiX;
13114 return qRound(screen->logicalDotsPerInchX());
13116 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13117 if (p->d_func()->extra && p->d_func()->extra->customDpiY)
13118 return p->d_func()->extra->customDpiY;
13120 return qRound(screen->logicalDotsPerInchY());
13121 case PdmPhysicalDpiX:
13122 return qRound(screen->physicalDotsPerInchX());
13123 case PdmPhysicalDpiY:
13124 return qRound(screen->physicalDotsPerInchY());
13125 case PdmDevicePixelRatio:
13126 return resolveDevicePixelRatio();
13127 case PdmDevicePixelRatioScaled:
13128 return QPaintDevice::devicePixelRatioFScale() * resolveDevicePixelRatio();
13129 case PdmDevicePixelRatioF_EncodedA:
13131 case PdmDevicePixelRatioF_EncodedB:
13132 return QPaintDevice::encodeMetricF(m, resolveDevicePixelRatio());
13136 return QPaintDevice::metric(m);
13140
13141
13142
13143
13144void QWidget::initPainter(QPainter *painter)
const
13146 const QPalette &pal = palette();
13147 painter->d_func()->state->pen = QPen(pal.brush(foregroundRole()), 1);
13148 painter->d_func()->state->bgBrush = pal.brush(backgroundRole());
13149 QFont f(font(),
this);
13150 painter->d_func()->state->deviceFont = f;
13151 painter->d_func()->state->font = f;
13155
13156
13157
13158
13159QPaintDevice *QWidget::redirected(QPoint *offset)
const
13161 return d_func()->redirected(offset);
13165
13166
13167
13168
13169QPainter *QWidget::sharedPainter()
const
13172 if (!d_func()->redirectDev)
13175 QPainter *sp = d_func()->sharedPainter();
13176 if (!sp || !sp->isActive())
13179 if (sp->paintEngine()->paintDevice() != d_func()->redirectDev)
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207void QWidget::setMask(
const QRegion &newMask)
13212 if (newMask == d->extra->mask)
13215#ifndef QT_NO_BACKINGSTORE
13216 const QRegion oldMask(d->extra->mask);
13219 d->extra->mask = newMask;
13220 d->extra->hasMask = !newMask.isEmpty();
13222 if (!testAttribute(Qt::WA_WState_Created))
13225 d->setMask_sys(newMask);
13227#ifndef QT_NO_BACKINGSTORE
13231 if (!d->extra->hasMask) {
13233 QRegion expose(rect());
13235 if (!expose.isEmpty()) {
13236 d->setDirtyOpaqueRegion();
13244 QRegion parentExpose(rect());
13245 parentExpose -= newMask;
13246 if (!parentExpose.isEmpty()) {
13247 d->setDirtyOpaqueRegion();
13248 parentExpose.translate(data->crect.topLeft());
13249 parentWidget()->update(parentExpose);
13253 if (!oldMask.isEmpty())
13254 update(newMask - oldMask);
13259void QWidgetPrivate::setMask_sys(
const QRegion ®ion)
13262 if (QWindow *window = q->windowHandle())
13263 window->setMask(region);
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291void QWidget::setMask(
const QBitmap &bitmap)
13293 setMask(QRegion(bitmap));
13297
13298
13299
13300
13301
13302
13303void QWidget::clearMask()
13306 if (!d->extra || !d->extra->hasMask)
13308 setMask(QRegion());
13311void QWidgetPrivate::setWidgetParentHelper(QObject *widgetAsObject, QObject *newParent)
13313 Q_ASSERT(widgetAsObject->isWidgetType());
13314 Q_ASSERT(!newParent || newParent->isWidgetType());
13315 QWidget *widget =
static_cast<QWidget*>(widgetAsObject);
13316 widget->setParent(
static_cast<QWidget*>(newParent));
13319std::string QWidgetPrivate::flagsForDumping()
const
13321 Q_Q(
const QWidget);
13322 std::string flags = QObjectPrivate::flagsForDumping();
13323 if (QApplication::focusWidget() == q)
13325 if (q->isVisible()) {
13326 std::stringstream s;
13328 << q->width() <<
'x' << q->height()
13329 << std::showpos << q->x() << q->y()
13338void QWidgetPrivate::setNetWmWindowTypes(
bool skipIfMissing)
13343 if (!q->windowHandle())
13346 QXcbWindow::WindowTypes wmWindowType = QXcbWindow::None;
13347 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDesktop))
13348 wmWindowType |= QXcbWindow::Desktop;
13349 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDock))
13350 wmWindowType |= QXcbWindow::Dock;
13351 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolBar))
13352 wmWindowType |= QXcbWindow::Toolbar;
13353 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeMenu))
13354 wmWindowType |= QXcbWindow::Menu;
13355 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeUtility))
13356 wmWindowType |= QXcbWindow::Utility;
13357 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeSplash))
13358 wmWindowType |= QXcbWindow::Splash;
13359 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDialog))
13360 wmWindowType |= QXcbWindow::Dialog;
13361 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu))
13362 wmWindowType |= QXcbWindow::DropDownMenu;
13363 if (q->testAttribute(Qt::WA_X11NetWmWindowTypePopupMenu))
13364 wmWindowType |= QXcbWindow::PopupMenu;
13365 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip))
13366 wmWindowType |= QXcbWindow::Tooltip;
13367 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeNotification))
13368 wmWindowType |= QXcbWindow::Notification;
13369 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeCombo))
13370 wmWindowType |= QXcbWindow::Combo;
13371 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDND))
13372 wmWindowType |= QXcbWindow::Dnd;
13374 if (wmWindowType == QXcbWindow::None && skipIfMissing)
13377 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(q->windowHandle()->handle()))
13378 xcbWindow->setWindowType(wmWindowType);
13380 Q_UNUSED(skipIfMissing);
13385
13386
13387
13388
13389bool QWidgetPrivate::hasChildWithFocusPolicy(Qt::FocusPolicy policy,
const QWidget *excludeChildrenOf)
const
13391 Q_Q(
const QWidget);
13392 const QWidgetList &children = q->findChildren<QWidget *>(Qt::FindChildrenRecursively);
13393 for (
const auto *child : children) {
13394 if (child->focusPolicy() == policy && child->isEnabled()
13395 && (!excludeChildrenOf || !excludeChildrenOf->isAncestorOf(child))) {
13402#ifndef QT_NO_DEBUG_STREAM
13405QDebug operator<<(QDebug debug,
const WidgetAttributes &attributes)
13407 const QDebugStateSaver saver(debug);
13410 if (
const QWidget *widget = attributes.widget) {
13411 const QMetaObject *qtMo = qt_getEnumMetaObject(Qt::WA_AttributeCount);
13412 const QMetaEnum me = qtMo->enumerator(qtMo->indexOfEnumerator(
"WidgetAttribute"));
13414 for (
int a = 0; a < Qt::WA_AttributeCount; ++a) {
13415 if (widget->testAttribute(
static_cast<Qt::WidgetAttribute>(a))) {
13418 debug << me.valueToKey(a);
13429 const QDebugStateSaver saver(debug);
13432 debug << widget->metaObject()->className() <<
'(' << (
const void *)widget;
13433 if (!widget->objectName().isEmpty())
13434 debug <<
", name=" << widget->objectName();
13435 if (debug.verbosity() > 2) {
13436 const QRect geometry = widget->geometry();
13437 const QRect frameGeometry = widget->frameGeometry();
13438 if (widget->isVisible())
13439 debug <<
", visible";
13440 if (!widget->isEnabled())
13441 debug <<
", disabled";
13442 debug <<
", states=" << widget->windowState()
13443 <<
", type=" << widget->windowType() <<
", flags=" << widget->windowFlags();
13444 debug <<
", attributes=" << WidgetAttributes{widget};
13445 if (widget->isWindow())
13446 debug <<
", window";
13447 debug <<
", " << geometry.width() <<
'x' << geometry.height()
13448 << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
13449 if (frameGeometry != geometry) {
13450 const QMargins margins(geometry.x() - frameGeometry.x(),
13451 geometry.y() - frameGeometry.y(),
13452 frameGeometry.right() - geometry.right(),
13453 frameGeometry.bottom() - geometry.bottom());
13454 debug <<
", margins=" << margins;
13456 debug <<
", devicePixelRatio=" << widget->devicePixelRatio();
13457 if (
const WId wid = widget->internalWinId())
13458 debug <<
", winId=0x" << Qt::hex << wid << Qt::dec;
13462 debug <<
"QWidget(0x0)";
13471#define FOCUS_NEXT(w) w->d_func()->focus_next
13472#define FOCUS_PREV(w) w->d_func()->focus_prev
13475
13476
13477
13478
13479QWidget *QWidgetPrivate::nextPrevElementInFocusChain(FocusDirection direction)
const
13481 Q_Q(
const QWidget);
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496bool QWidgetPrivate::removeFromFocusChain(FocusChainRemovalRules rules, FocusDirection direction)
13499 if (!isFocusChainConsistent()) {
13501 if (rules.testFlag(FocusChainRemovalRule::AssertConsistency))
13502 qFatal() << q <<
"has inconsistent focus chain.";
13504 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because of inconsistent focus chain.";
13508 if (!isInFocusChain()) {
13509 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because it is not part of a focus chain.";
13513 if (rules.testFlag(FocusChainRemovalRule::EnsureFocusOut))
13514 q->focusNextPrevChild(direction == FocusDirection::Next);
13519 qCDebug(lcWidgetFocus) << q <<
"removed from focus chain.";
13524
13525
13526
13527void QWidgetPrivate::initFocusChain()
13530 qCDebug(lcWidgetFocus) <<
"Initializing focus chain of" << q;
13536
13537
13538
13539
13540
13541
13542void QWidgetPrivate::reparentFocusChildren(FocusDirection direction)
13547 QWidget *firstOld =
nullptr;
13548 QWidget *lastOld =
nullptr;
13549 QWidget *lastNew = q;
13550 bool prevWasNew =
true;
13551 QWidget *widget = nextPrevElementInFocusChain(direction);
13556 while (widget != q) {
13557 bool currentIsNew = q->isAncestorOf(widget);
13558 if (currentIsNew) {
13578 widget = widget->d_func()->nextPrevElementInFocusChain(direction);
13579 prevWasNew = currentIsNew;
13588 if (!q->isWindow()) {
13589 QWidget *topLevel = q->window();
13604
13605
13606
13607
13608
13609bool QWidgetPrivate::insertIntoFocusChain(FocusDirection direction, QWidget *position)
13612 Q_ASSERT(position);
13616 switch (direction) {
13617 case FocusDirection::Next:
13618 if (previous == position) {
13619 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already before" << position;
13623 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13629 qCDebug(lcWidgetFocus) << q <<
"inserted after" << position;
13632 case FocusDirection::Previous:
13633 if (next == position) {
13634 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already after" << position;
13638 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13644 qCDebug(lcWidgetFocus) << q <<
"inserted before" << position;
13648 Q_ASSERT(isFocusChainConsistent());
13653
13654
13655
13656
13657
13658
13659
13660bool QWidgetPrivate::insertIntoFocusChain(
const QWidgetList &toBeInserted,
13661 FocusDirection direction, QWidget *position)
13663 if (toBeInserted.isEmpty()) {
13664 qCDebug(lcWidgetFocus) <<
"No-op insertion of an empty list";
13668 Q_ASSERT_X(!toBeInserted.contains(position),
13670 "Coding error: toBeInserted contains position");
13672 QWidget *first = toBeInserted.constFirst();
13673 QWidget *last = toBeInserted.constLast();
13676 if (toBeInserted.count() == 1)
13677 return first->d_func()->insertIntoFocusChain(direction, position);
13679 Q_ASSERT(first != last);
13680 switch (direction) {
13681 case FocusDirection::Previous:
13683 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already before"
13691 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted before" << position;
13693 case FocusDirection::Next:
13695 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already after"
13703 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted after" << position;
13707 Q_ASSERT(position->d_func()->isFocusChainConsistent());
13712
13713
13714
13715
13718 QWidgetList path({from});
13724 switch (direction) {
13725 case QWidgetPrivate::FocusDirection::Previous:
13726 current = current->previousInFocusChain();
13728 case QWidgetPrivate::FocusDirection::Next:
13729 current = current->nextInFocusChain();
13732 if (path.contains(current))
13733 return QWidgetList();
13735 }
while (current != to);
13741
13742
13743
13744
13745
13746QWidgetList QWidgetPrivate::takeFromFocusChain(QWidget *from,
13748 FocusDirection direction)
13751 const QWidgetList path = focusPath(from, to , direction);
13752 if (path.isEmpty()) {
13753 qCDebug(lcWidgetFocus) <<
"No-op removal. Focus chain from" << from <<
"doesn't lead to " << to;
13754 return QWidgetList();
13757 QWidget *first = path.constFirst();
13758 QWidget *last = path.constLast();
13759 if (first == last) {
13760 first->d_func()->removeFromFocusChain();
13761 return QWidgetList({first});
13768 qCDebug(lcWidgetFocus) << path <<
"removed from focus chain";
13773
13774
13775
13776
13777QWidget *QWidgetPrivate::determineLastFocusChild(QWidget *noFurtherThan)
13784 QWidget *lastFocusChild = q;
13786 QWidget *focusProxy = deepestFocusProxy();
13793 for (
auto *object : std::as_const(q->children())) {
13794 QWidget *w = qobject_cast<QWidget *>(object);
13795 if (w && w->focusProxy() == q) {
13796 lastFocusChild = w;
13800 }
else if (q->isAncestorOf(focusProxy)) {
13801 lastFocusChild = focusProxy;
13802 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
13803 focusNext != focusProxy && q->isAncestorOf(focusNext)
13804 && focusNext->window() == focusProxy->window();
13805 focusNext = focusNext->nextInFocusChain()) {
13806 if (focusNext == noFurtherThan)
13808 if (focusNext->focusPolicy() != Qt::NoFocus)
13809 lastFocusChild = focusNext;
13812 return lastFocusChild;
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827bool QWidgetPrivate::isInFocusChain()
const
13829 Q_Q(
const QWidget);
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855bool QWidgetPrivate::isFocusChainConsistent()
const
13857 Q_Q(
const QWidget);
13858 const bool skip = !QLoggingCategory(
"qt.widgets.focus").isDebugEnabled();
13862 if (!isInFocusChain())
13865 const QWidget *position = q;
13867 for (
int i = 0; i < QApplication::allWidgets().count(); ++i) {
13869 qCDebug(lcWidgetFocus) <<
"Nullptr found at:" << position
13870 <<
"Previous pointing to" <<
FOCUS_PREV(position)
13871 <<
"Next pointing to" <<
FOCUS_NEXT(position);
13876 qCDebug(lcWidgetFocus) <<
"Inconsistent focus chain at:" << position
13887 qCDebug(lcWidgetFocus) <<
"Focus chain leading from" << q <<
"to" << position <<
"is not closed.";
13897#include "moc_qwidget.cpp"
13898#include "moc_qwidget_p.cpp"