aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-11-10 20:42:22 +0100
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-11-15 11:05:51 +0100
commit7e0bc47335b7f64c8bc0da7447e5a0e468dd7c32 (patch)
tree3ba1bbd22f7ff5b9b89ed252aa43c38c41858ca1
parent4d480fdc685af34af74819f79bdc40f525ffbf3d (diff)
QQuickTableView: do a relayout whenever the view is resized
If you place a TableView in e.g a StackLayout, it will have a zero size until it becomes the active view the layout. If the delegate then has a binding that e.g looks like: implicitWidth: TableView.view.width it will fail, since the width of the view is zero at time of creation, but at the same time, never updated again once the view becomes visible (and resized). This patch will therefore ensure that we do a relayout each time the view is resized, since having such a dependency in the delegate (or in the columnWidthProvider) must be expected to be quite common. Change-Id: I92f4f51f6b6c634c2627716c41ea4971fb8d1653 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io> (cherry picked from commit 3cacdf288fa70e6f108b79a5752682329babf4c6) Reviewed-by: Samuel Ghinet <samuel.ghinet@qt.io>
-rw-r--r--src/quick/items/qquicktableview.cpp5
-rw-r--r--tests/auto/quick/qquicktableview/data/columnwidthboundtoviewwidth.qml40
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp27
3 files changed, 69 insertions, 3 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 8aaf00c775..43c3364e21 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -4955,7 +4955,10 @@ void QQuickTableView::geometryChange(const QRectF &newGeometry, const QRectF &ol
d->tableModel->drainReusableItemsPool(0);
}
- polish();
+ d->scheduleRebuildTable(
+ QQuickTableViewPrivate::RebuildOption::LayoutOnly |
+ QQuickTableViewPrivate::RebuildOption::CalculateNewContentWidth |
+ QQuickTableViewPrivate::RebuildOption::CalculateNewContentHeight);
}
void QQuickTableView::viewportMoved(Qt::Orientations orientation)
diff --git a/tests/auto/quick/qquicktableview/data/columnwidthboundtoviewwidth.qml b/tests/auto/quick/qquicktableview/data/columnwidthboundtoviewwidth.qml
new file mode 100644
index 0000000000..bbbdf74d4e
--- /dev/null
+++ b/tests/auto/quick/qquicktableview/data/columnwidthboundtoviewwidth.qml
@@ -0,0 +1,40 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+import QtQuick 2.12
+import QtQuick.Window 2.3
+
+Item {
+ width: 640
+ height: 450
+
+ property alias tableView: tableView
+ property Component delegate: tableViewDelegate
+
+ TableView {
+ id: tableView
+ width: 600
+ height: 400
+ anchors.margins: 1
+ clip: true
+ delegate: tableViewDelegate
+ columnSpacing: 1
+ rowSpacing: 1
+ }
+
+ Component {
+ id: tableViewDelegate
+ Rectangle {
+ implicitWidth: tableView.width
+ implicitHeight: 50
+ objectName: "tableViewDelegate"
+ color: "lightgray"
+ border.width: 1
+ Text {
+ anchors.centerIn: parent
+ text: modelData
+ }
+ }
+ }
+
+}
diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
index 67e32d80de..0005a9af6f 100644
--- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
+++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
@@ -90,6 +90,7 @@ private slots:
void checkColumnWidthProviderInvalidReturnValues();
void checkColumnWidthProviderNegativeReturnValue();
void checkColumnWidthProviderNotCallable();
+ void checkColumnWidthBoundToViewWidth();
void checkRowHeightWithoutProvider();
void checkRowHeightProvider();
void checkRowHeightProviderInvalidReturnValues();
@@ -286,8 +287,9 @@ void tst_QQuickTableView::emptyModel()
LOAD_TABLEVIEW("plaintableview.qml");
tableView->setModel(model);
- WAIT_UNTIL_POLISHED;
- QCOMPARE(tableViewPrivate->loadedItems.count(), 0);
+ if (QQuickTest::qIsPolishScheduled(tableView))
+ WAIT_UNTIL_POLISHED;
+ QCOMPARE(tableViewPrivate->loadedItems.size(), 0);
}
void tst_QQuickTableView::checkPreload_data()
@@ -506,6 +508,27 @@ void tst_QQuickTableView::checkColumnWidthProviderNotCallable()
QCOMPARE(fxItem->item->width(), kDefaultColumnWidth);
}
+void tst_QQuickTableView::checkColumnWidthBoundToViewWidth()
+{
+ // Check that you can bind the width of a delegate to the
+ // width of TableView, and that it updates when TableView is resized.
+ LOAD_TABLEVIEW("columnwidthboundtoviewwidth.qml");
+
+ auto model = TestModelAsVariant(10, 1);
+ tableView->setModel(model);
+
+ WAIT_UNTIL_POLISHED;
+
+ for (auto fxItem : tableViewPrivate->loadedItems)
+ QCOMPARE(fxItem->item->width(), tableView->width());
+
+ tableView->setWidth(200);
+ WAIT_UNTIL_POLISHED;
+
+ for (auto fxItem : tableViewPrivate->loadedItems)
+ QCOMPARE(fxItem->item->width(), 200);
+}
+
void tst_QQuickTableView::checkRowHeightWithoutProvider()
{
// Checks that a function isn't assigned to the rowHeightProvider property