diff options
author | Marcel Kummer <marcel.kummer@qt.io> | 2022-06-15 13:13:25 +0200 |
---|---|---|
committer | Marcel Kummer <marcel.kummer@qt.io> | 2022-06-17 18:29:11 +0200 |
commit | 29176bf2f836ba05206680ae1c00edf786202648 (patch) | |
tree | 324477965f81150e02fcf23f25328b8573197d7e /src/widgets/kernel | |
parent | a2cb9ae8cc6c512e9c16f3fecc79dad9d585948a (diff) |
QBoxLayout: Pull out handling for negative insertion indices
This snippet occurs in every insert function further down. Let's pull
it out in preparation for adding an assertion that the index is in
range, which would also need to be added to every insert function.
Fixes: QTBUG-103775
Change-Id: I419f57434f860df4c947853ac307826994f0198b
Pick-to: 6.2 6.3 6.4
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/kernel')
-rw-r--r-- | src/widgets/kernel/qboxlayout.cpp | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp index f7535a2d70..6384fda896 100644 --- a/src/widgets/kernel/qboxlayout.cpp +++ b/src/widgets/kernel/qboxlayout.cpp @@ -88,6 +88,7 @@ public: void effectiveMargins(int *left, int *top, int *right, int *bottom) const; QLayoutItem* replaceAt(int index, QLayoutItem*) override; + int validateIndex(int index) const; }; QBoxLayoutPrivate::~QBoxLayoutPrivate() @@ -407,6 +408,13 @@ QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item) return r; } +int QBoxLayoutPrivate::validateIndex(int index) const +{ + if (index < 0) + return list.count(); // append + + return index; +} /*! \class QBoxLayout @@ -811,9 +819,7 @@ void QBoxLayout::addItem(QLayoutItem *item) void QBoxLayout::insertItem(int index, QLayoutItem *item) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QBoxLayoutItem *it = new QBoxLayoutItem(item); d->list.insert(index, it); invalidate(); @@ -831,9 +837,7 @@ void QBoxLayout::insertItem(int index, QLayoutItem *item) void QBoxLayout::insertSpacing(int index, int size) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QLayoutItem *b; if (horz(d->dir)) b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum); @@ -856,9 +860,7 @@ void QBoxLayout::insertSpacing(int index, int size) void QBoxLayout::insertStretch(int index, int stretch) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QLayoutItem *b; if (horz(d->dir)) b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -883,9 +885,7 @@ void QBoxLayout::insertStretch(int index, int stretch) void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem) { Q_D(QBoxLayout); - if (index < 0) // append - index = d->list.count(); - + index = d->validateIndex(index); QBoxLayoutItem *it = new QBoxLayoutItem(spacerItem); it->magic = true; d->list.insert(index, it); @@ -907,8 +907,7 @@ void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch) return; if (!adoptLayout(layout)) return; - if (index < 0) // append - index = d->list.count(); + index = d->validateIndex(index); QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch); d->list.insert(index, it); invalidate(); @@ -941,8 +940,7 @@ void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch, if (!d->checkWidget(widget)) return; addChildWidget(widget); - if (index < 0) // append - index = d->list.count(); + index = d->validateIndex(index); QWidgetItem *b = QLayoutPrivate::createWidgetItem(this, widget); b->setAlignment(alignment); |