From 5055a2fb9954b5ba0f42b49c4933633f16113f7b Mon Sep 17 00:00:00 2001 From: Piotr Srebrny Date: Wed, 28 Apr 2021 14:37:44 +0200 Subject: Do not remove non-widget items when removeWidget() called with nullptr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit child->widget() returns null if the layout item is not a widget. Thus, calling removeWidget(nullptr) will remove all non-widget items such as layouts or strechers. Change-Id: I772c1158d0f7e8e2850b6e571b0405a2896f09b8 Reviewed-by: Tor Arne Vestbø Reviewed-by: David Faure (cherry picked from commit 867c0b8d8a53974074b1fff5b132f3ae9f150066) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp b/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp index dec332196b..d54fcc5420 100644 --- a/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp +++ b/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp @@ -69,6 +69,7 @@ private slots: void controlTypes2(); void adjustSizeShouldMakeSureLayoutIsActivated(); void testRetainSizeWhenHidden(); + void removeWidget(); }; tst_QLayout::tst_QLayout() @@ -381,5 +382,28 @@ void tst_QLayout::testRetainSizeWhenHidden() QCOMPARE(widget.sizeHint().height(), normalHeight); } +void tst_QLayout::removeWidget() +{ + QHBoxLayout layout; + QCOMPARE(layout.count(), 0); + QWidget w; + layout.addWidget(&w); + QCOMPARE(layout.count(), 1); + layout.removeWidget(&w); + QCOMPARE(layout.count(), 0); + + QPointer childLayout(new QHBoxLayout); + layout.addLayout(childLayout); + QCOMPARE(layout.count(), 1); + + layout.removeWidget(nullptr); + QCOMPARE(layout.count(), 1); + + layout.removeItem(childLayout); + QCOMPARE(layout.count(), 0); + + QVERIFY(!childLayout.isNull()); +} + QTEST_MAIN(tst_QLayout) #include "tst_qlayout.moc" -- cgit v1.2.3