569
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
794QWidgetMapper *QWidgetPrivate::mapper =
nullptr;
795QWidgetSet *QWidgetPrivate::allWidgets =
nullptr;
799
800
803
804
805
806
807
808
809
810
811
812
813
814
815
816
823#ifdef QT_NO_EXCEPTIONS
827 QWidgetPrivate::allWidgets->remove(that);
828 d->removeFromFocusChain();
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
859 : QWidget(*
new QWidgetPrivate, parent, f)
865
866QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
867 : QObject(dd,
nullptr), QPaintDevice()
874 QWidgetExceptionCleaner::cleanup(
this, d_func());
880
881
882int QWidget::devType()
const
884 return QInternal::Widget;
889void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
891 bool customize = (flags & (Qt::CustomizeWindowHint
892 | Qt::FramelessWindowHint
893 | Qt::WindowTitleHint
894 | Qt::WindowSystemMenuHint
895 | Qt::WindowMinimizeButtonHint
896 | Qt::WindowMaximizeButtonHint
897 | Qt::WindowCloseButtonHint
898 | Qt::WindowContextHelpButtonHint));
900 uint type = (flags & Qt::WindowType_Mask);
902 if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
907 if (flags & Qt::CustomizeWindowHint) {
912 if ((flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint))
914 && type != Qt::Dialog
917 flags |= Qt::WindowSystemMenuHint;
918 flags |= Qt::WindowTitleHint;
919 flags &= ~Qt::FramelessWindowHint;
921 }
else if (customize && !(flags & Qt::FramelessWindowHint)) {
925 flags |= Qt::WindowSystemMenuHint;
926 flags |= Qt::WindowTitleHint;
929 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
930 if (type != Qt::Dialog && type != Qt::Sheet && type != Qt::Tool)
931 flags |= Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowFullscreenButtonHint;
933 if (w->testAttribute(Qt::WA_TransparentForMouseEvents))
934 flags |= Qt::WindowTransparentForInput;
937void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
943#if QT_DEPRECATED_SINCE(6
, 11
)
944 QT_IGNORE_DEPRECATIONS(
945 const bool isDesktopWindow = (f & Qt::WindowType_Mask) == Qt::WindowType::Desktop;
946 Q_ASSERT_X(!isDesktopWindow, Q_FUNC_INFO,
"Qt::WindowType::Desktop is not allowed.");
948 f.setFlag(Qt::WindowType::Desktop,
false);
952 Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
954 if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
955 qFatal(
"QWidget: Cannot create a QWidget without QApplication");
957 Q_ASSERT(allWidgets);
959 allWidgets->insert(q);
963 Q_ASSERT_X(QThread::isMainThread(),
"QWidget",
964 "Widgets must be created in the GUI thread.");
968 data.fstrut_dirty =
true;
971 data.widget_attributes = 0;
972 data.window_flags = f;
973 data.window_state = 0;
974 data.focus_policy = 0;
975 data.context_menu_policy = Qt::DefaultContextMenu;
976 data.window_modality = Qt::NonModal;
978 data.sizehint_forced = 0;
979 data.is_closing =
false;
981 data.in_set_window_state = 0;
982 data.in_destructor =
false;
985 if (f & Qt::MSWindowsOwnDC) {
986 mustHaveWindowHandle = 1;
987 q->setAttribute(Qt::WA_NativeWindow);
990 q->setAttribute(Qt::WA_QuitOnClose);
991 adjustQuitOnCloseAttribute();
993 q->setAttribute(Qt::WA_WState_Hidden);
996 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
1000 q->setParent(parentWidget, data.window_flags);
1002 adjustFlags(data.window_flags, q);
1003 resolveLayoutDirection();
1005 const QBrush &background = q->palette().brush(QPalette::Window);
1006 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
1008 data.fnt = QFont(data.fnt, q);
1010 q->setAttribute(Qt::WA_PendingMoveEvent);
1011 q->setAttribute(Qt::WA_PendingResizeEvent);
1013 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
1014 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
1016 QEvent e(QEvent::Create);
1017 QCoreApplication::sendEvent(q, &e);
1018 QCoreApplication::postEvent(q,
new QEvent(QEvent::PolishRequest));
1020 extraPaintEngine =
nullptr;
1023void QWidgetPrivate::createRecursively()
1026 q->create(0,
true,
true);
1027 for (
int i = 0; i < children.size(); ++i) {
1028 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1029 if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
1030 child->d_func()->createRecursively();
1034QRhi *QWidgetPrivate::rhi()
const
1037 if (
auto *backingStore = q->backingStore()) {
1038 auto *window = windowHandle(WindowHandleMode::Closest);
1039 return backingStore->handle()->rhi(window);
1046
1047
1048
1049
1050
1051
1052
1053QWidget *QWidgetPrivate::closestParentWidgetWithWindowHandle()
const
1056 QWidget *parent = q->parentWidget();
1057 while (parent && !parent->windowHandle())
1058 parent = parent->parentWidget();
1062QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode)
const
1064 if (mode == WindowHandleMode::Direct || mode == WindowHandleMode::Closest) {
1065 if (QTLWExtra *x = maybeTopData()) {
1066 if (x->window !=
nullptr || mode == WindowHandleMode::Direct)
1070 if (mode == WindowHandleMode::Closest) {
1072 if (
auto nativeParent = q_func()->nativeParentWidget()) {
1073 if (
auto window = nativeParent->windowHandle())
1077 if (mode == WindowHandleMode::TopLevel || mode == WindowHandleMode::Closest) {
1078 if (
auto topLevel = q_func()->topLevelWidget()) {
1079 if (
auto window = topLevel ->windowHandle())
1087
1088
1089
1090
1091
1092QWindow *QWidgetPrivate::_q_closestWindowHandle()
const
1094 return windowHandle(QWidgetPrivate::WindowHandleMode::Closest);
1097QScreen *QWidgetPrivate::associatedScreen()
const
1099#if QT_CONFIG(graphicsview)
1101 if (nearestGraphicsProxyWidget(q_func()))
1104 if (
auto window = windowHandle(WindowHandleMode::Closest))
1105 return window->screen();
1112 QPlatformBackingStoreRhiConfig config = QWidgetPrivate::get(w)->rhiConfig();
1113 if (config.isEnabled()) {
1115 *outConfig = config;
1117 *outType = QBackingStoreRhiSupport::surfaceTypeForConfig(config);
1120 for (
const QObject *child : w->children()) {
1121 if (
const QWidget *childWidget = qobject_cast<
const QWidget *>(child)) {
1122 if (q_evaluateRhiConfigRecursive(childWidget, outConfig, outType)) {
1123 static bool optOut = qEnvironmentVariableIsSet(
"QT_WIDGETS_NO_CHILD_RHI");
1126 if (!optOut && childWidget->testAttribute(Qt::WA_NativeWindow))
1142 if (QBackingStoreRhiSupport::checkForceRhi(outConfig, outType)) {
1143 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluated to forced flushing with QRhi";
1149 if (q_evaluateRhiConfigRecursive(w, outConfig, outType)) {
1150 qCDebug(lcWidgetPainting) <<
"Tree with root" << w <<
"evaluates to flushing with QRhi";
1160
1161
1162
1163
1164
1165
1166
1167
1168
1170void QWidget::create(WId window,
bool initializeWindow,
bool destroyOldWindow)
1172 Q_UNUSED(initializeWindow);
1173 Q_UNUSED(destroyOldWindow);
1176 if (Q_UNLIKELY(window))
1177 qWarning(
"QWidget::create(): Parameter 'window' does not have any effect.");
1178 if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
1181 if (d->data.in_destructor)
1184 Qt::WindowType type = windowType();
1185 Qt::WindowFlags &flags = data->window_flags;
1187 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1189 flags |= Qt::Window;
1192 if (QWidget *parent = parentWidget()) {
1193 if (type & Qt::Window) {
1194 if (!parent->testAttribute(Qt::WA_WState_Created))
1195 parent->createWinId();
1196 }
else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
1197 && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
1203 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1204 Q_ASSERT(internalWinId());
1210 static const bool paintOnScreenEnv = qEnvironmentVariableIntValue(
"QT_ONSCREEN_PAINT") > 0;
1211 if (paintOnScreenEnv)
1212 setAttribute(Qt::WA_PaintOnScreen);
1214 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1215 setAttribute(Qt::WA_NativeWindow);
1218#if QT_CONFIG(graphicsview)
1219 && !graphicsProxyWidget()
1223 auto *topExtra = d->maybeTopData();
1224 if (!topExtra || !topExtra->explicitContentsMarginsRespectsSafeArea) {
1225 setAttribute_internal(Qt::WA_ContentsMarginsRespectsSafeArea,
1230 d->updateIsOpaque();
1232 setAttribute(Qt::WA_WState_Created);
1237 d->topData()->repaintManager.reset(
new QWidgetRepaintManager(
this));
1241 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1242 setAttribute(Qt::WA_DropSiteRegistered,
true);
1245 if (testAttribute(Qt::WA_SetWindowIcon))
1246 d->setWindowIcon_sys();
1248 if (isWindow() && !d->topData()->iconText.isEmpty())
1249 d->setWindowIconText_helper(d->topData()->iconText);
1250 if (isWindow() && !d->topData()->caption.isEmpty())
1251 d->setWindowTitle_helper(d->topData()->caption);
1252 if (isWindow() && !d->topData()->filePath.isEmpty())
1253 d->setWindowFilePath_helper(d->topData()->filePath);
1254 d->updateSystemBackground();
1256 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1257 d->setWindowIcon_sys();
1264 d->updateFrameStrut();
1269 QObjectList children = parentWidget->children();
1270 for (
int i = 0; i < children.size(); i++) {
1271 if (children.at(i)->isWidgetType()) {
1272 const QWidget *childWidget = qobject_cast<
const QWidget *>(children.at(i));
1274 if (childWidget->testAttribute(Qt::WA_NativeWindow)) {
1275 if (!childWidget->internalWinId())
1276 childWidget->winId();
1277 if (childWidget->windowHandle()) {
1278 if (childWidget->isWindow()) {
1279 childWidget->windowHandle()->setTransientParent(parentWidget->window()->windowHandle());
1281 childWidget->windowHandle()->setParent(childWidget->nativeParentWidget()->windowHandle());
1293void QWidgetPrivate::create()
1297 if (!q->testAttribute(Qt::WA_NativeWindow) && !q->isWindow())
1300 QWidgetWindow *win = topData()->window;
1305 Q_ASSERT(topData()->window);
1306 win = topData()->window;
1309 const auto dynamicPropertyNames = q->dynamicPropertyNames();
1310 for (
const QByteArray &propertyName : dynamicPropertyNames) {
1311 if (!qstrncmp(propertyName,
"_q_platform_", 12))
1312 win->setProperty(propertyName, q->property(propertyName));
1315 Qt::WindowFlags &flags = data.window_flags;
1317#if defined(QT_PLATFORM_UIKIT)
1318 if (q->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
1319 flags |= Qt::ExpandedClientAreaHint;
1322 if (q->testAttribute(Qt::WA_ShowWithoutActivating))
1323 win->setProperty(
"_q_showWithoutActivating", QVariant(
true));
1324 if (q->testAttribute(Qt::WA_MacAlwaysShowToolWindow))
1325 win->setProperty(
"_q_macAlwaysShowToolWindow", QVariant(
true));
1326 win->setFlags(flags);
1327 fixPosIncludesFrame();
1328 if (q->testAttribute(Qt::WA_Moved)
1329 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
1330 win->setGeometry(q->geometry());
1332 win->resize(q->size());
1333 if (win->isTopLevel()) {
1334 QScreen *targetScreen = topData()->initialScreen;
1335 topData()->initialScreen =
nullptr;
1337 targetScreen = q->screen();
1338 win->setScreen(targetScreen);
1341 QSurfaceFormat format = win->requestedFormat();
1342 if ((flags & Qt::Window) && win->surfaceType() != QSurface::OpenGLSurface
1343 && q->testAttribute(Qt::WA_TranslucentBackground)) {
1344 format.setAlphaBufferSize(8);
1346 win->setFormat(format);
1348 if (QWidget *nativeParent = q->nativeParentWidget()) {
1349 if (nativeParent->windowHandle()) {
1350 if (flags & Qt::Window) {
1363 const auto winType = Qt::WindowType((flags & Qt::WindowType_Mask).toInt());
1364 auto isPopupBased = [](Qt::WindowType type) {
1365 return type == Qt::Popup || type == Qt::Tool || type == Qt::ToolTip
1366 || type == Qt::SplashScreen;
1369 QWindow *transientParent = nativeParent->window()->windowHandle();
1370 if (!isPopupBased(winType)) {
1372 while (transientParent) {
1373 const Qt::WindowType tp = transientParent->type();
1374 if (!isPopupBased(tp))
1376 transientParent = transientParent->transientParent();
1379 win->setTransientParent(transientParent);
1380 win->setParent(
nullptr);
1382 win->setTransientParent(
nullptr);
1383 win->setParent(nativeParent->windowHandle());
1388 qt_window_private(win)->positionPolicy = topData()->posIncludesFrame ?
1389 QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
1393 if (QPlatformWindow *platformWindow = win->handle())
1394 platformWindow->setFrameStrutEventsEnabled(
true);
1396 data.window_flags = win->flags();
1399 if (!topData()->role.isNull()) {
1400 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(win->handle()))
1401 xcbWindow->setWindowRole(topData()->role);
1404#if QT_CONFIG(wayland)
1405 if (!topData()->role.isNull()) {
1406 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(win->handle()))
1407 waylandWindow->setSessionRestoreId(topData()->role);
1411 QBackingStore *store = q->backingStore();
1412 usesRhiFlush =
false;
1414 if (!store && q->isWindow())
1415 q->setBackingStore(
new QBackingStore(win));
1417 QPlatformBackingStoreRhiConfig rhiConfig;
1418 usesRhiFlush = q_evaluateRhiConfig(q, &rhiConfig,
nullptr);
1419 if (usesRhiFlush && q->backingStore()) {
1422 q->backingStore()->handle()->createRhi(win, rhiConfig);
1425 setWindowModified_helper();
1427 if (win->handle()) {
1428 WId id = win->winId();
1430 Q_ASSERT(id != WId(0));
1433 setNetWmWindowTypes(
true);
1436 q_createNativeChildrenAndSetParent(q);
1438 if (extra && !extra->mask.isEmpty())
1439 setMask_sys(extra->mask);
1441 if (data.crect.width() == 0 || data.crect.height() == 0) {
1442 q->setAttribute(Qt::WA_OutsideWSRange,
true);
1444 q->setAttribute(Qt::WA_OutsideWSRange,
false);
1445 if (q->isVisible()) {
1447 win->setNativeWindowVisibility(
true);
1453static const char activeXNativeParentHandleProperty[] =
"_q_embedded_native_parent_handle";
1456void QWidgetPrivate::createTLSysExtra()
1459 if (!extra->topextra->window && (q->testAttribute(Qt::WA_NativeWindow) || q->isWindow())) {
1460 extra->topextra->window =
new QWidgetWindow(q);
1461 if (extra->minw || extra->minh)
1462 extra->topextra->window->setMinimumSize(QSize(extra->minw, extra->minh));
1464 extra->topextra->window->setMaximumSize(QSize(extra->maxw, extra->maxh));
1465 if (extra->topextra->opacity != 255 && q->isWindow())
1466 extra->topextra->window->setOpacity(qreal(extra->topextra->opacity) / qreal(255));
1468#if QT_CONFIG(tooltip)
1469 const bool isTipLabel = qobject_cast<
const QTipLabel *>(q) !=
nullptr;
1471 const bool isAlphaWidget = !isTipLabel && q->inherits(
"QAlphaWidget");
1474 const QVariant activeXNativeParentHandle = q->property(activeXNativeParentHandleProperty);
1475 if (activeXNativeParentHandle.isValid())
1476 extra->topextra->window->setProperty(activeXNativeParentHandleProperty, activeXNativeParentHandle);
1477 if (isTipLabel || isAlphaWidget)
1478 extra->topextra->window->setProperty(
"_q_windowsDropShadow", QVariant(
true));
1480 if (isTipLabel || isAlphaWidget || q->inherits(
"QRollEffect"))
1481 qt_window_private(extra->topextra->window)->setAutomaticPositionAndResizeEnabled(
false);
1483 updateIsTranslucent();
1489
1490
1491
1492
1493
1498 d->data.in_destructor =
true;
1500#if QT_CONFIG(accessibility)
1501 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing && QAccessible::isActive())
1502 QAccessibleCache::instance()->sendObjectDestroyedEvent(
this);
1505#if defined (QT_CHECK_STATE)
1506 if (Q_UNLIKELY(paintingActive()))
1507 qWarning(
"QWidget: %s (%s) deleted while being painted", className(), name());
1510#ifndef QT_NO_GESTURES
1511 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1513 for (
auto it = d->gestureContext.keyBegin(), end = d->gestureContext.keyEnd(); it != end; ++it)
1514 manager->cleanupCachedGestures(
this, *it);
1516 d->gestureContext.clear();
1521 for (
auto action : std::as_const(d->actions)) {
1522 QActionPrivate *apriv = action->d_func();
1523 apriv->associatedObjects.removeAll(
this);
1528#ifndef QT_NO_SHORTCUT
1531 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1532 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0,
this, QKeySequence());
1537 d->layout =
nullptr;
1540 d->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency);
1543#if QT_CONFIG(graphicsview)
1544 const QWidget* w =
this;
1545 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
1546 w = w->d_func()->extra->focus_proxy;
1547 QWidget *window = w->window();
1548 QWExtra *e = window ? window->d_func()->extra.get() :
nullptr ;
1549 if (!e || !e->proxyWidget || (w->parentWidget() && w->parentWidget()->d_func()->focus_child ==
this))
1556 d->setDirtyOpaqueRegion();
1558 if (isWindow() && isVisible() && internalWinId()) {
1569 }
else if (isVisible()) {
1570 qApp->d_func()->sendSyntheticEnterLeave(
this);
1573 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
1574 repaintManager->removeDirtyWidget(
this);
1575 if (testAttribute(Qt::WA_StaticContents))
1576 repaintManager->removeStaticWidget(
this);
1579 delete d->needsFlush;
1580 d->needsFlush =
nullptr;
1584 bool blocked = d->blockSig;
1587 if (d->isSignalConnected(0)) {
1589 emit destroyed(
this);
1593 qWarning(
"Detected an unexpected exception in ~QWidget while emitting destroyed().");
1598 if (d->declarativeData) {
1599 d->wasDeleted =
true;
1600 if (QAbstractDeclarativeData::destroyed)
1601 QAbstractDeclarativeData::destroyed(d->declarativeData,
this);
1602 d->declarativeData =
nullptr;
1603 d->wasDeleted =
false;
1606 d->blockSig = blocked;
1608 if (!d->children.isEmpty())
1609 d->deleteChildren();
1611 QCoreApplication::removePostedEvents(
this);
1618 --QWidgetPrivate::instanceCounter;
1620 if (QWidgetPrivate::allWidgets)
1621 QWidgetPrivate::allWidgets->remove(
this);
1624 QEvent e(QEvent::Destroy);
1625 QCoreApplication::sendEvent(
this, &e);
1626 } QT_CATCH(
const std::exception&) {
1630#if QT_CONFIG(graphicseffect)
1631 delete d->graphicsEffect;
1635 d->isWidget =
false;
1638int QWidgetPrivate::instanceCounter = 0;
1639int QWidgetPrivate::maxInstances = 0;
1641void QWidgetPrivate::setWinId(WId id)
1644 if (mapper && data.winid) {
1645 mapper->remove(data.winid);
1648 const WId oldWinId = data.winid;
1652 mapper->insert(data.winid, q);
1655 if (oldWinId != id) {
1656 QEvent e(QEvent::WinIdChange);
1657 QCoreApplication::sendEvent(q, &e);
1661void QWidgetPrivate::createTLExtra()
1665 if (!extra->topextra) {
1666 extra->topextra = std::make_unique<QTLWExtra>();
1667 QTLWExtra* x = extra->topextra.get();
1668 x->backingStore =
nullptr;
1669 x->sharedPainter =
nullptr;
1670 x->incw = x->inch = 0;
1671 x->basew = x->baseh = 0;
1672 x->frameStrut.setCoords(0, 0, 0, 0);
1673 x->normalGeometry = QRect(0,0,-1,-1);
1674 x->savedFlags = { };
1676 x->posIncludesFrame = 0;
1677 x->sizeAdjusted =
false;
1679 x->explicitContentsMarginsRespectsSafeArea = 0;
1680 x->window =
nullptr;
1681 x->initialScreen =
nullptr;
1683#ifdef QWIDGET_EXTRA_DEBUG
1684 static int count = 0;
1685 qDebug() <<
"tlextra" << ++count;
1691
1692
1693
1695void QWidgetPrivate::createExtra()
1698 extra = std::make_unique<QWExtra>();
1699 extra->glContext =
nullptr;
1700#if QT_CONFIG(graphicsview)
1701 extra->proxyWidget =
nullptr;
1707 extra->customDpiX = 0;
1708 extra->customDpiY = 0;
1709 extra->explicitMinSize = 0;
1710 extra->explicitMaxSize = 0;
1711 extra->autoFillBackground = 0;
1712 extra->nativeChildrenForced = 0;
1713 extra->inRenderWithPainter = 0;
1714 extra->hasWindowContainer =
false;
1717#ifdef QWIDGET_EXTRA_DEBUG
1718 static int count = 0;
1719 qDebug() <<
"extra" << ++count;
1724void QWidgetPrivate::createSysExtra()
1729
1730
1731
1733void QWidgetPrivate::deleteExtra()
1737#if QT_CONFIG(style_stylesheet)
1739 if (QStyleSheetStyle *proxy = qt_styleSheet(extra->style))
1742 if (extra->topextra)
1750void QWidgetPrivate::deleteSysExtra()
1754void QWidgetPrivate::deleteTLSysExtra()
1757 if (extra && extra->topextra) {
1758 if (extra->hasWindowContainer)
1759 QWindowContainer::toplevelAboutToBeDestroyed(q);
1761 delete extra->topextra->window;
1762 extra->topextra->window =
nullptr;
1767
1768
1769
1771QRegion QWidgetPrivate::overlappedRegion(
const QRect &rect,
bool breakAfterFirst)
const
1775 const QWidget *w = q;
1782 QWidgetPrivate *pd = w->parentWidget()->d_func();
1784 for (
int i = 0; i < pd->children.size(); ++i) {
1785 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
1786 if (!sibling || !sibling->isVisible() || sibling->isWindow())
1789 above = (sibling == w);
1793 const QRect siblingRect = sibling->d_func()->effectiveRectFor(sibling->data->crect);
1794 if (qRectIntersects(siblingRect, r)) {
1795 const auto &siblingExtra = sibling->d_func()->extra;
1796 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
1797 && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
1800 region += siblingRect.translated(-p);
1801 if (breakAfterFirst)
1805 w = w->parentWidget();
1806 r.translate(pd->data.crect.topLeft());
1807 p += pd->data.crect.topLeft();
1812void QWidgetPrivate::syncBackingStore()
1814 if (shouldPaintOnScreen()) {
1815 paintOnScreen(dirty);
1817 }
else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1818 repaintManager->sync();
1822void QWidgetPrivate::syncBackingStore(
const QRegion ®ion)
1824 if (shouldPaintOnScreen())
1825 paintOnScreen(region);
1826 else if (QWidgetRepaintManager *repaintManager = maybeRepaintManager()) {
1827 repaintManager->sync(q_func(), region);
1831void QWidgetPrivate::paintOnScreen(
const QRegion &rgn)
1833 if (data.in_destructor)
1836 if (shouldDiscardSyncRequest())
1840 if (q->testAttribute(Qt::WA_StaticContents)) {
1843 extra->staticContentsSize = data.crect.size();
1846 QPaintEngine *engine = q->paintEngine();
1851 const bool noPartialUpdateSupport = (engine && (engine->type() == QPaintEngine::OpenGL
1852 || engine->type() == QPaintEngine::OpenGL2))
1853 && (usesDoubleBufferedGLContext || q->autoFillBackground());
1854 QRegion toBePainted(noPartialUpdateSupport ? q->rect() : rgn);
1856 toBePainted &= clipRect();
1857 clipToEffectiveMask(toBePainted);
1858 if (toBePainted.isEmpty())
1861 drawWidget(q, toBePainted, QPoint(), QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen,
nullptr);
1863 if (Q_UNLIKELY(q->paintingActive()))
1864 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
1867void QWidgetPrivate::setUpdatesEnabled_helper(
bool enable)
1871 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
1874 if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
1877 q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
1881 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
1882 for (
int i = 0; i < children.size(); ++i) {
1883 QWidget *w = qobject_cast<QWidget *>(children.at(i));
1884 if (w && !w->isWindow() && !w->testAttribute(attribute))
1885 w->d_func()->setUpdatesEnabled_helper(enable);
1890
1891
1892
1893
1894
1895
1896void QWidgetPrivate::propagatePaletteChange()
1900#if QT_CONFIG(graphicsview)
1901 if (!q->parentWidget() && extra && extra->proxyWidget) {
1902 QGraphicsProxyWidget *p = extra->proxyWidget;
1903 inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolveMask();
1906 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
1907 inheritedPaletteResolveMask = 0;
1910 directPaletteResolveMask = data.pal.resolveMask();
1911 auto mask = directPaletteResolveMask | inheritedPaletteResolveMask;
1913 const bool useStyleSheetPropagationInWidgetStyles =
1914 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
1916 QEvent pc(QEvent::PaletteChange);
1917 QCoreApplication::sendEvent(q, &pc);
1918 for (
int i = 0; i < children.size(); ++i) {
1919 QWidget *w = qobject_cast<QWidget*>(children.at(i));
1920 if (w && (!w->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
1921 && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
1922 QWidgetPrivate *wd = w->d_func();
1923 wd->inheritedPaletteResolveMask = mask;
1924 wd->resolvePalette();
1930
1931
1932QRect QWidgetPrivate::clipRect()
const
1935 const QWidget * w = q;
1936 if (!w->isVisible())
1938 QRect r = effectiveRectFor(q->rect());
1944 && w->parentWidget()) {
1947 w = w->parentWidget();
1948 r &= QRect(ox, oy, w->width(), w->height());
1954
1955
1956QRegion QWidgetPrivate::clipRegion()
const
1959 if (!q->isVisible())
1961 QRegion r(q->rect());
1962 const QWidget * w = q;
1963 const QWidget *ignoreUpTo;
1969 && w->parentWidget()) {
1973 w = w->parentWidget();
1974 r &= QRegion(ox, oy, w->width(), w->height());
1977 while(w->d_func()->children.at(i++) !=
static_cast<
const QObject *>(ignoreUpTo))
1979 for ( ; i < w->d_func()->children.size(); ++i) {
1980 if (QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
1981 if (sibling->isVisible() && !sibling->isWindow()) {
1982 QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
1983 sibling->width(), sibling->height());
1984 if (qRectIntersects(siblingRect, q->rect()))
1985 r -= QRegion(siblingRect);
1993void QWidgetPrivate::setSystemClip(QPaintEngine *paintEngine, qreal devicePixelRatio,
const QRegion ®ion)
1996 QTransform scaleTransform;
1997 scaleTransform.scale(devicePixelRatio, devicePixelRatio);
1999 paintEngine->d_func()->baseSystemClip = region;
2000 paintEngine->d_func()->setSystemTransform(scaleTransform);
2004#if QT_CONFIG(graphicseffect)
2005void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
2010 if (w->graphicsEffect()) {
2011 QWidgetEffectSourcePrivate *sourced =
2012 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
2013 if (!sourced->updateDueToGraphicsEffect)
2014 w->graphicsEffect()->source()->d_func()->invalidateCache();
2016 w = w->parentWidget();
2021void QWidgetPrivate::setDirtyOpaqueRegion()
2025 dirtyOpaqueChildren =
true;
2027#if QT_CONFIG(graphicseffect)
2028 invalidateGraphicsEffectsRecursively();
2034 QWidget *parent = q->parentWidget();
2039 QWidgetPrivate *pd = parent->d_func();
2040 if (!pd->dirtyOpaqueChildren)
2041 pd->setDirtyOpaqueRegion();
2044const QRegion &QWidgetPrivate::getOpaqueChildren()
const
2046 if (!dirtyOpaqueChildren)
2047 return opaqueChildren;
2049 QWidgetPrivate *that =
const_cast<QWidgetPrivate*>(
this);
2050 that->opaqueChildren = QRegion();
2052 for (
int i = 0; i < children.size(); ++i) {
2053 QWidget *child = qobject_cast<QWidget *>(children.at(i));
2054 if (!child || !child->isVisible() || child->isWindow())
2057 const QPoint offset = child->geometry().topLeft();
2058 QWidgetPrivate *childd = child->d_func();
2059 QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
2060 if (childd->extra && childd->extra->hasMask)
2061 r &= childd->extra->mask;
2064 r.translate(offset);
2065 that->opaqueChildren += r;
2068 that->opaqueChildren &= q_func()->rect();
2069 that->dirtyOpaqueChildren =
false;
2071 return that->opaqueChildren;
2074void QWidgetPrivate::subtractOpaqueChildren(QRegion &source,
const QRect &clipRect)
const
2076 if (children.isEmpty() || clipRect.isEmpty())
2079 const QRegion &r = getOpaqueChildren();
2081 source -= (r & clipRect);
2085void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion,
bool *hasDirtySiblingsAbove,
2086 bool alsoNonOpaque)
const
2089 static int disableSubtractOpaqueSiblings = qEnvironmentVariableIntValue(
"QT_NO_SUBTRACTOPAQUESIBLINGS");
2090 if (disableSubtractOpaqueSiblings || q->isWindow())
2093 QRect clipBoundingRect;
2094 bool dirtyClipBoundingRect =
true;
2097 bool dirtyParentClip =
true;
2099 QPoint parentOffset = data.crect.topLeft();
2101 const QWidget *w = q;
2106 QWidgetPrivate *pd = w->parentWidget()->d_func();
2107 const int myIndex = pd->children.indexOf(
const_cast<QWidget *>(w));
2108 const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
2109 for (
int i = myIndex + 1; i < pd->children.size(); ++i) {
2110 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
2111 if (!sibling || !sibling->isVisible() || sibling->isWindow())
2114 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2115 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2118 if (dirtyClipBoundingRect) {
2119 clipBoundingRect = sourceRegion.boundingRect();
2120 dirtyClipBoundingRect =
false;
2123 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2126 if (dirtyParentClip) {
2127 parentClip = sourceRegion.translated(parentOffset);
2128 dirtyParentClip =
false;
2131 const QPoint siblingPos(sibling->data->crect.topLeft());
2132 const QRect siblingClipRect(sibling->d_func()->clipRect());
2133 QRegion siblingDirty(parentClip);
2134 siblingDirty &= (siblingClipRect.translated(siblingPos));
2135 const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
2136 && !sibling->d_func()->graphicsEffect;
2138 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2139 if (siblingDirty.isEmpty())
2142 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2144 siblingDirty.translate(-parentOffset);
2145 sourceRegion -= siblingDirty;
2147 sourceRegion -= siblingGeometry.translated(-parentOffset);
2150 if (hasDirtySiblingsAbove)
2151 *hasDirtySiblingsAbove =
true;
2152 if (sibling->d_func()->children.isEmpty())
2154 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2155 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2156 sourceRegion -= opaqueSiblingChildren;
2158 if (sourceRegion.isEmpty())
2161 dirtyClipBoundingRect =
true;
2162 dirtyParentClip =
true;
2165 w = w->parentWidget();
2166 parentOffset += pd->data.crect.topLeft();
2167 dirtyParentClip =
true;
2171void QWidgetPrivate::clipToEffectiveMask(QRegion ®ion)
const
2175 const QWidget *w = q;
2178#if QT_CONFIG(graphicseffect)
2179 if (graphicsEffect && !w->isWindow()) {
2180 w = q->parentWidget();
2181 offset -= data.crect.topLeft();
2186 const QWidgetPrivate *wd = w->d_func();
2187 if (wd->extra && wd->extra->hasMask)
2188 region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
2191 offset -= wd->data.crect.topLeft();
2192 w = w->parentWidget();
2196bool QWidgetPrivate::shouldPaintOnScreen()
const
2198#if defined(QT_NO_BACKINGSTORE)
2202 if (q->testAttribute(Qt::WA_PaintOnScreen)
2203 || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
2211void QWidgetPrivate::updateIsOpaque()
2214 setDirtyOpaqueRegion();
2216#if QT_CONFIG(graphicseffect)
2217 if (graphicsEffect) {
2225 if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
2230 const QPalette &pal = q->palette();
2232 if (q->autoFillBackground()) {
2233 const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
2234 if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
2240 if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
2241 const QBrush &windowBrush = q->palette().brush(QPalette::Window);
2242 if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
2250void QWidgetPrivate::setOpaque(
bool opaque)
2252 if (isOpaque != opaque) {
2254 updateIsTranslucent();
2258void QWidgetPrivate::updateIsTranslucent()
2261 if (QWindow *window = q->windowHandle()) {
2262 QSurfaceFormat format = window->format();
2263 const int oldAlpha = format.alphaBufferSize();
2264 const int newAlpha = q->testAttribute(Qt::WA_TranslucentBackground) ? 8 : -1;
2265 if (oldAlpha != newAlpha) {
2282 if (!window->handle()) {
2283 format.setAlphaBufferSize(newAlpha);
2284 window->setFormat(format);
2290static inline void fillRegion(QPainter *painter,
const QRegion &rgn,
const QBrush &brush)
2294 if (brush.style() == Qt::TexturePattern) {
2295 const QRect rect(rgn.boundingRect());
2296 painter->setClipRegion(rgn);
2297 painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
2298 }
else if (brush.gradient()
2299 && (brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode
2300 || brush.gradient()->coordinateMode() == QGradient::ObjectMode)) {
2302 painter->setClipRegion(rgn);
2303 painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
2306 for (
const QRect &rect : rgn)
2307 painter->fillRect(rect, brush);
2311bool QWidgetPrivate::updateBrushOrigin(QPainter *painter,
const QBrush &brush)
const
2313#if QT_CONFIG(scrollarea)
2316 if (brush.style() == Qt::NoBrush || brush.style() == Qt::SolidPattern)
2318 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
2319 if (scrollArea && scrollArea->viewport() == q) {
2320 QObjectData *scrollPrivate =
static_cast<QWidget *>(scrollArea)->d_ptr.data();
2321 QAbstractScrollAreaPrivate *priv =
static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
2322 painter->setBrushOrigin(-priv->contentsOffset());
2328void QWidgetPrivate::paintBackground(QPainter *painter,
const QRegion &rgn, DrawWidgetFlags flags)
const
2332 bool brushOriginSet =
false;
2333 const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
2335 if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
2336 const QBrush bg = q->palette().brush(QPalette::Window);
2337 if (!brushOriginSet)
2338 brushOriginSet = updateBrushOrigin(painter, bg);
2339 if (!(flags & DontSetCompositionMode)) {
2341 QPainter::CompositionMode oldMode = painter->compositionMode();
2342 painter->setCompositionMode(QPainter::CompositionMode_Source);
2343 fillRegion(painter, rgn, bg);
2344 painter->setCompositionMode(oldMode);
2346 fillRegion(painter, rgn, bg);
2350 if (q->autoFillBackground()) {
2351 if (!brushOriginSet)
2352 brushOriginSet = updateBrushOrigin(painter, autoFillBrush);
2353 fillRegion(painter, rgn, autoFillBrush);
2356 if (q->testAttribute(Qt::WA_StyledBackground)) {
2357 painter->setClipRegion(rgn);
2360 q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
2365
2366
2367
2368
2369
2373void QWidgetPrivate::deactivateWidgetCleanup()
2377 if (QApplication::activeWindow() == q)
2378 QApplicationPrivate::setActiveWindow(
nullptr);
2380 if (q == qt_button_down)
2381 qt_button_down =
nullptr;
2386
2387
2388
2389
2390
2391
2392
2394QWidget *QWidget::find(WId id)
2396 return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) :
nullptr;
2402
2403
2404
2405
2406
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424WId QWidget::winId()
const
2426 if (!data->in_destructor
2427 && (!testAttribute(Qt::WA_WState_Created) || !internalWinId()))
2429 QWidget *that =
const_cast<QWidget*>(
this);
2430 that->setAttribute(Qt::WA_NativeWindow);
2431 that->d_func()->createWinId();
2432 return that->data->winid;
2437void QWidgetPrivate::createWinId()
2441 const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
2442 if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
2443 if (!q->isWindow()) {
2444 QWidget *parent = q->parentWidget();
2445 QWidgetPrivate *pd = parent->d_func();
2446 if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
2447 parent->setAttribute(Qt::WA_NativeWindow);
2448 if (!parent->internalWinId()) {
2452 for (
int i = 0; i < pd->children.size(); ++i) {
2453 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
2454 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
2455 || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
2466
2467
2468
2469
2470
2472bool QWidgetPrivate::setScreenForPoint(
const QPoint &pos)
2478 return setScreen(QGuiApplication::screenAt(pos));
2482
2483
2484
2485
2487bool QWidgetPrivate::setScreen(QScreen *screen)
2490 if (!screen || !q->isWindow())
2492 const QScreen *currentScreen = windowHandle() ? windowHandle()->screen() :
nullptr;
2493 if (currentScreen != screen) {
2494 topData()->initialScreen = screen;
2496 windowHandle()->setScreen(screen);
2503
2504
2505
2506
2508void QWidget::createWinId()
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529WId QWidget::effectiveWinId()
const
2531 const WId id = internalWinId();
2532 if (id || !testAttribute(Qt::WA_WState_Created))
2534 if (
const QWidget *realParent = nativeParentWidget())
2535 return realParent->internalWinId();
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550QWindow *QWidget::windowHandle()
const
2553 return d->windowHandle();
2557
2558
2559
2560
2561
2562
2563QScreen *QWidget::screen()
const
2566 if (
auto associatedScreen = d->associatedScreen())
2567 return associatedScreen;
2568 if (
auto topLevel = window()) {
2569 if (
auto topData = qt_widget_private(topLevel)->topData()) {
2570 if (topData->initialScreen)
2571 return topData->initialScreen;
2573 if (
auto screenByPos = QGuiApplication::screenAt(topLevel->geometry().center()))
2576 return QGuiApplication::primaryScreen();
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591void QWidget::setScreen(QScreen *screen)
2594 d->setScreen(screen);
2597#if QT_CONFIG(style_stylesheet)
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614QString QWidget::styleSheet()
const
2619 return d->extra->styleSheet;
2622void QWidget::setStyleSheet(
const QString& styleSheet)
2625 if (data->in_destructor)
2629 QStyleSheetStyle *proxy = qt_styleSheet(d->extra->style);
2630 d->extra->styleSheet = styleSheet;
2631 if (styleSheet.isEmpty()) {
2640 bool repolish = d->polished;
2642 const auto childWidgets = findChildren<QWidget*>();
2643 for (
auto child : childWidgets) {
2644 repolish = child->d_func()->polished;
2650 proxy->repolish(
this);
2654 if (testAttribute(Qt::WA_SetStyle)) {
2655 d->setStyle_helper(
new QStyleSheetStyle(d->extra->style),
true);
2657 d->setStyle_helper(
new QStyleSheetStyle(
nullptr),
true);
2664
2665
2667QStyle *QWidget::style()
const
2671 if (d->extra && d->extra->style)
2672 return d->extra->style;
2673 return QApplication::style();
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2697void QWidget::setStyle(QStyle *style)
2700 setAttribute(Qt::WA_SetStyle, style !=
nullptr);
2702#if QT_CONFIG(style_stylesheet)
2703 if (QStyleSheetStyle *styleSheetStyle = qt_styleSheet(style)) {
2706 styleSheetStyle->ref();
2707 d->setStyle_helper(style,
false);
2708 }
else if (qt_styleSheet(d->extra->style) || !qApp->styleSheet().isEmpty()) {
2710 d->setStyle_helper(
new QStyleSheetStyle(style),
true);
2713 d->setStyle_helper(style,
false);
2716void QWidgetPrivate::setStyle_helper(QStyle *newStyle,
bool propagate)
2719 QStyle *oldStyle = q->style();
2723#if QT_CONFIG(style_stylesheet)
2724 QPointer<QStyle> origStyle = extra->style;
2726 extra->style = newStyle;
2730 oldStyle->unpolish(q);
2731 q->style()->polish(q);
2736 const QObjectList childrenList = children;
2737 for (
int i = 0; i < childrenList.size(); ++i) {
2738 QWidget *c = qobject_cast<QWidget*>(childrenList.at(i));
2740 c->d_func()->inheritStyle();
2744#if QT_CONFIG(style_stylesheet)
2745 if (!qt_styleSheet(newStyle)) {
2746 if (
const QStyleSheetStyle* cssStyle = qt_styleSheet(origStyle)) {
2747 cssStyle->clearWidgetFont(q);
2752 QEvent e(QEvent::StyleChange);
2753 QCoreApplication::sendEvent(q, &e);
2755#if QT_CONFIG(style_stylesheet)
2757 if (QStyleSheetStyle *proxy = qt_styleSheet(origStyle))
2763void QWidgetPrivate::inheritStyle()
2765#if QT_CONFIG(style_stylesheet)
2768 QStyle *extraStyle = extra ? (QStyle*)extra->style :
nullptr;
2770 QStyleSheetStyle *proxy = qt_styleSheet(extraStyle);
2772 if (!q->styleSheet().isEmpty()) {
2777 if (inheritStyleRecursionGuard)
2779 inheritStyleRecursionGuard =
true;
2780 const auto resetGuard = qScopeGuard([&]() {
2781 inheritStyleRecursionGuard =
false;
2784 QStyle *origStyle = proxy ? proxy->base : extraStyle;
2785 QWidget *parent = q->parentWidget();
2786 QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style :
nullptr;
2789 if (!qApp->styleSheet().isEmpty() || qt_styleSheet(parentStyle)) {
2790 QStyle *newStyle = parentStyle;
2791 if (q->testAttribute(Qt::WA_SetStyle) && qt_styleSheet(origStyle) ==
nullptr)
2792 newStyle =
new QStyleSheetStyle(origStyle);
2793 else if (
auto *styleSheetStyle = qt_styleSheet(origStyle))
2794 newStyle = styleSheetStyle;
2795 else if (QStyleSheetStyle *newProxy = qt_styleSheet(parentStyle))
2798 setStyle_helper(newStyle,
true);
2804 if (origStyle == extraStyle)
2810 if (!q->testAttribute(Qt::WA_SetStyle))
2811 origStyle =
nullptr;
2813 setStyle_helper(origStyle,
true);
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2869Qt::WindowModality QWidget::windowModality()
const
2871 return static_cast<Qt::WindowModality>(data->window_modality);
2874void QWidget::setWindowModality(Qt::WindowModality windowModality)
2876 data->window_modality = windowModality;
2878 setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
2879 setAttribute(Qt::WA_SetWindowModality,
true);
2882void QWidgetPrivate::setModal_sys()
2885 if (q->windowHandle())
2886 q->windowHandle()->setModality(q->windowModality());
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911bool QWidget::isMinimized()
const
2912{
return data->window_state & Qt::WindowMinimized; }
2915
2916
2917
2918
2919
2920
2921
2922void QWidget::showMinimized()
2924 bool isMin = isMinimized();
2925 if (isMin && isVisible())
2931 setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951bool QWidget::isMaximized()
const
2952{
return data->window_state & Qt::WindowMaximized; }
2957
2958
2959
2960
2961
2962
2963Qt::WindowStates QWidget::windowState()
const
2965 return Qt::WindowStates(data->window_state);
2969
2970
2971
2972
2973
2974
2975void QWidget::overrideWindowState(Qt::WindowStates newstate)
2977 QWindowStateChangeEvent e(Qt::WindowStates(data->window_state),
true);
2978 data->window_state = newstate;
2979 QCoreApplication::sendEvent(
this, &e);
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012void QWidget::setWindowState(Qt::WindowStates newstate)
3015 Qt::WindowStates oldstate = windowState();
3016 if (newstate.testFlag(Qt::WindowMinimized))
3017 newstate.setFlag(Qt::WindowActive,
false);
3018 if (oldstate == newstate)
3020 if (isWindow() && !testAttribute(Qt::WA_WState_Created))
3023 data->window_state = newstate;
3024 data->in_set_window_state = 1;
3027 if (!testAttribute(Qt::WA_Resized) && !isVisible())
3031 if (!(oldstate & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)))
3032 d->topData()->normalGeometry = geometry();
3034 Q_ASSERT(windowHandle());
3035 windowHandle()->setWindowStates(newstate & ~Qt::WindowActive);
3037 data->in_set_window_state = 0;
3039 if (newstate & Qt::WindowActive)
3042 QWindowStateChangeEvent e(oldstate);
3043 QCoreApplication::sendEvent(
this, &e);
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057bool QWidget::isFullScreen()
const
3058{
return data->window_state & Qt::WindowFullScreen; }
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096void QWidget::showFullScreen()
3100 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
3101 | Qt::WindowFullScreen);
3103#if !defined Q_OS_QNX
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119void QWidget::showMaximized()
3123 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
3124 | Qt::WindowMaximized);
3129
3130
3131
3132
3133
3134
3135void QWidget::showNormal()
3139 setWindowState(windowState() & ~(Qt::WindowMinimized
3140 | Qt::WindowMaximized
3141 | Qt::WindowFullScreen));
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3165bool QWidget::isEnabledTo(
const QWidget *ancestor)
const
3167 const QWidget * w =
this;
3168 while (!w->testAttribute(Qt::WA_ForceDisabled)
3170 && w->parentWidget()
3171 && w->parentWidget() != ancestor)
3172 w = w->parentWidget();
3173 return !w->testAttribute(Qt::WA_ForceDisabled);
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192void QWidget::addAction(QAction *action)
3194 insertAction(
nullptr, action);
3198
3199
3200
3201
3202void QWidget::addActions(
const QList<QAction *> &actions)
3204 for(
int i = 0; i < actions.size(); i++)
3205 insertAction(
nullptr, actions.at(i));
3209
3210
3211
3212
3213
3214
3215
3216
3217void QWidget::insertAction(QAction *before, QAction *action)
3219 if (Q_UNLIKELY(!action)) {
3220 qWarning(
"QWidget::insertAction: Attempt to insert null action");
3225 if (d->actions.contains(action))
3226 removeAction(action);
3228 int pos = d->actions.indexOf(before);
3231 pos = d->actions.size();
3233 d->actions.insert(pos, action);
3235 QActionPrivate *apriv = action->d_func();
3236 apriv->associatedObjects.append(
this);
3238 QActionEvent e(QEvent::ActionAdded, action, before);
3239 QCoreApplication::sendEvent(
this, &e);
3243
3244
3245
3246
3247
3248
3249
3250
3251void QWidget::insertActions(QAction *before,
const QList<QAction*> &actions)
3253 for(
int i = 0; i < actions.size(); ++i)
3254 insertAction(before, actions.at(i));
3258
3259
3260
3261void QWidget::removeAction(QAction *action)
3268 QActionPrivate *apriv = action->d_func();
3269 apriv->associatedObjects.removeAll(
this);
3271 if (d->actions.removeAll(action)) {
3272 QActionEvent e(QEvent::ActionRemoved, action);
3273 QCoreApplication::sendEvent(
this, &e);
3278
3279
3280
3281
3282QList<QAction*> QWidget::actions()
const
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304QAction *QWidget::addAction(
const QString &text)
3306 QAction *ret =
new QAction(text,
this);
3311QAction *QWidget::addAction(
const QIcon &icon,
const QString &text)
3313 QAction *ret =
new QAction(icon, text,
this);
3318#if QT_CONFIG(shortcut)
3319QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut)
3321 QAction *ret = addAction(text);
3322 ret->setShortcut(shortcut);
3326QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut)
3328 QAction *ret = addAction(icon, text);
3329 ret->setShortcut(shortcut);
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352QAction *QWidget::addAction(
const QString &text,
const QObject *receiver,
const char* member,
3353 Qt::ConnectionType type)
3355 QAction *action = addAction(text);
3356 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3360QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
3361 const QObject *receiver,
const char* member,
3362 Qt::ConnectionType type)
3364 QAction *action = addAction(icon, text);
3365 QObject::connect(action, SIGNAL(triggered(
bool)), receiver, member, type);
3369#if QT_CONFIG(shortcut)
3370QAction *QWidget::addAction(
const QString &text,
const QKeySequence &shortcut,
3371 const QObject *receiver,
const char* member,
3372 Qt::ConnectionType type)
3374 QAction *action = addAction(text, receiver, member, type);
3375 action->setShortcut(shortcut);
3379QAction *QWidget::addAction(
const QIcon &icon,
const QString &text,
const QKeySequence &shortcut,
3380 const QObject *receiver,
const char* member,
3381 Qt::ConnectionType type)
3383 QAction *action = addAction(icon, text, receiver, member, type);
3384 action->setShortcut(shortcut);
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433void QWidget::setEnabled(
bool enable)
3437#if QT_CONFIG(accessibility)
3438 const bool wasEnabled = !testAttribute(Qt::WA_ForceDisabled);
3441 setAttribute(Qt::WA_ForceDisabled, !enable);
3442 d->setEnabled_helper(enable);
3444#if QT_CONFIG(accessibility)
3448 if (QAccessible::isActive() && wasEnabled != enable) {
3449 QAccessible::State states;
3450 states.disabled = 1;
3451 QAccessibleStateChangeEvent scEvent(
this, states);
3452 QAccessible::updateAccessibility(&scEvent);
3457void QWidgetPrivate::setEnabled_helper(
bool enable)
3461 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
3464 if (enable != q->testAttribute(Qt::WA_Disabled))
3467 q->setAttribute(Qt::WA_Disabled, !enable);
3468 updateSystemBackground();
3470 if (!enable && q->window()->focusWidget() == q) {
3471 bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
3472 if (!parentIsEnabled || !q->focusNextChild())
3476 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
3477 for (
int i = 0; i < children.size(); ++i) {
3478 QWidget *w = qobject_cast<QWidget *>(children.at(i));
3479 if (w && !w->testAttribute(attribute))
3480 w->d_func()->setEnabled_helper(enable);
3483 if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
3486 qt_qpa_set_cursor(q,
false);
3490 if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
3491 QWidget *focusWidget = effectiveFocusWidget();
3494 if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
3495 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3497 QGuiApplication::inputMethod()->commit();
3498 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
3502 QEvent e(QEvent::EnabledChange);
3503 QCoreApplication::sendEvent(q, &e);
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519bool QWidget::acceptDrops()
const
3521 return testAttribute(Qt::WA_AcceptDrops);
3524void QWidget::setAcceptDrops(
bool on)
3526 setAttribute(Qt::WA_AcceptDrops, on);
3531
3532
3533
3534
3535
3536
3537
3538void QWidget::setDisabled(
bool disable)
3540 setEnabled(!disable);
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556QRect QWidget::frameGeometry()
const
3559 if (isWindow() && ! (windowType() == Qt::Popup)) {
3560 QRect fs = d->frameStrut();
3561 return QRect(data->crect.x() - fs.left(),
3562 data->crect.y() - fs.top(),
3563 data->crect.width() + fs.left() + fs.right(),
3564 data->crect.height() + fs.top() + fs.bottom());
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582int QWidget::x()
const
3585 if (isWindow() && ! (windowType() == Qt::Popup))
3586 return data->crect.x() - d->frameStrut().left();
3587 return data->crect.x();
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602int QWidget::y()
const
3605 if (isWindow() && ! (windowType() == Qt::Popup))
3606 return data->crect.y() - d->frameStrut().top();
3607 return data->crect.y();
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637QPoint QWidget::pos()
const
3640 QPoint result = data->crect.topLeft();
3641 if (isWindow() && ! (windowType() == Qt::Popup))
3642 if (!d->maybeTopData() || !d->maybeTopData()->posIncludesFrame)
3643 result -= d->frameStrut().topLeft();
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759QRect QWidget::normalGeometry()
const
3765 if (!isMaximized() && !isFullScreen())
3768 return d->topData()->normalGeometry;
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3784QRect QWidget::childrenRect()
const
3787 QRect r(0, 0, 0, 0);
3788 for (
int i = 0; i < d->children.size(); ++i) {
3789 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3790 if (w && !w->isWindow() && !w->isHidden())
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3808QRegion QWidget::childrenRegion()
const
3812 for (
int i = 0; i < d->children.size(); ++i) {
3813 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3814 if (w && !w->isWindow() && !w->isHidden()) {
3815 QRegion mask = w->mask();
3819 r |= mask.translated(w->pos());
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3843QSize QWidget::minimumSize()
const
3846 return d->extra ? QSize(d->extra->minw, d->extra->minh) : QSize(0, 0);
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3865QSize QWidget::maximumSize()
const
3868 return d->extra ? QSize(d->extra->maxw, d->extra->maxh)
3874
3875
3876
3877
3878
3879
3880
3881
3882
3885
3886
3887
3888
3889
3890
3891
3892
3893
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944QSize QWidget::sizeIncrement()
const
3947 return (d->extra && d->extra->topextra)
3948 ? QSize(d->extra->topextra->incw, d->extra->topextra->inch)
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3965QSize QWidget::baseSize()
const
3968 return (d->extra && d->extra->topextra)
3969 ? QSize(d->extra->topextra->basew, d->extra->topextra->baseh)
3973bool QWidgetPrivate::setMinimumSize_helper(
int &minw,
int &minh)
3977 int mw = minw, mh = minh;
3983 qWarning(
"QWidget::setMinimumSize: (%s/%s) "
3984 "The largest allowed size is (%d,%d)",
3985 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
3990 if (Q_UNLIKELY(minw < 0 || minh < 0)) {
3991 qWarning(
"QWidget::setMinimumSize: (%s/%s) Negative sizes (%d,%d) "
3993 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), minw, minh);
3994 minw = mw = qMax(minw, 0);
3995 minh = mh = qMax(minh, 0);
3998 if (extra->minw == mw && extra->minh == mh)
4002 extra->explicitMinSize = (mw ? Qt::Horizontal : 0) | (mh ? Qt::Vertical : 0);
4006void QWidgetPrivate::setConstraints_sys()
4009 if (extra && q->windowHandle()) {
4010 QWindow *win = q->windowHandle();
4011 QWindowPrivate *winp = qt_window_private(win);
4013 winp->minimumSize = QSize(extra->minw, extra->minh);
4014 winp->maximumSize = QSize(extra->maxw, extra->maxh);
4016 if (extra->topextra) {
4017 winp->baseSize = QSize(extra->topextra->basew, extra->topextra->baseh);
4018 winp->sizeIncrement = QSize(extra->topextra->incw, extra->topextra->inch);
4021 if (winp->platformWindow) {
4022 fixPosIncludesFrame();
4023 winp->platformWindow->propagateSizeHints();
4029
4030
4031
4032
4033
4034
4036void QWidget::setMinimumSize(
int minw,
int minh)
4039 if (!d->setMinimumSize_helper(minw, minh))
4043 d->setConstraints_sys();
4044 if (minw > width() || minh > height()) {
4045 bool resized = testAttribute(Qt::WA_Resized);
4046 bool maximized = isMaximized();
4047 resize(qMax(minw,width()), qMax(minh,height()));
4048 setAttribute(Qt::WA_Resized, resized);
4050 data->window_state = data->window_state | Qt::WindowMaximized;
4052#if QT_CONFIG(graphicsview)
4054 if (d->extra->proxyWidget)
4055 d->extra->proxyWidget->setMinimumSize(minw, minh);
4058 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4061bool QWidgetPrivate::setMaximumSize_helper(
int &maxw,
int &maxh)
4065 qWarning(
"QWidget::setMaximumSize: (%s/%s) "
4066 "The largest allowed size is (%d,%d)",
4067 q->objectName().toLocal8Bit().data(), q->metaObject()->className(),
QWIDGETSIZE_MAX,
4072 if (Q_UNLIKELY(maxw < 0 || maxh < 0)) {
4073 qWarning(
"QWidget::setMaximumSize: (%s/%s) Negative sizes (%d,%d) "
4075 q->objectName().toLocal8Bit().data(), q->metaObject()->className(), maxw, maxh);
4076 maxw = qMax(maxw, 0);
4077 maxh = qMax(maxh, 0);
4080 if (extra->maxw == maxw && extra->maxh == maxh)
4084 extra->explicitMaxSize = (maxw !=
QWIDGETSIZE_MAX ? Qt::Horizontal : 0) |
4090
4091
4092
4093
4094
4095
4096void QWidget::setMaximumSize(
int maxw,
int maxh)
4099 if (!d->setMaximumSize_helper(maxw, maxh))
4103 d->setConstraints_sys();
4104 if (maxw < width() || maxh < height()) {
4105 bool resized = testAttribute(Qt::WA_Resized);
4106 resize(qMin(maxw,width()), qMin(maxh,height()));
4107 setAttribute(Qt::WA_Resized, resized);
4110#if QT_CONFIG(graphicsview)
4112 if (d->extra->proxyWidget)
4113 d->extra->proxyWidget->setMaximumSize(maxw, maxh);
4117 d->updateGeometry_helper(d->extra->minw == d->extra->maxw && d->extra->minh == d->extra->maxh);
4121
4122
4123
4124
4125
4126void QWidget::setSizeIncrement(
int w,
int h)
4130 QTLWExtra* x = d->topData();
4131 if (x->incw == w && x->inch == h)
4136 d->setConstraints_sys();
4140
4141
4142
4143
4144
4145void QWidget::setBaseSize(
int basew,
int baseh)
4149 QTLWExtra* x = d->topData();
4150 if (x->basew == basew && x->baseh == baseh)
4155 d->setConstraints_sys();
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4173void QWidget::setFixedSize(
const QSize & s)
4175 setFixedSize(s.width(), s.height());
4180
4181
4182
4183
4184
4186void QWidget::setFixedSize(
int w,
int h)
4189 bool minSizeSet = d->setMinimumSize_helper(w, h);
4190 bool maxSizeSet = d->setMaximumSize_helper(w, h);
4191 if (!minSizeSet && !maxSizeSet)
4195 d->setConstraints_sys();
4197 d->updateGeometry_helper(
true);
4203void QWidget::setMinimumWidth(
int w)
4207 uint expl = d->extra->explicitMinSize | (w ? Qt::Horizontal : 0);
4208 setMinimumSize(w, minimumSize().height());
4209 d->extra->explicitMinSize = expl;
4212void QWidget::setMinimumHeight(
int h)
4216 uint expl = d->extra->explicitMinSize | (h ? Qt::Vertical : 0);
4217 setMinimumSize(minimumSize().width(), h);
4218 d->extra->explicitMinSize = expl;
4221void QWidget::setMaximumWidth(
int w)
4225 uint expl = d->extra->explicitMaxSize | (w ==
QWIDGETSIZE_MAX ? 0 : Qt::Horizontal);
4226 setMaximumSize(w, maximumSize().height());
4227 d->extra->explicitMaxSize = expl;
4230void QWidget::setMaximumHeight(
int h)
4234 uint expl = d->extra->explicitMaxSize | (h ==
QWIDGETSIZE_MAX ? 0 : Qt::Vertical);
4235 setMaximumSize(maximumSize().width(), h);
4236 d->extra->explicitMaxSize = expl;
4240
4241
4242
4243
4244
4246void QWidget::setFixedWidth(
int w)
4250 uint explMin = d->extra->explicitMinSize | Qt::Horizontal;
4251 uint explMax = d->extra->explicitMaxSize | Qt::Horizontal;
4252 setMinimumSize(w, minimumSize().height());
4253 setMaximumSize(w, maximumSize().height());
4254 d->extra->explicitMinSize = explMin;
4255 d->extra->explicitMaxSize = explMax;
4260
4261
4262
4263
4264
4266void QWidget::setFixedHeight(
int h)
4270 uint explMin = d->extra->explicitMinSize | Qt::Vertical;
4271 uint explMax = d->extra->explicitMaxSize | Qt::Vertical;
4272 setMinimumSize(minimumSize().width(), h);
4273 setMaximumSize(maximumSize().width(), h);
4274 d->extra->explicitMinSize = explMin;
4275 d->extra->explicitMaxSize = explMax;
4280
4281
4282
4283
4284
4285
4286
4288QPointF QWidget::mapTo(
const QWidget *parent,
const QPointF &pos)
const
4292 const QWidget * w =
this;
4293 while (w != parent) {
4294 p = w->mapToParent(p);
4295 w = w->parentWidget();
4297 qWarning(
"QWidget::mapTo(): parent must be in parent hierarchy");
4306
4307
4308QPoint QWidget::mapTo(
const QWidget *parent,
const QPoint &pos)
const
4310 return mapTo(parent, QPointF(pos)).toPoint();
4314
4315
4316
4317
4318
4319
4320
4322QPointF QWidget::mapFrom(
const QWidget *parent,
const QPointF &pos)
const
4326 const QWidget * w =
this;
4327 while (w != parent) {
4328 p = w->mapFromParent(p);
4329 w = w->parentWidget();
4331 qWarning(
"QWidget::mapFrom(): parent must be in parent hierarchy");
4340
4341
4342QPoint QWidget::mapFrom(
const QWidget *parent,
const QPoint &pos)
const
4344 return mapFrom(parent, QPointF(pos)).toPoint();
4348
4349
4350
4351
4352
4353
4354
4355
4357QPointF QWidget::mapToParent(
const QPointF &pos)
const
4359 return pos + QPointF(data->crect.topLeft());
4363
4364
4365QPoint QWidget::mapToParent(
const QPoint &pos)
const
4367 return pos + data->crect.topLeft();
4371
4372
4373
4374
4375
4376
4377
4378
4380QPointF QWidget::mapFromParent(
const QPointF &pos)
const
4382 return pos - QPointF(data->crect.topLeft());
4386
4387
4388QPoint QWidget::mapFromParent(
const QPoint &pos)
const
4390 return pos - data->crect.topLeft();
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4407QWidget *QWidget::window()
const
4409 QWidget *w =
const_cast<QWidget *>(
this);
4410 QWidget *p = w->parentWidget();
4411 while (!w->isWindow() && p) {
4413 p = p->parentWidget();
4419
4420
4421
4422
4423
4424
4425
4426
4427QWidget *QWidget::nativeParentWidget()
const
4429 QWidget *parent = parentWidget();
4430 while (parent && !parent->internalWinId())
4431 parent = parent->parentWidget();
4436
4437
4438
4439
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454QPalette::ColorRole QWidget::backgroundRole()
const
4457 const QWidget *w =
this;
4459 QPalette::ColorRole role = w->d_func()->bg_role;
4460 if (role != QPalette::NoRole)
4462 if (w->isWindow() || w->windowType() == Qt::SubWindow)
4464 w = w->parentWidget();
4466 return QPalette::Window;
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4485void QWidget::setBackgroundRole(QPalette::ColorRole role)
4489 d->updateSystemBackground();
4490 d->propagatePaletteChange();
4491 d->updateIsOpaque();
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505QPalette::ColorRole QWidget::foregroundRole()
const
4508 QPalette::ColorRole rl = QPalette::ColorRole(d->fg_role);
4509 if (rl != QPalette::NoRole)
4511 QPalette::ColorRole role = QPalette::WindowText;
4512 switch (backgroundRole()) {
4513 case QPalette::Button:
4514 role = QPalette::ButtonText;
4516 case QPalette::Base:
4517 role = QPalette::Text;
4519 case QPalette::Dark:
4520 case QPalette::Shadow:
4521 role = QPalette::Light;
4523 case QPalette::Highlight:
4524 role = QPalette::HighlightedText;
4526 case QPalette::ToolTipBase:
4527 role = QPalette::ToolTipText;
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550void QWidget::setForegroundRole(QPalette::ColorRole role)
4554 d->updateSystemBackground();
4555 d->propagatePaletteChange();
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608const QPalette &QWidget::palette()
const
4611 data->pal.setCurrentColorGroup(QPalette::Disabled);
4612 }
else if ((!isVisible() || isActiveWindow())
4613#if defined(Q_OS_WIN)
4614 && !QApplicationPrivate::isBlockedByModal(
const_cast<QWidget *>(
this))
4617 data->pal.setCurrentColorGroup(QPalette::Active);
4619 data->pal.setCurrentColorGroup(QPalette::Inactive);
4624void QWidget::setPalette(
const QPalette &palette)
4627 setAttribute(Qt::WA_SetPalette, palette.resolveMask() != 0);
4633 QPalette naturalPalette = d->naturalWidgetPalette(d->inheritedPaletteResolveMask);
4634 QPalette resolvedPalette = palette.resolve(naturalPalette);
4635 d->setPalette_helper(resolvedPalette);
4639
4640
4641
4642
4643
4644
4645
4646
4647QPalette QWidgetPrivate::naturalWidgetPalette(QPalette::ResolveMask inheritedMask)
const
4651 const bool useStyleSheetPropagationInWidgetStyles =
4652 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4654 QPalette naturalPalette = QApplication::palette(q);
4655 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4656 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4657#if QT_CONFIG(graphicsview)
4658 || (extra && extra->proxyWidget)
4661 if (QWidget *p = q->parentWidget()) {
4662 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4663 if (!naturalPalette.isCopyOf(QGuiApplication::palette())) {
4664 QPalette inheritedPalette = p->palette();
4665 inheritedPalette.setResolveMask(inheritedMask);
4666 naturalPalette = inheritedPalette.resolve(naturalPalette);
4668 naturalPalette = p->palette();
4672#if QT_CONFIG(graphicsview)
4673 else if (extra && extra->proxyWidget) {
4674 QPalette inheritedPalette = extra->proxyWidget->palette();
4675 inheritedPalette.setResolveMask(inheritedMask);
4676 naturalPalette = inheritedPalette.resolve(naturalPalette);
4680 naturalPalette.setResolveMask(0);
4681 return naturalPalette;
4684
4685
4686
4687
4688
4689
4690
4691void QWidgetPrivate::resolvePalette()
4693 QPalette naturalPalette = naturalWidgetPalette(inheritedPaletteResolveMask);
4694 QPalette resolvedPalette = data.pal.resolve(naturalPalette);
4695 setPalette_helper(resolvedPalette);
4698void QWidgetPrivate::setPalette_helper(
const QPalette &palette)
4701 if (data.pal == palette && data.pal.resolveMask() == palette.resolveMask())
4704 updateSystemBackground();
4705 propagatePaletteChange();
4711void QWidgetPrivate::updateSystemBackground()
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4761void QWidget::setFont(
const QFont &font)
4765#if QT_CONFIG(style_stylesheet)
4766 const QStyleSheetStyle* style;
4767 if (d->extra && (style = qt_styleSheet(d->extra->style)))
4768 style->saveWidgetFont(
this, font);
4771 setAttribute(Qt::WA_SetFont, font.resolveMask() != 0);
4777 QFont naturalFont = d->naturalWidgetFont(d->inheritedFontResolveMask);
4778 QFont resolvedFont = font.resolve(naturalFont);
4779 d->setFont_helper(resolvedFont);
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794QFont QWidgetPrivate::naturalWidgetFont(uint inheritedMask)
const
4798 const bool useStyleSheetPropagationInWidgetStyles =
4799 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4801 QFont naturalFont = QApplication::font(q);
4802 if ((!q->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles)
4803 && (!q->isWindow() || q->testAttribute(Qt::WA_WindowPropagation)
4804#if QT_CONFIG(graphicsview)
4805 || (extra && extra->proxyWidget)
4808 if (QWidget *p = q->parentWidget()) {
4809 if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) {
4810 if (!naturalFont.isCopyOf(QApplication::font())) {
4811 if (inheritedMask != 0) {
4812 QFont inheritedFont = p->font();
4813 inheritedFont.setResolveMask(inheritedMask);
4814 naturalFont = inheritedFont.resolve(naturalFont);
4817 naturalFont = p->font();
4821#if QT_CONFIG(graphicsview)
4822 else if (extra && extra->proxyWidget) {
4823 if (inheritedMask != 0) {
4824 QFont inheritedFont = extra->proxyWidget->font();
4825 inheritedFont.setResolveMask(inheritedMask);
4826 naturalFont = inheritedFont.resolve(naturalFont);
4831 naturalFont.setResolveMask(0);
4836
4837
4838
4839
4840QFont QWidgetPrivate::localFont()
const
4842 QFont localfont = data.fnt;
4843 localfont.setResolveMask(directFontResolveMask);
4848
4849
4850
4851
4852
4853
4854
4855void QWidgetPrivate::resolveFont()
4857 QFont naturalFont = naturalWidgetFont(inheritedFontResolveMask);
4858 QFont resolvedFont = localFont().resolve(naturalFont);
4859 setFont_helper(resolvedFont);
4863
4864
4865
4866
4867
4868
4869
4870
4871void QWidgetPrivate::updateFont(
const QFont &font)
4874#if QT_CONFIG(style_stylesheet)
4875 const QStyleSheetStyle* cssStyle;
4876 cssStyle = extra ? qt_styleSheet(extra->style) :
nullptr;
4877 const bool useStyleSheetPropagationInWidgetStyles =
4878 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
4881 data.fnt = QFont(font, q);
4884#if QT_CONFIG(graphicsview)
4885 if (!q->parentWidget() && extra && extra->proxyWidget) {
4886 QGraphicsProxyWidget *p = extra->proxyWidget;
4887 inheritedFontResolveMask = p->d_func()->inheritedFontResolveMask | p->font().resolveMask();
4890 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
4891 inheritedFontResolveMask = 0;
4893 uint newMask = data.fnt.resolveMask() | inheritedFontResolveMask;
4897 directFontResolveMask = data.fnt.resolveMask();
4898 data.fnt.setResolveMask(newMask);
4900 for (
int i = 0; i < children.size(); ++i) {
4901 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4904#if QT_CONFIG(style_stylesheet)
4905 }
else if (!useStyleSheetPropagationInWidgetStyles && w->testAttribute(Qt::WA_StyleSheet)) {
4908 cssStyle->updateStyleSheetFont(w);
4910 }
else if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
4912 QWidgetPrivate *wd = w->d_func();
4913 wd->inheritedFontResolveMask = newMask;
4919#if QT_CONFIG(style_stylesheet)
4920 if (!useStyleSheetPropagationInWidgetStyles && cssStyle) {
4921 cssStyle->updateStyleSheetFont(q);
4925 QEvent e(QEvent::FontChange);
4926 QCoreApplication::sendEvent(q, &e);
4929void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction)
4933 if ( (direction == Qt::RightToLeft) == q->testAttribute(Qt::WA_RightToLeft))
4935 q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft));
4936 if (!children.isEmpty()) {
4937 for (
int i = 0; i < children.size(); ++i) {
4938 QWidget *w = qobject_cast<QWidget*>(children.at(i));
4939 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_SetLayoutDirection))
4940 w->d_func()->setLayoutDirection_helper(direction);
4943 QEvent e(QEvent::LayoutDirectionChange);
4944 QCoreApplication::sendEvent(q, &e);
4947void QWidgetPrivate::resolveLayoutDirection()
4950 if (!q->testAttribute(Qt::WA_SetLayoutDirection))
4951 setLayoutDirection_helper(q->isWindow() ? QGuiApplication::layoutDirection() : q->parentWidget()->layoutDirection());
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973void QWidget::setLayoutDirection(Qt::LayoutDirection direction)
4977 if (direction == Qt::LayoutDirectionAuto) {
4978 unsetLayoutDirection();
4982 setAttribute(Qt::WA_SetLayoutDirection);
4983 d->setLayoutDirection_helper(direction);
4986Qt::LayoutDirection QWidget::layoutDirection()
const
4988 return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
4991void QWidget::unsetLayoutDirection()
4994 setAttribute(Qt::WA_SetLayoutDirection,
false);
4995 d->resolveLayoutDirection();
4999
5000
5001
5002
5003
5004
5005
5008
5009
5010
5011
5012
5013
5014
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5042QCursor QWidget::cursor()
const
5045 if (testAttribute(Qt::WA_SetCursor))
5046 return (d->extra && d->extra->curs)
5048 : QCursor(Qt::ArrowCursor);
5049 if (isWindow() || !parentWidget())
5050 return QCursor(Qt::ArrowCursor);
5051 return parentWidget()->cursor();
5054void QWidget::setCursor(
const QCursor &cursor)
5057 if (cursor.shape() != Qt::ArrowCursor
5058 || (d->extra && d->extra->curs))
5061 d->extra->curs = std::make_unique<QCursor>(cursor);
5063 setAttribute(Qt::WA_SetCursor);
5064 d->setCursor_sys(cursor);
5066 QEvent event(QEvent::CursorChange);
5067 QCoreApplication::sendEvent(
this, &event);
5070void QWidgetPrivate::setCursor_sys(
const QCursor &cursor)
5074 qt_qpa_set_cursor(q,
false);
5077void QWidget::unsetCursor()
5081 d->extra->curs.reset();
5083 setAttribute(Qt::WA_SetCursor,
false);
5084 d->unsetCursor_sys();
5086 QEvent event(QEvent::CursorChange);
5087 QCoreApplication::sendEvent(
this, &event);
5090void QWidgetPrivate::unsetCursor_sys()
5093 qt_qpa_set_cursor(q,
false);
5098 if (QWindow *window = w->windowHandle())
5099 window->setCursor(c);
5104 if (QWindow *window = w->windowHandle())
5105 window->unsetCursor();
5110 if (!w->testAttribute(Qt::WA_WState_Created))
5116 }
else if (lastUnderMouse) {
5117 const WId lastWinId = lastUnderMouse->effectiveWinId();
5118 const WId winId = w->effectiveWinId();
5119 if (lastWinId && lastWinId == winId)
5121 }
else if (!w->internalWinId()) {
5125 while (!w->internalWinId() && w->parentWidget() && !w->isWindow()
5126 && !w->testAttribute(Qt::WA_SetCursor))
5127 w = w->parentWidget();
5130 if (!w->internalWinId())
5131 nativeParent = w->nativeParentWidget();
5132 if (!nativeParent || !nativeParent->internalWinId())
5135 if (w->isWindow() || w->testAttribute(Qt::WA_SetCursor)) {
5137 applyCursor(nativeParent, w->cursor());
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186void QWidget::render(QPaintDevice *target,
const QPoint &targetOffset,
5187 const QRegion &sourceRegion, RenderFlags renderFlags)
5190 render(&p, targetOffset, sourceRegion, renderFlags);
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206void QWidget::render(QPainter *painter,
const QPoint &targetOffset,
5207 const QRegion &sourceRegion, RenderFlags renderFlags)
5209 if (Q_UNLIKELY(!painter)) {
5210 qWarning(
"QWidget::render: Null pointer to painter");
5214 if (Q_UNLIKELY(!painter->isActive())) {
5215 qWarning(
"QWidget::render: Cannot render with an inactive painter");
5219 const qreal opacity = painter->opacity();
5220 if (qFuzzyIsNull(opacity))
5224 const bool inRenderWithPainter = d->extra && d->extra->inRenderWithPainter;
5225 const QRegion toBePainted = !inRenderWithPainter ? d->prepareToRender(sourceRegion, renderFlags)
5227 if (toBePainted.isEmpty())
5232 d->extra->inRenderWithPainter =
true;
5234 QPaintEngine *engine = painter->paintEngine();
5236 QPaintEnginePrivate *enginePriv = engine->d_func();
5237 Q_ASSERT(enginePriv);
5238 QPaintDevice *target = engine->paintDevice();
5242 if (!inRenderWithPainter && (opacity < 1.0 || (target->devType() == QInternal::Printer))) {
5243 d->render_helper(painter, targetOffset, toBePainted, renderFlags);
5244 d->extra->inRenderWithPainter = inRenderWithPainter;
5249 QPainter *oldPainter = d->sharedPainter();
5250 d->setSharedPainter(painter);
5253 const QTransform oldTransform = enginePriv->systemTransform;
5254 const QRegion oldSystemClip = enginePriv->systemClip;
5255 const QRegion oldBaseClip = enginePriv->baseSystemClip;
5256 const QRegion oldSystemViewport = enginePriv->systemViewport;
5257 const Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection();
5260 if (painter->hasClipping()) {
5261 const QRegion painterClip = painter->deviceTransform().map(painter->clipRegion());
5262 enginePriv->setSystemViewport(oldSystemClip.isEmpty() ? painterClip : oldSystemClip & painterClip);
5264 enginePriv->setSystemViewport(oldSystemClip);
5266 painter->setLayoutDirection(layoutDirection());
5268 d->render(target, targetOffset, toBePainted, renderFlags);
5271 enginePriv->baseSystemClip = oldBaseClip;
5272 enginePriv->setSystemTransformAndViewport(oldTransform, oldSystemViewport);
5273 enginePriv->systemStateChanged();
5274 painter->setLayoutDirection(oldLayoutDirection);
5277 d->setSharedPainter(oldPainter);
5279 d->extra->inRenderWithPainter = inRenderWithPainter;
5284 QResizeEvent e(target->size(), QSize());
5285 QCoreApplication::sendEvent(target, &e);
5287 const QObjectList children = target->children();
5288 for (
int i = 0; i < children.size(); ++i) {
5289 if (!children.at(i)->isWidgetType())
5292 if (!child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309QPixmap QWidget::grab(
const QRect &rectangle)
5312 if (testAttribute(Qt::WA_PendingResizeEvent) || !testAttribute(Qt::WA_WState_Created))
5313 sendResizeEvents(
this);
5315 const QWidget::RenderFlags renderFlags = QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask;
5317 const bool oldDirtyOpaqueChildren = d->dirtyOpaqueChildren;
5319 if (r.width() < 0 || r.height() < 0) {
5322 r = d->prepareToRender(QRegion(), renderFlags).boundingRect();
5323 r.setTopLeft(rectangle.topLeft());
5326 if (!r.intersects(rect()))
5329 const qreal dpr = devicePixelRatio();
5330 QPixmap res((QSizeF(r.size()) * dpr).toSize());
5331 res.setDevicePixelRatio(dpr);
5333 res.fill(Qt::transparent);
5334 d->render(&res, QPoint(), QRegion(r), renderFlags);
5336 d->dirtyOpaqueChildren = oldDirtyOpaqueChildren;
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350#if QT_CONFIG(graphicseffect)
5351QGraphicsEffect *QWidget::graphicsEffect()
const
5354 return d->graphicsEffect;
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380#if QT_CONFIG(graphicseffect)
5381void QWidget::setGraphicsEffect(QGraphicsEffect *effect)
5384 if (d->graphicsEffect == effect)
5387 if (d->graphicsEffect) {
5388 d->invalidateBackingStore(rect());
5389 delete d->graphicsEffect;
5390 d->graphicsEffect =
nullptr;
5395 QGraphicsEffectSourcePrivate *sourced =
new QWidgetEffectSourcePrivate(
this);
5396 QGraphicsEffectSource *source =
new QGraphicsEffectSource(*sourced);
5397 d->graphicsEffect = effect;
5398 effect->d_func()->setGraphicsEffectSource(source);
5402 d->updateIsOpaque();
5406bool QWidgetPrivate::isAboutToShow()
const
5416 QWidget *parent = q->parentWidget();
5417 return parent ? parent->d_func()->isAboutToShow() :
false;
5420QRegion QWidgetPrivate::prepareToRender(
const QRegion ®ion, QWidget::RenderFlags renderFlags)
5423 const bool isVisible = q->isVisible();
5426 if (!isVisible && !isAboutToShow()) {
5427 QWidget *topLevel = q->window();
5428 (
void)topLevel->d_func()->topData();
5429 topLevel->ensurePolished();
5433 QWidget *widget = q;
5434 QWidgetList hiddenWidgets;
5436 if (widget->isHidden()) {
5437 widget->setAttribute(Qt::WA_WState_Hidden,
false);
5438 hiddenWidgets.append(widget);
5439 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5440 widget->d_func()->updateGeometry_helper(
true);
5442 widget = widget->parentWidget();
5446 if (topLevel->d_func()->layout)
5447 topLevel->d_func()->layout->activate();
5450 QTLWExtra *topLevelExtra = topLevel->d_func()->maybeTopData();
5451 if (topLevelExtra && !topLevelExtra->sizeAdjusted
5452 && !topLevel->testAttribute(Qt::WA_Resized)) {
5453 topLevel->adjustSize();
5454 topLevel->setAttribute(Qt::WA_Resized,
false);
5458 topLevel->d_func()->activateChildLayoutsRecursively();
5461 for (
int i = 0; i < hiddenWidgets.size(); ++i) {
5462 QWidget *widget = hiddenWidgets.at(i);
5463 widget->setAttribute(Qt::WA_WState_Hidden);
5464 if (!widget->isWindow() && widget->parentWidget()->d_func()->layout)
5465 widget->parentWidget()->d_func()->layout->invalidate();
5467 }
else if (isVisible) {
5468 q->window()->d_func()->sendPendingMoveAndResizeEvents(
true,
true);
5472 QRegion toBePainted = !region.isEmpty() ? region : QRegion(q->rect());
5473 if (!(renderFlags & QWidget::IgnoreMask) && extra && extra->hasMask)
5474 toBePainted &= extra->mask;
5478void QWidgetPrivate::render_helper(QPainter *painter,
const QPoint &targetOffset,
const QRegion &toBePainted,
5479 QWidget::RenderFlags renderFlags)
5482 Q_ASSERT(!toBePainted.isEmpty());
5485 const QTransform originalTransform = painter->worldTransform();
5486 const bool useDeviceCoordinates = originalTransform.isScaling();
5487 if (!useDeviceCoordinates) {
5489 const QRect rect = toBePainted.boundingRect();
5490 const QSize size = rect.size();
5494 const qreal pixmapDevicePixelRatio = painter->device()->devicePixelRatio();
5495 QPixmap pixmap(size * pixmapDevicePixelRatio);
5496 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio);
5498 if (!(renderFlags & QWidget::DrawWindowBackground) || !isOpaque)
5499 pixmap.fill(Qt::transparent);
5500 q->render(&pixmap, QPoint(), toBePainted, renderFlags);
5502 const bool restore = !(painter->renderHints() & QPainter::SmoothPixmapTransform);
5503 painter->setRenderHints(QPainter::SmoothPixmapTransform,
true);
5505 painter->drawPixmap(targetOffset, pixmap);
5508 painter->setRenderHints(QPainter::SmoothPixmapTransform,
false);
5512 QTransform transform = originalTransform;
5513 transform.translate(targetOffset.x(), targetOffset.y());
5515 QPaintDevice *device = painter->device();
5519 const QRectF rect(toBePainted.boundingRect());
5520 QRect deviceRect = transform.mapRect(QRectF(0, 0, rect.width(), rect.height())).toAlignedRect();
5521 deviceRect &= QRect(0, 0, device->width(), device->height());
5523 QPixmap pixmap(deviceRect.size());
5524 pixmap.fill(Qt::transparent);
5527 QPainter pixmapPainter(&pixmap);
5528 pixmapPainter.setRenderHints(painter->renderHints());
5529 transform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y());
5530 pixmapPainter.setTransform(transform);
5532 q->render(&pixmapPainter, QPoint(), toBePainted, renderFlags);
5533 pixmapPainter.end();
5536 painter->setTransform(QTransform());
5537 painter->drawPixmap(deviceRect.topLeft(), pixmap);
5538 painter->setTransform(originalTransform);
5542void QWidgetPrivate::drawWidget(QPaintDevice *pdev,
const QRegion &rgn,
const QPoint &offset, DrawWidgetFlags flags,
5543 QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5550 qCInfo(lcWidgetPainting) <<
"Drawing" << rgn <<
"of" << q <<
"at" << offset
5551 <<
"into paint device" << pdev <<
"with" << flags;
5553 const bool asRoot = flags & DrawAsRoot;
5554 bool onScreen = shouldPaintOnScreen();
5556#if QT_CONFIG(graphicseffect)
5557 if (graphicsEffect && graphicsEffect->isEnabled()) {
5558 QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
5559 QWidgetEffectSourcePrivate *sourced =
static_cast<QWidgetEffectSourcePrivate *>
5561 if (!sourced->context) {
5562 const QRegion effectRgn((flags & UseEffectRegionBounds) ? rgn.boundingRect() : rgn);
5563 QWidgetPaintContext context(pdev, effectRgn, offset, flags, sharedPainter, repaintManager);
5564 sourced->context = &context;
5565 if (!sharedPainter) {
5566 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), effectRgn.translated(offset));
5568 p.translate(offset);
5569 context.painter = &p;
5570 graphicsEffect->draw(&p);
5571 setSystemClip(pdev->paintEngine(), 1, QRegion());
5573 context.painter = sharedPainter;
5574 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
5575 sourced->invalidateCache();
5576 sourced->lastEffectTransform = sharedPainter->worldTransform();
5578 sharedPainter->save();
5579 sharedPainter->translate(offset);
5580 setSystemClip(sharedPainter->paintEngine(), sharedPainter->device()->devicePixelRatio(), effectRgn.translated(offset));
5581 graphicsEffect->draw(sharedPainter);
5582 setSystemClip(sharedPainter->paintEngine(), 1, QRegion());
5583 sharedPainter->restore();
5585 sourced->context =
nullptr;
5588 repaintManager->markNeedsFlush(q, effectRgn, offset);
5594 flags = flags & ~UseEffectRegionBounds;
5596 const bool alsoOnScreen = flags & DrawPaintOnScreen;
5597 const bool recursive = flags & DrawRecursive;
5598 const bool alsoInvisible = flags & DrawInvisible;
5600 Q_ASSERT(sharedPainter ? sharedPainter->isActive() :
true);
5602 QRegion toBePainted(rgn);
5603 if (asRoot && !alsoInvisible)
5604 toBePainted &= clipRect();
5605 if (!(flags & DontSubtractOpaqueChildren))
5606 subtractOpaqueChildren(toBePainted, q->rect());
5608 if (!toBePainted.isEmpty()) {
5609 if (!onScreen || alsoOnScreen) {
5611 if (Q_UNLIKELY(q->testAttribute(Qt::WA_WState_InPaintEvent)))
5612 qWarning(
"QWidget::repaint: Recursive repaint detected");
5613 q->setAttribute(Qt::WA_WState_InPaintEvent);
5616 QPaintEngine *paintEngine = pdev->paintEngine();
5618 setRedirected(pdev, -offset);
5621 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted);
5623 paintEngine->d_func()->systemRect = q->data->crect;
5626 if ((asRoot || q->autoFillBackground() || onScreen || q->testAttribute(Qt::WA_StyledBackground))
5627 && !q->testAttribute(Qt::WA_OpaquePaintEvent) && !q->testAttribute(Qt::WA_NoSystemBackground)) {
5628 beginBackingStorePainting();
5630 p.setRenderHint(QPainter::SmoothPixmapTransform);
5631 paintBackground(&p, toBePainted, (asRoot || onScreen) ? (flags | DrawAsRoot) : DrawWidgetFlags());
5632 endBackingStorePainting();
5636 setSystemClip(pdev->paintEngine(), pdev->devicePixelRatio(), toBePainted.translated(offset));
5638 if (!onScreen && !asRoot && !isOpaque && q->testAttribute(Qt::WA_TintedBackground)) {
5639 beginBackingStorePainting();
5641 QColor tint = q->palette().window().color();
5642 tint.setAlphaF(.6f);
5643 p.fillRect(toBePainted.boundingRect(), tint);
5644 endBackingStorePainting();
5649 qDebug() <<
"painting" << q <<
"opaque ==" << isOpaque();
5650 qDebug() <<
"clipping to" << toBePainted <<
"location == " << offset
5651 <<
"geometry ==" << QRect(q->mapTo(q->window(), QPoint(0, 0)), q->size());
5654 bool skipPaintEvent =
false;
5655 if (renderToTexture) {
5658 beginBackingStorePainting();
5659 if (!q->testAttribute(Qt::WA_AlwaysStackOnTop) && repaintManager) {
5661 p.setCompositionMode(QPainter::CompositionMode_Source);
5662 p.fillRect(q->rect(), Qt::transparent);
5663 }
else if (!repaintManager) {
5665 QImage img = grabFramebuffer();
5668 if (img.format() == QImage::Format_RGB32)
5669 img.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied);
5671 p.drawImage(q->rect(), img);
5672 skipPaintEvent =
true;
5674 endBackingStorePainting();
5675 if (renderToTextureReallyDirty)
5676 renderToTextureReallyDirty = 0;
5678 skipPaintEvent =
true;
5681 if (!skipPaintEvent) {
5683 sendPaintEvent(toBePainted);
5687 repaintManager->markNeedsFlush(q, toBePainted, offset);
5691 restoreRedirected();
5693 paintEngine->d_func()->systemRect = QRect();
5695 paintEngine->d_func()->currentClipDevice =
nullptr;
5697 setSystemClip(pdev->paintEngine(), 1, QRegion());
5699 q->setAttribute(Qt::WA_WState_InPaintEvent,
false);
5700 if (Q_UNLIKELY(q->paintingActive()))
5701 qWarning(
"QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent");
5703 if (paintEngine && paintEngine->autoDestruct()) {
5706 }
else if (q->isWindow()) {
5707 QPaintEngine *engine = pdev->paintEngine();
5710 p.setClipRegion(toBePainted);
5711 const QBrush bg = q->palette().brush(QPalette::Window);
5712 if (bg.style() == Qt::TexturePattern)
5713 p.drawTiledPixmap(q->rect(), bg.texture());
5715 p.fillRect(q->rect(), bg);
5717 if (engine->autoDestruct())
5723 if (recursive && !children.isEmpty()) {
5724 paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot,
5725 sharedPainter, repaintManager);
5729void QWidgetPrivate::sendPaintEvent(
const QRegion &toBePainted)
5732 QPaintEvent e(toBePainted);
5733 QCoreApplication::sendSpontaneousEvent(q, &e);
5735 if (renderToTexture)
5739void QWidgetPrivate::render(QPaintDevice *target,
const QPoint &targetOffset,
5740 const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
5742 if (Q_UNLIKELY(!target)) {
5743 qWarning(
"QWidget::render: null pointer to paint device");
5747 const bool inRenderWithPainter = extra && extra->inRenderWithPainter;
5748 QRegion paintRegion = !inRenderWithPainter
5749 ? prepareToRender(sourceRegion, renderFlags)
5751 if (paintRegion.isEmpty())
5754 QPainter *oldSharedPainter = inRenderWithPainter ? sharedPainter() :
nullptr;
5758 if (target->devType() == QInternal::Widget) {
5759 QWidgetPrivate *targetPrivate =
static_cast<QWidget *>(target)->d_func();
5760 if (targetPrivate->extra && targetPrivate->extra->inRenderWithPainter) {
5761 QPainter *targetPainter = targetPrivate->sharedPainter();
5762 if (targetPainter && targetPainter->isActive())
5763 setSharedPainter(targetPainter);
5770 QPoint offset = targetOffset;
5771 offset -= paintRegion.boundingRect().topLeft();
5772 QPoint redirectionOffset;
5773 QPaintDevice *redirected =
nullptr;
5775 if (target->devType() == QInternal::Widget)
5776 redirected =
static_cast<QWidget *>(target)->d_func()->redirected(&redirectionOffset);
5779 target = redirected;
5780 offset -= redirectionOffset;
5783 if (!inRenderWithPainter) {
5784 if (QPaintEngine *targetEngine = target->paintEngine()) {
5785 const QRegion targetSystemClip = targetEngine->systemClip();
5786 if (!targetSystemClip.isEmpty())
5787 paintRegion &= targetSystemClip.translated(-offset);
5792 DrawWidgetFlags flags = DrawPaintOnScreen | DrawInvisible;
5793 if (renderFlags & QWidget::DrawWindowBackground)
5794 flags |= DrawAsRoot;
5796 if (renderFlags & QWidget::DrawChildren)
5797 flags |= DrawRecursive;
5799 flags |= DontSubtractOpaqueChildren;
5801 flags |= DontSetCompositionMode;
5804 drawWidget(target, paintRegion, offset, flags, sharedPainter());
5807 if (oldSharedPainter)
5808 setSharedPainter(oldSharedPainter);
5811void QWidgetPrivate::paintSiblingsRecursive(QPaintDevice *pdev,
const QObjectList& siblings,
int index,
const QRegion &rgn,
5812 const QPoint &offset, DrawWidgetFlags flags
5813 , QPainter *sharedPainter, QWidgetRepaintManager *repaintManager)
5815 QWidget *w =
nullptr;
5817 bool dirtyBoundingRect =
true;
5818 const bool exludeOpaqueChildren = (flags & DontDrawOpaqueChildren);
5819 const bool excludeNativeChildren = (flags & DontDrawNativeChildren);
5822 QWidget *x = qobject_cast<QWidget*>(siblings.at(index));
5823 if (x && !(exludeOpaqueChildren && x->d_func()->isOpaque) && !x->isHidden() && !x->isWindow()
5824 && !(excludeNativeChildren && x->internalWinId())) {
5825 if (dirtyBoundingRect) {
5826 boundingRect = rgn.boundingRect();
5827 dirtyBoundingRect =
false;
5830 if (qRectIntersects(boundingRect, x->d_func()->effectiveRectFor(x->data->crect))) {
5836 }
while (index >= 0);
5841 QWidgetPrivate *wd = w->d_func();
5842 const QPoint widgetPos(w->data->crect.topLeft());
5843 const bool hasMask = wd->extra && wd->extra->hasMask && !wd->graphicsEffect;
5847 wr -= hasMask ? wd->extra->mask.translated(widgetPos) : w->data->crect;
5848 paintSiblingsRecursive(pdev, siblings, --index, wr, offset, flags,
5849 sharedPainter, repaintManager);
5852 if (w->updatesEnabled()
5853#if QT_CONFIG(graphicsview)
5854 && (!w->d_func()->extra || !w->d_func()->extra->proxyWidget)
5857 QRegion wRegion(rgn);
5858 wRegion &= wd->effectiveRectFor(w->data->crect);
5859 wRegion.translate(-widgetPos);
5861 wRegion &= wd->extra->mask;
5862 wd->drawWidget(pdev, wRegion, offset + widgetPos, flags, sharedPainter, repaintManager);
5866#if QT_CONFIG(graphicseffect)
5867QRectF QWidgetEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system)
const
5869 if (system != Qt::DeviceCoordinates)
5870 return m_widget->rect();
5872 if (Q_UNLIKELY(!context)) {
5874 qWarning(
"QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
5878 return context->painter->worldTransform().mapRect(m_widget->rect());
5881void QWidgetEffectSourcePrivate::draw(QPainter *painter)
5883 if (!context || context->painter != painter) {
5884 m_widget->render(painter);
5890 QRegion toBePainted = context->rgn;
5891 toBePainted &= m_widget->rect();
5892 QWidgetPrivate *wd = qt_widget_private(m_widget);
5893 if (wd->extra && wd->extra->hasMask)
5894 toBePainted &= wd->extra->mask;
5896 wd->drawWidget(context->pdev, toBePainted, context->offset, context->flags,
5897 context->sharedPainter, context->repaintManager);
5900QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
5901 QGraphicsEffect::PixmapPadMode mode)
const
5903 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
5904 if (Q_UNLIKELY(!context && deviceCoordinates)) {
5906 qWarning(
"QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
5910 QPoint pixmapOffset;
5911 QRectF sourceRect = m_widget->rect();
5913 if (deviceCoordinates) {
5914 const QTransform &painterTransform = context->painter->worldTransform();
5915 sourceRect = painterTransform.mapRect(sourceRect);
5916 pixmapOffset = painterTransform.map(pixmapOffset);
5921 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect)
5922 effectRect = m_widget->graphicsEffect()->boundingRectFor(sourceRect).toAlignedRect();
5923 else if (mode == QGraphicsEffect::PadToTransparentBorder)
5924 effectRect = sourceRect.adjusted(-1, -1, 1, 1).toAlignedRect();
5926 effectRect = sourceRect.toAlignedRect();
5929 *offset = effectRect.topLeft();
5931 pixmapOffset -= effectRect.topLeft();
5934 if (
const auto *paintDevice = context->painter->device())
5935 dpr = paintDevice->devicePixelRatio();
5937 qWarning(
"QWidgetEffectSourcePrivate::pixmap: Painter not active");
5938 QPixmap pixmap(effectRect.size() * dpr);
5939 pixmap.setDevicePixelRatio(dpr);
5941 pixmap.fill(Qt::transparent);
5942 m_widget->render(&pixmap, pixmapOffset, QRegion(), QWidget::DrawChildren);
5947#if QT_CONFIG(graphicsview)
5949
5950
5951
5952
5953
5954
5955
5956QGraphicsProxyWidget *QWidgetPrivate::nearestGraphicsProxyWidget(
const QWidget *origin)
5959 const auto &extra = origin->d_func()->extra;
5960 if (extra && extra->proxyWidget)
5961 return extra->proxyWidget;
5962 return nearestGraphicsProxyWidget(origin->parentWidget());
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5983void QWidgetPrivate::setLocale_helper(
const QLocale &loc,
bool forceUpdate)
5986 if (locale == loc && !forceUpdate)
5991 if (!children.isEmpty()) {
5992 for (
int i = 0; i < children.size(); ++i) {
5993 QWidget *w = qobject_cast<QWidget*>(children.at(i));
5996 if (w->testAttribute(Qt::WA_SetLocale))
5998 if (w->isWindow() && !w->testAttribute(Qt::WA_WindowPropagation))
6000 w->d_func()->setLocale_helper(loc, forceUpdate);
6003 QEvent e(QEvent::LocaleChange);
6004 QCoreApplication::sendEvent(q, &e);
6007void QWidget::setLocale(
const QLocale &locale)
6011 setAttribute(Qt::WA_SetLocale);
6012 d->setLocale_helper(locale);
6015QLocale QWidget::locale()
const
6022void QWidgetPrivate::resolveLocale()
6026 if (!q->testAttribute(Qt::WA_SetLocale)) {
6027 QWidget *parent = q->parentWidget();
6028 setLocale_helper(!parent || (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation))
6029 ? QLocale() : parent->locale());
6033void QWidget::unsetLocale()
6036 setAttribute(Qt::WA_SetLocale,
false);
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063QString QWidget::windowTitle()
const
6066 if (d->extra && d->extra->topextra) {
6067 if (!d->extra->topextra->caption.isEmpty())
6068 return d->extra->topextra->caption;
6069 if (!d->extra->topextra->filePath.isEmpty())
6070 return QFileInfo(d->extra->topextra->filePath).fileName() +
"[*]"_L1;
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6090 QString cap = title;
6094 const auto placeHolder =
"[*]"_L1;
6095 int index = cap.indexOf(placeHolder);
6098 while (index != -1) {
6099 index += placeHolder.size();
6101 while (cap.indexOf(placeHolder, index) == index) {
6103 index += placeHolder.size();
6107 int lastIndex = cap.lastIndexOf(placeHolder, index - 1);
6108 if (widget->isWindowModified()
6109 && widget->style()->styleHint(QStyle::SH_TitleBar_ModifyNotification,
nullptr, widget))
6110 cap.replace(lastIndex, 3, QWidget::tr(
"*"));
6112 cap.remove(lastIndex, 3);
6115 index = cap.indexOf(placeHolder, index);
6118 cap.replace(
"[*][*]"_L1, placeHolder);
6123void QWidgetPrivate::setWindowTitle_helper(
const QString &title)
6126 if (q->testAttribute(Qt::WA_WState_Created))
6127 setWindowTitle_sys(qt_setWindowTitle_helperHelper(title, q));
6130void QWidgetPrivate::setWindowTitle_sys(
const QString &caption)
6136 if (QWindow *window = q->windowHandle())
6138#if QT_CONFIG(accessibility)
6139 QString oldAccessibleName;
6140 const QAccessibleInterface *accessible = QAccessible::isActive()
6141 ? QAccessible::queryAccessibleInterface(q)
6144 oldAccessibleName = accessible->text(QAccessible::Name);
6147 window->setTitle(caption);
6149#if QT_CONFIG(accessibility)
6150 if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) {
6151 QAccessibleEvent event(q, QAccessible::NameChanged);
6152 QAccessible::updateAccessibility(&event);
6158void QWidgetPrivate::setWindowIconText_helper(
const QString &title)
6161 if (q->testAttribute(Qt::WA_WState_Created))
6162 setWindowIconText_sys(qt_setWindowTitle_helperHelper(title, q));
6165void QWidgetPrivate::setWindowIconText_sys(
const QString &iconText)
6171 if (QWindow *window = q->windowHandle()) {
6172 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(window->handle()))
6173 xcbWindow->setWindowIconText(iconText);
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6192void QWidget::setWindowIconText(
const QString &iconText)
6194 if (QWidget::windowIconText() == iconText)
6198 d->topData()->iconText = iconText;
6199 d->setWindowIconText_helper(iconText);
6201 QEvent e(QEvent::IconTextChange);
6202 QCoreApplication::sendEvent(
this, &e);
6204 emit windowIconTextChanged(iconText);
6208
6209
6210
6211
6212
6213
6214
6216void QWidget::setWindowTitle(
const QString &title)
6218 if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
6222 d->topData()->caption = title;
6223 d->setWindowTitle_helper(title);
6225 QEvent e(QEvent::WindowTitleChange);
6226 QCoreApplication::sendEvent(
this, &e);
6228 emit windowTitleChanged(title);
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246QIcon QWidget::windowIcon()
const
6248 const QWidget *w =
this;
6250 const QWidgetPrivate *d = w->d_func();
6251 if (d->extra && d->extra->topextra && d->extra->topextra->icon)
6252 return *d->extra->topextra->icon;
6253 w = w->parentWidget();
6255 return QApplication::windowIcon();
6258void QWidgetPrivate::setWindowIcon_helper()
6261 QEvent e(QEvent::WindowIconChange);
6267 if (!q->windowHandle())
6268 QCoreApplication::sendEvent(q, &e);
6269 for (
int i = 0; i < children.size(); ++i) {
6270 QWidget *w = qobject_cast<QWidget *>(children.at(i));
6271 if (w && !w->isWindow())
6272 QCoreApplication::sendEvent(w, &e);
6277
6278
6279
6280
6281
6282
6283
6285void QWidget::setWindowIcon(
const QIcon &icon)
6289 setAttribute(Qt::WA_SetWindowIcon, !icon.isNull());
6292 if (!d->extra->topextra->icon)
6293 d->extra->topextra->icon = std::make_unique<QIcon>(icon);
6295 *d->extra->topextra->icon = icon;
6297 d->setWindowIcon_sys();
6298 d->setWindowIcon_helper();
6300 emit windowIconChanged(icon);
6303void QWidgetPrivate::setWindowIcon_sys()
6306 if (QWindow *window = q->windowHandle())
6307 window->setIcon(q->windowIcon());
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6325QString QWidget::windowIconText()
const
6328 return (d->extra && d->extra->topextra) ? d->extra->topextra->iconText : QString();
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6355QString QWidget::windowFilePath()
const
6358 return (d->extra && d->extra->topextra) ? d->extra->topextra->filePath : QString();
6361void QWidget::setWindowFilePath(
const QString &filePath)
6363 if (filePath == windowFilePath())
6369 d->extra->topextra->filePath = filePath;
6370 d->setWindowFilePath_helper(filePath);
6373void QWidgetPrivate::setWindowFilePath_helper(
const QString &filePath)
6375 if (extra->topextra && extra->topextra->caption.isEmpty()) {
6377 setWindowTitle_helper(QFileInfo(filePath).fileName());
6381 setWindowTitle_helper(q->windowTitle());
6385 setWindowFilePath_sys(filePath);
6389void QWidgetPrivate::setWindowFilePath_sys(
const QString &filePath)
6395 if (QWindow *window = q->windowHandle())
6396 window->setFilePath(filePath);
6400
6401
6402
6403
6405QString QWidget::windowRole()
const
6408 return (d->extra && d->extra->topextra) ? d->extra->topextra->role : QString();
6412
6413
6414
6415void QWidget::setWindowRole(
const QString &role)
6417#if QT_CONFIG(xcb) || QT_CONFIG(wayland)
6420 d->topData()->role = role;
6425 if (windowHandle()) {
6427 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(windowHandle()->handle()))
6428 xcbWindow->setWindowRole(role);
6430#if QT_CONFIG(wayland)
6431 if (
auto *waylandWindow =
dynamic_cast<QWaylandWindow*>(windowHandle()->handle()))
6432 waylandWindow->setSessionRestoreId(role);
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6486void QWidget::setFocusProxy(QWidget * w)
6489 if (!w && !d->extra)
6492 for (QWidget* fp = w; fp; fp = fp->focusProxy()) {
6493 if (Q_UNLIKELY(fp ==
this)) {
6494 qWarning(
"QWidget: %s (%s) already in focus proxy chain", metaObject()->className(), objectName().toLocal8Bit().constData());
6499 const bool moveFocusToProxy = (QApplicationPrivate::focus_widget ==
this);
6502 d->extra->focus_proxy = w;
6504 if (w && isAncestorOf(w)) {
6510 const QWidget *parentOfW = w->parentWidget();
6511 Q_ASSERT(parentOfW);
6512 QWidget *firstChild =
nullptr;
6513 const auto childList = children();
6514 for (QObject *child : childList) {
6515 if ((firstChild = qobject_cast<QWidget *>(child)))
6518 Q_ASSERT(firstChild);
6519 d->insertIntoFocusChainBefore(firstChild);
6520 }
else if (w && w->isAncestorOf(
this)) {
6525 QWidget *parentsNext = w->nextInFocusChain();
6526 if (parentsNext ==
this) {
6528 Q_ASSERT(previousInFocusChain() == w);
6530 d->QWidgetPrivate::insertIntoFocusChainAfter(w);
6534 if (moveFocusToProxy)
6535 setFocus(Qt::OtherFocusReason);
6540
6541
6542
6543
6545QWidget *QWidget::focusProxy()
const
6548 return d->extra ? d->extra->focus_proxy.data() :
nullptr;
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564bool QWidget::hasFocus()
const
6566 const QWidget* w =
this;
6567 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
6568 w = w->d_func()->extra->focus_proxy;
6569#if QT_CONFIG(graphicsview)
6570 if (QWidget *window = w->window()) {
6571 const auto &e = window->d_func()->extra;
6572 if (e && e->proxyWidget && e->proxyWidget->hasFocus() && window->focusWidget() == w)
6576 return (QApplication::focusWidget() == w);
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6614void QWidget::setFocus(Qt::FocusReason reason)
6619 QWidget *f = d_func()->deepestFocusProxy();
6623 if (QApplication::focusWidget() == f)
6626#if QT_CONFIG(graphicsview)
6627 QWidget *previousProxyFocus =
nullptr;
6628 if (
const auto &topData = window()->d_func()->extra) {
6629 if (topData->proxyWidget && topData->proxyWidget->hasFocus()) {
6630 previousProxyFocus = topData->proxyWidget->widget()->focusWidget();
6631 if (previousProxyFocus && previousProxyFocus->focusProxy())
6632 previousProxyFocus = previousProxyFocus->focusProxy();
6633 if (previousProxyFocus == f && !topData->proxyWidget->d_func()->proxyIsGivingFocus)
6639#if QT_CONFIG(graphicsview)
6641 if (
const auto &topData = window()->d_func()->extra) {
6642 if (topData->proxyWidget && !topData->proxyWidget->hasFocus()) {
6643 f->d_func()->updateFocusChild();
6644 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 1;
6645 topData->proxyWidget->setFocus(reason);
6646 topData->proxyWidget->d_func()->focusFromWidgetToProxy = 0;
6651 if (f->isActiveWindow()) {
6652 QWidget *prev = QApplicationPrivate::focus_widget;
6654 if (reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason
6655 && prev->testAttribute(Qt::WA_InputMethodEnabled)) {
6656 QGuiApplication::inputMethod()->commit();
6659 if (reason != Qt::NoFocusReason) {
6660 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason);
6661 QCoreApplication::sendEvent(prev, &focusAboutToChange);
6665 f->d_func()->updateFocusChild();
6667 QApplicationPrivate::setFocusWidget(f, reason);
6668#if QT_CONFIG(accessibility)
6670 if (!(f->inherits(
"QMenuBar") || f->inherits(
"QMenu") || f->inherits(
"QMenuItem")))
6672 QAccessibleEvent event(f, QAccessible::Focus);
6673 QAccessible::updateAccessibility(&event);
6676#if QT_CONFIG(graphicsview)
6677 if (
const auto &topData = window()->d_func()->extra) {
6678 if (topData->proxyWidget) {
6679 if (previousProxyFocus && previousProxyFocus != f) {
6681 QFocusEvent event(QEvent::FocusOut, reason);
6682 QPointer<QWidget> that = previousProxyFocus;
6683 QCoreApplication::sendEvent(previousProxyFocus, &event);
6685 QCoreApplication::sendEvent(that->style(), &event);
6688#if QT_CONFIG(graphicsview)
6690 if (
const auto &topData = window()->d_func()->extra)
6691 if (topData->proxyWidget && topData->proxyWidget->hasFocus())
6692 topData->proxyWidget->d_func()->updateProxyInputMethodAcceptanceFromWidget();
6695 QFocusEvent event(QEvent::FocusIn, reason);
6696 QPointer<QWidget> that = f;
6697 QCoreApplication::sendEvent(f, &event);
6699 QCoreApplication::sendEvent(that->style(), &event);
6705 f->d_func()->updateFocusChild();
6711
6712
6713
6714
6715
6716QWidget *QWidgetPrivate::deepestFocusProxy()
const
6720 QWidget *focusProxy = q->focusProxy();
6724 while (QWidget *nextFocusProxy = focusProxy->focusProxy())
6725 focusProxy = nextFocusProxy;
6732 const auto platformWindow = w->handle();
6733 return platformWindow && platformWindow->isEmbedded();
6736void QWidgetPrivate::setFocus_sys()
6741 if (extra && extra->hasWindowContainer)
6745 if (QWindow *nativeWindow = q->testAttribute(Qt::WA_WState_Created) ? q->window()->windowHandle() :
nullptr) {
6746 if (nativeWindow->type() != Qt::Popup && nativeWindow != QGuiApplication::focusWindow()
6747 && (QGuiApplication::applicationState() == Qt::ApplicationActive
6748 || QCoreApplication::testAttribute(Qt::AA_PluginApplication)
6749 || isEmbedded(nativeWindow))) {
6750 nativeWindow->requestActivate();
6756void QWidgetPrivate::updateFocusChild()
6761 if (q->isHidden()) {
6762 while (w && w->isHidden()) {
6763 w->d_func()->focus_child = q;
6764 w = w->isWindow() ?
nullptr : w->parentWidget();
6768 w->d_func()->focus_child = q;
6769 w = w->isWindow() ?
nullptr : w->parentWidget();
6773 if (QTLWExtra *extra = q->window()->d_func()->maybeTopData()) {
6775 emit extra->window->focusObjectChanged(q);
6780
6781
6782
6783
6784
6785
6786
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6801void QWidget::clearFocus()
6804 if (testAttribute(Qt::WA_InputMethodEnabled))
6805 QGuiApplication::inputMethod()->commit();
6807 QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange);
6808 QCoreApplication::sendEvent(
this, &focusAboutToChange);
6811 QTLWExtra *extra = window()->d_func()->maybeTopData();
6812 QObject *originalFocusObject =
nullptr;
6813 if (extra && extra->window) {
6814 originalFocusObject = extra->window->focusObject();
6817 if (!originalFocusObject)
6818 originalFocusObject = focusWidget();
6824 if (w->d_func()->focus_child ==
this)
6825 w->d_func()->focus_child =
nullptr;
6826 w = w->parentWidget();
6833 if (originalFocusObject && originalFocusObject != extra->window->focusObject())
6834 emit extra->window->focusObjectChanged(extra->window->focusObject());
6836#if QT_CONFIG(graphicsview)
6837 const auto &topData = d_func()->extra;
6838 if (topData && topData->proxyWidget)
6839 topData->proxyWidget->clearFocus();
6844 QApplicationPrivate::setFocusWidget(
nullptr, Qt::OtherFocusReason);
6845#if QT_CONFIG(accessibility)
6846 QAccessibleEvent event(
this, QAccessible::Focus);
6847 QAccessible::updateAccessibility(&event);
6854
6855
6856
6857
6858
6859
6860
6861
6864
6865
6866
6867
6868
6869
6870
6871
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6895bool QWidget::focusNextPrevChild(
bool next)
6897 QWidget* p = parentWidget();
6898 bool isSubWindow = (windowType() == Qt::SubWindow);
6899 if (!isWindow() && !isSubWindow && p)
6900 return p->focusNextPrevChild(next);
6901#if QT_CONFIG(graphicsview)
6903 if (d->extra && d->extra->proxyWidget)
6904 return d->extra->proxyWidget->focusNextPrevChild(next);
6907 bool wrappingOccurred =
false;
6908 QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(
this, next,
6910 if (!w)
return false;
6912 Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
6915
6916
6917
6918
6919 if (wrappingOccurred) {
6920 QWindow *window = windowHandle();
6921 if (window !=
nullptr) {
6922 QWindowPrivate *winp = qt_window_private(window);
6924 if (winp->platformWindow !=
nullptr) {
6925 QFocusEvent event(QEvent::FocusIn, reason);
6927 winp->platformWindow->windowEvent(&event);
6928 if (event.isAccepted())
return true;
6933 w->setFocus(reason);
6938
6939
6940
6941
6942
6943
6944
6946QWidget *QWidget::focusWidget()
const
6948 return const_cast<QWidget *>(d_func()->focus_child);
6951QObject *QWidgetPrivate::focusObject()
6954 QWidget *proxy = deepestFocusProxy();
6955 return proxy ? proxy : q;
6959
6960
6961
6962
6963QWidget *QWidget::nextInFocusChain()
const
6966 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Next);
6970
6971
6972
6973
6974
6975
6976
6977QWidget *QWidget::previousInFocusChain()
const
6980 return d->nextPrevElementInFocusChain(QWidgetPrivate::FocusDirection::Previous);
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998bool QWidget::isActiveWindow()
const
7000 QWidget *tlw = window();
7001 if (tlw == QApplication::activeWindow() || (isVisible() && (tlw->windowType() == Qt::Popup)))
7004#if QT_CONFIG(graphicsview)
7005 if (
const auto &tlwExtra = tlw->d_func()->extra) {
7006 if (isVisible() && tlwExtra->proxyWidget)
7007 return tlwExtra->proxyWidget->isActiveWindow();
7011 if (style()->styleHint(QStyle::SH_Widget_ShareActivation,
nullptr,
this)) {
7012 if (tlw->windowType() == Qt::Tool &&
7014 (!tlw->parentWidget() || tlw->parentWidget()->isActiveWindow()))
7016 QWidget *w = QApplication::activeWindow();
7017 while(w && tlw->windowType() == Qt::Tool &&
7018 !w->isModal() && w->parentWidget()) {
7019 w = w->parentWidget()->window();
7026 if (QWindow *ww = QGuiApplication::focusWindow()) {
7028 QWidgetWindow *qww = qobject_cast<QWidgetWindow *>(ww);
7029 QWindowContainer *qwc = qww ? qobject_cast<QWindowContainer *>(qww->widget()) : 0;
7030 if (qwc && qwc->topLevelWidget() == tlw)
7041 if (
const QWindow *w = tlw->windowHandle()) {
7043 return w->handle()->isActive();
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099void QWidget::setTabOrder(QWidget* first, QWidget *second)
7101 if (!first || !second || first == second
7102 || first->focusPolicy() == Qt::NoFocus
7103 || second->focusPolicy() == Qt::NoFocus)
7106 if (Q_UNLIKELY(first->window() != second->window())) {
7107 qWarning(
"QWidget::setTabOrder: 'first' and 'second' must be in the same window");
7111 const auto determineLastFocusChild = [](QWidget *target, QWidget *noFurtherThan)
7117 QWidget *lastFocusChild = target;
7119 QWidget *focusProxy = target->d_func()->deepestFocusProxy();
7126 for (
auto *object : target->children()) {
7127 QWidget *w = qobject_cast<QWidget*>(object);
7128 if (w && w->focusProxy() == target) {
7133 }
else if (target->isAncestorOf(focusProxy)) {
7134 lastFocusChild = focusProxy;
7135 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
7136 focusNext != focusProxy && target->isAncestorOf(focusNext) && focusNext->window() == focusProxy->window();
7137 focusNext = focusNext->nextInFocusChain()) {
7138 if (focusNext == noFurtherThan)
7140 if (focusNext->focusPolicy() != Qt::NoFocus)
7141 lastFocusChild = focusNext;
7144 return lastFocusChild;
7147 QWidget *lastFocusChildOfFirst = determineLastFocusChild(first, second);
7148 if (lastFocusChildOfFirst == second)
7149 lastFocusChildOfFirst = first;
7150 QWidget *lastFocusChildOfSecond = determineLastFocusChild(second, first);
7151 if (lastFocusChildOfSecond == first)
7152 lastFocusChildOfSecond = second;
7155 QWidget *oldPrev = second->previousInFocusChain();
7156 QWidget *prevWithFocus = oldPrev;
7157 while (prevWithFocus->focusPolicy() == Qt::NoFocus)
7158 prevWithFocus = prevWithFocus->previousInFocusChain();
7159 if (prevWithFocus == first)
7161 const QWidgetList chain = QWidgetPrivate::takeFromFocusChain(second, lastFocusChildOfSecond);
7162 QWidgetPrivate::insertIntoFocusChain(chain, QWidgetPrivate::FocusDirection::Next, lastFocusChildOfFirst);
7165void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets)
7167 QWidget *prev =
nullptr;
7168 for (
const auto &widget : widgets) {
7172 QWidget::setTabOrder(prev, widget);
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7191void QWidgetPrivate::reparentFocusWidgets(QWidget * oldtlw)
7194 if (oldtlw == q->window())
7198 focus_child->clearFocus();
7200 reparentFocusChildren(QWidgetPrivate::FocusDirection::Next);
7204
7205
7206
7207
7208
7209
7210QSize QWidget::frameSize()
const
7213 if (isWindow() && !(windowType() == Qt::Popup)) {
7214 QRect fs = d->frameStrut();
7215 return QSize(data->crect.width() + fs.left() + fs.right(),
7216 data->crect.height() + fs.top() + fs.bottom());
7218 return data->crect.size();
7222
7223
7224
7225
7226
7228void QWidget::move(
const QPoint &p)
7231 setAttribute(Qt::WA_Moved);
7232 if (testAttribute(Qt::WA_WState_Created)) {
7234 d->topData()->posIncludesFrame =
false;
7235 d->setGeometry_sys(p.x() + geometry().x() - QWidget::x(),
7236 p.y() + geometry().y() - QWidget::y(),
7237 width(), height(),
true);
7238 d->setDirtyOpaqueRegion();
7242 d->topData()->posIncludesFrame =
true;
7243 data->crect.moveTopLeft(p);
7244 setAttribute(Qt::WA_PendingMoveEvent);
7247 if (d->extra && d->extra->hasWindowContainer)
7248 QWindowContainer::parentWasMoved(
this);
7260void QWidgetPrivate::fixPosIncludesFrame()
7263 if (QTLWExtra *te = maybeTopData()) {
7264 if (te->posIncludesFrame) {
7267 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
7268 te->posIncludesFrame = 0;
7270 if (q->windowHandle() && q->windowHandle()->handle()) {
7272 if (!q->data->fstrut_dirty) {
7273 data.crect.translate(te->frameStrut.x(), te->frameStrut.y());
7274 te->posIncludesFrame = 0;
7283
7284
7285
7286
7288void QWidget::resize(
const QSize &s)
7291 setAttribute(Qt::WA_Resized);
7292 if (testAttribute(Qt::WA_WState_Created)) {
7293 d->fixPosIncludesFrame();
7294 d->setGeometry_sys(geometry().x(), geometry().y(), s.width(), s.height(),
false);
7295 d->setDirtyOpaqueRegion();
7297 const auto oldRect = data->crect;
7298 data->crect.setSize(s.boundedTo(maximumSize()).expandedTo(minimumSize()));
7299 if (oldRect != data->crect)
7300 setAttribute(Qt::WA_PendingResizeEvent);
7304void QWidget::setGeometry(
const QRect &r)
7307 setAttribute(Qt::WA_Resized);
7308 setAttribute(Qt::WA_Moved);
7310 d->topData()->posIncludesFrame = 0;
7311 if (testAttribute(Qt::WA_WState_Created)) {
7312 d->setGeometry_sys(r.x(), r.y(), r.width(), r.height(),
true);
7313 d->setDirtyOpaqueRegion();
7315 const auto oldRect = data->crect;
7316 data->crect.setTopLeft(r.topLeft());
7317 data->crect.setSize(r.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
7318 if (oldRect != data->crect) {
7319 setAttribute(Qt::WA_PendingMoveEvent);
7320 setAttribute(Qt::WA_PendingResizeEvent);
7324 if (d->extra && d->extra->hasWindowContainer)
7325 QWindowContainer::parentWasMoved(
this);
7328void QWidgetPrivate::setGeometry_sys(
int x,
int y,
int w,
int h,
bool isMove)
7332 w = qMin(w,extra->maxw);
7333 h = qMin(h,extra->maxh);
7334 w = qMax(w,extra->minw);
7335 h = qMax(h,extra->minh);
7338 if (q->isWindow() && q->windowHandle()) {
7339 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
7340 if (!integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) {
7343 w = q->windowHandle()->width();
7344 h = q->windowHandle()->height();
7348 QPoint oldp = q->geometry().topLeft();
7349 QSize olds = q->size();
7350 QRect r(x, y, w, h);
7352 bool isResize = olds != r.size();
7354 isMove = oldp != r.topLeft();
7359 if (r.size() == olds && oldp == r.topLeft())
7362 if (!data.in_set_window_state) {
7363 q->data->window_state &= ~Qt::WindowMaximized;
7364 q->data->window_state &= ~Qt::WindowFullScreen;
7366 topData()->normalGeometry = QRect(0, 0, -1, -1);
7369 QPoint oldPos = q->pos();
7372 bool needsShow =
false;
7374 if (q->isWindow() || q->windowHandle()) {
7375 if (!(data.window_state & Qt::WindowFullScreen) && (w == 0 || h == 0)) {
7376 q->setAttribute(Qt::WA_OutsideWSRange,
true);
7379 data.crect = QRect(x, y, w, h);
7380 }
else if (q->testAttribute(Qt::WA_OutsideWSRange)) {
7381 q->setAttribute(Qt::WA_OutsideWSRange,
false);
7386 if (q->isVisible()) {
7387 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && !q->testAttribute(Qt::WA_OutsideWSRange)) {
7388 if (QWindow *win = q->windowHandle()) {
7389 if (q->isWindow()) {
7390 if (isResize && !isMove)
7392 else if (isMove && !isResize)
7393 win->setPosition(x, y);
7395 win->setGeometry(q->geometry());
7397 QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());
7398 win->setGeometry(QRect(posInNativeParent,r.size()));
7405 if (!q->isWindow()) {
7406 if (renderToTexture) {
7407 QRegion updateRegion(q->geometry());
7408 updateRegion += QRect(oldPos, olds);
7409 q->parentWidget()->d_func()->invalidateBackingStore(updateRegion);
7410 }
else if (isMove && !isResize) {
7411 moveRect(QRect(oldPos, olds), x - oldPos.x(), y - oldPos.y());
7413 invalidateBackingStore_resizeHelper(oldPos, olds);
7419 QMoveEvent e(q->pos(), oldPos);
7420 QCoreApplication::sendEvent(q, &e);
7423 QResizeEvent e(r.size(), olds);
7424 QCoreApplication::sendEvent(q, &e);
7425 if (q->windowHandle())
7429 if (isMove && q->pos() != oldPos)
7430 q->setAttribute(Qt::WA_PendingMoveEvent,
true);
7432 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7435#if QT_CONFIG(accessibility)
7436 if (QAccessible::isActive() && q->isVisible()) {
7437 QAccessibleEvent event(q, QAccessible::LocationChanged);
7438 QAccessible::updateAccessibility(&event);
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460QByteArray QWidget::saveGeometry()
const
7463 QDataStream stream(&array, QIODevice::WriteOnly);
7464 stream.setVersion(QDataStream::Qt_4_0);
7465 const quint32 magicNumber = 0x1D9D0CB;
7470 quint16 majorVersion = 3;
7471 quint16 minorVersion = 0;
7472 const int screenNumber = QGuiApplication::screens().indexOf(screen());
7473 stream << magicNumber
7478 << qint32(screenNumber)
7479 << quint8(windowState() & Qt::WindowMaximized)
7480 << quint8(windowState() & Qt::WindowFullScreen)
7481 << qint32(screen()->geometry().width())
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509void QWidgetPrivate::checkRestoredGeometry(
const QRect &availableGeometry, QRect *restoredGeometry,
7513 const int height = restoredGeometry->height() + frameHeight;
7517 if (availableGeometry.height() <= height)
7518 restoredGeometry->setHeight(availableGeometry.height() - 2 - frameHeight);
7519 if (availableGeometry.width() <= restoredGeometry->width())
7520 restoredGeometry->setWidth(availableGeometry.width() - 2);
7524 const QRect restored = restoredGeometry->adjusted(0, -frameHeight, 0, 0);
7527 if (availableGeometry.contains(restored))
7533 if (restored.top() <= availableGeometry.top()) {
7534 restoredGeometry->moveTop(availableGeometry.top() + 1 + frameHeight);
7535 }
else if (restored.bottom() >= availableGeometry.bottom()) {
7537 restoredGeometry->moveBottom(availableGeometry.bottom() - 1);
7541 if (restored.left() <= availableGeometry.left()) {
7542 restoredGeometry->moveLeft(availableGeometry.left() + 1);
7543 }
else if (restored.right() >= availableGeometry.right()) {
7545 restoredGeometry->moveRight(availableGeometry.right() - 1);
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572bool QWidget::restoreGeometry(
const QByteArray &geometry)
7574 if (geometry.size() < 4)
7576 QDataStream stream(geometry);
7577 stream.setVersion(QDataStream::Qt_4_0);
7579 const quint32 magicNumber = 0x1D9D0CB;
7580 quint32 storedMagicNumber;
7581 stream >> storedMagicNumber;
7582 if (storedMagicNumber != magicNumber)
7585 const quint16 currentMajorVersion = 3;
7586 quint16 majorVersion = 0;
7587 quint16 minorVersion = 0;
7589 stream >> majorVersion >> minorVersion;
7591 if (majorVersion > currentMajorVersion)
7595 QRect restoredFrameGeometry;
7596 QRect restoredGeometry;
7597 QRect restoredNormalGeometry;
7598 qint32 restoredScreenNumber;
7601 qint32 restoredScreenWidth = 0;
7603 stream >> restoredFrameGeometry
7604 >> restoredNormalGeometry
7605 >> restoredScreenNumber
7609 if (majorVersion > 1)
7610 stream >> restoredScreenWidth;
7611 if (majorVersion > 2)
7612 stream >> restoredGeometry;
7616 if (restoredScreenNumber >= qMax(QGuiApplication::screens().size(), 1))
7617 restoredScreenNumber = 0;
7618 const QScreen *restoredScreen = QGuiApplication::screens().value(restoredScreenNumber,
nullptr);
7619 const qreal screenWidthF = restoredScreen ? qreal(restoredScreen->geometry().width()) : 0;
7622 if (restoredScreenWidth) {
7623 const qreal factor = qreal(restoredScreenWidth) / screenWidthF;
7624 if (factor < 0.8 || factor > 1.25)
7629 if (!maximized && !fullScreen && qreal(restoredFrameGeometry.width()) / screenWidthF > 1.5)
7633 const int frameHeight = QApplication::style()
7634 ? QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight,
nullptr,
this)
7637 if (!restoredNormalGeometry.isValid())
7638 restoredNormalGeometry = QRect(QPoint(0, frameHeight), sizeHint());
7639 if (!restoredNormalGeometry.isValid()) {
7641 restoredNormalGeometry.setSize(restoredNormalGeometry
7643 .expandedTo(d_func()->adjustedSize()));
7646 const QRect availableGeometry = restoredScreen ? restoredScreen->availableGeometry()
7654 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredGeometry, frameHeight);
7655 QWidgetPrivate::checkRestoredGeometry(availableGeometry, &restoredNormalGeometry, frameHeight);
7657 if (maximized || fullScreen) {
7660 Qt::WindowStates ws = windowState();
7662 setGeometry(restoredNormalGeometry);
7664 if (ws & Qt::WindowFullScreen) {
7666 move(availableGeometry.topLeft());
7667 }
else if (ws & Qt::WindowMaximized) {
7671 if (restoredScreen != screen()) {
7672 setWindowState(Qt::WindowNoState);
7673 setGeometry(restoredNormalGeometry);
7676 setGeometry(restoredNormalGeometry);
7680 ws |= Qt::WindowMaximized;
7682 ws |= Qt::WindowFullScreen;
7684 d_func()->topData()->normalGeometry = restoredNormalGeometry;
7686 setWindowState(windowState() & ~(Qt::WindowMaximized | Qt::WindowFullScreen));
7689 if (majorVersion > 2)
7690 setGeometry(restoredGeometry);
7692 setGeometry(restoredNormalGeometry);
7698
7699
7700
7701
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713void QWidget::setContentsMargins(
int left,
int top,
int right,
int bottom)
7716 if (left == d->leftmargin && top == d->topmargin
7717 && right == d->rightmargin && bottom == d->bottommargin)
7719 d->leftmargin = left;
7721 d->rightmargin = right;
7722 d->bottommargin = bottom;
7724 d->updateContentsRect();
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743void QWidget::setContentsMargins(
const QMargins &margins)
7745 setContentsMargins(margins.left(), margins.top(),
7746 margins.right(), margins.bottom());
7749void QWidgetPrivate::updateContentsRect()
7756 q->updateGeometry();
7758 if (q->isVisible()) {
7760 QResizeEvent e(q->data->crect.size(), q->data->crect.size());
7761 QCoreApplication::sendEvent(q, &e);
7763 q->setAttribute(Qt::WA_PendingResizeEvent,
true);
7766 QEvent e(QEvent::ContentsRectChange);
7767 QCoreApplication::sendEvent(q, &e);
7771
7772
7773
7774
7775
7776
7777QMargins QWidget::contentsMargins()
const
7780 QMargins userMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
7781 return testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea) ?
7782 userMargins | d->safeAreaMargins() : userMargins;
7786
7787
7788
7789
7790QRect QWidget::contentsRect()
const
7792 return rect() - contentsMargins();
7795QMargins QWidgetPrivate::safeAreaMargins()
const
7799#if QT_CONFIG(graphicsview)
7802 if (nearestGraphicsProxyWidget(q))
7806 QWidget *nativeWidget = q->window();
7807 if (!nativeWidget->windowHandle())
7810 QMargins safeAreaMargins = nativeWidget->windowHandle()->safeAreaMargins();
7812 if (!q->isWindow()) {
7820 if (safeAreaMargins.isNull())
7827 const QWidget *assumedSafeWidget =
nullptr;
7828 for (
const QWidget *w = q; w != nativeWidget; w = w->parentWidget()) {
7829 QWidget *parentWidget = w->parentWidget();
7830 if (!parentWidget->testAttribute(Qt::WA_ContentsMarginsRespectsSafeArea))
7833 if (parentWidget->testAttribute(Qt::WA_LayoutOnEntireRect))
7836 QLayout *layout = parentWidget->layout();
7840 if (layout->geometry().isNull())
7843 if (layout->indexOf(w) < 0)
7846 assumedSafeWidget = w;
7850#if !defined(QT_DEBUG)
7851 if (assumedSafeWidget) {
7862 QPoint topLeftMargins = q->mapFrom(nativeWidget, QPoint(safeAreaMargins.left(), safeAreaMargins.top()));
7863 QRect widgetRect = q->isVisible() ? q->visibleRegion().boundingRect() : q->rect();
7864 QPoint bottomRightMargins = widgetRect.bottomRight() - q->mapFrom(nativeWidget,
7865 nativeWidget->rect().bottomRight() - QPoint(safeAreaMargins.right(), safeAreaMargins.bottom()));
7868 safeAreaMargins = QMargins(qMax(0, topLeftMargins.x()), qMax(0, topLeftMargins.y()),
7869 qMax(0, bottomRightMargins.x()), qMax(0, bottomRightMargins.y()));
7871 if (!safeAreaMargins.isNull() && assumedSafeWidget) {
7872 QLayout *layout = assumedSafeWidget->parentWidget()->layout();
7873 qWarning() << layout <<
"is laying out" << assumedSafeWidget
7874 <<
"outside of the contents rect of" << layout->parentWidget();
7879 return safeAreaMargins;
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7912Qt::ContextMenuPolicy QWidget::contextMenuPolicy()
const
7914 return (Qt::ContextMenuPolicy)data->context_menu_policy;
7917void QWidget::setContextMenuPolicy(Qt::ContextMenuPolicy policy)
7919 data->context_menu_policy = (uint) policy;
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7944Qt::FocusPolicy QWidget::focusPolicy()
const
7946 return (Qt::FocusPolicy)data->focus_policy;
7949void QWidget::setFocusPolicy(Qt::FocusPolicy policy)
7951 data->focus_policy = (uint) policy;
7953 if (d->extra && d->extra->focus_proxy)
7954 d->extra->focus_proxy->setFocusPolicy(policy);
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985void QWidget::setUpdatesEnabled(
bool enable)
7988 setAttribute(Qt::WA_ForceUpdatesDisabled, !enable);
7989 d->setUpdatesEnabled_helper(enable);
7993
7994
7995
7996
7997
7998
7999
8000
8001
8009 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
8010 Qt::WindowState defaultState = platformIntegration->defaultWindowState(data->window_flags);
8011 if (defaultState == Qt::WindowFullScreen)
8013 else if (defaultState == Qt::WindowMaximized)
8021
8022
8023
8024
8025void QWidgetPrivate::show_recursive()
8030 if (!q->testAttribute(Qt::WA_WState_Created))
8031 createRecursively();
8032 q->ensurePolished();
8034 if (!q->isWindow() && q->parentWidget()->d_func()->layout && !q->parentWidget()->data->in_show)
8035 q->parentWidget()->d_func()->layout->activate();
8043void QWidgetPrivate::sendPendingMoveAndResizeEvents(
bool recursive,
bool disableUpdates)
8047 disableUpdates = disableUpdates && q->updatesEnabled();
8049 q->setAttribute(Qt::WA_UpdatesDisabled);
8051 if (q->testAttribute(Qt::WA_PendingMoveEvent)) {
8052 QMoveEvent e(data.crect.topLeft(), data.crect.topLeft());
8053 QCoreApplication::sendEvent(q, &e);
8054 q->setAttribute(Qt::WA_PendingMoveEvent,
false);
8057 if (q->testAttribute(Qt::WA_PendingResizeEvent)) {
8058 QResizeEvent e(data.crect.size(), QSize());
8059 QCoreApplication::sendEvent(q, &e);
8060 q->setAttribute(Qt::WA_PendingResizeEvent,
false);
8064 q->setAttribute(Qt::WA_UpdatesDisabled,
false);
8069 for (
int i = 0; i < children.size(); ++i) {
8070 if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
8071 child->d_func()->sendPendingMoveAndResizeEvents(recursive, disableUpdates);
8075void QWidgetPrivate::activateChildLayoutsRecursively()
8077 sendPendingMoveAndResizeEvents(
false,
true);
8079 for (
int i = 0; i < children.size(); ++i) {
8080 QWidget *child = qobject_cast<QWidget *>(children.at(i));
8081 if (!child || child->isHidden() || child->isWindow())
8084 child->ensurePolished();
8087 QWidgetPrivate *childPrivate = child->d_func();
8088 if (childPrivate->layout)
8089 childPrivate->layout->activate();
8092 const bool wasVisible = child->isVisible();
8094 child->setAttribute(Qt::WA_WState_Visible);
8097 childPrivate->activateChildLayoutsRecursively();
8101 child->setAttribute(Qt::WA_WState_Visible,
false);
8105void QWidgetPrivate::show_helper()
8108 data.in_show =
true;
8110 sendPendingMoveAndResizeEvents();
8113 q->setAttribute(Qt::WA_WState_Visible);
8116 showChildren(
false);
8120 const bool isWindow = q->isWindow();
8121#if QT_CONFIG(graphicsview)
8122 bool isEmbedded = isWindow && q->graphicsProxyWidget() !=
nullptr;
8124 bool isEmbedded =
false;
8130 if (isWindow && !isEmbedded) {
8131 if ((q->windowType() == Qt::Tool) || (q->windowType() == Qt::Popup) || q->windowType() == Qt::ToolTip) {
8133 if (q->parentWidget() && q->parentWidget()->window()->testAttribute(Qt::WA_KeyboardFocusChange))
8134 q->setAttribute(Qt::WA_KeyboardFocusChange);
8136 while (QApplication::activePopupWidget()) {
8137 if (!QApplication::activePopupWidget()->close())
8145#if QT_CONFIG(graphicsview)
8147 if (!isEmbedded && !bypassGraphicsProxyWidget(q)) {
8148 QGraphicsProxyWidget *ancestorProxy = nearestGraphicsProxyWidget(q->parentWidget());
8149 if (ancestorProxy) {
8151 ancestorProxy->d_func()->embedSubWindow(q);
8156 Q_UNUSED(isEmbedded);
8160 QShowEvent showEvent;
8161 QCoreApplication::sendEvent(q, &showEvent);
8165 if (!isEmbedded && q->windowType() == Qt::Popup)
8166 qApp->d_func()->openPopup(q);
8168#if QT_CONFIG(accessibility)
8169 if (q->windowType() != Qt::ToolTip) {
8170 QAccessibleEvent event(q, QAccessible::ObjectShow);
8171 QAccessible::updateAccessibility(&event);
8175 if (QApplicationPrivate::hidden_focus_widget == q) {
8176 QApplicationPrivate::hidden_focus_widget =
nullptr;
8177 q->setFocus(Qt::OtherFocusReason);
8183 if (!
qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen)
8184 QCoreApplication::processEvents();
8186 data.in_show =
false;
8189void QWidgetPrivate::show_sys()
8193 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8195 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8196 invalidateBackingStore(q->rect());
8197 q->setAttribute(Qt::WA_Mapped);
8199 if (window && q->isWindow()
8200#if QT_CONFIG(graphicsview)
8201 && (!extra || !extra->proxyWidget)
8203 && q->windowModality() != Qt::NonModal) {
8204 QGuiApplicationPrivate::showModalWindow(window);
8209 if (renderToTexture && !q->isWindow())
8210 QCoreApplication::postEvent(q->parentWidget(),
new QUpdateLaterEvent(q->geometry()));
8212 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(q->rect()));
8214 if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow))
8215 || q->testAttribute(Qt::WA_OutsideWSRange)) {
8221 fixPosIncludesFrame();
8222 QRect geomRect = q->geometry();
8223 if (!q->isWindow()) {
8224 QPoint topLeftOfWindow = q->mapTo(q->nativeParentWidget(),QPoint());
8225 geomRect.moveTopLeft(topLeftOfWindow);
8227 const QRect windowRect = window->geometry();
8228 if (windowRect != geomRect) {
8229 if (q->testAttribute(Qt::WA_Moved)
8230 || !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowManagement))
8231 window->setGeometry(geomRect);
8233 window->resize(geomRect.size());
8237 qt_qpa_set_cursor(q,
false);
8239 invalidateBackingStore(q->rect());
8240 window->setNativeWindowVisibility(
true);
8242 if (window->isTopLevel()) {
8243 const QPoint crectTopLeft = q->data->crect.topLeft();
8244 const QPoint windowTopLeft = window->geometry().topLeft();
8245 if (crectTopLeft == QPoint(0, 0) && windowTopLeft != crectTopLeft)
8246 q->data->crect.moveTopLeft(windowTopLeft);
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8268
8269void QWidgetPrivate::hide_helper()
8273 bool isEmbedded =
false;
8274#if QT_CONFIG(graphicsview)
8275 isEmbedded = q->isWindow() && !bypassGraphicsProxyWidget(q) && nearestGraphicsProxyWidget(q->parentWidget()) !=
nullptr;
8277 Q_UNUSED(isEmbedded);
8280 if (!isEmbedded && (q->windowType() == Qt::Popup))
8281 qApp->d_func()->closePopup(q);
8283 q->setAttribute(Qt::WA_Mapped,
false);
8286 bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
8289 q->setAttribute(Qt::WA_WState_Visible,
false);
8293 QHideEvent hideEvent;
8294 QCoreApplication::sendEvent(q, &hideEvent);
8295 hideChildren(
false);
8300 qApp->d_func()->sendSyntheticEnterLeave(q);
8301 QWidget *fw = QApplication::focusWidget();
8302 while (fw && !fw->isWindow()) {
8304 q->focusNextPrevChild(
true);
8307 fw = fw->parentWidget();
8311 if (QWidgetRepaintManager *repaintManager = maybeRepaintManager())
8312 repaintManager->removeDirtyWidget(q);
8314#if QT_CONFIG(accessibility)
8316 QAccessibleEvent event(q, QAccessible::ObjectHide);
8317 QAccessible::updateAccessibility(&event);
8322void QWidgetPrivate::hide_sys()
8326 auto window = qobject_cast<QWidgetWindow *>(windowHandle());
8328 if (q->testAttribute(Qt::WA_DontShowOnScreen)) {
8329 q->setAttribute(Qt::WA_Mapped,
false);
8331 if (window && q->isWindow()
8332#if QT_CONFIG(graphicsview)
8333 && (!extra || !extra->proxyWidget)
8335 && q->windowModality() != Qt::NonModal) {
8336 QGuiApplicationPrivate::hideModalWindow(window);
8341 deactivateWidgetCleanup();
8343 if (!q->isWindow()) {
8344 QWidget *p = q->parentWidget();
8345 if (p &&p->isVisible()) {
8346 if (renderToTexture)
8347 p->d_func()->invalidateBackingStore(q->geometry());
8349 invalidateBackingStore(q->rect());
8352 invalidateBackingStore(q->rect());
8356 window->setNativeWindowVisibility(
false);
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8382void QWidget::setVisible(
bool visible)
8385 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" <<
this
8386 <<
"with attributes" << WidgetAttributes{
this}
8387 <<
"to" << visible <<
"via QWidget";
8389 if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) == !visible)
8392 if (d->dontSetExplicitShowHide) {
8393 d->dontSetExplicitShowHide =
false;
8396 setAttribute(Qt::WA_WState_ExplicitShowHide);
8399 d->setVisible(visible);
8405void QWidgetPrivate::setVisible(
bool visible)
8408 qCDebug(lcWidgetShowHide) <<
"Setting visibility of" << q
8409 <<
"with attributes" << WidgetAttributes{q}
8410 <<
"to" << visible <<
"via QWidgetPrivate";
8414 if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
8415 && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
8416 q->parentWidget()->window()->d_func()->createRecursively();
8419 QWidget *pw = q->parentWidget();
8420 if (!q->testAttribute(Qt::WA_WState_Created)
8421 && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
8425 bool wasResized = q->testAttribute(Qt::WA_Resized);
8426 Qt::WindowStates initialWindowState = q->windowState();
8429 q->ensurePolished();
8432 bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
8434 q->setAttribute(Qt::WA_WState_Hidden,
false);
8436 if (needUpdateGeometry)
8437 updateGeometry_helper(
true);
8443 if (!q->isWindow()) {
8444 QWidget *parent = q->parentWidget();
8445 while (parent && parent->isVisible() && parent->d_func()->layout && !parent->data->in_show) {
8446 parent->d_func()->layout->activate();
8447 if (parent->isWindow())
8449 parent = parent->parentWidget();
8452 parent->d_func()->setDirtyOpaqueRegion();
8457 && (q->isWindow() || !q->parentWidget()->d_func()->layout)) {
8458 if (q->isWindow()) {
8460 if (q->windowState() != initialWindowState)
8461 q->setWindowState(initialWindowState);
8465 q->setAttribute(Qt::WA_Resized,
false);
8468 q->setAttribute(Qt::WA_KeyboardFocusChange,
false);
8470 if (q->isWindow() || q->parentWidget()->isVisible()) {
8473 qApp->d_func()->sendSyntheticEnterLeave(q);
8476 QEvent showToParentEvent(QEvent::ShowToParent);
8477 QCoreApplication::sendEvent(q, &showToParentEvent);
8479 if (QApplicationPrivate::hidden_focus_widget == q)
8480 QApplicationPrivate::hidden_focus_widget =
nullptr;
8486 if (!q->isWindow() && q->parentWidget())
8487 q->parentWidget()->d_func()->setDirtyOpaqueRegion();
8489 if (!q->testAttribute(Qt::WA_WState_Hidden)) {
8490 q->setAttribute(Qt::WA_WState_Hidden);
8495 if (!q->isWindow() && q->parentWidget()) {
8496 if (q->parentWidget()->d_func()->layout)
8497 q->parentWidget()->d_func()->layout->invalidate();
8498 else if (q->parentWidget()->isVisible())
8499 QCoreApplication::postEvent(q->parentWidget(),
new QEvent(QEvent::LayoutRequest));
8502 QEvent hideToParentEvent(QEvent::HideToParent);
8503 QCoreApplication::sendEvent(q, &hideToParentEvent);
8508
8509
8510void QWidget::setHidden(
bool hidden)
8512 setVisible(!hidden);
8515bool QWidgetPrivate::isExplicitlyHidden()
const
8518 return q->isHidden() && q->testAttribute(Qt::WA_WState_ExplicitShowHide);
8521void QWidgetPrivate::_q_showIfNotHidden()
8524 if (!isExplicitlyHidden())
8525 q->setVisible(
true);
8528void QWidgetPrivate::showChildren(
bool spontaneous)
8531 qCDebug(lcWidgetShowHide) <<
"Showing children of" << q
8532 <<
"spontaneously" << spontaneous;
8534 QList<QObject*> childList = children;
8535 for (
int i = 0; i < childList.size(); ++i) {
8536 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8539 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8540 <<
"with attributes" << WidgetAttributes{widget};
8541 if (widget->windowHandle() && !widget->testAttribute(Qt::WA_WState_ExplicitShowHide))
8542 widget->setAttribute(Qt::WA_WState_Hidden,
false);
8543 if (widget->isWindow() || widget->testAttribute(Qt::WA_WState_Hidden))
8546 widget->setAttribute(Qt::WA_Mapped);
8547 widget->d_func()->showChildren(
true);
8549 QApplication::sendSpontaneousEvent(widget, &e);
8551 if (widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) {
8552 widget->d_func()->show_recursive();
8558 widget->d_func()->dontSetExplicitShowHide =
true;
8559 widget->setVisible(
true);
8560 widget->d_func()->dontSetExplicitShowHide =
false;
8566void QWidgetPrivate::hideChildren(
bool spontaneous)
8569 qCDebug(lcWidgetShowHide) <<
"Hiding children of" << q
8570 <<
"spontaneously" << spontaneous;
8572 QList<QObject*> childList = children;
8573 for (
int i = 0; i < childList.size(); ++i) {
8574 QWidget *widget = qobject_cast<QWidget*>(childList.at(i));
8577 qCDebug(lcWidgetShowHide) <<
"Considering" << widget
8578 <<
"with attributes" << WidgetAttributes{widget};
8579 if (widget->isWindow() || !widget->isVisible())
8583 widget->setAttribute(Qt::WA_Mapped,
false);
8585 widget->setAttribute(Qt::WA_WState_Visible,
false);
8586 widget->d_func()->hideChildren(spontaneous);
8589 QApplication::sendSpontaneousEvent(widget, &e);
8591 QCoreApplication::sendEvent(widget, &e);
8592 if (widget->internalWinId()
8593 && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) {
8596 widget->d_func()->hide_sys();
8599 qApp->d_func()->sendSyntheticEnterLeave(widget);
8600#if QT_CONFIG(accessibility)
8602 QAccessibleEvent event(widget, QAccessible::ObjectHide);
8603 QAccessible::updateAccessibility(&event);
8611 if (QWidget* widgetWindow = q->window();
8612 widgetWindow && widgetWindow->data->is_closing) {
8613 q->setAttribute(Qt::WA_UnderMouse,
false);
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635bool QWidgetPrivate::handleClose(CloseMode mode)
8638 qCDebug(lcWidgetShowHide) <<
"Handling close event for" << q;
8640 if (data.is_closing)
8644 data.is_closing =
true;
8646 QPointer<QWidget> that = q;
8648 if (data.in_destructor)
8649 mode = CloseNoEvent;
8651 if (mode != CloseNoEvent) {
8653 if (mode == CloseWithSpontaneousEvent)
8654 QApplication::sendSpontaneousEvent(q, &e);
8656 QCoreApplication::sendEvent(q, &e);
8657 if (!that.isNull() && !e.isAccepted()) {
8658 data.is_closing =
false;
8664 if (!that.isNull() && !q->isHidden())
8667 if (!that.isNull()) {
8668 data.is_closing =
false;
8669 if (q->testAttribute(Qt::WA_DeleteOnClose)) {
8670 q->setAttribute(Qt::WA_DeleteOnClose,
false);
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8700bool QWidget::close()
8702 return d_func()->close();
8705bool QWidgetPrivate::close()
8715 if (QWindow *widgetWindow = windowHandle()) {
8716 if (widgetWindow->isTopLevel())
8717 return widgetWindow->close();
8720 return handleClose(QWidgetPrivate::CloseWithEvent);
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8781bool QWidget::isVisibleTo(
const QWidget *ancestor)
const
8785 const QWidget * w =
this;
8786 while (!w->isHidden()
8788 && w->parentWidget()
8789 && w->parentWidget() != ancestor)
8790 w = w->parentWidget();
8791 return !w->isHidden();
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805QRegion QWidget::visibleRegion()
const
8809 QRect clipRect = d->clipRect();
8810 if (clipRect.isEmpty())
8812 QRegion r(clipRect);
8813 d->subtractOpaqueChildren(r, clipRect);
8814 d->subtractOpaqueSiblings(r);
8819QSize QWidgetPrivate::adjustedSize()
const
8823 QSize s = q->sizeHint();
8825 if (q->isWindow()) {
8826 Qt::Orientations exp;
8828 if (layout->hasHeightForWidth())
8829 s.setHeight(layout->totalHeightForWidth(s.width()));
8830 exp = layout->expandingDirections();
8833 if (q->sizePolicy().hasHeightForWidth())
8834 s.setHeight(q->heightForWidth(s.width()));
8835 exp = q->sizePolicy().expandingDirections();
8837 if (exp & Qt::Horizontal)
8838 s.setWidth(qMax(s.width(), 200));
8839 if (exp & Qt::Vertical)
8840 s.setHeight(qMax(s.height(), 100));
8843 if (
const QScreen *screenAtPoint = QGuiApplication::screenAt(q->pos()))
8844 screen = screenAtPoint->geometry();
8846 screen = QGuiApplication::primaryScreen()->geometry();
8848 s.setWidth(qMin(s.width(), screen.width()*2/3));
8849 s.setHeight(qMin(s.height(), screen.height()*2/3));
8851 if (QTLWExtra *extra = maybeTopData())
8852 extra->sizeAdjusted =
true;
8856 QRect r = q->childrenRect();
8859 s = r.size() + QSize(2 * r.x(), 2 * r.y());
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8881void QWidget::adjustSize()
8885 QSize s = d->adjustedSize();
8888 d->layout->activate();
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8910QSize QWidget::sizeHint()
const
8914 return d->layout->totalSizeHint();
8915 return QSize(-1, -1);
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937QSize QWidget::minimumSizeHint()
const
8941 return d->layout->totalMinimumSize();
8942 return QSize(-1, -1);
8947
8948
8949
8950
8951
8955
8956
8957
8958
8960bool QWidget::isAncestorOf(
const QWidget *child)
const
8965 if (child->isWindow())
8967 child = child->parentWidget();
8973
8974
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9004bool QWidget::event(QEvent *event)
9010 switch(event->type()) {
9011 case QEvent::TabletPress:
9012 case QEvent::TabletRelease:
9013 case QEvent::TabletMove:
9014 case QEvent::MouseButtonPress:
9015 case QEvent::MouseButtonRelease:
9016 case QEvent::MouseButtonDblClick:
9017 case QEvent::MouseMove:
9018 case QEvent::TouchBegin:
9019 case QEvent::TouchUpdate:
9020 case QEvent::TouchEnd:
9021 case QEvent::TouchCancel:
9022 case QEvent::ContextMenu:
9023 case QEvent::KeyPress:
9024 case QEvent::KeyRelease:
9025#if QT_CONFIG(wheelevent)
9033 switch (event->type()) {
9034 case QEvent::PlatformSurface: {
9036 switch (
static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
9037 case QPlatformSurfaceEvent::SurfaceCreated:
9038 if (!testAttribute(Qt::WA_WState_Created))
9041 case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
9042 if (testAttribute(Qt::WA_WState_Created)) {
9045 destroy(
false,
false);
9051 case QEvent::MouseMove:
9052 mouseMoveEvent((QMouseEvent*)event);
9055 case QEvent::MouseButtonPress:
9056 mousePressEvent((QMouseEvent*)event);
9059 case QEvent::MouseButtonRelease:
9060 mouseReleaseEvent((QMouseEvent*)event);
9063 case QEvent::MouseButtonDblClick:
9064 mouseDoubleClickEvent((QMouseEvent*)event);
9066#if QT_CONFIG(wheelevent)
9068 wheelEvent((QWheelEvent*)event);
9071#if QT_CONFIG(tabletevent)
9072 case QEvent::TabletMove:
9073 if (
static_cast<QTabletEvent *>(event)->buttons() == Qt::NoButton && !testAttribute(Qt::WA_TabletTracking))
9076 case QEvent::TabletPress:
9077 case QEvent::TabletRelease:
9078 tabletEvent((QTabletEvent*)event);
9081 case QEvent::KeyPress: {
9082 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
9084 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
9085 if (k->key() == Qt::Key_Backtab
9086 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
9087 res = focusNextPrevChild(
false);
9088 else if (k->key() == Qt::Key_Tab)
9089 res = focusNextPrevChild(
true);
9094#ifdef QT_KEYPAD_NAVIGATION
9095 if (!k->isAccepted() && QApplication::keypadNavigationEnabled()
9096 && !(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::ShiftModifier))) {
9097 if (QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder) {
9098 if (k->key() == Qt::Key_Up)
9099 res = focusNextPrevChild(
false);
9100 else if (k->key() == Qt::Key_Down)
9101 res = focusNextPrevChild(
true);
9102 }
else if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
9103 if (k->key() == Qt::Key_Up)
9104 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionNorth);
9105 else if (k->key() == Qt::Key_Right)
9106 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionEast);
9107 else if (k->key() == Qt::Key_Down)
9108 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionSouth);
9109 else if (k->key() == Qt::Key_Left)
9110 res = QWidgetPrivate::navigateToDirection(QWidgetPrivate::DirectionWest);
9118#if QT_CONFIG(whatsthis)
9119 if (!k->isAccepted()
9120 && k->modifiers() & Qt::ShiftModifier && k->key() == Qt::Key_F1
9121 && d->whatsThis.size()) {
9122 QWhatsThis::showText(mapToGlobal(inputMethodQuery(Qt::ImCursorRectangle).toRect().center()), d->whatsThis,
this);
9129 case QEvent::KeyRelease:
9130 keyReleaseEvent((QKeyEvent*)event);
9132 case QEvent::ShortcutOverride:
9135 case QEvent::InputMethod:
9136 inputMethodEvent((QInputMethodEvent *) event);
9139 case QEvent::InputMethodQuery: {
9140 QInputMethodQueryEvent *query =
static_cast<QInputMethodQueryEvent *>(event);
9141 Qt::InputMethodQueries queries = query->queries();
9142 for (uint i = 0; i < 32; ++i) {
9143 Qt::InputMethodQuery q = (Qt::InputMethodQuery)(
int)(queries & (1<<i));
9145 QVariant v = inputMethodQuery(q);
9146 if (q == Qt::ImEnabled && !v.isValid() && isEnabled()) {
9153 v = QVariant(testAttribute(Qt::WA_InputMethodEnabled));
9155 query->setValue(q, v);
9162 case QEvent::PolishRequest:
9166 case QEvent::Polish: {
9167 style()->polish(
this);
9168 setAttribute(Qt::WA_WState_Polished);
9169 if (!QApplication::font(
this).isCopyOf(QApplication::font()))
9171 if (!QApplication::palette(
this).isCopyOf(QGuiApplication::palette()))
9172 d->resolvePalette();
9176 case QEvent::ApplicationWindowIconChange:
9177 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon)) {
9178 d->setWindowIcon_sys();
9179 d->setWindowIcon_helper();
9182 case QEvent::FocusIn:
9183 focusInEvent((QFocusEvent*)event);
9184 d->updateWidgetTransform(event);
9187 case QEvent::FocusOut:
9188 focusOutEvent((QFocusEvent*)event);
9192#if QT_CONFIG(statustip)
9193 if (d->statusTip.size()) {
9194 QStatusTipEvent tip(d->statusTip);
9195 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9198 enterEvent(
static_cast<QEnterEvent*>(event));
9202#if QT_CONFIG(statustip)
9203 if (d->statusTip.size()) {
9205 QStatusTipEvent tip(empty);
9206 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &tip);
9212 case QEvent::HoverEnter:
9213 case QEvent::HoverLeave:
9221 paintEvent((QPaintEvent*)event);
9225 moveEvent((QMoveEvent*)event);
9226 d->updateWidgetTransform(event);
9229 case QEvent::Resize:
9230 resizeEvent((QResizeEvent*)event);
9231 d->updateWidgetTransform(event);
9235 closeEvent((QCloseEvent *)event);
9238#ifndef QT_NO_CONTEXTMENU
9239 case QEvent::ContextMenu:
9240 switch (data->context_menu_policy) {
9241 case Qt::PreventContextMenu:
9243 case Qt::DefaultContextMenu:
9244 contextMenuEvent(
static_cast<QContextMenuEvent *>(event));
9246 case Qt::CustomContextMenu:
9247 emit customContextMenuRequested(
static_cast<QContextMenuEvent *>(event)->pos());
9250 case Qt::ActionsContextMenu:
9251 if (d->actions.size()) {
9252 QMenu::exec(d->actions,
static_cast<QContextMenuEvent *>(event)->globalPos(),
9265#if QT_CONFIG(draganddrop)
9267 dropEvent((QDropEvent*) event);
9270 case QEvent::DragEnter:
9271 dragEnterEvent((QDragEnterEvent*) event);
9274 case QEvent::DragMove:
9275 dragMoveEvent((QDragMoveEvent*) event);
9278 case QEvent::DragLeave:
9279 dragLeaveEvent((QDragLeaveEvent*) event);
9284 showEvent((QShowEvent*) event);
9288 hideEvent((QHideEvent*) event);
9291 case QEvent::ShowWindowRequest:
9296 case QEvent::ApplicationFontChange:
9299 case QEvent::ApplicationPaletteChange:
9300 d->resolvePalette();
9303 case QEvent::ToolBarChange:
9304 case QEvent::ActivationChange:
9305 case QEvent::EnabledChange:
9306 case QEvent::FontChange:
9307 case QEvent::StyleChange:
9308 case QEvent::PaletteChange:
9309 case QEvent::WindowTitleChange:
9310 case QEvent::IconTextChange:
9311 case QEvent::ModifiedChange:
9312 case QEvent::MouseTrackingChange:
9313 case QEvent::TabletTrackingChange:
9314 case QEvent::ParentChange:
9315 case QEvent::LocaleChange:
9316 case QEvent::MacSizeChange:
9317 case QEvent::ContentsRectChange:
9318 case QEvent::ThemeChange:
9319 case QEvent::ReadOnlyChange:
9323 case QEvent::WindowStateChange: {
9324 const bool wasMinimized =
static_cast<
const QWindowStateChangeEvent *>(event)->oldState() & Qt::WindowMinimized;
9325 if (wasMinimized != isMinimized()) {
9326 QWidget *widget =
const_cast<QWidget *>(
this);
9329 if (!d->childrenShownByExpose) {
9331 d->showChildren(
true);
9332 QShowEvent showEvent;
9333 QCoreApplication::sendSpontaneousEvent(widget, &showEvent);
9335 d->childrenHiddenByWState =
false;
9337 QHideEvent hideEvent;
9338 QCoreApplication::sendSpontaneousEvent(widget, &hideEvent);
9339 d->hideChildren(
true);
9340 d->childrenHiddenByWState =
true;
9342 d->childrenShownByExpose =
false;
9348 case QEvent::WindowActivate:
9349 case QEvent::WindowDeactivate: {
9350 if (isVisible() && !palette().isEqual(QPalette::Active, QPalette::Inactive))
9352 QList<QObject*> childList = d->children;
9353 for (
int i = 0; i < childList.size(); ++i) {
9354 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9355 if (w && w->isVisible() && !w->isWindow())
9356 QCoreApplication::sendEvent(w, event);
9360 case QEvent::LanguageChange:
9363 QList<QObject*> childList = d->children;
9364 for (
int i = 0; i < childList.size(); ++i) {
9365 QObject *o = childList.at(i);
9367 QCoreApplication::sendEvent(o, event);
9373 case QEvent::ApplicationLayoutDirectionChange:
9374 d->resolveLayoutDirection();
9377 case QEvent::LayoutDirectionChange:
9379 d->layout->invalidate();
9383 case QEvent::UpdateRequest:
9384 d->syncBackingStore();
9386 case QEvent::UpdateLater:
9387 update(
static_cast<QUpdateLaterEvent*>(event)->region());
9389 case QEvent::StyleAnimationUpdate:
9390 if (isVisible() && !window()->isMinimized()) {
9396 case QEvent::WindowBlocked:
9397 case QEvent::WindowUnblocked:
9398 if (!d->children.isEmpty()) {
9399 QWidget *modalWidget = QApplication::activeModalWidget();
9400 for (
int i = 0; i < d->children.size(); ++i) {
9401 QObject *o = d->children.at(i);
9402 if (o && o != modalWidget && o->isWidgetType()) {
9403 QWidget *w =
static_cast<QWidget *>(o);
9406 QCoreApplication::sendEvent(w, event);
9411#if QT_CONFIG(tooltip)
9412 case QEvent::ToolTip:
9413 if (!d->toolTip.isEmpty())
9414 QToolTip::showText(
static_cast<QHelpEvent*>(event)->globalPos(), d->toolTip,
this, QRect(), d->toolTipDuration);
9419#if QT_CONFIG(whatsthis)
9420 case QEvent::WhatsThis:
9421 if (d->whatsThis.size())
9422 QWhatsThis::showText(
static_cast<QHelpEvent *>(event)->globalPos(), d->whatsThis,
this);
9426 case QEvent::QueryWhatsThis:
9427 if (d->whatsThis.isEmpty())
9431 case QEvent::EmbeddingControl:
9432 d->topData()->frameStrut.setCoords(0 ,0, 0, 0);
9433 data->fstrut_dirty =
false;
9436 case QEvent::ActionAdded:
9437 case QEvent::ActionRemoved:
9438 case QEvent::ActionChanged:
9439 actionEvent((QActionEvent*)event);
9443 case QEvent::KeyboardLayoutChange:
9448 QList<QObject*> childList = d->children;
9449 for (
int i = 0; i < childList.size(); ++i) {
9450 QWidget *w = qobject_cast<QWidget *>(childList.at(i));
9451 if (w && w->isVisible() && !w->isWindow())
9452 QCoreApplication::sendEvent(w, event);
9456 case QEvent::TouchBegin:
9457 case QEvent::TouchUpdate:
9458 case QEvent::TouchEnd:
9459 case QEvent::TouchCancel:
9464#ifndef QT_NO_GESTURES
9465 case QEvent::Gesture:
9469 case QEvent::ScreenChangeInternal:
9470 if (
const QTLWExtra *te = d->maybeTopData()) {
9471 const QWindow *win = te->window;
9472 d->setWinId((win && win->handle()) ? win->handle()->winId() : 0);
9475 case QEvent::DevicePixelRatioChange:
9476 if (d->data.fnt.d->dpi != logicalDpiY())
9477 d->updateFont(d->data.fnt);
9478 d->renderToTextureReallyDirty = 1;
9480 case QEvent::DynamicPropertyChange: {
9481 const QByteArray &propName =
static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName();
9482 if (propName.size() == 13 && !qstrncmp(propName,
"_q_customDpi", 12)) {
9483 uint value = property(propName.constData()).toUInt();
9486 const char axis = propName.at(12);
9488 d->extra->customDpiX = value;
9489 else if (axis ==
'Y')
9490 d->extra->customDpiY = value;
9491 d->updateFont(d->data.fnt);
9493 if (windowHandle() && !qstrncmp(propName,
"_q_platform_", 12))
9494 windowHandle()->setProperty(propName, property(propName));
9498 return QObject::event(event);
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519void QWidget::changeEvent(QEvent * event)
9521 switch(event->type()) {
9522 case QEvent::EnabledChange: {
9524#if QT_CONFIG(accessibility)
9525 QAccessible::State s;
9527 QAccessibleStateChangeEvent event(
this, s);
9528 QAccessible::updateAccessibility(&event);
9533 case QEvent::FontChange:
9534 case QEvent::StyleChange: {
9539 d->layout->invalidate();
9543 case QEvent::PaletteChange:
9547 case QEvent::ThemeChange:
9548 if (QGuiApplication::desktopSettingsAware()
9549 &&
qApp && !QCoreApplication::closingDown()) {
9550 if (testAttribute(Qt::WA_WState_Polished))
9551 QApplication::style()->unpolish(
this);
9552 if (testAttribute(Qt::WA_WState_Polished))
9553 QApplication::style()->polish(
this);
9554 QEvent styleChangedEvent(QEvent::StyleChange);
9555 QCoreApplication::sendEvent(
this, &styleChangedEvent);
9562 case QEvent::MacSizeChange:
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9598void QWidget::mouseMoveEvent(QMouseEvent *event)
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9620void QWidget::mousePressEvent(QMouseEvent *event)
9623 if ((windowType() == Qt::Popup)) {
9626 while ((w = QApplication::activePopupWidget()) && w !=
this){
9628 if (QApplication::activePopupWidget() == w)
9631 if (!rect().contains(event->position().toPoint())){
9638
9639
9640
9641
9642
9643
9645void QWidget::mouseReleaseEvent(QMouseEvent *event)
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9667void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
9669 mousePressEvent(event);
9672#if QT_CONFIG(wheelevent)
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9687void QWidget::wheelEvent(QWheelEvent *event)
9693#if QT_CONFIG(tabletevent)
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9714void QWidget::tabletEvent(QTabletEvent *event)
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9743void QWidget::keyPressEvent(QKeyEvent *event)
9745#ifndef QT_NO_SHORTCUT
9746 if ((windowType() == Qt::Popup) && event->matches(QKeySequence::Cancel)) {
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9778void QWidget::keyReleaseEvent(QKeyEvent *event)
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9802void QWidget::focusInEvent(QFocusEvent *)
9804 if (focusPolicy() != Qt::NoFocus || !isWindow()) {
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9828void QWidget::focusOutEvent(QFocusEvent *)
9830 if (focusPolicy() != Qt::NoFocus || !isWindow())
9833#if !defined(QT_PLATFORM_UIKIT)
9835 if (
qApp->autoSipEnabled() && testAttribute(Qt::WA_InputMethodEnabled))
9836 QGuiApplication::inputMethod()->hide();
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9852void QWidget::enterEvent(QEnterEvent *)
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9868void QWidget::leaveEvent(QEvent *)
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9920void QWidget::paintEvent(QPaintEvent *)
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9938void QWidget::moveEvent(QMoveEvent *)
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9959void QWidget::resizeEvent(QResizeEvent * )
9965
9966
9967
9968
9969
9970
9971
9972void QWidget::actionEvent(QActionEvent *)
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9993void QWidget::closeEvent(QCloseEvent *event)
9998#ifndef QT_NO_CONTEXTMENU
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10012void QWidget::contextMenuEvent(QContextMenuEvent *event)
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035void QWidget::inputMethodEvent(QInputMethodEvent *event)
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050QVariant QWidget::inputMethodQuery(Qt::InputMethodQuery query)
const
10053 case Qt::ImCursorRectangle:
10054 return QRect(width()/2, 0, 1, height());
10057 case Qt::ImAnchorPosition:
10059 return inputMethodQuery(Qt::ImCursorPosition);
10061 return (
int)inputMethodHints();
10062 case Qt::ImInputItemClipRectangle:
10063 return d_func()->clipRect();
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094Qt::InputMethodHints QWidget::inputMethodHints()
const
10097 const QWidgetPrivate *priv = d_func();
10098 while (priv->inheritsInputMethodHints) {
10099 priv = priv->q_func()->parentWidget()->d_func();
10102 return priv->imHints;
10104 return Qt::ImhNone;
10108void QWidget::setInputMethodHints(Qt::InputMethodHints hints)
10112 if (d->imHints == hints)
10114 d->imHints = hints;
10115 if (
this == QGuiApplication::focusObject())
10116 QGuiApplication::inputMethod()->update(Qt::ImHints);
10123#if QT_CONFIG(draganddrop)
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139void QWidget::dragEnterEvent(QDragEnterEvent *)
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157void QWidget::dragMoveEvent(QDragMoveEvent *)
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173void QWidget::dragLeaveEvent(QDragLeaveEvent *)
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188void QWidget::dropEvent(QDropEvent *)
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213void QWidget::showEvent(QShowEvent *)
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235void QWidget::hideEvent(QHideEvent *)
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10266bool QWidget::nativeEvent(
const QByteArray &eventType,
void *message, qintptr *result)
10268 Q_UNUSED(eventType);
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297void QWidget::ensurePolished()
const
10299 Q_D(
const QWidget);
10301 const QMetaObject *m = metaObject();
10302 if (m == d->polished)
10306 QEvent e(QEvent::Polish);
10307 QCoreApplication::sendEvent(
const_cast<QWidget *>(
this), &e);
10310 QList<QObject*> children = d->children;
10311 for (
int i = 0; i < children.size(); ++i) {
10312 QObject *o = children.at(i);
10313 if (!o->isWidgetType())
10315 if (QWidget *w = qobject_cast<QWidget *>(o))
10316 w->ensurePolished();
10319 if (d->parent && d->sendChildEvents) {
10320 QChildEvent e(QEvent::ChildPolished,
const_cast<QWidget *>(
this));
10321 QCoreApplication::sendEvent(d->parent, &e);
10326
10327
10328
10329
10330
10331QRegion QWidget::mask()
const
10333 Q_D(
const QWidget);
10334 return d->extra ? d->extra->mask : QRegion();
10338
10339
10340
10341
10342
10343
10344
10345
10346QLayout *QWidget::layout()
const
10348 return d_func()->layout;
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10377void QWidget::setLayout(QLayout *l)
10379 if (Q_UNLIKELY(!l)) {
10380 qWarning(
"QWidget::setLayout: Cannot set layout to 0");
10384 if (Q_UNLIKELY(layout() != l))
10385 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", which already has a"
10386 " layout", l->objectName().toLocal8Bit().data(), metaObject()->className(),
10387 objectName().toLocal8Bit().data());
10391 QObject *oldParent = l->parent();
10392 if (oldParent && oldParent !=
this) {
10393 if (oldParent->isWidgetType()) {
10396 QWidget *oldParentWidget =
static_cast<QWidget *>(oldParent);
10397 oldParentWidget->takeLayout();
10399 qWarning(
"QWidget::setLayout: Attempting to set QLayout \"%s\" on %s \"%s\", when the QLayout already has a parent",
10400 l->objectName().toLocal8Bit().data(), metaObject()->className(),
10401 objectName().toLocal8Bit().data());
10407 l->d_func()->topLevel =
true;
10409 if (oldParent !=
this) {
10410 l->setParent(
this);
10411 l->d_func()->reparentChildWidgets(
this);
10415 if (isWindow() && d->maybeTopData())
10416 d->topData()->sizeAdjusted =
false;
10420
10421
10422
10423
10424
10426QLayout *QWidget::takeLayout()
10429 QLayout *l = layout();
10432 d->layout =
nullptr;
10433 l->setParent(
nullptr);
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462QSizePolicy QWidget::sizePolicy()
const
10464 Q_D(
const QWidget);
10465 return d->size_policy;
10468void QWidget::setSizePolicy(QSizePolicy policy)
10471 setAttribute(Qt::WA_WState_OwnSizePolicy);
10472 if (policy == d->size_policy)
10475 if (d->size_policy.retainSizeWhenHidden() != policy.retainSizeWhenHidden())
10476 d->retainSizeWhenHiddenChanged = 1;
10478 d->size_policy = policy;
10480#if QT_CONFIG(graphicsview)
10481 if (
const auto &extra = d->extra) {
10482 if (extra->proxyWidget)
10483 extra->proxyWidget->setSizePolicy(policy);
10488 d->retainSizeWhenHiddenChanged = 0;
10490 if (isWindow() && d->maybeTopData())
10491 d->topData()->sizeAdjusted =
false;
10495
10496
10497
10498
10499
10500
10501
10502
10505
10506
10507
10508
10509
10510
10511
10513int QWidget::heightForWidth(
int w)
const
10515 if (layout() && layout()->hasHeightForWidth())
10516 return layout()->totalHeightForWidth(w);
10522
10523
10524
10525
10526bool QWidget::hasHeightForWidth()
const
10528 Q_D(
const QWidget);
10529 return d->layout ? d->layout->hasHeightForWidth() : d->size_policy.hasHeightForWidth();
10533
10534
10535
10536
10537
10538
10541
10542
10543
10544
10545
10547QWidget *QWidget::childAt(
const QPoint &p)
const
10549 return d_func()->childAt_helper(QPointF(p),
false);
10553
10554
10555
10556
10557
10558
10560QWidget *QWidget::childAt(
const QPointF &p)
const
10562 return d_func()->childAt_helper(p,
false);
10565QWidget *QWidgetPrivate::childAt_helper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10567 if (children.isEmpty())
10570 if (!pointInsideRectAndMask(p))
10572 return childAtRecursiveHelper(p, ignoreChildrenInDestructor);
10575QWidget *QWidgetPrivate::childAtRecursiveHelper(
const QPointF &p,
bool ignoreChildrenInDestructor)
const
10577 for (
int i = children.size() - 1; i >= 0; --i) {
10578 QWidget *child = qobject_cast<QWidget *>(children.at(i));
10579 if (!child || child->isWindow() || child->isHidden() || child->testAttribute(Qt::WA_TransparentForMouseEvents)
10580 || (ignoreChildrenInDestructor && child->data->in_destructor)) {
10585 QPointF childPoint = p;
10586 childPoint -= child->data->crect.topLeft();
10589 if (!child->d_func()->pointInsideRectAndMask(childPoint))
10593 if (QWidget *w = child->d_func()->childAtRecursiveHelper(childPoint, ignoreChildrenInDestructor))
10602void QWidgetPrivate::updateGeometry_helper(
bool forceUpdate)
10606 widgetItem->invalidateSizeCache();
10608 if (forceUpdate || !extra || extra->minw != extra->maxw || extra->minh != extra->maxh) {
10609 const int isHidden = q->isHidden() && !size_policy.retainSizeWhenHidden() && !retainSizeWhenHiddenChanged;
10611 if (!q->isWindow() && !isHidden && (parent = q->parentWidget())) {
10612 if (parent->d_func()->layout)
10613 parent->d_func()->layout->invalidate();
10614 else if (parent->isVisible())
10615 QCoreApplication::postEvent(parent,
new QEvent(QEvent::LayoutRequest));
10621
10622
10623
10624
10625
10626
10627
10628
10630void QWidget::updateGeometry()
10633 d->updateGeometry_helper(
false);
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654void QWidget::setWindowFlags(Qt::WindowFlags flags)
10657 d->setWindowFlags(flags);
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672void QWidget::setWindowFlag(Qt::WindowType flag,
bool on)
10676 d->setWindowFlags(data->window_flags | flag);
10678 d->setWindowFlags(data->window_flags & ~flag);
10682
10683
10684
10685void QWidgetPrivate::setWindowFlags(Qt::WindowFlags flags)
10688#if QT_DEPRECATED_SINCE(6
, 11
)
10689 QT_IGNORE_DEPRECATIONS(
10690 if ((flags & Qt::WindowType_Mask) == Qt::WindowType::Desktop) {
10691 qWarning() <<
"Qt::WindowType::Desktop has been deprecated in Qt 6. Ignoring.";
10692 flags.setFlag(Qt::WindowType::Desktop,
false);
10697 if (q->data->window_flags == flags)
10700 if ((q->data->window_flags | flags) & Qt::Window) {
10702 QPoint oldPos = q->pos();
10703 bool visible = q->isVisible();
10704 const bool windowFlagChanged = (q->data->window_flags ^ flags) & Qt::Window;
10705 q->setParent(q->parentWidget(), flags);
10709 if (!windowFlagChanged && (visible || q->testAttribute(Qt::WA_Moved)))
10712 adjustQuitOnCloseAttribute();
10714 q->data->window_flags = flags;
10719
10720
10721
10722
10723
10724
10725
10726
10727void QWidget::overrideWindowFlags(Qt::WindowFlags flags)
10729 data->window_flags = flags;
10733
10734
10735
10736
10737
10738
10739
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769void QWidget::setParent(QWidget *parent)
10771 if (parent == parentWidget())
10773 setParent((QWidget*)parent, windowFlags() & ~Qt::WindowType_Mask);
10779 if (d->renderToTexture) {
10780 QEvent e(eventType);
10781 QCoreApplication::sendEvent(widget, &e);
10784 for (
int i = 0; i < d->children.size(); ++i) {
10786 if (w && !w->isWindow())
10787 qSendWindowChangeToTextureChildrenRecursively(w, eventType);
10791 if (
auto *window = d->windowHandle(QWidgetPrivate::WindowHandleMode::Direct)) {
10792 QEvent e(eventType);
10793 QCoreApplication::sendEvent(window, &e);
10798
10799
10800
10801
10803void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
10806 Q_ASSERT_X(
this != parent, Q_FUNC_INFO,
"Cannot parent a QWidget to itself");
10808 const auto checkForParentChildLoops = qScopeGuard([&](){
10810 auto p = parentWidget();
10812 if (++depth == QObjectPrivate::CheckForParentChildLoopsWarnDepth) {
10813 qWarning(
"QWidget %p (class: '%s', object name: '%s') may have a loop in its parent-child chain; "
10814 "this is undefined behavior",
10815 this, metaObject()->className(), qPrintable(objectName()));
10817 p = p->parentWidget();
10822 const bool resized = testAttribute(Qt::WA_Resized);
10823 const bool wasCreated = testAttribute(Qt::WA_WState_Created);
10824 QWidget *oldtlw = window();
10826 QWidget *oldParentWithWindow = d->closestParentWidgetWithWindowHandle();
10828 if (f & Qt::Window)
10829 d->data.fstrut_dirty =
true;
10831 bool newParent = (parent != parentWidget());
10833 if (newParent && parent) {
10834 if (testAttribute(Qt::WA_NativeWindow) && !QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings))
10835 parent->d_func()->enforceNativeChildren();
10836 else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen))
10837 setAttribute(Qt::WA_NativeWindow);
10841 if (!testAttribute(Qt::WA_WState_Hidden)) {
10856 setAttribute(Qt::WA_WState_ExplicitShowHide,
false);
10859 QEvent e(QEvent::ParentAboutToChange);
10860 QCoreApplication::sendEvent(
this, &e);
10866 const bool oldWidgetUsesRhiFlush = oldParentWithWindow ? oldParentWithWindow->d_func()->usesRhiFlush
10867 : oldtlw->d_func()->usesRhiFlush;
10868 if (oldWidgetUsesRhiFlush && ((!parent && parentWidget()) || (parent && parent->window() != oldtlw)))
10869 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowAboutToChangeInternal);
10873 if (newParent && isAncestorOf(focusWidget()) && !(f & Qt::Window))
10874 focusWidget()->clearFocus();
10876 d->setParent_sys(parent, f);
10878 if (d->textureChildSeen && parent) {
10880 QWidgetPrivate::get(parent)->setTextureChildSeen();
10883 if (QWidgetRepaintManager *oldPaintManager = oldtlw->d_func()->maybeRepaintManager()) {
10885 oldPaintManager->removeDirtyWidget(
this);
10888 oldPaintManager->moveStaticWidgets(
this);
10891 d->reparentFocusWidgets(oldtlw);
10892 setAttribute(Qt::WA_Resized, resized);
10894 const bool useStyleSheetPropagationInWidgetStyles =
10895 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
10897 if (!useStyleSheetPropagationInWidgetStyles && !testAttribute(Qt::WA_StyleSheet)
10898 && (!parent || !parent->testAttribute(Qt::WA_StyleSheet))) {
10901 const auto pd = parent->d_func();
10902 d->inheritedFontResolveMask = pd->directFontResolveMask | pd->inheritedFontResolveMask;
10903 d->inheritedPaletteResolveMask = pd->directPaletteResolveMask | pd->inheritedPaletteResolveMask;
10906 d->resolvePalette();
10908 d->resolveLayoutDirection();
10909 d->resolveLocale();
10915 if (newParent || !wasCreated
10916#if QT_CONFIG(opengles2)
10917 || (f & Qt::MSWindowsOwnDC)
10922 if (!testAttribute(Qt::WA_ForceDisabled))
10923 d->setEnabled_helper(parent ? parent->isEnabled() :
true);
10924 if (!testAttribute(Qt::WA_ForceUpdatesDisabled))
10925 d->setUpdatesEnabled_helper(parent ? parent->updatesEnabled() :
true);
10930 if (parent && d->sendChildEvents) {
10931 QChildEvent e(QEvent::ChildAdded,
this);
10932 QCoreApplication::sendEvent(parent, &e);
10935 if (parent && d->sendChildEvents && d->polished) {
10936 QChildEvent e(QEvent::ChildPolished,
this);
10937 QCoreApplication::sendEvent(parent, &e);
10940 QEvent e(QEvent::ParentChange);
10941 QCoreApplication::sendEvent(
this, &e);
10946 if (oldWidgetUsesRhiFlush && oldtlw != window())
10947 qSendWindowChangeToTextureChildrenRecursively(
this, QEvent::WindowChangeInternal);
10950 if (isWindow() || parentWidget()->isVisible())
10951 setAttribute(Qt::WA_WState_Hidden,
true);
10952 else if (!testAttribute(Qt::WA_WState_ExplicitShowHide))
10953 setAttribute(Qt::WA_WState_Hidden,
false);
10956 d->updateIsOpaque();
10958#if QT_CONFIG(graphicsview)
10961 if (oldtlw->graphicsProxyWidget()) {
10962 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(oldtlw))
10963 ancestorProxy->d_func()->unembedSubWindow(
this);
10965 if (isWindow() && parent && !graphicsProxyWidget() && !bypassGraphicsProxyWidget(
this)) {
10966 if (QGraphicsProxyWidget *ancestorProxy = d->nearestGraphicsProxyWidget(parent))
10967 ancestorProxy->d_func()->embedSubWindow(
this);
10971 if (d->extra && d->extra->hasWindowContainer)
10972 QWindowContainer::parentWasChanged(
this);
10974 QWidget *newParentWithWindow = d->closestParentWidgetWithWindowHandle();
10975 if (newParentWithWindow && newParentWithWindow != oldParentWithWindow) {
10977 qCDebug(lcWidgetPainting) <<
"Evaluating whether reparenting of" <<
this
10978 <<
"into" << parent <<
"requires RHI enablement for" << newParentWithWindow;
10980 QPlatformBackingStoreRhiConfig rhiConfig;
10981 QSurface::SurfaceType surfaceType = QSurface::RasterSurface;
10987 if (q_evaluateRhiConfig(
this, &rhiConfig, &surfaceType)) {
10991 if (q_evaluateRhiConfig(newParentWithWindow,
nullptr,
nullptr)) {
10994 auto *existingWindow = newParentWithWindow->windowHandle();
10995 auto existingSurfaceType = existingWindow->surfaceType();
10996 if (existingSurfaceType != surfaceType) {
10997 qCDebug(lcWidgetPainting)
10998 <<
"Recreating" << existingWindow
10999 <<
"with current type" << existingSurfaceType
11000 <<
"to support" << surfaceType;
11001 const auto windowStateBeforeDestroy = newParentWithWindow->windowState();
11002 const auto visibilityBeforeDestroy = newParentWithWindow->isVisible();
11003 const auto positionBeforeDestroy = newParentWithWindow->pos();
11004 newParentWithWindow->d_func()->recreate();
11005 Q_ASSERT(newParentWithWindow->windowHandle());
11006 newParentWithWindow->windowHandle()->setWindowStates(windowStateBeforeDestroy);
11007 newParentWithWindow->move(positionBeforeDestroy);
11008 QWidgetPrivate::get(newParentWithWindow)->setVisible(visibilityBeforeDestroy);
11009 }
else if (
auto *backingStore = newParentWithWindow->backingStore()) {
11012 backingStore->handle()->createRhi(existingWindow, rhiConfig);
11014 QWidgetPrivate::get(newParentWithWindow)->usesRhiFlush =
true;
11020#if QT_CONFIG(accessibility)
11021 if (QGuiApplicationPrivate::is_app_running && !QGuiApplicationPrivate::is_app_closing) {
11022 QAccessibleEvent qaEvent(
this, QAccessible::ParentChanged);
11023 QAccessible::updateAccessibility(&qaEvent);
11029void QWidgetPrivate::setParent_sys(QWidget *newparent, Qt::WindowFlags f)
11033 Qt::WindowFlags oldFlags = data.window_flags;
11034 bool wasCreated = q->testAttribute(Qt::WA_WState_Created);
11036 QScreen *targetScreen =
nullptr;
11042 targetScreen = q->parentWidget()->window()->screen();
11045 const bool destroyWindow = (
11047 (oldFlags & Qt::Window) && !(f & Qt::Window)
11049 && wasCreated && !q->testAttribute(Qt::WA_NativeWindow)
11052 if (parent != newparent) {
11054 QObjectPrivate::setParent_helper(newparent);
11056 if (q->windowHandle())
11057 q->windowHandle()->setFlags(f);
11061 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11064 if (destroyWindow) {
11065 reparentWidgetWindowChildren(parentWithWindow);
11075 reparentWidgetWindows(parentWithWindow, f);
11079 bool explicitlyHidden = isExplicitlyHidden();
11081 if (destroyWindow) {
11082 if (extra && extra->hasWindowContainer)
11083 QWindowContainer::toplevelAboutToBeDestroyed(q);
11087 if (!q->windowHandle()->children().isEmpty()) {
11088 QWidget *parentWithWindow = closestParentWidgetWithWindowHandle();
11089 QWindow *newParentWindow = parentWithWindow ? parentWithWindow->windowHandle() :
nullptr;
11090 for (QObject *child : q->windowHandle()->children()) {
11091 if (QWindow *childWindow = qobject_cast<QWindow *>(child)) {
11092 qCWarning(lcWidgetWindow) <<
"Reparenting" << childWindow
11093 <<
"before destroying" <<
this;
11094 childWindow->setParent(newParentWindow);
11102 q->destroy(
true,
false);
11106 data.window_flags = f;
11107 q->setAttribute(Qt::WA_WState_Created,
false);
11108 q->setAttribute(Qt::WA_WState_Visible,
false);
11109 q->setAttribute(Qt::WA_WState_Hidden,
false);
11111 if (newparent && wasCreated && (q->testAttribute(Qt::WA_NativeWindow) || (f & Qt::Window)))
11114 if (q->isWindow() || (!newparent || newparent->isVisible()) || explicitlyHidden)
11115 q->setAttribute(Qt::WA_WState_Hidden);
11116 q->setAttribute(Qt::WA_WState_ExplicitShowHide, explicitlyHidden);
11119 if (!newparent && targetScreen) {
11121 if (q->testAttribute(Qt::WA_WState_Created))
11122 q->windowHandle()->setScreen(targetScreen);
11124 topData()->initialScreen = targetScreen;
11128void QWidgetPrivate::reparentWidgetWindows(QWidget *parentWithWindow, Qt::WindowFlags windowFlags)
11130 if (QWindow *window = windowHandle()) {
11132 if (parentWithWindow) {
11133 if (windowFlags & Qt::Window) {
11136 QWidget *topLevel = parentWithWindow->window();
11137 auto *transientParent = topLevel->windowHandle();
11138 Q_ASSERT(transientParent);
11139 qCDebug(lcWidgetWindow) <<
"Setting" << window <<
"transient parent to" << transientParent;
11140 window->setTransientParent(transientParent);
11141 window->setParent(
nullptr);
11143 auto *parentWindow = parentWithWindow->windowHandle();
11144 qCDebug(lcWidgetWindow) <<
"Reparenting" << window <<
"into" << parentWindow;
11145 window->setTransientParent(
nullptr);
11146 window->setParent(parentWindow);
11149 qCDebug(lcWidgetWindow) <<
"Making" << window <<
"top level window";
11150 window->setTransientParent(
nullptr);
11151 window->setParent(
nullptr);
11154 reparentWidgetWindowChildren(parentWithWindow);
11158void QWidgetPrivate::reparentWidgetWindowChildren(QWidget *parentWithWindow)
11160 for (
auto *child : std::as_const(children)) {
11161 if (
auto *childWidget = qobject_cast<QWidget*>(child)) {
11162 auto *childPrivate = QWidgetPrivate::get(childWidget);
11165 childPrivate->reparentWidgetWindows(parentWithWindow, childWidget->windowFlags());
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11191void QWidget::scroll(
int dx,
int dy)
11193 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11195 if (dx == 0 && dy == 0)
11198#if QT_CONFIG(graphicsview)
11199 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11203 for (
const QRect &rect : d->dirty)
11204 proxy->update(rect.translated(dx, dy));
11205 proxy->scroll(dx, dy, proxy->subWidgetRect(
this));
11206 d->scrollChildren(dx, dy);
11210 d->setDirtyOpaqueRegion();
11211 d->scroll_sys(dx, dy);
11214void QWidgetPrivate::scroll_sys(
int dx,
int dy)
11217 scrollChildren(dx, dy);
11218 scrollRect(q->rect(), dx, dy);
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231void QWidget::scroll(
int dx,
int dy,
const QRect &r)
11234 if ((!updatesEnabled() && children().size() == 0) || !isVisible())
11236 if (dx == 0 && dy == 0)
11239#if QT_CONFIG(graphicsview)
11240 if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(
this)) {
11244 if (!d->dirty.isEmpty()) {
11245 for (
const QRect &rect : d->dirty.translated(dx, dy) & r)
11246 proxy->update(rect);
11248 proxy->scroll(dx, dy, r.translated(proxy->subWidgetRect(
this).topLeft().toPoint()));
11252 d->scroll_sys(dx, dy, r);
11255void QWidgetPrivate::scroll_sys(
int dx,
int dy,
const QRect &r)
11257 scrollRect(r, dx, dy);
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11276void QWidget::repaint()
11282
11283
11284
11285
11286
11287
11288
11289void QWidget::repaint(
int x,
int y,
int w,
int h)
11291 if (x > data->crect.width() || y > data->crect.height())
11295 w = data->crect.width() - x;
11297 h = data->crect.height() - y;
11299 repaint(QRect(x, y, w, h));
11303
11304
11305
11306void QWidget::repaint(
const QRect &rect)
11313
11314
11315
11316
11317void QWidget::repaint(
const QRegion &rgn)
11323template <
typename T>
11324void QWidgetPrivate::repaint(T r)
11328 if (!q->isVisible() || !q->updatesEnabled() || r.isEmpty())
11331 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11332 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11333 tlwExtra->repaintManager->markDirty(r, q, QWidgetRepaintManager::UpdateNow);
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354void QWidget::update()
11360
11361
11362
11363
11364
11367
11368
11369
11370
11371void QWidget::update(
const QRect &rect)
11378
11379
11380
11381
11382void QWidget::update(
const QRegion &rgn)
11388template <
typename T>
11389void QWidgetPrivate::update(T r)
11393 if (renderToTexture && !q->isVisible()) {
11394 renderToTextureReallyDirty = 1;
11398 if (!q->isVisible() || !q->updatesEnabled())
11401 T clipped = r & q->rect();
11403 if (clipped.isEmpty())
11406 if (q->testAttribute(Qt::WA_WState_InPaintEvent)) {
11407 QCoreApplication::postEvent(q,
new QUpdateLaterEvent(clipped));
11411 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
11412 if (tlwExtra && tlwExtra->backingStore && tlwExtra->repaintManager)
11413 tlwExtra->repaintManager->markDirty(clipped, q);
11417
11418
11419
11420
11424 if (attribute <
int(8*
sizeof(uint))) {
11426 data->widget_attributes |= (1<<attribute);
11428 data->widget_attributes &= ~(1<<attribute);
11430 const int x = attribute - 8*
sizeof(uint);
11431 const int int_off = x / (8*
sizeof(uint));
11433 d->high_attributes[int_off] |= (1<<(x-(int_off*8*
sizeof(uint))));
11435 d->high_attributes[int_off] &= ~(1<<(x-(int_off*8*
sizeof(uint))));
11440void QWidgetPrivate::macUpdateSizeAttribute()
11443 QEvent event(QEvent::MacSizeChange);
11444 QCoreApplication::sendEvent(q, &event);
11445 for (
int i = 0; i < children.size(); ++i) {
11446 QWidget *w = qobject_cast<QWidget *>(children.at(i));
11447 if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
11448 && !w->testAttribute(Qt::WA_MacMiniSize)
11449 && !w->testAttribute(Qt::WA_MacSmallSize)
11450 && !w->testAttribute(Qt::WA_MacNormalSize))
11451 w->d_func()->macUpdateSizeAttribute();
11458
11459
11460
11461
11462
11463void QWidget::setAttribute(Qt::WidgetAttribute attribute,
bool on)
11467 if (attribute == Qt::WA_ContentsMarginsRespectsSafeArea) {
11469 auto *topExtra = d->topData();
11470 topExtra->explicitContentsMarginsRespectsSafeArea =
true;
11474 if (testAttribute(attribute) == on)
11477 static_assert(
sizeof(d->high_attributes)*8 >= (Qt::WA_AttributeCount -
sizeof(uint)*8),
11478 "QWidget::setAttribute(WidgetAttribute, bool): "
11479 "QWidgetPrivate::high_attributes[] too small to contain all attributes in WidgetAttribute");
11482 if (attribute == Qt::WA_PaintOnScreen && on && !inherits(
"QGLWidget")) {
11485 if (d->noPaintOnScreen)
11491 if (attribute == Qt::WA_NativeWindow && !d->mustHaveWindowHandle) {
11492 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
11493 if (!platformIntegration->hasCapability(QPlatformIntegration::NativeWidgets))
11497 setAttribute_internal(attribute, on, data, d);
11499 switch (attribute) {
11501#if QT_CONFIG(draganddrop)
11502 case Qt::WA_AcceptDrops: {
11503 if (on && !testAttribute(Qt::WA_DropSiteRegistered))
11504 setAttribute(Qt::WA_DropSiteRegistered,
true);
11505 else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)))
11506 setAttribute(Qt::WA_DropSiteRegistered,
false);
11507 QEvent e(QEvent::AcceptDropsChange);
11508 QCoreApplication::sendEvent(
this, &e);
11511 case Qt::WA_DropSiteRegistered: {
11512 for (
int i = 0; i < d->children.size(); ++i) {
11513 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
11514 if (w && !w->isWindow() && !w->testAttribute(Qt::WA_AcceptDrops) && w->testAttribute(Qt::WA_DropSiteRegistered) != on)
11515 w->setAttribute(Qt::WA_DropSiteRegistered, on);
11521 case Qt::WA_NoChildEventsForParent:
11522 d->sendChildEvents = !on;
11524 case Qt::WA_NoChildEventsFromChildren:
11525 d->receiveChildEvents = !on;
11527 case Qt::WA_MacNormalSize:
11528 case Qt::WA_MacSmallSize:
11529 case Qt::WA_MacMiniSize:
11533 const Qt::WidgetAttribute MacSizes[] = { Qt::WA_MacNormalSize, Qt::WA_MacSmallSize,
11534 Qt::WA_MacMiniSize };
11535 for (
int i = 0; i < 3; ++i) {
11536 if (MacSizes[i] != attribute)
11537 setAttribute_internal(MacSizes[i],
false, data, d);
11539 d->macUpdateSizeAttribute();
11543 case Qt::WA_ShowModal:
11546 data->window_modality = Qt::NonModal;
11547 }
else if (data->window_modality == Qt::NonModal) {
11550 data->window_modality = Qt::ApplicationModal;
11555 if (testAttribute(Qt::WA_WState_Created)) {
11560 case Qt::WA_MouseTracking: {
11561 QEvent e(QEvent::MouseTrackingChange);
11562 QCoreApplication::sendEvent(
this, &e);
11564 case Qt::WA_TabletTracking: {
11565 QEvent e(QEvent::TabletTrackingChange);
11566 QCoreApplication::sendEvent(
this, &e);
11568 case Qt::WA_NativeWindow: {
11569 d->createTLExtra();
11571 d->createTLSysExtra();
11573 QWidget *focusWidget = d->effectiveFocusWidget();
11574 if (on && !internalWinId() &&
this == QGuiApplication::focusObject()
11575 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11576 QGuiApplication::inputMethod()->commit();
11577 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11579 if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget())
11580 parentWidget()->d_func()->enforceNativeChildren();
11581 if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created))
11583 if (isEnabled() && focusWidget->isEnabled() &&
this == QGuiApplication::focusObject()
11584 && focusWidget->testAttribute(Qt::WA_InputMethodEnabled)) {
11585 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11590 case Qt::WA_PaintOnScreen:
11591 d->updateIsOpaque();
11593 case Qt::WA_OpaquePaintEvent:
11594 d->updateIsOpaque();
11596 case Qt::WA_NoSystemBackground:
11597 d->updateIsOpaque();
11599 case Qt::WA_UpdatesDisabled:
11600 d->updateSystemBackground();
11602 case Qt::WA_TransparentForMouseEvents:
11604 case Qt::WA_InputMethodEnabled: {
11606 if (QGuiApplication::focusObject() ==
this) {
11608 QGuiApplication::inputMethod()->commit();
11609 QGuiApplication::inputMethod()->update(Qt::ImEnabled);
11614 case Qt::WA_WindowPropagation:
11615 d->resolvePalette();
11617 d->resolveLocale();
11619 case Qt::WA_DontShowOnScreen: {
11620 if (on && isVisible()) {
11630 case Qt::WA_X11NetWmWindowTypeDesktop:
11631 case Qt::WA_X11NetWmWindowTypeDock:
11632 case Qt::WA_X11NetWmWindowTypeToolBar:
11633 case Qt::WA_X11NetWmWindowTypeMenu:
11634 case Qt::WA_X11NetWmWindowTypeUtility:
11635 case Qt::WA_X11NetWmWindowTypeSplash:
11636 case Qt::WA_X11NetWmWindowTypeDialog:
11637 case Qt::WA_X11NetWmWindowTypeDropDownMenu:
11638 case Qt::WA_X11NetWmWindowTypePopupMenu:
11639 case Qt::WA_X11NetWmWindowTypeToolTip:
11640 case Qt::WA_X11NetWmWindowTypeNotification:
11641 case Qt::WA_X11NetWmWindowTypeCombo:
11642 case Qt::WA_X11NetWmWindowTypeDND:
11643 d->setNetWmWindowTypes();
11646 case Qt::WA_StaticContents:
11647 if (QWidgetRepaintManager *repaintManager = d->maybeRepaintManager()) {
11649 repaintManager->addStaticWidget(
this);
11651 repaintManager->removeStaticWidget(
this);
11654 case Qt::WA_TranslucentBackground:
11656 setAttribute(Qt::WA_NoSystemBackground);
11657 d->updateIsTranslucent();
11660 case Qt::WA_AcceptTouchEvents:
11668
11669
11670
11671
11672
11673
11674bool QWidget::testAttribute_helper(Qt::WidgetAttribute attribute)
const
11676 Q_D(
const QWidget);
11677 const int x = attribute - 8*
sizeof(uint);
11678 const int int_off = x / (8*
sizeof(uint));
11679 return (d->high_attributes[int_off] & (1<<(x-(int_off*8*
sizeof(uint)))));
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707qreal QWidget::windowOpacity()
const
11709 Q_D(
const QWidget);
11710 return (isWindow() && d->maybeTopData()) ? d->maybeTopData()->opacity / 255. : 1.0;
11713void QWidget::setWindowOpacity(qreal opacity)
11719 opacity = qBound(qreal(0.0), opacity, qreal(1.0));
11720 QTLWExtra *extra = d->topData();
11721 extra->opacity = uint(opacity * 255);
11722 setAttribute(Qt::WA_WState_WindowOpacitySet);
11723 d->setWindowOpacity_sys(opacity);
11725 if (!testAttribute(Qt::WA_WState_Created))
11728#if QT_CONFIG(graphicsview)
11729 if (QGraphicsProxyWidget *proxy = graphicsProxyWidget()) {
11731 if (proxy->cacheMode() == QGraphicsItem::NoCache)
11733 else if (QGraphicsScene *scene = proxy->scene())
11734 scene->update(proxy->sceneBoundingRect());
11740void QWidgetPrivate::setWindowOpacity_sys(qreal level)
11743 if (q->windowHandle())
11744 q->windowHandle()->setOpacity(level);
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771bool QWidget::isWindowModified()
const
11773 return testAttribute(Qt::WA_WindowModified);
11776void QWidget::setWindowModified(
bool mod)
11779 setAttribute(Qt::WA_WindowModified, mod);
11781 d->setWindowModified_helper();
11783 QEvent e(QEvent::ModifiedChange);
11784 QCoreApplication::sendEvent(
this, &e);
11787void QWidgetPrivate::setWindowModified_helper()
11790 QWindow *window = q->windowHandle();
11793 QPlatformWindow *platformWindow = window->handle();
11794 if (!platformWindow)
11796 bool on = q->testAttribute(Qt::WA_WindowModified);
11797 if (!platformWindow->setWindowModified(on)) {
11798 if (Q_UNLIKELY(on && !q->windowTitle().contains(
"[*]"_L1)))
11799 qWarning(
"QWidget::setWindowModified: The window title does not contain a '[*]' placeholder");
11800 setWindowTitle_helper(q->windowTitle());
11801 setWindowIconText_helper(q->windowIconText());
11805#if QT_CONFIG(tooltip)
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824void QWidget::setToolTip(
const QString &s)
11829 QEvent event(QEvent::ToolTipChange);
11830 QCoreApplication::sendEvent(
this, &event);
11833QString QWidget::toolTip()
const
11835 Q_D(
const QWidget);
11840
11841
11842
11843
11844
11845
11846
11847
11848
11850void QWidget::setToolTipDuration(
int msec)
11853 d->toolTipDuration = msec;
11856int QWidget::toolTipDuration()
const
11858 Q_D(
const QWidget);
11859 return d->toolTipDuration;
11865#if QT_CONFIG(statustip)
11867
11868
11869
11870
11871
11872
11873
11874void QWidget::setStatusTip(
const QString &s)
11880QString QWidget::statusTip()
const
11882 Q_D(
const QWidget);
11883 return d->statusTip;
11887#if QT_CONFIG(whatsthis)
11889
11890
11891
11892
11893
11894
11895
11896
11897void QWidget::setWhatsThis(
const QString &s)
11903QString QWidget::whatsThis()
const
11905 Q_D(
const QWidget);
11906 return d->whatsThis;
11910#if QT_CONFIG(accessibility)
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932void QWidget::setAccessibleName(
const QString &name)
11935 if (d->accessibleName == name)
11938 d->accessibleName = name;
11939 QAccessibleEvent event(
this, QAccessible::NameChanged);
11940 QAccessible::updateAccessibility(&event);
11943QString QWidget::accessibleName()
const
11945 Q_D(
const QWidget);
11946 return d->accessibleName;
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965void QWidget::setAccessibleDescription(
const QString &description)
11968 if (d->accessibleDescription == description)
11971 d->accessibleDescription = description;
11972 QAccessibleEvent event(
this, QAccessible::DescriptionChanged);
11973 QAccessible::updateAccessibility(&event);
11976QString QWidget::accessibleDescription()
const
11978 Q_D(
const QWidget);
11979 return d->accessibleDescription;
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993void QWidget::setAccessibleIdentifier(
const QString &identifier)
11996 if (d->accessibleIdentifier == identifier)
11999 d->accessibleIdentifier = identifier;
12000 QAccessibleEvent event(
this, QAccessible::IdentifierChanged);
12001 QAccessible::updateAccessibility(&event);
12004QString QWidget::accessibleIdentifier()
const
12006 Q_D(
const QWidget);
12007 return d->accessibleIdentifier;
12012#ifndef QT_NO_SHORTCUT
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036int QWidget::grabShortcut(
const QKeySequence &key, Qt::ShortcutContext context)
12041 setAttribute(Qt::WA_GrabbedShortcut);
12042 return QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(
this, key, context, qWidgetShortcutContextMatcher);
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060void QWidget::releaseShortcut(
int id)
12064 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id,
this, 0);
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079void QWidget::setShortcutEnabled(
int id,
bool enable)
12083 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id,
this, 0);
12087
12088
12089
12090
12091
12092
12093
12094void QWidget::setShortcutAutoRepeat(
int id,
bool enable)
12098 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(enable, id,
this, 0);
12103
12104
12105
12106void QWidget::updateMicroFocus(Qt::InputMethodQuery query)
12108 if (
this == QGuiApplication::focusObject())
12109 QGuiApplication::inputMethod()->update(query);
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12124void QWidget::raise()
12128 QWidget *p = parentWidget();
12129 const int parentChildCount = p->d_func()->children.size();
12130 if (parentChildCount < 2)
12132 const int from = p->d_func()->children.indexOf(
this);
12133 Q_ASSERT(from >= 0);
12135 if (from != parentChildCount -1)
12136 p->d_func()->children.move(from, parentChildCount - 1);
12137 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12139 else if (from == parentChildCount - 1)
12142 QRegion region(rect());
12143 d->subtractOpaqueSiblings(region);
12144 d->invalidateBackingStore(region);
12146 if (testAttribute(Qt::WA_WState_Created))
12149 if (d->extra && d->extra->hasWindowContainer)
12150 QWindowContainer::parentWasRaised(
this);
12152 QEvent e(QEvent::ZOrderChange);
12153 QCoreApplication::sendEvent(
this, &e);
12156void QWidgetPrivate::raise_sys()
12159 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12160 q->windowHandle()->raise();
12161 }
else if (renderToTexture) {
12162 if (QWidget *p = q->parentWidget()) {
12163 setDirtyOpaqueRegion();
12164 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12170
12171
12172
12173
12174
12175
12176
12178void QWidget::lower()
12182 QWidget *p = parentWidget();
12183 const int parentChildCount = p->d_func()->children.size();
12184 if (parentChildCount < 2)
12186 const int from = p->d_func()->children.indexOf(
this);
12187 Q_ASSERT(from >= 0);
12190 p->d_func()->children.move(from, 0);
12191 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12193 else if (from == 0)
12196 if (testAttribute(Qt::WA_WState_Created))
12199 if (d->extra && d->extra->hasWindowContainer)
12200 QWindowContainer::parentWasLowered(
this);
12202 QEvent e(QEvent::ZOrderChange);
12203 QCoreApplication::sendEvent(
this, &e);
12206void QWidgetPrivate::lower_sys()
12209 if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
12210 Q_ASSERT(q->testAttribute(Qt::WA_WState_Created));
12211 q->windowHandle()->lower();
12212 }
else if (QWidget *p = q->parentWidget()) {
12213 setDirtyOpaqueRegion();
12214 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12219
12220
12221
12222
12223
12224
12225void QWidget::stackUnder(QWidget* w)
12228 QWidget *p = parentWidget();
12229 if (!w || isWindow() || p != w->parentWidget() ||
this == w)
12232 int from = p->d_func()->children.indexOf(
this);
12233 int to = p->d_func()->children.indexOf(w);
12234 Q_ASSERT(from >= 0);
12240 p->d_func()->children.move(from, to);
12241 if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
12243 else if (from == to)
12246 if (testAttribute(Qt::WA_WState_Created))
12247 d->stackUnder_sys(w);
12249 QEvent e(QEvent::ZOrderChange);
12250 QCoreApplication::sendEvent(
this, &e);
12253void QWidgetPrivate::stackUnder_sys(QWidget*)
12256 if (QWidget *p = q->parentWidget()) {
12257 setDirtyOpaqueRegion();
12258 p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
12263
12264
12265
12266
12267
12270
12271
12272
12275
12276
12277
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12293
12294
12295
12296
12297
12298
12299
12300
12302QRect QWidgetPrivate::frameStrut()
const
12304 Q_Q(
const QWidget);
12305 if (!q->isWindow() || q->testAttribute(Qt::WA_DontShowOnScreen)) {
12307 return QRect(0, 0, 1, 1);
12310 if (data.fstrut_dirty
12313 && q->testAttribute(Qt::WA_WState_Created))
12314 const_cast<QWidgetPrivate *>(
this)->updateFrameStrut();
12316 return maybeTopData() ? maybeTopData()->frameStrut : QRect();
12319void QWidgetPrivate::updateFrameStrut()
12322 if (q->data->fstrut_dirty) {
12323 if (QTLWExtra *te = maybeTopData()) {
12324 if (te->window && te->window->handle()) {
12325 const QMargins margins = te->window->frameMargins();
12326 if (!margins.isNull()) {
12327 te->frameStrut.setCoords(margins.left(), margins.top(), margins.right(), margins.bottom());
12328 q->data->fstrut_dirty =
false;
12335#ifdef QT_KEYPAD_NAVIGATION
12337
12338
12339
12340
12341
12342
12343
12344bool QWidgetPrivate::navigateToDirection(Direction direction)
12346 QWidget *targetWidget = widgetInNavigationDirection(direction);
12348 targetWidget->setFocus();
12349 return (targetWidget != 0);
12353
12354
12355
12356
12357
12358
12359
12360
12361QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
12363 const QWidget *sourceWidget = QApplication::focusWidget();
12366 const QRect sourceRect = sourceWidget->rect().translated(sourceWidget->mapToGlobal(QPoint()));
12367 const int sourceX =
12368 (direction == DirectionNorth || direction == DirectionSouth) ?
12369 (sourceRect.left() + (sourceRect.right() - sourceRect.left()) / 2)
12370 :(direction == DirectionEast ? sourceRect.right() : sourceRect.left());
12371 const int sourceY =
12372 (direction == DirectionEast || direction == DirectionWest) ?
12373 (sourceRect.top() + (sourceRect.bottom() - sourceRect.top()) / 2)
12374 :(direction == DirectionSouth ? sourceRect.bottom() : sourceRect.top());
12375 const QPoint sourcePoint(sourceX, sourceY);
12376 const QPoint sourceCenter = sourceRect.center();
12377 const QWidget *sourceWindow = sourceWidget->window();
12379 QWidget *targetWidget =
nullptr;
12380 int shortestDistance = INT_MAX;
12382 const auto targetCandidates = QApplication::allWidgets();
12383 for (QWidget *targetCandidate : targetCandidates) {
12385 const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));
12390 if (targetCandidate->focusProxy() || targetCandidateRect.isEmpty())
12394 if ( targetCandidate != sourceWidget
12396 && targetCandidate->focusPolicy() & Qt::TabFocus
12398 && !(direction == DirectionNorth && targetCandidateRect.bottom() > sourceRect.top())
12400 && !(direction == DirectionEast && targetCandidateRect.left() < sourceRect.right())
12402 && !(direction == DirectionSouth && targetCandidateRect.top() < sourceRect.bottom())
12404 && !(direction == DirectionWest && targetCandidateRect.right() > sourceRect.left())
12406 && targetCandidate->isEnabled()
12408 && targetCandidate->isVisible()
12410 && targetCandidate->window() == sourceWindow) {
12411 const int targetCandidateDistance = [](
const QPoint &sourcePoint,
12412 const QRect &targetCandidateRect) {
12415 if (p.x() < r.left())
12416 dx = r.left() - p.x();
12417 else if (p.x() > r.right())
12418 dx = p.x() - r.right();
12419 if (p.y() < r.top())
12420 dy = r.top() - p.y();
12421 else if (p.y() > r.bottom())
12422 dy = p.y() - r.bottom();
12425 if (targetCandidateDistance < shortestDistance) {
12426 shortestDistance = targetCandidateDistance;
12427 targetWidget = targetCandidate;
12431 return targetWidget;
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445bool QWidgetPrivate::canKeypadNavigate(Qt::Orientation orientation)
12447 return orientation == Qt::Horizontal?
12448 (QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionEast)
12449 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionWest))
12450 :(QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionNorth)
12451 || QWidgetPrivate::widgetInNavigationDirection(QWidgetPrivate::DirectionSouth));
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465bool QWidgetPrivate::inTabWidget(QWidget *widget)
12467 for (QWidget *tabWidget = widget; tabWidget; tabWidget = tabWidget->parentWidget())
12468 if (qobject_cast<
const QTabWidget*>(tabWidget))
12475
12476
12477
12478
12479
12480
12481void QWidget::setBackingStore(QBackingStore *store)
12490 QTLWExtra *topData = d->topData();
12491 if (topData->backingStore == store)
12494 QBackingStore *oldStore = topData->backingStore;
12495 delete topData->backingStore;
12496 topData->backingStore = store;
12498 QWidgetRepaintManager *repaintManager = d->maybeRepaintManager();
12499 if (!repaintManager)
12503 if (repaintManager->backingStore() != oldStore && repaintManager->backingStore() != store)
12504 delete repaintManager->backingStore();
12505 repaintManager->setBackingStore(store);
12510
12511
12512
12513
12514QBackingStore *QWidget::backingStore()
const
12516 Q_D(
const QWidget);
12517 QTLWExtra *extra = d->maybeTopData();
12518 if (extra && extra->backingStore)
12519 return extra->backingStore;
12522 return window()->backingStore();
12527void QWidgetPrivate::getLayoutItemMargins(
int *left,
int *top,
int *right,
int *bottom)
const
12530 *left = (
int)leftLayoutItemMargin;
12532 *top = (
int)topLayoutItemMargin;
12534 *right = (
int)rightLayoutItemMargin;
12536 *bottom = (
int)bottomLayoutItemMargin;
12539void QWidgetPrivate::setLayoutItemMargins(
int left,
int top,
int right,
int bottom)
12541 if (leftLayoutItemMargin == left
12542 && topLayoutItemMargin == top
12543 && rightLayoutItemMargin == right
12544 && bottomLayoutItemMargin == bottom)
12548 leftLayoutItemMargin = (
signed char)left;
12549 topLayoutItemMargin = (
signed char)top;
12550 rightLayoutItemMargin = (
signed char)right;
12551 bottomLayoutItemMargin = (
signed char)bottom;
12552 q->updateGeometry();
12555void QWidgetPrivate::setLayoutItemMargins(QStyle::SubElement element,
const QStyleOption *opt)
12558 QStyleOption myOpt;
12561 myOpt.rect.setRect(0, 0, 32768, 32768);
12565 QRect liRect = q->style()->subElementRect(element, opt, q);
12566 if (liRect.isValid()) {
12567 leftLayoutItemMargin = (
signed char)(opt->rect.left() - liRect.left());
12568 topLayoutItemMargin = (
signed char)(opt->rect.top() - liRect.top());
12569 rightLayoutItemMargin = (
signed char)(liRect.right() - opt->rect.right());
12570 bottomLayoutItemMargin = (
signed char)(liRect.bottom() - opt->rect.bottom());
12572 leftLayoutItemMargin = 0;
12573 topLayoutItemMargin = 0;
12574 rightLayoutItemMargin = 0;
12575 bottomLayoutItemMargin = 0;
12579void QWidgetPrivate::adjustQuitOnCloseAttribute()
12583 if (!q->parentWidget()) {
12584 Qt::WindowType type = q->windowType();
12585 if (type == Qt::Widget || type == Qt::SubWindow)
12587 if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
12588 q->setAttribute(Qt::WA_QuitOnClose,
false);
12592void QWidgetPrivate::sendComposeStatus(QWidget *w,
bool end)
12594 QWidgetPrivate *wd = QWidgetPrivate::get(w);
12595 if (!wd->textureChildSeen)
12600 wd->beginCompose();
12601 for (
int i = 0; i < wd->children.size(); ++i) {
12602 w = qobject_cast<QWidget *>(wd->children.at(i));
12603 if (w && !w->isWindow() && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen)
12604 sendComposeStatus(w, end);
12608Q_WIDGETS_EXPORT QWidgetData *qt_qwidget_data(QWidget *widget)
12610 return widget->data;
12615 return widget->d_func();
12619#if QT_CONFIG(graphicsview)
12621
12622
12623
12624
12625
12626
12627
12628
12629QGraphicsProxyWidget *QWidget::graphicsProxyWidget()
const
12631 Q_D(
const QWidget);
12633 return d->extra->proxyWidget;
12639#ifndef QT_NO_GESTURES
12641
12642
12643
12644
12645
12646void QWidget::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
12649 d->gestureContext.insert(gesture, flags);
12650 (
void)QGestureManager::instance();
12654
12655
12656
12657
12658
12659void QWidget::ungrabGesture(Qt::GestureType gesture)
12663 if (d->gestureContext.remove(gesture)) {
12664 if (QGestureManager *manager = QGestureManager::instance())
12665 manager->cleanupCachedGestures(
this, gesture);
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683void QWidget::destroy(
bool destroyWindow,
bool destroySubWindows)
12687 d->aboutToDestroy();
12688 if (!isWindow() && parentWidget())
12689 parentWidget()->d_func()->invalidateBackingStore(d->effectiveRectFor(geometry()));
12690 d->deactivateWidgetCleanup();
12692 if ((windowType() == Qt::Popup) &&
qApp)
12693 qApp->d_func()->closePopup(
this);
12695 if (
this ==
qApp->activeWindow())
12696 QApplicationPrivate::setActiveWindow(
nullptr);
12697 if (QWidget::mouseGrabber() ==
this)
12699 if (QWidget::keyboardGrabber() ==
this)
12702 setAttribute(Qt::WA_WState_Created,
false);
12704 if (destroySubWindows) {
12705 QObjectList childList(children());
12706 for (
int i = 0; i < childList.size(); i++) {
12707 QWidget *widget = qobject_cast<QWidget *>(childList.at(i));
12708 if (widget && widget->testAttribute(Qt::WA_NativeWindow)) {
12709 if (widget->windowHandle()) {
12715 if (destroyWindow) {
12716 d->deleteTLSysExtra();
12718 if (parentWidget() && parentWidget()->testAttribute(Qt::WA_WState_Created)) {
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736QPaintEngine *QWidget::paintEngine()
const
12738 qWarning(
"QWidget::paintEngine: Should no longer be called");
12755 const_cast<QWidgetPrivate *>(d_func())->noPaintOnScreen = 1;
12764 return window->handle() && !qt_window_private(window)->resizeEventPending;
12767#if QT_CONFIG(graphicsview)
12768static inline QGraphicsProxyWidget *graphicsProxyWidget(
const QWidget *w)
12770 QGraphicsProxyWidget *result =
nullptr;
12771 const QWidgetPrivate *d = qt_widget_private(
const_cast<QWidget *>(w));
12773 result = d->extra->proxyWidget;
12787 for ( ; w ; w = w->parentWidget()) {
12788#if QT_CONFIG(graphicsview)
12789 if (QGraphicsProxyWidget *qgpw = graphicsProxyWidget(w)) {
12790 if (
const QGraphicsScene *scene = qgpw->scene()) {
12791 const QList <QGraphicsView *> views = scene->views();
12792 if (!views.isEmpty()) {
12793 auto *viewP =
static_cast<QGraphicsViewPrivate *>(qt_widget_private(views.constFirst()));
12794 result.transform *= viewP->mapToViewTransform(qgpw);
12795 w = views.first()->viewport();
12800 QWindow *window = w->windowHandle();
12806 const auto &geometry = w->geometry();
12807 result.transform *= QTransform::fromTranslate(geometry.x(), geometry.y());
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824QPointF QWidget::mapToGlobal(
const QPointF &pos)
const
12826 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12827 const QPointF g = t.transform.map(pos);
12828 return t.window ? t.window->mapToGlobal(g) : g;
12832
12833
12834QPoint QWidget::mapToGlobal(
const QPoint &pos)
const
12836 return mapToGlobal(QPointF(pos)).toPoint();
12840
12841
12842
12843
12844
12845
12846
12847
12848QPointF QWidget::mapFromGlobal(
const QPointF &pos)
const
12850 const MapToGlobalTransformResult t = mapToGlobalTransform(
this);
12851 const QPointF windowLocal = t.window ? t.window->mapFromGlobal(pos) : pos;
12852 return t.transform.inverted().map(windowLocal);
12856
12857
12858QPoint QWidget::mapFromGlobal(
const QPoint &pos)
const
12860 return mapFromGlobal(QPointF(pos)).toPoint();
12869 QWindow *window = w->windowHandle();
12871 if (
const QWidget *nativeParent = w->nativeParentWidget())
12872 window = nativeParent->windowHandle();
12876#ifndef QT_NO_CURSOR
12887#ifndef QT_NO_CURSOR
12890 QGuiApplication::setOverrideCursor(*cursor);
12893 window->setMouseGrabEnabled(
true);
12903#ifndef QT_NO_CURSOR
12905 QGuiApplication::restoreOverrideCursor();
12909 window->setMouseGrabEnabled(
false);
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943void QWidget::grabMouse()
12945 grabMouseForWidget(
this);
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964#ifndef QT_NO_CURSOR
12965void QWidget::grabMouse(
const QCursor &cursor)
12967 grabMouseForWidget(
this, &cursor);
12971bool QWidgetPrivate::stealMouseGrab(
bool grab)
12976 QWindow *window = grabberWindow(q);
12977 return window ? window->setMouseGrabEnabled(grab) :
false;
12981
12982
12983
12984
12985
12986
12987void QWidget::releaseMouse()
12989 releaseMouseGrabOfWidget(
this);
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011void QWidget::grabKeyboard()
13014 keyboardGrb->releaseKeyboard();
13015 if (QWindow *window = grabberWindow(
this))
13016 window->setKeyboardGrabEnabled(
true);
13017 keyboardGrb =
this;
13020bool QWidgetPrivate::stealKeyboardGrab(
bool grab)
13025 QWindow *window = grabberWindow(q);
13026 return window ? window->setKeyboardGrabEnabled(grab) :
false;
13030
13031
13032
13033
13034
13035
13036void QWidget::releaseKeyboard()
13038 if (keyboardGrb ==
this) {
13039 if (QWindow *window = grabberWindow(
this))
13040 window->setKeyboardGrabEnabled(
false);
13041 keyboardGrb =
nullptr;
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055QWidget *QWidget::mouseGrabber()
13057 return qt_mouseGrb;
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070QWidget *QWidget::keyboardGrabber()
13072 return keyboardGrb;
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099void QWidget::activateWindow()
13101 QWindow *
const wnd = window()->windowHandle();
13104 wnd->requestActivate();
13108
13109
13110
13111
13112
13113
13114int QWidget::metric(PaintDeviceMetric m)
const
13116 QScreen *screen =
this->screen();
13119 if (m == PdmDpiX || m == PdmDpiY)
13121 return QPaintDevice::metric(m);
13124 auto resolveDevicePixelRatio = [
this, screen]() -> qreal {
13127 static bool downscale = qEnvironmentVariableIntValue(
"QT_WIDGETS_HIGHDPI_DOWNSCALE") > 0;
13128 QWindow *window =
this->window()->windowHandle();
13130 return downscale ? std::ceil(window->devicePixelRatio()) : window->devicePixelRatio();
13131 return screen->devicePixelRatio();
13136 return data->crect.width();
13138 return data->crect.width() * screen->physicalSize().width() / screen->geometry().width();
13140 return data->crect.height();
13142 return data->crect.height() * screen->physicalSize().height() / screen->geometry().height();
13144 return screen->depth();
13146 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13147 if (p->d_func()->extra && p->d_func()->extra->customDpiX)
13148 return p->d_func()->extra->customDpiX;
13150 return qRound(screen->logicalDotsPerInchX());
13152 for (
const QWidget *p =
this; p; p = p->parentWidget()) {
13153 if (p->d_func()->extra && p->d_func()->extra->customDpiY)
13154 return p->d_func()->extra->customDpiY;
13156 return qRound(screen->logicalDotsPerInchY());
13157 case PdmPhysicalDpiX:
13158 return qRound(screen->physicalDotsPerInchX());
13159 case PdmPhysicalDpiY:
13160 return qRound(screen->physicalDotsPerInchY());
13161 case PdmDevicePixelRatio:
13162 return resolveDevicePixelRatio();
13163 case PdmDevicePixelRatioScaled:
13164 return QPaintDevice::devicePixelRatioFScale() * resolveDevicePixelRatio();
13165 case PdmDevicePixelRatioF_EncodedA:
13167 case PdmDevicePixelRatioF_EncodedB:
13168 return QPaintDevice::encodeMetricF(m, resolveDevicePixelRatio());
13172 return QPaintDevice::metric(m);
13176
13177
13178
13179
13180void QWidget::initPainter(QPainter *painter)
const
13182 const QPalette &pal = palette();
13183 QPainterPrivate *painterPrivate = QPainterPrivate::get(painter);
13185 painterPrivate->state->pen = QPen(pal.brush(foregroundRole()), 1);
13186 painterPrivate->state->bgBrush = pal.brush(backgroundRole());
13187 QFont f(font(),
this);
13188 painterPrivate->state->deviceFont = f;
13189 painterPrivate->state->font = f;
13191 painterPrivate->setEngineDirtyFlags({
13192 QPaintEngine::DirtyPen,
13193 QPaintEngine::DirtyBrush,
13194 QPaintEngine::DirtyFont,
13197 if (painterPrivate->extended)
13198 painterPrivate->extended->penChanged();
13202
13203
13204
13205
13206QPaintDevice *QWidget::redirected(QPoint *offset)
const
13208 return d_func()->redirected(offset);
13212
13213
13214
13215
13216QPainter *QWidget::sharedPainter()
const
13219 if (!d_func()->redirectDev)
13222 QPainter *sp = d_func()->sharedPainter();
13223 if (!sp || !sp->isActive())
13226 if (sp->paintEngine()->paintDevice() != d_func()->redirectDev)
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254void QWidget::setMask(
const QRegion &newMask)
13259 if (newMask == d->extra->mask)
13262#ifndef QT_NO_BACKINGSTORE
13263 const QRegion oldMask(d->extra->mask);
13266 d->extra->mask = newMask;
13267 d->extra->hasMask = !newMask.isEmpty();
13269 if (!testAttribute(Qt::WA_WState_Created))
13272 d->setMask_sys(newMask);
13274#ifndef QT_NO_BACKINGSTORE
13278 if (!d->extra->hasMask) {
13280 QRegion expose(rect());
13282 if (!expose.isEmpty()) {
13283 d->setDirtyOpaqueRegion();
13291 QRegion parentExpose(rect());
13292 parentExpose -= newMask;
13293 if (!parentExpose.isEmpty()) {
13294 d->setDirtyOpaqueRegion();
13295 parentExpose.translate(data->crect.topLeft());
13296 parentWidget()->update(parentExpose);
13300 if (!oldMask.isEmpty())
13301 update(newMask - oldMask);
13306void QWidgetPrivate::setMask_sys(
const QRegion ®ion)
13309 if (QWindow *window = q->windowHandle())
13310 window->setMask(region);
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338void QWidget::setMask(
const QBitmap &bitmap)
13340 setMask(QRegion(bitmap));
13344
13345
13346
13347
13348
13349
13350void QWidget::clearMask()
13353 if (!d->extra || !d->extra->hasMask)
13355 setMask(QRegion());
13358void QWidgetPrivate::setWidgetParentHelper(QObject *widgetAsObject, QObject *newParent)
13360 Q_ASSERT(widgetAsObject->isWidgetType());
13361 Q_ASSERT(!newParent || newParent->isWidgetType());
13362 QWidget *widget =
static_cast<QWidget*>(widgetAsObject);
13363 widget->setParent(
static_cast<QWidget*>(newParent));
13366std::string QWidgetPrivate::flagsForDumping()
const
13368 Q_Q(
const QWidget);
13369 std::string flags = QObjectPrivate::flagsForDumping();
13370 if (QApplication::focusWidget() == q)
13372 if (q->isVisible()) {
13373 std::stringstream s;
13375 << q->width() <<
'x' << q->height()
13376 << std::showpos << q->x() << q->y()
13385void QWidgetPrivate::setNetWmWindowTypes(
bool skipIfMissing)
13390 if (!q->windowHandle())
13393 QXcbWindow::WindowTypes wmWindowType = QXcbWindow::None;
13394 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDesktop))
13395 wmWindowType |= QXcbWindow::Desktop;
13396 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDock))
13397 wmWindowType |= QXcbWindow::Dock;
13398 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolBar))
13399 wmWindowType |= QXcbWindow::Toolbar;
13400 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeMenu))
13401 wmWindowType |= QXcbWindow::Menu;
13402 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeUtility))
13403 wmWindowType |= QXcbWindow::Utility;
13404 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeSplash))
13405 wmWindowType |= QXcbWindow::Splash;
13406 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDialog))
13407 wmWindowType |= QXcbWindow::Dialog;
13408 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu))
13409 wmWindowType |= QXcbWindow::DropDownMenu;
13410 if (q->testAttribute(Qt::WA_X11NetWmWindowTypePopupMenu))
13411 wmWindowType |= QXcbWindow::PopupMenu;
13412 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip))
13413 wmWindowType |= QXcbWindow::Tooltip;
13414 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeNotification))
13415 wmWindowType |= QXcbWindow::Notification;
13416 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeCombo))
13417 wmWindowType |= QXcbWindow::Combo;
13418 if (q->testAttribute(Qt::WA_X11NetWmWindowTypeDND))
13419 wmWindowType |= QXcbWindow::Dnd;
13421 if (wmWindowType == QXcbWindow::None && skipIfMissing)
13424 if (
auto *xcbWindow =
dynamic_cast<QXcbWindow*>(q->windowHandle()->handle()))
13425 xcbWindow->setWindowType(wmWindowType);
13427 Q_UNUSED(skipIfMissing);
13432
13433
13434
13435
13436bool QWidgetPrivate::hasChildWithFocusPolicy(Qt::FocusPolicy policy,
const QWidget *excludeChildrenOf)
const
13438 Q_Q(
const QWidget);
13439 const QWidgetList &children = q->findChildren<QWidget *>(Qt::FindChildrenRecursively);
13440 for (
const auto *child : children) {
13441 if (child->focusPolicy() == policy && child->isEnabled()
13442 && (!excludeChildrenOf || !excludeChildrenOf->isAncestorOf(child))) {
13449#ifndef QT_NO_DEBUG_STREAM
13452QDebug operator<<(QDebug debug,
const WidgetAttributes &attributes)
13454 const QDebugStateSaver saver(debug);
13457 if (
const QWidget *widget = attributes.widget) {
13458 const QMetaObject *qtMo = qt_getEnumMetaObject(Qt::WA_AttributeCount);
13459 const QMetaEnum me = qtMo->enumerator(qtMo->indexOfEnumerator(
"WidgetAttribute"));
13461 for (
int a = 0; a < Qt::WA_AttributeCount; ++a) {
13462 if (widget->testAttribute(
static_cast<Qt::WidgetAttribute>(a))) {
13465 debug << me.valueToKey(a);
13476 const QDebugStateSaver saver(debug);
13479 debug << widget->metaObject()->className() <<
'(' << (
const void *)widget;
13480 if (!widget->objectName().isEmpty())
13481 debug <<
", name=" << widget->objectName();
13482 if (debug.verbosity() > 2) {
13483 const QRect geometry = widget->geometry();
13484 const QRect frameGeometry = widget->frameGeometry();
13485 if (widget->isVisible())
13486 debug <<
", visible";
13487 if (!widget->isEnabled())
13488 debug <<
", disabled";
13489 debug <<
", states=" << widget->windowState()
13490 <<
", type=" << widget->windowType() <<
", flags=" << widget->windowFlags();
13491 debug <<
", attributes=" << WidgetAttributes{widget};
13492 if (widget->isWindow())
13493 debug <<
", window";
13494 debug <<
", " << geometry.width() <<
'x' << geometry.height()
13495 << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign;
13496 if (frameGeometry != geometry) {
13497 const QMargins margins(geometry.x() - frameGeometry.x(),
13498 geometry.y() - frameGeometry.y(),
13499 frameGeometry.right() - geometry.right(),
13500 frameGeometry.bottom() - geometry.bottom());
13501 debug <<
", margins=" << margins;
13503 debug <<
", devicePixelRatio=" << widget->devicePixelRatio();
13504 if (
const WId wid = widget->internalWinId())
13505 debug <<
", winId=0x" << Qt::hex << wid << Qt::dec;
13509 debug <<
"QWidget(0x0)";
13518#define FOCUS_NEXT(w) w->d_func()->focus_next
13519#define FOCUS_PREV(w) w->d_func()->focus_prev
13522
13523
13524
13525
13526QWidget *QWidgetPrivate::nextPrevElementInFocusChain(FocusDirection direction)
const
13528 Q_Q(
const QWidget);
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543bool QWidgetPrivate::removeFromFocusChain(FocusChainRemovalRules rules, FocusDirection direction)
13546 if (!isFocusChainConsistent()) {
13548 if (rules.testFlag(FocusChainRemovalRule::AssertConsistency))
13549 qFatal() << q <<
"has inconsistent focus chain.";
13551 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because of inconsistent focus chain.";
13555 if (!isInFocusChain()) {
13556 qCDebug(lcWidgetFocus) << q <<
"wasn't removed, because it is not part of a focus chain.";
13560 if (rules.testFlag(FocusChainRemovalRule::EnsureFocusOut))
13561 q->focusNextPrevChild(direction == FocusDirection::Next);
13566 qCDebug(lcWidgetFocus) << q <<
"removed from focus chain.";
13571
13572
13573
13574void QWidgetPrivate::initFocusChain()
13577 qCDebug(lcWidgetFocus) <<
"Initializing focus chain of" << q;
13583
13584
13585
13586
13587
13588
13589void QWidgetPrivate::reparentFocusChildren(FocusDirection direction)
13594 QWidget *firstOld =
nullptr;
13595 QWidget *lastOld =
nullptr;
13596 QWidget *lastNew = q;
13597 bool prevWasNew =
true;
13598 QWidget *widget = nextPrevElementInFocusChain(direction);
13603 while (widget != q) {
13604 bool currentIsNew = q->isAncestorOf(widget);
13605 if (currentIsNew) {
13625 widget = widget->d_func()->nextPrevElementInFocusChain(direction);
13626 prevWasNew = currentIsNew;
13635 if (!q->isWindow()) {
13636 QWidget *topLevel = q->window();
13651
13652
13653
13654
13655
13656bool QWidgetPrivate::insertIntoFocusChain(FocusDirection direction, QWidget *position)
13659 Q_ASSERT(position);
13663 switch (direction) {
13664 case FocusDirection::Next:
13665 if (previous == position) {
13666 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already before" << position;
13670 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13676 qCDebug(lcWidgetFocus) << q <<
"inserted after" << position;
13679 case FocusDirection::Previous:
13680 if (next == position) {
13681 qCDebug(lcWidgetFocus) <<
"No-op insertion." << q <<
"is already after" << position;
13685 removeFromFocusChain(FocusChainRemovalRule::AssertConsistency);
13691 qCDebug(lcWidgetFocus) << q <<
"inserted before" << position;
13695 Q_ASSERT(isFocusChainConsistent());
13700
13701
13702
13703
13704
13705
13706
13707bool QWidgetPrivate::insertIntoFocusChain(
const QWidgetList &toBeInserted,
13708 FocusDirection direction, QWidget *position)
13710 if (toBeInserted.isEmpty()) {
13711 qCDebug(lcWidgetFocus) <<
"No-op insertion of an empty list";
13715 Q_ASSERT_X(!toBeInserted.contains(position),
13717 "Coding error: toBeInserted contains position");
13719 QWidget *first = toBeInserted.constFirst();
13720 QWidget *last = toBeInserted.constLast();
13723 if (toBeInserted.count() == 1)
13724 return first->d_func()->insertIntoFocusChain(direction, position);
13726 Q_ASSERT(first != last);
13727 switch (direction) {
13728 case FocusDirection::Previous:
13730 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already before"
13738 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted before" << position;
13740 case FocusDirection::Next:
13742 qCDebug(lcWidgetFocus) <<
"No-op insertion." << toBeInserted <<
"is already after"
13750 qCDebug(lcWidgetFocus) << toBeInserted <<
"inserted after" << position;
13754 Q_ASSERT(position->d_func()->isFocusChainConsistent());
13759
13760
13761
13762
13765 QWidgetList path({from});
13771 switch (direction) {
13772 case QWidgetPrivate::FocusDirection::Previous:
13773 current = current->previousInFocusChain();
13775 case QWidgetPrivate::FocusDirection::Next:
13776 current = current->nextInFocusChain();
13779 if (path.contains(current))
13780 return QWidgetList();
13782 }
while (current != to);
13788
13789
13790
13791
13792
13793QWidgetList QWidgetPrivate::takeFromFocusChain(QWidget *from,
13795 FocusDirection direction)
13798 const QWidgetList path = focusPath(from, to , direction);
13799 if (path.isEmpty()) {
13800 qCDebug(lcWidgetFocus) <<
"No-op removal. Focus chain from" << from <<
"doesn't lead to " << to;
13801 return QWidgetList();
13804 QWidget *first = path.constFirst();
13805 QWidget *last = path.constLast();
13806 if (first == last) {
13807 first->d_func()->removeFromFocusChain();
13808 return QWidgetList({first});
13815 qCDebug(lcWidgetFocus) << path <<
"removed from focus chain";
13820
13821
13822
13823
13824QWidget *QWidgetPrivate::determineLastFocusChild(QWidget *noFurtherThan)
13831 QWidget *lastFocusChild = q;
13833 QWidget *focusProxy = deepestFocusProxy();
13840 for (
auto *object : std::as_const(q->children())) {
13841 QWidget *w = qobject_cast<QWidget *>(object);
13842 if (w && w->focusProxy() == q) {
13843 lastFocusChild = w;
13847 }
else if (q->isAncestorOf(focusProxy)) {
13848 lastFocusChild = focusProxy;
13849 for (QWidget *focusNext = lastFocusChild->nextInFocusChain();
13850 focusNext != focusProxy && q->isAncestorOf(focusNext)
13851 && focusNext->window() == focusProxy->window();
13852 focusNext = focusNext->nextInFocusChain()) {
13853 if (focusNext == noFurtherThan)
13855 if (focusNext->focusPolicy() != Qt::NoFocus)
13856 lastFocusChild = focusNext;
13859 return lastFocusChild;
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874bool QWidgetPrivate::isInFocusChain()
const
13876 Q_Q(
const QWidget);
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902bool QWidgetPrivate::isFocusChainConsistent()
const
13904 Q_Q(
const QWidget);
13905 const bool skip = !QLoggingCategory(
"qt.widgets.focus").isDebugEnabled();
13909 if (!isInFocusChain())
13912 const QWidget *position = q;
13914 for (
int i = 0; i < QApplication::allWidgets().count(); ++i) {
13916 qCDebug(lcWidgetFocus) <<
"Nullptr found at:" << position
13917 <<
"Previous pointing to" <<
FOCUS_PREV(position)
13918 <<
"Next pointing to" <<
FOCUS_NEXT(position);
13923 qCDebug(lcWidgetFocus) <<
"Inconsistent focus chain at:" << position
13934 qCDebug(lcWidgetFocus) <<
"Focus chain leading from" << q <<
"to" << position <<
"is not closed.";
13944#include "moc_qwidget.cpp"
13945#include "moc_qwidget_p.cpp"