Prevent recursive calls to QWindow::close

QWidget will call close() in its destructor, which we might end up
in if a user deletes the widget in the closeEvent.

Pick-to: 6.2
Change-Id: I39684aec0ca130033dad60f2bbf823364a5edcec
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Tor Arne Vestbø 2021-10-13 15:36:08 +02:00
parent 826fc8c9bd
commit acb86da793
2 changed files with 29 additions and 0 deletions

View File

@ -2257,6 +2257,8 @@ void QWindow::showNormal()
bool QWindow::close()
{
Q_D(QWindow);
if (d->inClose)
return true;
// Do not close non top level windows
if (!isTopLevel())

View File

@ -1602,6 +1602,23 @@ void tst_QWindow::sizes()
QCOMPARE(maximumHeightSpy.count(), 1);
}
class CloseOnCloseEventWindow : public QWindow
{
public:
inline static int closeEvents;
CloseOnCloseEventWindow() { closeEvents = 0; }
protected:
void closeEvent(QCloseEvent *e) override
{
if (++closeEvents > 1)
return;
close();
e->accept();
}
};
void tst_QWindow::close()
{
{
@ -1683,6 +1700,16 @@ void tst_QWindow::close()
QVERIFY(c.handle());
}
}
{
// A QWidget will call close() from the destructor, and
// we allow widgets deleting itself in the closeEvent,
// so we need to guard against close being called recursively.
CloseOnCloseEventWindow w;
w.create();
w.close();
QCOMPARE(CloseOnCloseEventWindow::closeEvents, 1);
}
}
void tst_QWindow::activateAndClose()