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
Pick-to: 6.7
Change-Id: If9cb04e693c2663ef9da14164611f26becafc4b4
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Eike Ziller 2024-04-29 15:24:52 +02:00 committed by Volker Hilsheimer
parent ec449a8379
commit 24859d7dea
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);
if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) {
d->hiddenBecauseNoMatch = false;
if (d->popup && d->popup->isVisible())
return true;
if (o == d->widget) {
switch (e->type()) {
case QEvent::FocusOut:
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)

View File

@ -109,6 +109,8 @@ private slots:
void dynamicSortOrder();
void disabledItems();
void hideWidget();
// task-specific tests below me
void task178797_activatedOnReturn();
void task189564_omitNonSelectableItems();
@ -1182,6 +1184,32 @@ void tst_QCompleter::disabledItems()
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()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))