From 42d681f9cfc984046a93b9efe19903d46ac68bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lund=20Martsum?= Date: Mon, 3 Sep 2012 06:35:39 +0200 Subject: Add widget replace function to QLayout Sometimes it is nice to be able to replace a widget in a layout. Change-Id: I23a6a65e417e94d53bc48639503db1a142bc3f10 Reviewed-by: J-P Nurmi --- .../widgets/kernel/qformlayout/tst_qformlayout.cpp | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp') diff --git a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp index b970de8e87..135605f185 100644 --- a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp +++ b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp @@ -122,6 +122,7 @@ private slots: void itemAt(); void takeAt(); void layoutAlone(); + void replaceWidget(); /* void setGeometry(const QRect &rect); QSize minimumSize() const; @@ -934,6 +935,54 @@ void tst_QFormLayout::taskQTBUG_27420_takeAtShouldUnparentLayout() QVERIFY(!inner.isNull()); } +void tst_QFormLayout::replaceWidget() +{ + QWidget w; + QFormLayout *layout = new QFormLayout(); + w.setLayout(layout); + QLineEdit *edit1 = new QLineEdit(); + QLineEdit *edit2 = new QLineEdit(); + QLineEdit *edit3 = new QLineEdit(); + QLabel *label1 = new QLabel(); + QLabel *label2 = new QLabel(); + + layout->addRow("Label", edit1); + layout->addRow(label1, edit2); + + // Verify controls not in layout + QCOMPARE(layout->indexOf(edit3), -1); + QCOMPARE(layout->indexOf(label2), -1); + + // Verify controls in layout + int editIndex = layout->indexOf(edit1); + int labelIndex = layout->indexOf(label1); + QVERIFY(editIndex > 0); + QVERIFY(labelIndex > 0); + int rownum; + QFormLayout::ItemRole role; + + // replace editor + layout->replaceWidget(edit1, edit3); + edit1->hide(); // Not strictly needed for the test, but for normal usage it is. + QCOMPARE(layout->indexOf(edit1), -1); + QCOMPARE(layout->indexOf(edit3), editIndex); + QCOMPARE(layout->indexOf(label1), labelIndex); + rownum = -1; + role = QFormLayout::SpanningRole; + layout->getWidgetPosition(edit3, &rownum, &role); + QCOMPARE(rownum, 0); + QCOMPARE(role, QFormLayout::FieldRole); + + layout->replaceWidget(label1, label2); + label1->hide(); + QCOMPARE(layout->indexOf(label1), -1); + QCOMPARE(layout->indexOf(label2), labelIndex); + layout->getWidgetPosition(label2, &rownum, &role); + QCOMPARE(rownum, 1); + QCOMPARE(role, QFormLayout::LabelRole); + +} + QTEST_MAIN(tst_QFormLayout) #include "tst_qformlayout.moc" -- cgit v1.2.3