GridView: Set content position when changing cell size

Content size was calculated before the new data was applied.
This caused problems when calculating the column count while
resizing the window.

Pick-to: 5.15
Pick-to: 6.2
Fixes: QTBUG-92998
Change-Id: Ia4401c0e34cd11dc9e6c24a0320dbf0ca2f59ab9
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Tony Leinonen 2021-05-04 17:12:49 +03:00 committed by Shawn Rutledge
parent 2971585b88
commit 6254b38330
3 changed files with 75 additions and 0 deletions

View File

@ -1649,6 +1649,7 @@ void QQuickGridView::setCellWidth(qreal cellWidth)
d->updateViewport();
emit cellWidthChanged();
d->forceLayoutPolish();
QQuickFlickable::setContentX(d->contentXForPosition(d->position()));
}
}
@ -1666,6 +1667,7 @@ void QQuickGridView::setCellHeight(qreal cellHeight)
d->updateViewport();
emit cellHeightChanged();
d->forceLayoutPolish();
QQuickFlickable::setContentY(d->contentYForPosition(d->position()));
}
}
/*!

View File

@ -0,0 +1,53 @@
import QtQuick 2.12
import QtQuick.Window 2.12
Item {
width: 450
height: 650
GridView {
objectName: "gridview"
id: gridView
width: 450
height: 650
layoutDirection: Qt.RightToLeft
property int cells: calcCells(width)
cellWidth: width / cells
cellHeight: cellWidth
delegate: Component {
Item {
width: gridView.cellWidth
height: gridView.cellHeight
Rectangle {
anchors {
fill: parent
margins: 10
}
color: "green"
}
}
}
model: [
{ number: "1" },
{ number: "2" },
{ number: "3" },
{ number: "4" },
{ number: "5" },
{ number: "6" },
{ number: "7" },
{ number: "8" },
{ number: "9" },
{ number: "10" },
{ number: "11" },
{ number: "12" },
{ number: "13" },
{ number: "14" },
{ number: "15" },
{ number: "16" }];
function calcCells(w) {
var rw = 120;
var c = Math.max(1, Math.round(w / rw));
return c;
}
}
}

View File

@ -214,6 +214,7 @@ private slots:
void QTBUG_49218();
void QTBUG_48870_fastModelUpdates();
void QTBUG_86255();
void resizeDynamicCellWidthRtL();
void keyNavigationEnabled();
void releaseItems();
@ -6810,6 +6811,25 @@ void tst_QQuickGridView::QTBUG_86255()
QTRY_COMPARE(view->isFlicking(), false);
}
void tst_QQuickGridView::resizeDynamicCellWidthRtL()
{
QScopedPointer<QQuickView> window(createView());
QTRY_VERIFY(window);
window->setSource(testFileUrl("qtbug92998.qml"));
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window.data()));
QQuickGridView *gridview = findItem<QQuickGridView>(window->rootObject(), "gridview");
QTRY_VERIFY(gridview != nullptr);
QVERIFY(QQuickTest::qWaitForItemPolished(gridview));
gridview->setWidth(460);
QVERIFY(QQuickTest::qWaitForItemPolished(gridview));
QTRY_COMPARE(gridview->contentX(), 0.f);
gridview->setWidth(360);
QVERIFY(QQuickTest::qWaitForItemPolished(gridview));
QTRY_COMPARE(gridview->contentX(), 0.f);
}
void tst_QQuickGridView::releaseItems()
{
QScopedPointer<QQuickView> view(createView());