From 6b86d9d08b407a56bad2d37f65169e36356b0741 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 31 Oct 2024 18:03:00 +0100 Subject: [PATCH] QCompleter: add more nullptr checks for the widget The widget might become nullptr, especially after delivering events to it. So consistently test for nullptr before calling the event function. Fixes: QTBUG-128478 Pick-to: 6.8 6.5 Change-Id: Ieed4e8b68b41488fafd9946d718c1d77fad39a44 Reviewed-by: Axel Spoerl --- src/widgets/util/qcompleter.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/widgets/util/qcompleter.cpp b/src/widgets/util/qcompleter.cpp index 3c8851089d4..93cea978d1f 100644 --- a/src/widgets/util/qcompleter.cpp +++ b/src/widgets/util/qcompleter.cpp @@ -1370,11 +1370,13 @@ bool QCompleter::eventFilter(QObject *o, QEvent *e) return false; } - // Send the event to the widget. If the widget accepted the event, do nothing - // If the widget did not accept the event, provide a default implementation - d->eatFocusOut = false; - (static_cast(d->widget))->event(ke); - d->eatFocusOut = true; + if (d->widget) { + // Send the event to the widget. If the widget accepted the event, do nothing + // If the widget did not accept the event, provide a default implementation + d->eatFocusOut = false; + (static_cast(d->widget))->event(ke); + d->eatFocusOut = true; + } if (!d->widget || e->isAccepted() || !d->popup->isVisible()) { // widget lost focus, hide the popup if (d->widget && (!d->widget->hasFocus() @@ -1426,8 +1428,9 @@ bool QCompleter::eventFilter(QObject *o, QEvent *e) #ifdef QT_KEYPAD_NAVIGATION case QEvent::KeyRelease: { - QKeyEvent *ke = static_cast(e); - if (QApplicationPrivate::keypadNavigationEnabled() && ke->key() == Qt::Key_Back) { + if (d->widget && + QApplicationPrivate::keypadNavigationEnabled() && ke->key() == Qt::Key_Back) { + QKeyEvent *ke = static_cast(e); // Send the event to the 'widget'. This is what we did for KeyPress, so we need // to do the same for KeyRelease, in case the widget's KeyPress event set // up something (such as a timer) that is relying on also receiving the @@ -1444,7 +1447,8 @@ bool QCompleter::eventFilter(QObject *o, QEvent *e) case QEvent::MouseButtonPress: { #ifdef QT_KEYPAD_NAVIGATION - if (QApplicationPrivate::keypadNavigationEnabled()) { + if (d->widget + && QApplicationPrivate::keypadNavigationEnabled()) { // if we've clicked in the widget (or its descendant), let it handle the click QWidget *source = qobject_cast(o); if (source) { @@ -1473,7 +1477,8 @@ bool QCompleter::eventFilter(QObject *o, QEvent *e) return true; case QEvent::InputMethod: case QEvent::ShortcutOverride: - QCoreApplication::sendEvent(d->widget, e); + if (d->widget) + QCoreApplication::sendEvent(d->widget, e); break; default: