SwipeView: reposition items that aren't visible after startup too

7a5bbc2315 repositioned items upon
startup, but didn't account for items added afterwards. Do that here
so that semi-transparent pages don't show pages beneath them.

Fixes: QTBUG-115468
Pick-to: 6.2 6.5 6.6
Change-Id: If5ca587a6c14ddae5108c0a45d13fa2d290d1865
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Mitch Curtis 2023-08-02 16:24:09 +08:00
parent 08b2302310
commit 2cf897f2a1
2 changed files with 30 additions and 17 deletions

View File

@ -77,7 +77,7 @@ class QQuickSwipeViewPrivate : public QQuickContainerPrivate
Q_DECLARE_PUBLIC(QQuickSwipeView)
public:
void resizeItem(QQuickItem *item);
void resizeItem(int index, QQuickItem *item);
void resizeItems();
static QQuickSwipeViewPrivate *get(QQuickSwipeView *view);
@ -111,25 +111,29 @@ public:
int currentIndex = -1;
};
void QQuickSwipeViewPrivate::resizeItem(int index, QQuickItem *item)
{
QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors;
// TODO: expose QQuickAnchorLine so we can test for other conflicting anchors
if (anchors && (anchors->fill() || anchors->centerIn()) && !item->property("_q_QQuickSwipeView_warned").toBool()) {
qmlWarning(item) << "SwipeView has detected conflicting anchors. Unable to layout the item.";
item->setProperty("_q_QQuickSwipeView_warned", true);
}
if (orientation == Qt::Horizontal)
item->setPosition({index * (contentItem->width() + spacing), 0});
else
item->setPosition({0, index * (contentItem->height() + spacing)});
item->setSize(QSizeF(contentItem->width(), contentItem->height()));
}
void QQuickSwipeViewPrivate::resizeItems()
{
Q_Q(QQuickSwipeView);
const int count = q->count();
for (int i = 0; i < count; ++i) {
QQuickItem *item = itemAt(i);
if (item) {
QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors;
// TODO: expose QQuickAnchorLine so we can test for other conflicting anchors
if (anchors && (anchors->fill() || anchors->centerIn()) && !item->property("_q_QQuickSwipeView_warned").toBool()) {
qmlWarning(item) << "SwipeView has detected conflicting anchors. Unable to layout the item.";
item->setProperty("_q_QQuickSwipeView_warned", true);
}
if (orientation == Qt::Horizontal)
item->setPosition({i * (contentItem->width() + spacing), 0});
else
item->setPosition({0, i * (contentItem->height() + spacing)});
item->setSize(QSizeF(contentItem->width(), contentItem->height()));
}
if (item)
resizeItem(i, item);
}
}
@ -286,7 +290,7 @@ void QQuickSwipeView::itemAdded(int index, QQuickItem *item)
{
Q_D(QQuickSwipeView);
if (isComponentComplete())
item->setSize(QSizeF(d->contentItem->width(), d->contentItem->height()));
d->resizeItem(index, item);
QQuickSwipeViewAttached *attached = qobject_cast<QQuickSwipeViewAttached *>(qmlAttachedPropertiesObject<QQuickSwipeView>(item));
if (attached)
QQuickSwipeViewAttachedPrivate::get(attached)->update(this, index);

View File

@ -669,7 +669,7 @@ TestCase {
compare(control.spacing, 10)
compare(control.orientation, Qt.Horizontal)
for (var i = 0; i < control.count; ++i) {
for (let i = 0; i < control.count; ++i) {
const page = control.itemAt(i)
compare(page.x, i * (control.contentItem.width + control.spacing))
compare(page.y, 0)
@ -677,12 +677,21 @@ TestCase {
compare(page.height, control.contentItem.height)
}
control.orientation = Qt.Vertical
for (var i = 0; i < control.count; ++i) {
for (let i = 0; i < control.count; ++i) {
const page = control.itemAt(i)
compare(page.y, i * (control.contentItem.height + control.spacing))
compare(page.x, 0)
compare(page.width, control.contentItem.width)
compare(page.height, control.contentItem.height)
}
// QTBUG-115468: add a page after startup and check that that works too.
control.orientation = Qt.Horizontal
let page4 = page.createObject(control, { text: "page 4", "font.pointSize": 40 })
control.insertItem(control.count, page4)
compare(page4.x, (control.count - 1) * 310)
compare(page4.y, 0)
compare(page4.width, control.contentItem.width)
compare(page4.height, control.contentItem.height)
}
}