diff options
author | Jan Arve Sæther <jan-arve.saether@qt.io> | 2022-09-21 13:44:19 +0200 |
---|---|---|
committer | Jan Arve Sæther <jan-arve.saether@qt.io> | 2022-10-07 12:32:00 +0200 |
commit | c41075d9e2808f636d793c93e237f80a8ecadb2e (patch) | |
tree | 05c419c97ba16a2087d156b1a53ca293af8a1110 /src/quicklayouts | |
parent | b25555426eb0255f5831f6d4c482c9710c5b46a3 (diff) |
Fix wrong item-sizeHint-cache when StackLayout children were reordered
This fixes the Tests_StackLayout::test_addAndRemoveItems() autotest
The code uses an index to look up the cached size hints. This index
corresponds to the layout's item child index (which doesn't have to
correspond to the children index). The vector of sizeHints then had to
be in sync with the index of the child layout items for this to work.
The problem here was that if the first item in the stack was removed (or
siblings was reordered), the vector was not adjusted for this
(basically we could get the size hint of an previously removed item).
In order to avoid to keep the QVector<SizeHints> index in sync with the
layout children index, we change it to use a QHash instead, where we
look up by QQuickItem*.
QHash<QQuickItem*, SizeHints>
Task-number: QTBUG-106520
Task-number: QTBUG-106521
Pick-to: 6.4 6.2 5.15
Change-Id: I7c1f9fb018fcab093b074c45dfaba264f76749f4
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicklayouts')
-rw-r--r-- | src/quicklayouts/qquickstacklayout.cpp | 21 | ||||
-rw-r--r-- | src/quicklayouts/qquickstacklayout_p.h | 2 |
2 files changed, 13 insertions, 10 deletions
diff --git a/src/quicklayouts/qquickstacklayout.cpp b/src/quicklayouts/qquickstacklayout.cpp index c81b0b5acc..823a79f0ca 100644 --- a/src/quicklayouts/qquickstacklayout.cpp +++ b/src/quicklayouts/qquickstacklayout.cpp @@ -168,6 +168,7 @@ void QQuickStackLayout::itemChange(QQuickItem::ItemChange change, const QQuickIt stackLayoutAttached->setIndex(-1); stackLayoutAttached->setIsCurrentItem(false); } + m_cachedItemSizeHints.remove(item); childItemsChanged(); invalidate(); } else if (change == ItemChildAddedChange) { @@ -256,11 +257,11 @@ void QQuickStackLayout::setAlignment(QQuickItem * /*item*/, Qt::Alignment /*alig void QQuickStackLayout::invalidate(QQuickItem *childItem) { - const int indexOfChild = indexOf(childItem); - if (indexOfChild >= 0 && indexOfChild < m_cachedItemSizeHints.count()) { - m_cachedItemSizeHints[indexOfChild].min() = QSizeF(); - m_cachedItemSizeHints[indexOfChild].pref() = QSizeF(); - m_cachedItemSizeHints[indexOfChild].max() = QSizeF(); + if (childItem) { + SizeHints &hints = m_cachedItemSizeHints[childItem]; + hints.min() = QSizeF(); + hints.pref() = QSizeF(); + hints.max() = QSizeF(); } for (int i = 0; i < Qt::NSizeHints; ++i) @@ -285,7 +286,6 @@ void QQuickStackLayout::childItemsChanged() if (count != d->count) { d->count = count; emit countChanged(); - m_cachedItemSizeHints.resize(count); } for (int i = 0; i < count; ++i) { QQuickItem *child = itemAt(i); @@ -301,10 +301,13 @@ void QQuickStackLayout::childItemsChanged() } } -QQuickStackLayout::SizeHints &QQuickStackLayout::cachedItemSizeHints(int index) const { - SizeHints &hints = m_cachedItemSizeHints[index]; +QQuickStackLayout::SizeHints &QQuickStackLayout::cachedItemSizeHints(int index) const +{ + QQuickItem *item = itemAt(index); + Q_ASSERT(item); + SizeHints &hints = m_cachedItemSizeHints[item]; // will create an entry if it doesn't exist if (!hints.min().isValid()) - QQuickStackLayout::collectItemSizeHints(itemAt(index), hints.array); + QQuickStackLayout::collectItemSizeHints(item, hints.array); return hints; } diff --git a/src/quicklayouts/qquickstacklayout_p.h b/src/quicklayouts/qquickstacklayout_p.h index bf52e83e73..a27f7f6f7a 100644 --- a/src/quicklayouts/qquickstacklayout_p.h +++ b/src/quicklayouts/qquickstacklayout_p.h @@ -74,7 +74,7 @@ private: QSizeF array[Qt::NSizeHints]; }; - mutable QVector<SizeHints> m_cachedItemSizeHints; + mutable QHash<QQuickItem*, SizeHints> m_cachedItemSizeHints; mutable QSizeF m_cachedSizeHints[Qt::NSizeHints]; SizeHints &cachedItemSizeHints(int index) const; }; |