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 <richard.gustavsen@qt.io>
bb10
Giuseppe D'Angelo 2020-08-08 02:18:53 +02:00
parent fded035b1c
commit 7a5f865186
2 changed files with 14 additions and 14 deletions

View File

@ -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;
}

View File

@ -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