diff options
Diffstat (limited to 'src/widgets/kernel/qwidget.cpp')
-rw-r--r-- | src/widgets/kernel/qwidget.cpp | 237 |
1 files changed, 139 insertions, 98 deletions
diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index 353a80d1c5..f0230f4f32 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -79,6 +79,7 @@ #include "private/qstylesheetstyle_p.h" #include "private/qstyle_p.h" #include "qfileinfo.h" +#include "qscopeguard.h" #include <QtGui/private/qhighdpiscaling_p.h> #include <QtGui/qinputmethod.h> #include <QtGui/qopenglcontext.h> @@ -358,7 +359,7 @@ void QWidgetPrivate::scrollChildren(int dx, int dy) w->d_func()->setWSGeometry(); w->d_func()->setDirtyOpaqueRegion(); QMoveEvent e(r.topLeft(), oldp); - QApplication::sendEvent(w, &e); + QCoreApplication::sendEvent(w, &e); } } } @@ -392,7 +393,7 @@ QPointer<QWidget> QWidgetPrivate::editingWidget; This feature is only available in Qt for Embedded Linux. - \sa setEditFocus(), QApplication::keypadNavigationEnabled() + \sa setEditFocus(), QApplication::navigationMode() */ bool QWidget::hasEditFocus() const { @@ -412,7 +413,7 @@ bool QWidget::hasEditFocus() const This feature is only available in Qt for Embedded Linux. - \sa hasEditFocus(), QApplication::keypadNavigationEnabled() + \sa hasEditFocus(), QApplication::navigationMode() */ void QWidget::setEditFocus(bool on) { @@ -434,13 +435,13 @@ void QWidget::setEditFocus(bool on) if (!on && QWidgetPrivate::editingWidget == f) { QWidgetPrivate::editingWidget = 0; QEvent event(QEvent::LeaveEditFocus); - QApplication::sendEvent(f, &event); - QApplication::sendEvent(f->style(), &event); + QCoreApplication::sendEvent(f, &event); + QCoreApplication::sendEvent(f->style(), &event); } else if (on) { QWidgetPrivate::editingWidget = f; QEvent event(QEvent::EnterEditFocus); - QApplication::sendEvent(f, &event); - QApplication::sendEvent(f->style(), &event); + QCoreApplication::sendEvent(f, &event); + QCoreApplication::sendEvent(f->style(), &event); } } #endif @@ -1123,6 +1124,8 @@ void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w) void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f) { Q_Q(QWidget); + Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO, "Cannot parent a QWidget to itself"); + if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance()))) qFatal("QWidget: Cannot create a QWidget without QApplication"); @@ -1220,8 +1223,8 @@ void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f) q->create(); QEvent e(QEvent::Create); - QApplication::sendEvent(q, &e); - QApplication::postEvent(q, new QEvent(QEvent::PolishRequest)); + QCoreApplication::sendEvent(q, &e); + QCoreApplication::postEvent(q, new QEvent(QEvent::PolishRequest)); extraPaintEngine = 0; @@ -1249,6 +1252,35 @@ void QWidgetPrivate::createRecursively() } } +QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode) const +{ + if (mode == WindowHandleMode::Direct || mode == WindowHandleMode::Closest) { + if (QTLWExtra *x = maybeTopData()) { + if (x->window != nullptr || mode == WindowHandleMode::Direct) + return x->window; + } + } + if (mode == WindowHandleMode::Closest) { + if (auto nativeParent = q_func()->nativeParentWidget()) { + if (auto window = nativeParent->windowHandle()) + return window; + } + } + if (mode == WindowHandleMode::TopLevel || mode == WindowHandleMode::Closest) { + if (auto topLevel = q_func()->topLevelWidget()) { + if (auto window = topLevel ->windowHandle()) + return window; + } + } + return nullptr; +} + +QScreen *QWidgetPrivate::associatedScreen() const +{ + if (auto window = windowHandle(WindowHandleMode::Closest)) + return window->screen(); + return nullptr; +} // ### fixme: Qt 6: Remove parameter window from QWidget::create() @@ -1340,11 +1372,6 @@ void QWidget::create(WId window, bool initializeWindow, bool destroyOldWindow) if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)) setAttribute(Qt::WA_DropSiteRegistered, true); -#ifdef QT_EVAL - extern void qt_eval_init_widget(QWidget *w); - qt_eval_init_widget(this); -#endif - // need to force the resting of the icon after changing parents if (testAttribute(Qt::WA_SetWindowIcon)) d->setWindowIcon_sys(); @@ -1587,7 +1614,7 @@ QWidget::~QWidget() // Remove all shortcuts grabbed by this // widget, unless application is closing if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut)) - qApp->d_func()->shortcutMap.removeShortcut(0, this, QKeySequence()); + QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(0, this, QKeySequence()); #endif // delete layout while we still are a valid widget @@ -1695,7 +1722,7 @@ QWidget::~QWidget() if (!d->children.isEmpty()) d->deleteChildren(); - QApplication::removePostedEvents(this); + QCoreApplication::removePostedEvents(this); QT_TRY { destroy(); // platform-dependent cleanup @@ -2007,7 +2034,7 @@ void QWidgetPrivate::propagatePaletteChange() QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles); QEvent pc(QEvent::PaletteChange); - QApplication::sendEvent(q, &pc); + QCoreApplication::sendEvent(q, &pc); for (int i = 0; i < children.size(); ++i) { QWidget *w = qobject_cast<QWidget*>(children.at(i)); if (w && (!w->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) @@ -2811,7 +2838,7 @@ void QWidgetPrivate::setStyle_helper(QStyle *newStyle, bool propagate) #endif QEvent e(QEvent::StyleChange); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); #ifndef QT_NO_STYLE_STYLESHEET // dereference the old stylesheet style @@ -3029,7 +3056,7 @@ void QWidget::overrideWindowState(Qt::WindowStates newstate) { QWindowStateChangeEvent e(Qt::WindowStates(data->window_state), true); data->window_state = newstate; - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } /*! @@ -3091,7 +3118,7 @@ void QWidget::setWindowState(Qt::WindowStates newstate) activateWindow(); QWindowStateChangeEvent e(oldstate); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } /*! @@ -3317,7 +3344,7 @@ void QWidget::insertAction(QAction *before, QAction *action) apriv->widgets.append(this); QActionEvent e(QEvent::ActionAdded, action, before); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } /*! @@ -3355,7 +3382,7 @@ void QWidget::removeAction(QAction *action) if (d->actions.removeAll(action)) { QActionEvent e(QEvent::ActionRemoved, action); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } } @@ -3462,7 +3489,7 @@ void QWidgetPrivate::setEnabled_helper(bool enable) } #endif //QT_NO_IM QEvent e(QEvent::EnabledChange); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } /*! @@ -4529,7 +4556,7 @@ void QWidget::setForegroundRole(QPalette::ColorRole role) the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color". - \sa QApplication::palette(), QWidget::font(), {Qt Style Sheets} + \sa QGuiApplication::palette(), QWidget::font(), {Qt Style Sheets} */ const QPalette &QWidget::palette() const { @@ -4592,7 +4619,7 @@ QPalette QWidgetPrivate::naturalWidgetPalette(uint inheritedMask) const )) { if (QWidget *p = q->parentWidget()) { if (!p->testAttribute(Qt::WA_StyleSheet) || useStyleSheetPropagationInWidgetStyles) { - if (!naturalPalette.isCopyOf(QApplication::palette())) { + if (!naturalPalette.isCopyOf(QGuiApplication::palette())) { QPalette inheritedPalette = p->palette(); inheritedPalette.resolve(inheritedMask); naturalPalette = inheritedPalette.resolve(naturalPalette); @@ -4858,7 +4885,7 @@ void QWidgetPrivate::updateFont(const QFont &font) #endif QEvent e(QEvent::FontChange); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction) @@ -4876,14 +4903,14 @@ void QWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction) } } QEvent e(QEvent::LayoutDirectionChange); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } void QWidgetPrivate::resolveLayoutDirection() { Q_Q(const QWidget); if (!q->testAttribute(Qt::WA_SetLayoutDirection)) - setLayoutDirection_helper(q->isWindow() ? QApplication::layoutDirection() : q->parentWidget()->layoutDirection()); + setLayoutDirection_helper(q->isWindow() ? QGuiApplication::layoutDirection() : q->parentWidget()->layoutDirection()); } /*! @@ -5003,7 +5030,7 @@ void QWidget::setCursor(const QCursor &cursor) d->setCursor_sys(cursor); QEvent event(QEvent::CursorChange); - QApplication::sendEvent(this, &event); + QCoreApplication::sendEvent(this, &event); } void QWidgetPrivate::setCursor_sys(const QCursor &cursor) @@ -5025,7 +5052,7 @@ void QWidget::unsetCursor() d->unsetCursor_sys(); QEvent event(QEvent::CursorChange); - QApplication::sendEvent(this, &event); + QCoreApplication::sendEvent(this, &event); } void QWidgetPrivate::unsetCursor_sys() @@ -5223,7 +5250,7 @@ void QWidget::render(QPainter *painter, const QPoint &targetOffset, static void sendResizeEvents(QWidget *target) { QResizeEvent e(target->size(), QSize()); - QApplication::sendEvent(target, &e); + QCoreApplication::sendEvent(target, &e); const QObjectList children = target->children(); for (int i = 0; i < children.size(); ++i) { @@ -5977,7 +6004,7 @@ void QWidgetPrivate::setLocale_helper(const QLocale &loc, bool forceUpdate) } } QEvent e(QEvent::LocaleChange); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } void QWidget::setLocale(const QLocale &locale) @@ -6063,13 +6090,7 @@ QString qt_setWindowTitle_helperHelper(const QString &title, const QWidget *widg { Q_ASSERT(widget); -#ifdef QT_EVAL - extern QString qt_eval_adapt_window_title(const QString &title); - QString cap = qt_eval_adapt_window_title(title); -#else QString cap = title; -#endif - if (cap.isEmpty()) return cap; @@ -6158,7 +6179,7 @@ void QWidget::setWindowIconText(const QString &iconText) d->setWindowIconText_helper(iconText); QEvent e(QEvent::IconTextChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); emit windowIconTextChanged(iconText); } @@ -6182,7 +6203,7 @@ void QWidget::setWindowTitle(const QString &title) d->setWindowTitle_helper(title); QEvent e(QEvent::WindowTitleChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); emit windowTitleChanged(title); } @@ -6220,11 +6241,11 @@ void QWidgetPrivate::setWindowIcon_helper() // QWidgetWindow to the top level QWidget ensures that the event reaches // the top level anyhow if (!q->windowHandle()) - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); for (int i = 0; i < children.size(); ++i) { QWidget *w = qobject_cast<QWidget *>(children.at(i)); if (w && !w->isWindow()) - QApplication::sendEvent(w, &e); + QCoreApplication::sendEvent(w, &e); } } @@ -6567,7 +6588,7 @@ void QWidget::setFocus(Qt::FocusReason reason) if (reason != Qt::NoFocusReason) { QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange, reason); - QApplication::sendEvent(prev, &focusAboutToChange); + QCoreApplication::sendEvent(prev, &focusAboutToChange); } } @@ -6594,9 +6615,9 @@ void QWidget::setFocus(Qt::FocusReason reason) // Send event to self QFocusEvent event(QEvent::FocusOut, reason); QPointer<QWidget> that = previousProxyFocus; - QApplication::sendEvent(previousProxyFocus, &event); + QCoreApplication::sendEvent(previousProxyFocus, &event); if (that) - QApplication::sendEvent(that->style(), &event); + QCoreApplication::sendEvent(that->style(), &event); } if (!isHidden()) { #if QT_CONFIG(graphicsview) @@ -6608,9 +6629,9 @@ void QWidget::setFocus(Qt::FocusReason reason) // Send event to self QFocusEvent event(QEvent::FocusIn, reason); QPointer<QWidget> that = f; - QApplication::sendEvent(f, &event); + QCoreApplication::sendEvent(f, &event); if (that) - QApplication::sendEvent(that->style(), &event); + QCoreApplication::sendEvent(that->style(), &event); } } } @@ -6717,7 +6738,7 @@ void QWidget::clearFocus() QGuiApplication::inputMethod()->commit(); QFocusEvent focusAboutToChange(QEvent::FocusAboutToChange); - QApplication::sendEvent(this, &focusAboutToChange); + QCoreApplication::sendEvent(this, &focusAboutToChange); } QWidget *w = this; @@ -7364,11 +7385,11 @@ void QWidgetPrivate::setGeometry_sys(int x, int y, int w, int h, bool isMove) if (isMove) { QMoveEvent e(q->pos(), oldPos); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } if (isResize) { QResizeEvent e(r.size(), olds); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); if (q->windowHandle()) q->update(); } @@ -7663,13 +7684,13 @@ void QWidgetPrivate::updateContentsRect() if (q->isVisible()) { q->update(); QResizeEvent e(q->data->crect.size(), q->data->crect.size()); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } else { q->setAttribute(Qt::WA_PendingResizeEvent, true); } QEvent e(QEvent::ContentsRectChange); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); } /*! @@ -7965,13 +7986,13 @@ void QWidgetPrivate::sendPendingMoveAndResizeEvents(bool recursive, bool disable if (q->testAttribute(Qt::WA_PendingMoveEvent)) { QMoveEvent e(data.crect.topLeft(), data.crect.topLeft()); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); q->setAttribute(Qt::WA_PendingMoveEvent, false); } if (q->testAttribute(Qt::WA_PendingResizeEvent)) { QResizeEvent e(data.crect.size(), QSize()); - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); q->setAttribute(Qt::WA_PendingResizeEvent, false); } @@ -8081,7 +8102,7 @@ void QWidgetPrivate::show_helper() // send the show event before showing the window QShowEvent showEvent; - QApplication::sendEvent(q, &showEvent); + QCoreApplication::sendEvent(q, &showEvent); show_sys(); @@ -8104,7 +8125,7 @@ void QWidgetPrivate::show_helper() // is spinnning; otherwise it might not show up on particular platforms. // This makes QSplashScreen behave the same on all platforms. if (!qApp->d_func()->in_exec && q->windowType() == Qt::SplashScreen) - QApplication::processEvents(); + QCoreApplication::processEvents(); data.in_show = false; // reset qws optimization } @@ -8113,7 +8134,7 @@ void QWidgetPrivate::show_sys() { Q_Q(QWidget); - QWidgetWindow *window = windowHandle(); + auto window = qobject_cast<QWidgetWindow *>(windowHandle()); if (q->testAttribute(Qt::WA_DontShowOnScreen)) { invalidateBackingStore(q->rect()); @@ -8130,9 +8151,9 @@ void QWidgetPrivate::show_sys() } if (renderToTexture && !q->isWindow()) - QApplication::postEvent(q->parentWidget(), new QUpdateLaterEvent(q->geometry())); + QCoreApplication::postEvent(q->parentWidget(), new QUpdateLaterEvent(q->geometry())); else - QApplication::postEvent(q, new QUpdateLaterEvent(q->rect())); + QCoreApplication::postEvent(q, new QUpdateLaterEvent(q->rect())); if ((!q->isWindow() && !q->testAttribute(Qt::WA_NativeWindow)) || q->testAttribute(Qt::WA_OutsideWSRange)) { @@ -8220,7 +8241,7 @@ void QWidgetPrivate::hide_helper() } QHideEvent hideEvent; - QApplication::sendEvent(q, &hideEvent); + QCoreApplication::sendEvent(q, &hideEvent); hideChildren(false); // next bit tries to move the focus if the focus widget is now @@ -8252,7 +8273,7 @@ void QWidgetPrivate::hide_sys() { Q_Q(QWidget); - QWidgetWindow *window = windowHandle(); + auto window = qobject_cast<QWidgetWindow *>(windowHandle()); if (q->testAttribute(Qt::WA_DontShowOnScreen)) { q->setAttribute(Qt::WA_Mapped, false); @@ -8391,7 +8412,7 @@ void QWidgetPrivate::setVisible(bool visible) } QEvent showToParentEvent(QEvent::ShowToParent); - QApplication::sendEvent(q, &showToParentEvent); + QCoreApplication::sendEvent(q, &showToParentEvent); } else { // hide #if 0 // Used to be included in Qt4 for Q_WS_WIN // reset WS_DISABLED style in a Blocked window @@ -8422,11 +8443,11 @@ void QWidgetPrivate::setVisible(bool visible) if (q->parentWidget()->d_func()->layout) q->parentWidget()->d_func()->layout->invalidate(); else if (q->parentWidget()->isVisible()) - QApplication::postEvent(q->parentWidget(), new QEvent(QEvent::LayoutRequest)); + QCoreApplication::postEvent(q->parentWidget(), new QEvent(QEvent::LayoutRequest)); } QEvent hideToParentEvent(QEvent::HideToParent); - QApplication::sendEvent(q, &hideToParentEvent); + QCoreApplication::sendEvent(q, &hideToParentEvent); } } @@ -8501,7 +8522,7 @@ void QWidgetPrivate::hideChildren(bool spontaneous) if (spontaneous) { QApplication::sendSpontaneousEvent(widget, &e); } else { - QApplication::sendEvent(widget, &e); + QCoreApplication::sendEvent(widget, &e); if (widget->internalWinId() && widget->testAttribute(Qt::WA_DontCreateNativeAncestors)) { // hide_sys() on an ancestor won't have any affect on this @@ -8536,7 +8557,7 @@ bool QWidgetPrivate::close_helper(CloseMode mode) if (mode == CloseWithSpontaneousEvent) QApplication::sendSpontaneousEvent(q, &e); else - QApplication::sendEvent(q, &e); + QCoreApplication::sendEvent(q, &e); if (!that.isNull() && !e.isAccepted()) { data.is_closing = 0; return false; @@ -9051,7 +9072,7 @@ bool QWidget::event(QEvent *event) setAttribute(Qt::WA_WState_Polished); if (!QApplication::font(this).isCopyOf(QApplication::font())) d->resolveFont(); - if (!QApplication::palette(this).isCopyOf(QApplication::palette())) + if (!QApplication::palette(this).isCopyOf(QGuiApplication::palette())) d->resolvePalette(); } break; @@ -9075,7 +9096,7 @@ bool QWidget::event(QEvent *event) #if QT_CONFIG(statustip) if (d->statusTip.size()) { QStatusTipEvent tip(d->statusTip); - QApplication::sendEvent(const_cast<QWidget *>(this), &tip); + QCoreApplication::sendEvent(const_cast<QWidget *>(this), &tip); } #endif enterEvent(event); @@ -9086,7 +9107,7 @@ bool QWidget::event(QEvent *event) if (d->statusTip.size()) { QString empty; QStatusTipEvent tip(empty); - QApplication::sendEvent(const_cast<QWidget *>(this), &tip); + QCoreApplication::sendEvent(const_cast<QWidget *>(this), &tip); } #endif leaveEvent(event); @@ -9237,7 +9258,7 @@ bool QWidget::event(QEvent *event) for (int i = 0; i < childList.size(); ++i) { QWidget *w = qobject_cast<QWidget *>(childList.at(i)); if (w && w->isVisible() && !w->isWindow()) - QApplication::sendEvent(w, event); + QCoreApplication::sendEvent(w, event); } break; } @@ -9248,7 +9269,7 @@ bool QWidget::event(QEvent *event) for (int i = 0; i < childList.size(); ++i) { QObject *o = childList.at(i); if (o) - QApplication::sendEvent(o, event); + QCoreApplication::sendEvent(o, event); } } update(); @@ -9287,7 +9308,7 @@ bool QWidget::event(QEvent *event) QWidget *w = static_cast<QWidget *>(o); // do not forward the event to child windows; QApplication does this for us if (!w->isWindow()) - QApplication::sendEvent(w, event); + QCoreApplication::sendEvent(w, event); } } } @@ -9339,7 +9360,7 @@ bool QWidget::event(QEvent *event) for (int i = 0; i < childList.size(); ++i) { QWidget *w = qobject_cast<QWidget *>(childList.at(i)); if (w && w->isVisible() && !w->isWindow()) - QApplication::sendEvent(w, event); + QCoreApplication::sendEvent(w, event); } break; } @@ -9442,8 +9463,8 @@ void QWidget::changeEvent(QEvent * event) break; case QEvent::ThemeChange: - if (QApplication::desktopSettingsAware() && windowType() != Qt::Desktop - && qApp && !QApplication::closingDown()) { + if (QGuiApplication::desktopSettingsAware() && windowType() != Qt::Desktop + && qApp && !QCoreApplication::closingDown()) { if (testAttribute(Qt::WA_WState_Polished)) QApplication::style()->unpolish(this); if (testAttribute(Qt::WA_WState_Polished)) @@ -10170,7 +10191,11 @@ void QWidget::hideEvent(QHideEvent *) \endtable */ +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +bool QWidget::nativeEvent(const QByteArray &eventType, void *message, qintptr *result) +#else bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result) +#endif { Q_UNUSED(eventType); Q_UNUSED(message); @@ -10506,7 +10531,7 @@ void QWidgetPrivate::updateGeometry_helper(bool forceUpdate) if (parent->d_func()->layout) parent->d_func()->layout->invalidate(); else if (parent->isVisible()) - QApplication::postEvent(parent, new QEvent(QEvent::LayoutRequest)); + QCoreApplication::postEvent(parent, new QEvent(QEvent::LayoutRequest)); } } } @@ -10660,7 +10685,7 @@ static void sendWindowChangeToTextureChildrenRecursively(QWidget *widget) QWidgetPrivate *d = QWidgetPrivate::get(widget); if (d->renderToTexture) { QEvent e(QEvent::WindowChangeInternal); - QApplication::sendEvent(widget, &e); + QCoreApplication::sendEvent(widget, &e); } for (int i = 0; i < d->children.size(); ++i) { @@ -10680,6 +10705,22 @@ static void sendWindowChangeToTextureChildrenRecursively(QWidget *widget) void QWidget::setParent(QWidget *parent, Qt::WindowFlags f) { Q_D(QWidget); + Q_ASSERT_X(this != parent, Q_FUNC_INFO, "Cannot parent a QWidget to itself"); +#ifdef QT_DEBUG + const auto checkForParentChildLoops = qScopeGuard([&](){ + int depth = 0; + auto p = parentWidget(); + while (p) { + if (++depth == QObjectPrivate::CheckForParentChildLoopsWarnDepth) { + qWarning("QWidget %p (class: '%s', object name: '%s') may have a loop in its parent-child chain; " + "this is undefined behavior", + this, metaObject()->className(), qPrintable(objectName())); + } + p = p->parentWidget(); + } + }); +#endif + bool resized = testAttribute(Qt::WA_Resized); bool wasCreated = testAttribute(Qt::WA_WState_Created); QWidget *oldtlw = window(); @@ -10693,7 +10734,7 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f) bool newParent = (parent != parentWidget()) || !wasCreated || desktopWidget; if (newParent && parent && !desktopWidget) { - if (testAttribute(Qt::WA_NativeWindow) && !qApp->testAttribute(Qt::AA_DontCreateNativeWidgetSiblings)) + if (testAttribute(Qt::WA_NativeWindow) && !QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings)) parent->d_func()->enforceNativeChildren(); else if (parent->d_func()->nativeChildrenForced() || parent->testAttribute(Qt::WA_PaintOnScreen)) setAttribute(Qt::WA_NativeWindow); @@ -10706,7 +10747,7 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f) } if (newParent) { QEvent e(QEvent::ParentAboutToChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } } if (newParent && isAncestorOf(focusWidget())) @@ -10779,7 +10820,7 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f) // send and post remaining QObject events if (parent && d->sendChildEvents) { QChildEvent e(QEvent::ChildAdded, this); - QApplication::sendEvent(parent, &e); + QCoreApplication::sendEvent(parent, &e); } //### already hidden above ---> must probably do something smart on the mac @@ -10797,7 +10838,7 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f) } QEvent e(QEvent::ParentChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } #ifndef QT_NO_OPENGL //renderToTexture widgets also need to know when their top-level window changes @@ -11160,7 +11201,7 @@ void QWidgetPrivate::update(T r) return; if (q->testAttribute(Qt::WA_WState_InPaintEvent)) { - QApplication::postEvent(q, new QUpdateLaterEvent(clipped)); + QCoreApplication::postEvent(q, new QUpdateLaterEvent(clipped)); return; } @@ -11197,7 +11238,7 @@ void QWidgetPrivate::macUpdateSizeAttribute() { Q_Q(QWidget); QEvent event(QEvent::MacSizeChange); - QApplication::sendEvent(q, &event); + QCoreApplication::sendEvent(q, &event); for (int i = 0; i < children.size(); ++i) { QWidget *w = qobject_cast<QWidget *>(children.at(i)); if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation)) @@ -11253,7 +11294,7 @@ void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on) else if (!on && (isWindow() || !parentWidget() || !parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))) setAttribute(Qt::WA_DropSiteRegistered, false); QEvent e(QEvent::AcceptDropsChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); break; } case Qt::WA_DropSiteRegistered: { @@ -11336,11 +11377,11 @@ void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on) break; case Qt::WA_MouseTracking: { QEvent e(QEvent::MouseTrackingChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); break; } case Qt::WA_TabletTracking: { QEvent e(QEvent::TabletTrackingChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); break; } case Qt::WA_NativeWindow: { d->createTLExtra(); @@ -11353,7 +11394,7 @@ void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on) QGuiApplication::inputMethod()->commit(); QGuiApplication::inputMethod()->update(Qt::ImEnabled); } - if (!qApp->testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget()) + if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings) && parentWidget()) parentWidget()->d_func()->enforceNativeChildren(); if (on && !internalWinId() && testAttribute(Qt::WA_WState_Created)) d->createWinId(); @@ -11588,7 +11629,7 @@ void QWidget::setWindowModified(bool mod) d->setWindowModified_helper(); QEvent e(QEvent::ModifiedChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } void QWidgetPrivate::setWindowModified_helper() @@ -11634,7 +11675,7 @@ void QWidget::setToolTip(const QString &s) d->toolTip = s; QEvent event(QEvent::ToolTipChange); - QApplication::sendEvent(this, &event); + QCoreApplication::sendEvent(this, &event); } QString QWidget::toolTip() const @@ -11811,7 +11852,7 @@ int QWidget::grabShortcut(const QKeySequence &key, Qt::ShortcutContext context) if (key.isEmpty()) return 0; setAttribute(Qt::WA_GrabbedShortcut); - return qApp->d_func()->shortcutMap.addShortcut(this, key, context, qWidgetShortcutContextMatcher); + return QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(this, key, context, qWidgetShortcutContextMatcher); } /*! @@ -11833,7 +11874,7 @@ void QWidget::releaseShortcut(int id) { Q_ASSERT(qApp); if (id) - qApp->d_func()->shortcutMap.removeShortcut(id, this, 0); + QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id, this, 0); } /*! @@ -11852,7 +11893,7 @@ void QWidget::setShortcutEnabled(int id, bool enable) { Q_ASSERT(qApp); if (id) - qApp->d_func()->shortcutMap.setShortcutEnabled(enable, id, this, 0); + QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enable, id, this, 0); } /*! @@ -11867,7 +11908,7 @@ void QWidget::setShortcutAutoRepeat(int id, bool enable) { Q_ASSERT(qApp); if (id) - qApp->d_func()->shortcutMap.setShortcutAutoRepeat(enable, id, this, 0); + QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(enable, id, this, 0); } #endif // QT_NO_SHORTCUT @@ -11922,7 +11963,7 @@ void QWidget::raise() QWindowContainer::parentWasRaised(this); QEvent e(QEvent::ZOrderChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } void QWidgetPrivate::raise_sys() @@ -11972,7 +12013,7 @@ void QWidget::lower() QWindowContainer::parentWasLowered(this); QEvent e(QEvent::ZOrderChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } void QWidgetPrivate::lower_sys() @@ -12019,7 +12060,7 @@ void QWidget::stackUnder(QWidget* w) d->stackUnder_sys(w); QEvent e(QEvent::ZOrderChange); - QApplication::sendEvent(this, &e); + QCoreApplication::sendEvent(this, &e); } void QWidgetPrivate::stackUnder_sys(QWidget*) @@ -13191,7 +13232,7 @@ QDebug operator<<(QDebug debug, const QWidget *widget) if (widget->isWindow()) debug << ", window"; debug << ", " << geometry.width() << 'x' << geometry.height() - << forcesign << geometry.x() << geometry.y() << noforcesign; + << Qt::forcesign << geometry.x() << geometry.y() << Qt::noforcesign; if (frameGeometry != geometry) { const QMargins margins(geometry.x() - frameGeometry.x(), geometry.y() - frameGeometry.y(), @@ -13201,7 +13242,7 @@ QDebug operator<<(QDebug debug, const QWidget *widget) } debug << ", devicePixelRatio=" << widget->devicePixelRatioF(); if (const WId wid = widget->internalWinId()) - debug << ", winId=0x" << hex << wid << dec; + debug << ", winId=0x" << Qt::hex << wid << Qt::dec; } debug << ')'; } else { |