diff options
-rw-r--r-- | src/qml/types/qqmldelegatemodel.cpp | 8 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml | 94 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/tst_qquicktableview.cpp | 29 |
3 files changed, 126 insertions, 5 deletions
diff --git a/src/qml/types/qqmldelegatemodel.cpp b/src/qml/types/qqmldelegatemodel.cpp index 41970ce626..6732be9844 100644 --- a/src/qml/types/qqmldelegatemodel.cpp +++ b/src/qml/types/qqmldelegatemodel.cpp @@ -2093,9 +2093,7 @@ void QQmlDelegateModelItem::Dispose() void QQmlDelegateModelItem::setModelIndex(int idx, int newRow, int newColumn) { - if (idx == index) - return; - + const int prevIndex = index; const int prevRow = row; const int prevColumn = column; @@ -2103,8 +2101,8 @@ void QQmlDelegateModelItem::setModelIndex(int idx, int newRow, int newColumn) row = newRow; column = newColumn; - Q_EMIT modelIndexChanged(); - + if (idx != prevIndex) + emit modelIndexChanged(); if (row != prevRow) emit rowChanged(); if (column != prevColumn) diff --git a/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml b/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml new file mode 100644 index 0000000000..bef0df2501 --- /dev/null +++ b/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** 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 +import Qt.labs.qmlmodels 1.0 + +Item { + width: 640 + height: 450 + + property alias tableView: tableView + + TableView { + id: tableView + width: 600 + height: 400 + delegate: DelegateChooser { + DelegateChoice { + row: 0 + column: 0 + delegate: maskDelegate + } + DelegateChoice { + row: 1 + column: 1 + delegate: maskDelegate + } + DelegateChoice { + delegate: tableViewDelegate + } + } + } + + Component { + // Add this mask delegate, to force QQmlTableInstanceModel to + // reuse the precise cells that we want to swap in the test + id: maskDelegate + Rectangle { + implicitWidth: 100 + implicitHeight: 50 + color: "green" + } + } + + Component { + id: tableViewDelegate + Rectangle { + implicitWidth: 100 + implicitHeight: 50 + Text { + anchors.fill: parent + text: column + "," + row + } + } + } + +} diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index b49164a78c..ea3531ba13 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -127,6 +127,7 @@ private slots: void checkContextProperties(); void checkContextPropertiesQQmlListProperyModel_data(); void checkContextPropertiesQQmlListProperyModel(); + void checkRowAndColumnChangedButNotIndex(); }; tst_QQuickTableView::tst_QQuickTableView() @@ -1593,6 +1594,34 @@ void tst_QQuickTableView::checkContextPropertiesQQmlListProperyModel() } } +void tst_QQuickTableView::checkRowAndColumnChangedButNotIndex() +{ + // Check that context row and column changes even if the index stays the + // same when the item is reused. This can happen in rare cases if the item + // is first used at e.g (row 1, col 0), but then reused at (row 0, col 1) + // while the model has changed row count in-between. + LOAD_TABLEVIEW("checkrowandcolumnnotchanged.qml"); + + TestModel model(2, 1); + tableView->setModel(QVariant::fromValue(&model)); + + WAIT_UNTIL_POLISHED; + + model.removeRow(1); + model.insertColumn(1); + tableView->forceLayout(); + + const auto item = tableViewPrivate->loadedTableItem(QPoint(1, 0))->item; + const auto context = qmlContext(item.data()); + const int contextIndex = context->contextProperty("index").toInt(); + const int contextRow = context->contextProperty("row").toInt(); + const int contextColumn = context->contextProperty("column").toInt(); + + QCOMPARE(contextIndex, 1); + QCOMPARE(contextRow, 0); + QCOMPARE(contextColumn, 1); +} + QTEST_MAIN(tst_QQuickTableView) #include "tst_qquicktableview.moc" |