diff options
author | Piotr Srebrny <piotr.srebrny@qt.io> | 2021-04-28 14:37:44 +0200 |
---|---|---|
committer | Piotr Srebrny <piotr.srebrny@qt.io> | 2021-05-11 16:57:17 +0000 |
commit | 867c0b8d8a53974074b1fff5b132f3ae9f150066 (patch) | |
tree | 3096671fa5d48c00df7e6d7e0ba7b3738e6932b1 | |
parent | 1e85dfacf3479ebbe5a4502895b8b8d93fbe1477 (diff) |
-rw-r--r-- | src/widgets/kernel/qlayout.cpp | 5 | ||||
-rw-r--r-- | tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp | 24 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp index 8688e011a0..044176d141 100644 --- a/src/widgets/kernel/qlayout.cpp +++ b/src/widgets/kernel/qlayout.cpp @@ -1355,6 +1355,11 @@ QRect QLayout::alignmentRect(const QRect &r) const */ void QLayout::removeWidget(QWidget *widget) { + if (Q_UNLIKELY(!widget)) { + qWarning("QLayout::removeWidget: Cannot remove a null widget."); + return; + } + int i = 0; QLayoutItem *child; while ((child = itemAt(i))) { diff --git a/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp b/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp index 01b7c8ef9c..deb473ab8c 100644 --- a/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp +++ b/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp @@ -70,6 +70,7 @@ private slots: void controlTypes2(); void adjustSizeShouldMakeSureLayoutIsActivated(); void testRetainSizeWhenHidden(); + void removeWidget(); }; tst_QLayout::tst_QLayout() @@ -398,5 +399,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<QLayout> 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" |