From 7a342372bb1ecbe4146811cff48ede974c7761b5 Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Thu, 1 Feb 2018 12:36:52 +0000 Subject: Introduce QLayout::indexOf(QLayoutItem *) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was the missing counter-part to indexOf(QWidget *), which is sometimes implemented in user code. Not sure why the original code doesn't use a for-loop and instead accesses an out-of-bounds element, but I'll preserve the behavior of very old working code. Change-Id: I7d7fa56b56a4626789774c15c23fdfef41d723e7 Reviewed-by: David Faure Reviewed-by: Jan Arve Sæther --- src/widgets/kernel/qlayout.cpp | 20 ++++++++++++++++++++ src/widgets/kernel/qlayout.h | 1 + .../widgets/kernel/qboxlayout/tst_qboxlayout.cpp | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp index f3db4f4e2d..64acd8d229 100644 --- a/src/widgets/kernel/qlayout.cpp +++ b/src/widgets/kernel/qlayout.cpp @@ -1243,6 +1243,26 @@ int QLayout::indexOf(QWidget *widget) const return -1; } +/*! + \since 5.12 + Searches for layout item \a layoutItem in this layout (not including child + layouts). + + Returns the index of \a layoutItem, or -1 if \a layoutItem is not found. +*/ +int QLayout::indexOf(QLayoutItem *layoutItem) const +{ + int i = 0; + QLayoutItem *item = itemAt(i); + while (item) { + if (item == layoutItem) + return i; + ++i; + item = itemAt(i); + } + return -1; +} + /*! \enum QLayout::SizeConstraint diff --git a/src/widgets/kernel/qlayout.h b/src/widgets/kernel/qlayout.h index bcc33a0811..616f4e7164 100644 --- a/src/widgets/kernel/qlayout.h +++ b/src/widgets/kernel/qlayout.h @@ -122,6 +122,7 @@ public: virtual QLayoutItem *itemAt(int index) const = 0; virtual QLayoutItem *takeAt(int index) = 0; virtual int indexOf(QWidget *) const; + QT6_VIRTUAL int indexOf(QLayoutItem *) const; virtual int count() const = 0; bool isEmpty() const override; QSizePolicy::ControlTypes controlTypes() const override; diff --git a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp index 2f1a305710..3dfb9c35b3 100644 --- a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp @@ -56,6 +56,7 @@ private slots: void taskQTBUG_40609_addingWidgetToItsOwnLayout(); void taskQTBUG_40609_addingLayoutToItself(); void replaceWidget(); + void indexOf(); }; class CustomLayoutStyle : public QProxyStyle @@ -514,5 +515,24 @@ void tst_QBoxLayout::replaceWidget() QCOMPARE(boxLayout->indexOf(replaceTo), 1); } +void tst_QBoxLayout::indexOf() +{ + QWidget w; + auto outer = new QVBoxLayout(&w); + auto inner = new QHBoxLayout(); + outer->addLayout(inner); + auto widget1 = new QWidget(); + QWidget widget2; + inner->addWidget(widget1); + + QCOMPARE(inner->indexOf(widget1), 0); + QCOMPARE(inner->indexOf(&widget2), -1); + QCOMPARE(outer->indexOf(widget1), -1); + QCOMPARE(outer->indexOf(&widget2), -1); + QCOMPARE(outer->indexOf(outer), -1); + QCOMPARE(outer->indexOf(inner), 0); + QCOMPARE(inner->indexOf(inner->itemAt(0)), 0); +} + QTEST_MAIN(tst_QBoxLayout) #include "tst_qboxlayout.moc" -- cgit v1.2.3