From da04b1d716617fbe4a2d93dc5a51f38dbd03a5b8 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Fri, 23 Aug 2019 13:11:31 +0200 Subject: QQuickTableView: always relayout after a rebuild The current logic was based on the idea that if both rowHeight-, and columnWidthProveders were set, we didn't have to relayout the items at the end of a rebuild. Because in that case, the row and column sizes would already be correct after the initial load. This assumption turns out to be false, because the providers are allowed to return -1 to signal that the size of a row or column should use default values (meaning, calculated by TableView). And for those cases, we need to do a relayout at the end of a rebuild. Fixes: QTBUG-77074 Change-Id: I0e0f2fdca1cfa9e98f2a0a2b227c3715c16a70f9 Reviewed-by: Mitch Curtis --- src/quick/items/qquicktableview.cpp | 10 +-- .../qquicktableview/data/tweakimplicitsize.qml | 89 ++++++++++++++++++++++ .../quick/qquicktableview/tst_qquicktableview.cpp | 27 +++++++ 3 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 tests/auto/quick/qquicktableview/data/tweakimplicitsize.qml diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 83fa11a446..38b38ddfda 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -1541,15 +1541,7 @@ void QQuickTableViewPrivate::beginRebuildTable() void QQuickTableViewPrivate::layoutAfterLoadingInitialTable() { - if (rowHeightProvider.isUndefined() || columnWidthProvider.isUndefined()) { - // Since we don't have both size providers, we need to calculate the - // size of each row and column based on the size of the delegate items. - // This couldn't be done while we were loading the initial rows and - // columns, since during the process, we didn't have all the items - // available yet for the calculation. So we do it now. - relayoutTable(); - } - + relayoutTable(); updateAverageEdgeSize(); updateContentWidth(); updateContentHeight(); diff --git a/tests/auto/quick/qquicktableview/data/tweakimplicitsize.qml b/tests/auto/quick/qquicktableview/data/tweakimplicitsize.qml new file mode 100644 index 0000000000..ecc79a9368 --- /dev/null +++ b/tests/auto/quick/qquicktableview/data/tweakimplicitsize.qml @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.12 +import QtQuick.Window 2.3 + +Item { + width: 640 + height: 450 + + property alias tableView: tableView + property real delegateSize: 10 + property int hideRow: -1 + + TableView { + id: tableView + width: 600 + height: 400 + anchors.margins: 1 + clip: true + delegate: tableViewDelegate + columnSpacing: 1 + rowSpacing: 1 + columnWidthProvider: function(column) { + return -1 + } + rowHeightProvider: function(row) { + if (row === hideRow) + return 0 + return -1 + } + } + + Component { + id: tableViewDelegate + Rectangle { + objectName: "tableViewDelegate" + implicitWidth: row === 0 ? 10 : delegateSize + implicitHeight: column === 0 ? 10 : delegateSize + color: "lightgray" + border.width: 1 + + Text { + id: textItem + anchors.centerIn: parent + text: model.display + renderType: Text.NativeRendering + } + } + } + +} + diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index 60d48bb59f..acb475b1c5 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -119,6 +119,7 @@ private slots: void checkRowHeightProviderNegativeReturnValue(); void checkRowHeightProviderNotCallable(); void checkForceLayoutFunction(); + void checkForceLayoutEndUpDoingALayout(); void checkContentWidthAndHeight(); void checkPageFlicking(); void checkExplicitContentWidthAndHeight(); @@ -550,6 +551,32 @@ void tst_QQuickTableView::checkForceLayoutFunction() QCOMPARE(fxItem->item->width(), newColumnWidth); } +void tst_QQuickTableView::checkForceLayoutEndUpDoingALayout() +{ + // QTBUG-77074 + // Check that we change the implicit size of the delegate after + // the initial loading, and at the same time hide some rows or + // columns, and then do a forceLayout(), we end up with a + // complete relayout that respects the new implicit size. + LOAD_TABLEVIEW("tweakimplicitsize.qml"); + + auto model = TestModelAsVariant(10, 10); + + tableView->setModel(model); + + WAIT_UNTIL_POLISHED; + + const qreal newDelegateSize = 20; + view->rootObject()->setProperty("delegateSize", newDelegateSize); + // Hide a row, just to force the following relayout to + // do a complete reload (and not just a relayout) + view->rootObject()->setProperty("hideRow", 1); + tableView->forceLayout(); + + for (auto fxItem : tableViewPrivate->loadedItems) + QCOMPARE(fxItem->item->height(), newDelegateSize); +} + void tst_QQuickTableView::checkContentWidthAndHeight() { // Check that contentWidth/Height reports the correct size of the the -- cgit v1.2.3