aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/quick/tableview/gameoflife/main.qml7
-rw-r--r--src/quick/items/qquicktableview.cpp8
-rw-r--r--src/quick/items/qquicktableview_p_p.h1
-rw-r--r--tests/auto/quick/qquicktableview/data/setcontentpos.qml8
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp26
5 files changed, 32 insertions, 18 deletions
diff --git a/examples/quick/tableview/gameoflife/main.qml b/examples/quick/tableview/gameoflife/main.qml
index bf8c1151fe..90be69b9c0 100644
--- a/examples/quick/tableview/gameoflife/main.qml
+++ b/examples/quick/tableview/gameoflife/main.qml
@@ -97,11 +97,8 @@ ApplicationWindow {
//! [model]
//! [scroll]
- Component.onCompleted: {
- tableView.forceLayout()
- tableView.contentX = (tableView.contentWidth - tableView.width) / 2;
- tableView.contentY = (tableView.contentHeight - tableView.height) / 2;
- }
+ contentX: (contentWidth - width) / 2;
+ contentY: (contentHeight - height) / 2;
//! [scroll]
}
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 508b5b0450..25701e82b4 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -1495,6 +1495,14 @@ void QQuickTableViewPrivate::updatePolish()
loadAndUnloadVisibleEdges();
}
+void QQuickTableViewPrivate::fixup(QQuickFlickablePrivate::AxisData &data, qreal minExtent, qreal maxExtent)
+{
+ if (rebuildScheduled || rebuildState != RebuildState::Done)
+ return;
+
+ QQuickFlickablePrivate::fixup(data, minExtent, maxExtent);
+}
+
void QQuickTableViewPrivate::createWrapperModel()
{
Q_Q(QQuickTableView);
diff --git a/src/quick/items/qquicktableview_p_p.h b/src/quick/items/qquicktableview_p_p.h
index 9d5b5783e5..9ff2b2e10b 100644
--- a/src/quick/items/qquicktableview_p_p.h
+++ b/src/quick/items/qquicktableview_p_p.h
@@ -193,6 +193,7 @@ public:
static inline QQuickTableViewPrivate *get(QQuickTableView *q) { return q->d_func(); }
void updatePolish() override;
+ void fixup(AxisData &data, qreal minExtent, qreal maxExtent) override;
public:
QHash<int, FxTableItem *> loadedItems;
diff --git a/tests/auto/quick/qquicktableview/data/setcontentpos.qml b/tests/auto/quick/qquicktableview/data/setcontentpos.qml
index bff97e4e5e..fa040d3959 100644
--- a/tests/auto/quick/qquicktableview/data/setcontentpos.qml
+++ b/tests/auto/quick/qquicktableview/data/setcontentpos.qml
@@ -48,15 +48,13 @@ Item {
TableView {
id: tableView
- width: 600
+ width: 400
height: 400
anchors.margins: 1
clip: true
delegate: tableViewDelegate
- columnSpacing: 1
- rowSpacing: 1
- contentX: 250
- contentY: 250
+ contentX: (contentWidth - width) / 2;
+ contentY: (contentHeight - height) / 2;
}
Component {
diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
index dfcce6afdb..9eb942fc93 100644
--- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
+++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
@@ -594,21 +594,31 @@ void tst_QQuickTableView::checkExplicitContentWidthAndHeight()
void tst_QQuickTableView::checkContentXY()
{
- // Check that you can set contentX and contentY on
- // startup, and that this is respected by TableView
+ // Check that you can bind contentX and contentY to
+ // e.g show the center of the table at start-up
LOAD_TABLEVIEW("setcontentpos.qml");
auto model = TestModelAsVariant(10, 10);
tableView->setModel(model);
WAIT_UNTIL_POLISHED;
- QCOMPARE(tableView->contentX(), 250);
- QCOMPARE(tableView->contentY(), 250);
+ QCOMPARE(tableView->width(), 400);
+ QCOMPARE(tableView->height(), 400);
+ QCOMPARE(tableView->contentWidth(), 1000);
+ QCOMPARE(tableView->contentHeight(), 1000);
- // Since we flick the content item, we expect the
- // loaded table to end up at row/column 2,2
- QCOMPARE(tableViewPrivate->loadedTable.left(), 2);
- QCOMPARE(tableViewPrivate->loadedTable.top(), 2);
+ // Check that the content item is positioned according
+ // to the binding in the QML file (which will set the
+ // viewport to be at the center of the table).
+ const qreal expectedXY = (tableView->contentWidth() - tableView->width()) / 2;
+ QCOMPARE(tableView->contentX(), expectedXY);
+ QCOMPARE(tableView->contentY(), expectedXY);
+
+ // Check that we end up at the correct top-left cell:
+ const qreal delegateWidth = tableViewPrivate->loadedItems.values().first()->item->width();
+ const int expectedCellXY = qCeil(expectedXY / delegateWidth);
+ QCOMPARE(tableViewPrivate->loadedTable.left(), expectedCellXY);
+ QCOMPARE(tableViewPrivate->loadedTable.top(), expectedCellXY);
}
void tst_QQuickTableView::noDelegate()