From 716d33d2a73ade42eb31be3e8ecbaeecdd5ddd21 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 21 Oct 2012 20:10:22 +0200 Subject: [QTBUG-27420] Make Q{Box,Grid,Form}Layout::takeAt() unparent a nested layout QStackedLayout doesn't have support for QLayout, only QWidget, so the issue doesn't arise there. Reported-by: Johannes Schaub Task-number: QTBUG-27420 Change-Id: I71f8d10a036918c16d8f8c9197a2ec61cd76cf01 Reviewed-by: Friedemann Kleint --- .../widgets/kernel/qboxlayout/tst_qboxlayout.cpp | 22 ++++++++++++++++++++ .../widgets/kernel/qformlayout/tst_qformlayout.cpp | 24 ++++++++++++++++++++++ .../widgets/kernel/qgridlayout/tst_qgridlayout.cpp | 24 ++++++++++++++++++++++ 3 files changed, 70 insertions(+) (limited to 'tests/auto/widgets/kernel') diff --git a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp index 3564800a8b..4f1615a48e 100644 --- a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp @@ -66,6 +66,7 @@ private slots: void setStyleShouldChangeSpacing(); void taskQTBUG_7103_minMaxWidthNotRespected(); + void taskQTBUG_27420_takeAtShouldUnparentLayout(); }; class CustomLayoutStyle : public QProxyStyle @@ -273,5 +274,26 @@ void tst_QBoxLayout::taskQTBUG_7103_minMaxWidthNotRespected() QCOMPARE(label->height(), height); } +void tst_QBoxLayout::taskQTBUG_27420_takeAtShouldUnparentLayout() +{ + QSharedPointer outer(new QHBoxLayout); + QPointer inner = new QVBoxLayout; + + outer->addLayout(inner); + QCOMPARE(outer->count(), 1); + QCOMPARE(inner->parent(), outer.data()); + + QLayoutItem *item = outer->takeAt(0); + QCOMPARE(item->layout(), inner.data()); + QVERIFY(!item->layout()->parent()); + + outer.reset(); + + if (inner) + delete item; // success: a taken item/layout should not be deleted when the old parent is deleted + else + QVERIFY(!inner.isNull()); +} + QTEST_MAIN(tst_QBoxLayout) #include "tst_qboxlayout.moc" diff --git a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp index 3aa0ea03f4..b2cdb87221 100644 --- a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp +++ b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include @@ -123,6 +124,8 @@ private slots: Qt::Orientations expandingDirections() const; */ + void taskQTBUG_27420_takeAtShouldUnparentLayout(); + }; tst_QFormLayout::tst_QFormLayout() @@ -901,6 +904,27 @@ void tst_QFormLayout::layoutAlone() QTest::qWait(500); } +void tst_QFormLayout::taskQTBUG_27420_takeAtShouldUnparentLayout() +{ + QSharedPointer outer(new QFormLayout); + QPointer inner = new QFormLayout; + + outer->addRow(inner); + QCOMPARE(outer->count(), 1); + QCOMPARE(inner->parent(), outer.data()); + + QLayoutItem *item = outer->takeAt(0); + QCOMPARE(item->layout(), inner.data()); + QVERIFY(!item->layout()->parent()); + + outer.reset(); + + if (inner) + delete item; // success: a taken item/layout should not be deleted when the old parent is deleted + else + QVERIFY(!inner.isNull()); +} + QTEST_MAIN(tst_QFormLayout) #include "tst_qformlayout.moc" diff --git a/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp b/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp index 34b38c1e53..e52ff3fa8b 100644 --- a/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp +++ b/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp @@ -52,6 +52,7 @@ #include #include #include +#include class tst_QGridLayout : public QObject { @@ -89,6 +90,8 @@ private slots: void contentsRect(); void distributeMultiCell(); + void taskQTBUG_27420_takeAtShouldUnparentLayout(); + private: QWidget *testWidget; QGridLayout *testLayout; @@ -1605,5 +1608,26 @@ void tst_QGridLayout::distributeMultiCell() QCOMPARE(w.sizeHint().height(), 11 + 57 + 11); } +void tst_QGridLayout::taskQTBUG_27420_takeAtShouldUnparentLayout() +{ + QSharedPointer outer(new QGridLayout); + QPointer inner = new QGridLayout; + + outer->addLayout(inner, 0, 0); + QCOMPARE(outer->count(), 1); + QCOMPARE(inner->parent(), outer.data()); + + QLayoutItem *item = outer->takeAt(0); + QCOMPARE(item->layout(), inner.data()); + QVERIFY(!item->layout()->parent()); + + outer.reset(); + + if (inner) + delete item; // success: a taken item/layout should not be deleted when the old parent is deleted + else + QVERIFY(!inner.isNull()); +} + QTEST_MAIN(tst_QGridLayout) #include "tst_qgridlayout.moc" -- cgit v1.2.3