diff options
Diffstat (limited to 'src/widgets/kernel/qboxlayout.cpp')
-rw-r--r-- | src/widgets/kernel/qboxlayout.cpp | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp index f7535a2d70..501883e85a 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() @@ -232,7 +233,7 @@ void QBoxLayoutPrivate::setupGeom() hasHfw = false; - int n = list.count(); + int n = list.size(); geomArray.clear(); QList<QLayoutStruct> a(n); @@ -364,7 +365,7 @@ void QBoxLayoutPrivate::setupGeom() void QBoxLayoutPrivate::calcHfw(int w) { QList<QLayoutStruct> &a = geomArray; - int n = a.count(); + int n = a.size(); int h = 0; int mh = 0; @@ -407,6 +408,14 @@ QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item) return r; } +int QBoxLayoutPrivate::validateIndex(int index) const +{ + if (index < 0) + return list.size(); // append + + Q_ASSERT_X(index >= 0 && index <= list.size(), "QBoxLayout::insert", "index out of range"); + return index; +} /*! \class QBoxLayout @@ -671,7 +680,7 @@ void QBoxLayout::invalidate() int QBoxLayout::count() const { Q_D(const QBoxLayout); - return d->list.count(); + return d->list.size(); } /*! @@ -680,7 +689,7 @@ int QBoxLayout::count() const QLayoutItem *QBoxLayout::itemAt(int index) const { Q_D(const QBoxLayout); - return index >= 0 && index < d->list.count() ? d->list.at(index)->item : nullptr; + return index >= 0 && index < d->list.size() ? d->list.at(index)->item : nullptr; } /*! @@ -689,7 +698,7 @@ QLayoutItem *QBoxLayout::itemAt(int index) const QLayoutItem *QBoxLayout::takeAt(int index) { Q_D(QBoxLayout); - if (index < 0 || index >= d->list.count()) + if (index < 0 || index >= d->list.size()) return nullptr; QBoxLayoutItem *b = d->list.takeAt(index); QLayoutItem *item = b->item; @@ -740,7 +749,7 @@ void QBoxLayout::setGeometry(const QRect &r) QList<QLayoutStruct> a = d->geomArray; int pos = horz(d->dir) ? s.x() : s.y(); int space = horz(d->dir) ? s.width() : s.height(); - int n = a.count(); + int n = a.size(); if (d->hasHfw && !horz(d->dir)) { for (int i = 0; i < n; i++) { QBoxLayoutItem *box = d->list.at(i); @@ -811,9 +820,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 +838,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 +861,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 +886,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 +908,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 +941,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); |