QLayout: Consume ChildRemoved event, when layout is disabled

QLayout::widgetEvent() returned early, when the layout was disabled.
That suppressed ChildRemoved events and lead to a crash, when the
layout was enabled again.

Don't return early on ChildRemoved events.
Add an autotest in tst_layout::removeWidget().

Fixes: QTBUG-124151
Pick-to: 6.5 6.2
Change-Id: Ib0a0bb73978d9fc2c9777d300cf38a8c4496b702
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Chris René Lerner <chris.lerner@qt.io>
(cherry picked from commit ef8e548cf1adf8032c030df04e69988f238c77fb)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Axel Spoerl 2024-04-09 19:23:33 +02:00 committed by Qt Cherry-pick Bot
parent 8cb7b906a6
commit 8f27ca2346
2 changed files with 12 additions and 5 deletions

View File

@ -520,10 +520,11 @@ void QLayoutPrivate::doResize()
void QLayout::widgetEvent(QEvent *e) void QLayout::widgetEvent(QEvent *e)
{ {
Q_D(QLayout); Q_D(QLayout);
if (!d->enabled) const QEvent::Type type = e->type();
if (!d->enabled && type != QEvent::ChildRemoved)
return; return;
switch (e->type()) { switch (type) {
case QEvent::Resize: case QEvent::Resize:
if (d->activated) if (d->activated)
d->doResize(); d->doResize();

View File

@ -378,10 +378,10 @@ void tst_QLayout::removeWidget()
{ {
QHBoxLayout layout; QHBoxLayout layout;
QCOMPARE(layout.count(), 0); QCOMPARE(layout.count(), 0);
QWidget w; std::unique_ptr<QWidget> w(new QWidget);
layout.addWidget(&w); layout.addWidget(w.get());
QCOMPARE(layout.count(), 1); QCOMPARE(layout.count(), 1);
layout.removeWidget(&w); layout.removeWidget(w.get());
QCOMPARE(layout.count(), 0); QCOMPARE(layout.count(), 0);
QPointer<QLayout> childLayout(new QHBoxLayout); QPointer<QLayout> childLayout(new QHBoxLayout);
@ -395,6 +395,12 @@ void tst_QLayout::removeWidget()
QCOMPARE(layout.count(), 0); QCOMPARE(layout.count(), 0);
QVERIFY(!childLayout.isNull()); QVERIFY(!childLayout.isNull());
// Test inactive layout consumes ChildRemoved event (QTBUG-124151)
layout.addWidget(w.get());
layout.setEnabled(false);
w.reset();
layout.setEnabled(true);
} }
QTEST_MAIN(tst_QLayout) QTEST_MAIN(tst_QLayout)