From 7a5f865186b3dd831c5943ca6050437fc17abb87 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sat, 8 Aug 2020 02:18:53 +0200 Subject: [PATCH] QLayout::indexOf: redo implementation Stop relying on the "magic" of itemAt returning nullptr for out of bounds. Just use count(). Unfortunately, QMainWindowLayout breaks the API contract by NOT implementing count() properly. So, make its count() crash if called; and move the itemAt implementation there. Change-Id: I120686a834bab15dd537598a56bd93d6a5924aa5 Reviewed-by: Richard Moe Gustavsen --- src/widgets/kernel/qlayout.cpp | 22 ++++++++++------------ src/widgets/widgets/qmainwindowlayout.cpp | 6 ++++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp index 431c93596a..c62c8fae41 100644 --- a/src/widgets/kernel/qlayout.cpp +++ b/src/widgets/kernel/qlayout.cpp @@ -1181,14 +1181,13 @@ QLayoutItem *QLayout::replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOpt */ int QLayout::indexOf(const QWidget *widget) const { - int i = 0; - QLayoutItem *item = itemAt(i); - while (item) { - if (item->widget() == widget) + const int c = count(); + + for (int i = 0; i < c; ++i) { + if (itemAt(i)->widget() == widget) return i; - ++i; - item = itemAt(i); } + return -1; } @@ -1201,14 +1200,13 @@ int QLayout::indexOf(const QWidget *widget) const */ int QLayout::indexOf(const QLayoutItem *layoutItem) const { - int i = 0; - QLayoutItem *item = itemAt(i); - while (item) { - if (item == layoutItem) + const int c = count(); + + for (int i = 0; i < c; ++i) { + if (itemAt(i) == layoutItem) return i; - ++i; - item = itemAt(i); } + return -1; } diff --git a/src/widgets/widgets/qmainwindowlayout.cpp b/src/widgets/widgets/qmainwindowlayout.cpp index 017e33692b..76d9a33e06 100644 --- a/src/widgets/widgets/qmainwindowlayout.cpp +++ b/src/widgets/widgets/qmainwindowlayout.cpp @@ -1895,8 +1895,10 @@ void QMainWindowLayout::raise(QDockWidget *widget) int QMainWindowLayout::count() const { - qWarning("QMainWindowLayout::count: ?"); - return 0; //################################################# + int result = 0; + while (itemAt(result)) + ++result; + return result; } QLayoutItem *QMainWindowLayout::itemAt(int index) const