QCompleter: Hide popup when widget is hidden

When the widget the completer is attached to was hidden,
the popup stayed open. It would "hang around" with no
corresponding UI being around anymore, which is weird.

Fixes: QTBUG-124861
Change-Id: If9cb04e693c2663ef9da14164611f26becafc4b4
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 24859d7deaf995e992f0d9439a1c476126d5f654)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Eike Ziller 2024-04-29 15:24:52 +02:00 committed by Qt Cherry-pick Bot
parent 5310cce3ce
commit 272808600a
2 changed files with 43 additions and 4 deletions

View File

@ -1296,10 +1296,21 @@ bool QCompleter::eventFilter(QObject *o, QEvent *e)
{ {
Q_D(QCompleter); Q_D(QCompleter);
if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) { if (o == d->widget) {
d->hiddenBecauseNoMatch = false; switch (e->type()) {
if (d->popup && d->popup->isVisible()) case QEvent::FocusOut:
return true; if (d->eatFocusOut) {
d->hiddenBecauseNoMatch = false;
if (d->popup && d->popup->isVisible())
return true;
}
break;
case QEvent::Hide:
if (d->popup)
d->popup->hide();
default:
break;
}
} }
if (o != d->popup) if (o != d->popup)

View File

@ -109,6 +109,8 @@ private slots:
void dynamicSortOrder(); void dynamicSortOrder();
void disabledItems(); void disabledItems();
void hideWidget();
// task-specific tests below me // task-specific tests below me
void task178797_activatedOnReturn(); void task178797_activatedOnReturn();
void task189564_omitNonSelectableItems(); void task189564_omitNonSelectableItems();
@ -1185,6 +1187,32 @@ void tst_QCompleter::disabledItems()
QVERIFY(!view->isVisible()); QVERIFY(!view->isVisible());
} }
void tst_QCompleter::hideWidget()
{
// hiding the widget should hide/close the popup
QWidget w;
w.setWindowTitle(QLatin1String(QTest::currentTestFunction()));
w.setLayout(new QVBoxLayout);
QLineEdit edit;
edit.setCompleter(new QCompleter({ "foo", "bar" }));
w.layout()->addWidget(&edit);
const auto pos = w.screen()->availableGeometry().topLeft() + QPoint(200, 200);
w.move(pos);
w.show();
QApplicationPrivate::setActiveWindow(&w);
QVERIFY(QTest::qWaitForWindowActive(&w));
// activate the completer
QTest::keyClick(&edit, Qt::Key_F);
QVERIFY(edit.completer()->popup());
QTRY_VERIFY(edit.completer()->popup()->isVisible());
edit.hide();
QVERIFY(!edit.completer()->popup()->isVisible());
}
void tst_QCompleter::task178797_activatedOnReturn() void tst_QCompleter::task178797_activatedOnReturn()
{ {
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))