diff options
author | Jan Arve Sæther <jan-arve.saether@qt.io> | 2022-08-24 15:41:29 +0200 |
---|---|---|
committer | Jan Arve Sæther <jan-arve.saether@qt.io> | 2022-09-06 11:17:45 +0200 |
commit | 47a4d10548a37a3fb8654848d78d648b47e6fb7f (patch) | |
tree | 29eb53ce5fd0646e82c197419739f6de93fea269 | |
parent | e404db276e958bcfd462c7e0cb2db89d11bedc44 (diff) |
StackLayout: Do not set size of children to (-1, -1)
The item-size-hint-cache in StackLayout was not always valid when
QQuickStackLayout::rearrange() was entered, so it would end up setting
the size of the item to (-1, -1)
Fixes: QTBUG-105899
Change-Id: I632aa18bb10b84d59af35cd3c7cb0c675d8d1692
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
(cherry picked from commit 9a4874ab7f18a54b2497f689dfd7f7a2ee0516b8)
-rw-r--r-- | src/imports/layouts/qquickstacklayout.cpp | 14 | ||||
-rw-r--r-- | src/imports/layouts/qquickstacklayout_p.h | 1 | ||||
-rw-r--r-- | tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml | 50 |
3 files changed, 61 insertions, 4 deletions
diff --git a/src/imports/layouts/qquickstacklayout.cpp b/src/imports/layouts/qquickstacklayout.cpp index 443fa0d266..bd2f93c630 100644 --- a/src/imports/layouts/qquickstacklayout.cpp +++ b/src/imports/layouts/qquickstacklayout.cpp @@ -192,10 +192,8 @@ QSizeF QQuickStackLayout::sizeHint(Qt::SizeHint whichSizeHint) const maxS = QSizeF(std::numeric_limits<qreal>::infinity(), std::numeric_limits<qreal>::infinity()); const int count = itemCount(); - m_cachedItemSizeHints.resize(count); for (int i = 0; i < count; ++i) { - SizeHints &hints = m_cachedItemSizeHints[i]; - QQuickStackLayout::collectItemSizeHints(itemAt(i), hints.array); + SizeHints &hints = cachedItemSizeHints(i); minS = minS.expandedTo(hints.min()); prefS = prefS.expandedTo(hints.pref()); //maxS = maxS.boundedTo(hints.max()); // Can be resized to be larger than any of its items. @@ -286,6 +284,7 @@ void QQuickStackLayout::updateLayoutItems() if (count != d->count) { d->count = count; emit countChanged(); + m_cachedItemSizeHints.resize(count); } for (int i = 0; i < count; ++i) { QQuickItem *child = itemAt(i); @@ -294,6 +293,13 @@ void QQuickStackLayout::updateLayoutItems() } } +QQuickStackLayout::SizeHints &QQuickStackLayout::cachedItemSizeHints(int index) const { + SizeHints &hints = m_cachedItemSizeHints[index]; + if (!hints.min().isValid()) + QQuickStackLayout::collectItemSizeHints(itemAt(index), hints.array); + return hints; +} + void QQuickStackLayout::rearrange(const QSizeF &newSize) { Q_D(QQuickStackLayout); @@ -305,7 +311,7 @@ void QQuickStackLayout::rearrange(const QSizeF &newSize) if (d->currentIndex == -1 || d->currentIndex >= m_cachedItemSizeHints.count()) return; - QQuickStackLayout::SizeHints &hints = m_cachedItemSizeHints[d->currentIndex]; + QQuickStackLayout::SizeHints &hints = cachedItemSizeHints(d->currentIndex); QQuickItem *item = itemAt(d->currentIndex); Q_ASSERT(item); item->setPosition(QPointF(0,0)); // ### respect alignment? diff --git a/src/imports/layouts/qquickstacklayout_p.h b/src/imports/layouts/qquickstacklayout_p.h index e1bd84eb3c..e46c608926 100644 --- a/src/imports/layouts/qquickstacklayout_p.h +++ b/src/imports/layouts/qquickstacklayout_p.h @@ -97,6 +97,7 @@ private: mutable QVector<SizeHints> m_cachedItemSizeHints; mutable QSizeF m_cachedSizeHints[Qt::NSizeHints]; + SizeHints &cachedItemSizeHints(int index) const; }; class QQuickStackLayoutPrivate : public QQuickLayoutPrivate diff --git a/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml b/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml index 3a41bdb3e0..de335386cf 100644 --- a/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml +++ b/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml @@ -103,5 +103,55 @@ Item { layout.destroy() } + + Component { + id: layout_setCurrentIndex_Component + + StackLayout { + width: 200 + height: 200 + + property alias firstItem : rect + property alias secondItem: rowLayout + + Rectangle { + id: rect + color: "red" + implicitWidth: 10 + implicitHeight: 10 + } + RowLayout { + id: rowLayout + spacing: 0 + Rectangle { + color: "green" + implicitWidth: 10 + implicitHeight: 10 + Layout.fillWidth: true + Layout.fillHeight: true + } + Rectangle { + color: "blue" + implicitWidth: 10 + implicitHeight: 10 + Layout.fillWidth: true + Layout.fillHeight: true + } + } + } + } + + function test_setCurrentIndex() + { + var layout = layout_setCurrentIndex_Component.createObject(container) + compare(layout.firstItem.width, 200) + + // Invalidate the StackLayout (and its cached size hints) + layout.firstItem.implicitWidth = 42 + + layout.currentIndex = 1 + compare(layout.secondItem.width, 200) // width should not be -1 + layout.destroy() + } } } |