diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2023-01-20 10:49:31 +0100 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2023-02-14 10:19:49 +0100 |
commit | 291d3ed0da263103ca2d612eb2f470ee160e8fb6 (patch) | |
tree | 70c9dff53b09f66a79caa673209a80c1988a4ad0 | |
parent | 301c09c7ad39a24ce1d3e9544c1cff78707e740a (diff) |
TableView: deprecate modelIndex(row, column) in favor of index(row, column)
Deprecate modelIndex(row, column) in favor of a new
function index(row, column), starting from Qt 6.4.3.
This has the advantage that we can print out a deprecation
warning when modelIndex() is is used at run-time from QML.
This will inform the developer early on that he should
avoid using modelIndex(row, column), since the argument
order differs between different versions of Qt.
Relying on compile time deprecation macros alone will not
work, since the API break is in a private class and only
used publicly from QML (except for our own internal auto
tests).
Since the equivalent function in QAIM is also called
index(row, column), this will additionally make the
naming more consistent with the model classes.
[ChangeLog][Quick][TableView] modelIndex(row, column)
has been deprecated in favor of index(row, column).
Task-number: QTBUG-109542
Change-Id: Iec8f5f8fa2a0ca714153128717b546bbd62c7e2c
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit aea8c9e0934b7250b73d7c055f907abab7c8e868)
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 42 | ||||
-rw-r--r-- | src/quick/items/qquicktableview_p.h | 4 | ||||
-rw-r--r-- | src/quick/items/qquicktreeview.cpp | 17 | ||||
-rw-r--r-- | src/quick/items/qquicktreeview_p.h | 6 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/tst_qquicktableview.cpp | 2 | ||||
-rw-r--r-- | tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp | 46 |
6 files changed, 75 insertions, 42 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 78994b9de0..c8d07ace6f 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -859,18 +859,14 @@ /*! \qmlmethod QModelIndex QtQuick::TableView::modelIndex(int column, int row) \since 6.4 + \deprecated - Returns the \l QModelIndex that maps to \a column and \a row in the view. + Use \l index(row, column) instead. - \a row and \a column should be the row and column in the view (table row and - table column), and not a row and column in the model. - - \note Because of an API incompatible change in Qt 6.4.0 and Qt 6.4.1, the + \note Because of an API incompatible change between Qt 6.4.0 and Qt 6.4.2, the order of \c row and \c column was specified in the opposite order. If you rely on the order to be \c {modelIndex(column, row)}, you can set the environment variable \c QT_QUICK_TABLEVIEW_COMPAT_VERSION to \c 6.4 - - \sa rowAtIndex(), columnAtIndex() */ /*! @@ -889,12 +885,28 @@ */ /*! + \qmlmethod QModelIndex QtQuick::TableView::index(int row, int column) + \since 6.4.3 + + Returns the \l QModelIndex that maps to \a row and \a column in the view. + + \a row and \a column should be the row and column in the view (table row and + table column), and not a row and column in the model. For a plain + TableView, this is equivalent of calling \c {model.index(row, column).} + But for a subclass of TableView, like TreeView, where the data model is + wrapped inside an internal proxy model that flattens the tree structure + into a table, you need to use this function to resolve the model index. + + \sa rowAtIndex(), columnAtIndex() +*/ + +/*! \qmlmethod int QtQuick::TableView::rowAtIndex(QModelIndex modelIndex) \since 6.4 Returns the row in the view that maps to \a modelIndex in the model. - \sa columnAtIndex(), modelIndex() + \sa columnAtIndex(), index() */ /*! @@ -903,7 +915,7 @@ Returns the column in the view that maps to \a modelIndex in the model. - \sa rowAtIndex(), modelIndex() + \sa rowAtIndex(), index() */ /*! @@ -4934,6 +4946,7 @@ QPoint QQuickTableView::cellAtIndex(const QModelIndex &index) const return {index.column(), index.row()}; } +#if QT_DEPRECATED_SINCE(6, 4) QModelIndex QQuickTableView::modelIndex(int row, int column) const { static bool compat6_4 = qEnvironmentVariable("QT_QUICK_TABLEVIEW_COMPAT_VERSION") == QStringLiteral("6.4"); @@ -4944,9 +4957,18 @@ QModelIndex QQuickTableView::modelIndex(int row, int column) const // to continue accepting calls to modelIndex(column, row). return modelIndex({row, column}); } else { + qmlWarning(this) << "modelIndex(row, column) is deprecated. " + "Use index(row, column) instead. For more information, see " + "https://doc.qt.io/qt-6/qml-qtquick-tableview-obsolete.html"; return modelIndex({column, row}); } } +#endif + +QModelIndex QQuickTableView::index(int row, int column) const +{ + return modelIndex({column, row}); +} int QQuickTableView::rowAtIndex(const QModelIndex &index) const { @@ -5050,7 +5072,7 @@ void QQuickTableView::keyPressEvent(QKeyEvent *e) case Qt::Key_Right: // Special case: the current index doesn't map to a cell in the view (perhaps // because it isn't set yet). In that case, we set it to be the top-left cell. - const QModelIndex topLeftIndex = modelIndex(leftColumn(), topRow()); + const QModelIndex topLeftIndex = index(topRow(), leftColumn()); d->selectionModel->setCurrentIndex(topLeftIndex, QItemSelectionModel::NoUpdate); } return; diff --git a/src/quick/items/qquicktableview_p.h b/src/quick/items/qquicktableview_p.h index e0fe72efea..442e490a87 100644 --- a/src/quick/items/qquicktableview_p.h +++ b/src/quick/items/qquicktableview_p.h @@ -159,6 +159,8 @@ public: Q_REVISION(6, 4) Q_INVOKABLE QPoint cellAtPosition(const QPointF &position, bool includeSpacing = false) const; Q_REVISION(6, 4) Q_INVOKABLE QPoint cellAtPosition(qreal x, qreal y, bool includeSpacing = false) const; #if QT_DEPRECATED_SINCE(6, 4) + QT_DEPRECATED_VERSION_X_6_4("Use index(row, column) instead") + Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(int row, int column) const; QT_DEPRECATED_VERSION_X_6_4("Use cellAtPosition() instead") Q_INVOKABLE QPoint cellAtPos(const QPointF &position, bool includeSpacing = false) const; Q_INVOKABLE QPoint cellAtPos(qreal x, qreal y, bool includeSpacing = false) const; @@ -172,8 +174,8 @@ public: Q_REVISION(6, 2) Q_INVOKABLE qreal implicitColumnWidth(int column) const; Q_REVISION(6, 2) Q_INVOKABLE qreal implicitRowHeight(int row) const; + Q_REVISION(6, 4) Q_INVOKABLE QModelIndex index(int row, int column) const; Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(const QPoint &cell) const; - Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(int row, int column) const; Q_REVISION(6, 4) Q_INVOKABLE virtual QPoint cellAtIndex(const QModelIndex &index) const; Q_REVISION(6, 4) Q_INVOKABLE int rowAtIndex(const QModelIndex &index) const; Q_REVISION(6, 4) Q_INVOKABLE int columnAtIndex(const QModelIndex &index) const; diff --git a/src/quick/items/qquicktreeview.cpp b/src/quick/items/qquicktreeview.cpp index 0dd6f68cf0..3ba739d0ca 100644 --- a/src/quick/items/qquicktreeview.cpp +++ b/src/quick/items/qquicktreeview.cpp @@ -338,8 +338,8 @@ void QQuickTreeViewPrivate::updateSelection(const QRect &oldSelection, const QRe for (int row = newRect.y(); row <= newRect.y() + newRect.height(); ++row) { if (oldRect.y() != -1 && oldRect.y() <= row && row <= oldRect.y() + oldRect.height()) continue; - const QModelIndex startIndex = q->modelIndex(row, newRect.x()); - const QModelIndex endIndex = q->modelIndex(row, newRect.x() + newRect.width()); + const QModelIndex startIndex = q->index(row, newRect.x()); + const QModelIndex endIndex = q->index(row, newRect.x() + newRect.width()); selectionModel->select(QItemSelection(startIndex, endIndex), QItemSelectionModel::Select); } @@ -351,15 +351,15 @@ void QQuickTreeViewPrivate::updateSelection(const QRect &oldSelection, const QRe if (oldRect.x() <= column && column <= oldRect.x() + oldRect.width()) continue; for (int row = newRect.y(); row <= newRect.y() + newRect.height(); ++row) - selectionModel->select(q->modelIndex(row, column), QItemSelectionModel::Select); + selectionModel->select(q->index(row, column), QItemSelectionModel::Select); } // Unselect the rows inside oldRect that don't overlap with newRect for (int row = oldRect.y(); row <= oldRect.y() + oldRect.height(); ++row) { if (newRect.y() <= row && row <= newRect.y() + newRect.height()) continue; - const QModelIndex startIndex = q->modelIndex(row, oldRect.x()); - const QModelIndex endIndex = q->modelIndex(row, oldRect.x() + oldRect.width()); + const QModelIndex startIndex = q->index(row, oldRect.x()); + const QModelIndex endIndex = q->index(row, oldRect.x() + oldRect.width()); selectionModel->select(QItemSelection(startIndex, endIndex), QItemSelectionModel::Deselect); } @@ -375,7 +375,7 @@ void QQuickTreeViewPrivate::updateSelection(const QRect &oldSelection, const QRe // performance. But large selections containing a lot of columns is not normally // the case for a treeview, so accept this potential corner case for now. for (int row = newRect.y(); row <= newRect.y() + newRect.height(); ++row) - selectionModel->select(q->modelIndex(row, column), QItemSelectionModel::Deselect); + selectionModel->select(q->index(row, column), QItemSelectionModel::Deselect); } } } @@ -588,6 +588,7 @@ QPoint QQuickTreeView::cellAtIndex(const QModelIndex &index) const return QPoint(tableIndex.column(), tableIndex.row()); } +#if QT_DEPRECATED_SINCE(6, 4) QModelIndex QQuickTreeView::modelIndex(int row, int column) const { static const bool compat6_4 = qEnvironmentVariable("QT_QUICK_TABLEVIEW_COMPAT_VERSION") == QStringLiteral("6.4"); @@ -599,9 +600,13 @@ QModelIndex QQuickTreeView::modelIndex(int row, int column) const // to continue accepting calls to modelIndex(column, row). return modelIndex({row, column}); } else { + qmlWarning(this) << "modelIndex(row, column) is deprecated. " + "Use index(row, column) instead. For more information, see " + "https://doc.qt.io/qt-6/qml-qtquick-tableview-obsolete.html"; return modelIndex({column, row}); } } +#endif void QQuickTreeView::keyPressEvent(QKeyEvent *event) { diff --git a/src/quick/items/qquicktreeview_p.h b/src/quick/items/qquicktreeview_p.h index b499f900cd..3b77477684 100644 --- a/src/quick/items/qquicktreeview_p.h +++ b/src/quick/items/qquicktreeview_p.h @@ -44,9 +44,13 @@ public: Q_REVISION(6, 4) Q_INVOKABLE void expandToIndex(const QModelIndex &index); Q_INVOKABLE QModelIndex modelIndex(const QPoint &cell) const override; - Q_INVOKABLE QModelIndex modelIndex(int row, int column) const override; Q_INVOKABLE QPoint cellAtIndex(const QModelIndex &index) const override; +#if QT_DEPRECATED_SINCE(6, 4) + QT_DEPRECATED_VERSION_X_6_4("Use index(row, column) instead") + Q_REVISION(6, 4) Q_INVOKABLE QModelIndex modelIndex(int row, int column) const override; +#endif + Q_SIGNALS: void expanded(int row, int depth); void collapsed(int row, bool recursively); diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index 0005a9af6f..c433e65e53 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -5058,7 +5058,7 @@ void tst_QQuickTableView::disablePointerNavigation() // Enable navigation, and try again tableView->setPointerNavigationEnabled(true); QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, pos); - QCOMPARE(selectionModel.currentIndex(), tableView->modelIndex(0, 0)); + QCOMPARE(selectionModel.currentIndex(), tableView->index(0, 0)); QVERIFY(item0_0->property("current").toBool()); QCOMPARE(tableView->currentColumn(), cell0_0.x()); QCOMPARE(tableView->currentRow(), cell0_0.y()); diff --git a/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp b/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp index c487d5b6dc..71aa10a518 100644 --- a/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp +++ b/tests/auto/quick/qquicktreeview/tst_qquicktreeview.cpp @@ -301,7 +301,7 @@ void tst_qquicktreeview::requiredPropertiesChildren() QCOMPARE(viewProp, treeView); QCOMPARE(isTreeNode, true); QCOMPARE(expanded, row == 4); - QCOMPARE(hasChildren, model->hasChildren(treeView->modelIndex(row, 0))); + QCOMPARE(hasChildren, model->hasChildren(treeView->index(row, 0))); QCOMPARE(depth, row <= 4 ? 1 : 2); } } @@ -324,7 +324,7 @@ void tst_qquicktreeview::emptyModel() QCOMPARE(treeView->depth(0), -1); QCOMPARE(treeView->isExpanded(0), false); - QVERIFY(!treeView->modelIndex(10, 10).isValid()); + QVERIFY(!treeView->index(10, 10).isValid()); QCOMPARE(treeView->rowAtIndex(QModelIndex()), -1); QCOMPARE(treeView->columnAtIndex(QModelIndex()), -1); } @@ -457,7 +457,7 @@ void tst_qquicktreeview::expandRecursivelyRoot() // Check that all rows after rowToExpand, that are also // children of that row, is expanded (down to depth) for (int currentRow = rowToExpand + 1; currentRow < treeView->rows(); ++currentRow) { - const auto modelIndex = treeView->modelIndex(currentRow, 0); + const auto modelIndex = treeView->index(currentRow, 0); const int currentDepth = treeView->depth(currentRow); const bool isChild = currentDepth > rowToExpandDepth; const bool isExpandable = model->rowCount(modelIndex) > 0; @@ -528,7 +528,7 @@ void tst_qquicktreeview::expandRecursivelyChild() for (int currentRow = rowToExpand + 1; currentRow < treeView->rows(); ++currentRow) { const int currentDepth = treeView->depth(currentRow); const bool isChild = currentDepth > rowToExpandDepth; - const auto modelIndex = treeView->modelIndex(currentRow, 0); + const auto modelIndex = treeView->index(currentRow, 0); const bool isExpandable = model->rowCount(modelIndex) > 0; const bool shouldBeExpanded = isChild && isExpandable && currentDepth < effectiveMaxDepth; QCOMPARE(treeView->isExpanded(currentRow), shouldBeExpanded); @@ -551,7 +551,7 @@ void tst_qquicktreeview::expandRecursivelyWholeTree() // Check that all rows that have children are expanded for (int currentRow = 0; currentRow < treeView->rows(); ++currentRow) { - const auto modelIndex = treeView->modelIndex(currentRow, 0); + const auto modelIndex = treeView->index(currentRow, 0); const bool isExpandable = model->rowCount(modelIndex) > 0; QCOMPARE(treeView->isExpanded(currentRow), isExpandable); } @@ -591,7 +591,7 @@ void tst_qquicktreeview::collapseRecursivelyRoot() // We can do that by simply iterate over the rows in the view as we expand. int currentRow = 0; while (currentRow < treeView->rows()) { - const QModelIndex currentIndex = treeView->modelIndex(currentRow, 0); + const QModelIndex currentIndex = treeView->index(currentRow, 0); if (model->hasChildren(currentIndex)) { QVERIFY(!treeView->isExpanded(currentRow)); treeView->expand(currentRow); @@ -622,7 +622,7 @@ void tst_qquicktreeview::collapseRecursivelyChild() // Collapse the 8th child recursive const int rowToCollapse = 8; - const QModelIndex collapseIndex = treeView->modelIndex(rowToCollapse, 0); + const QModelIndex collapseIndex = treeView->index(rowToCollapse, 0); const auto expectedLabel = model->data(collapseIndex, Qt::DisplayRole); QCOMPARE(expectedLabel, QStringLiteral("3, 0")); treeView->collapseRecursively(rowToCollapse); @@ -642,7 +642,7 @@ void tst_qquicktreeview::collapseRecursivelyChild() // We can do that by simply iterate over the rows in the view as we expand. int currentRow = 1; // start at first child while (currentRow < treeView->rows()) { - const QModelIndex currentIndex = treeView->modelIndex(currentRow, 0); + const QModelIndex currentIndex = treeView->index(currentRow, 0); if (model->hasChildren(currentIndex)) { if (treeView->depth(currentRow) == 1 && currentIndex.row() == 2) { // We did only recursively expand the 4th child, so the @@ -727,7 +727,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys() QQuickWindow *window = treeView->window(); // Start by making cell 0, 0 current - treeView->selectionModel()->setCurrentIndex(treeView->modelIndex(0, 0), QItemSelectionModel::NoUpdate); + treeView->selectionModel()->setCurrentIndex(treeView->index(0, 0), QItemSelectionModel::NoUpdate); // Expand row 0 const int row0 = 0; @@ -743,7 +743,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys() // Hitting Key_Right again should be a no-op QTest::keyPress(window, Qt::Key_Right); QVERIFY(treeView->isExpanded(row0)); - QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row0, 0)); + QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row0, 0)); // Move down to row 1 and try to expand it. Since Row 1 // doesn't have children, expanding it will be a no-op. @@ -754,7 +754,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys() QTest::keyPress(window, Qt::Key_Down); QTest::keyPress(window, Qt::Key_Right); QVERIFY(!treeView->isExpanded(row1)); - QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row1, 0)); + QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row1, 0)); // Move down to row 4 and expand it const int row4 = 4; @@ -764,7 +764,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys() QVERIFY(!treeView->isExpanded(row4)); QTest::keyPress(window, Qt::Key_Right); QVERIFY(treeView->isExpanded(row4)); - QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row4, 0)); + QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row4, 0)); // Move up again to row 0 and collapse it while (treeView->currentRow() != row0) @@ -777,7 +777,7 @@ void tst_qquicktreeview::toggleExpandedUsingArrowKeys() // Hitting Key_Left again should be a no-op QTest::keyPress(window, Qt::Key_Left); QVERIFY(!treeView->isExpanded(row0)); - QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->modelIndex(row0, 0)); + QCOMPARE(treeView->selectionModel()->currentIndex(), treeView->index(row0, 0)); } void tst_qquicktreeview::selectionBehaviorCells_data() @@ -853,7 +853,7 @@ void tst_qquicktreeview::selectionBehaviorCells() for (int x = x1; x < x2; ++x) { for (int y = y1; y < y2; ++y) { - const auto index = treeView->modelIndex(y, x); + const auto index = treeView->index(y, x); QVERIFY(selectionModel->isSelected(index)); } } @@ -906,7 +906,7 @@ void tst_qquicktreeview::selectionBehaviorRows() for (int x = 0; x < treeView->columns(); ++x) { for (int y = 0; y < 3; ++y) { - const auto index = treeView->modelIndex(y, x); + const auto index = treeView->index(y, x); QVERIFY(selectionModel->isSelected(index)); } } @@ -925,7 +925,7 @@ void tst_qquicktreeview::selectionBehaviorRows() for (int x = 0; x < treeView->columns(); ++x) { for (int y = 0; y < 3; ++y) { - const auto index = treeView->modelIndex(y, x); + const auto index = treeView->index(y, x); QVERIFY(selectionModel->isSelected(index)); } } @@ -957,7 +957,7 @@ void tst_qquicktreeview::selectionBehaviorColumns() for (int x = 0; x < 3; ++x) { for (int y = 0; y < treeView->rows(); ++y) { - const auto index = treeView->modelIndex(y, x); + const auto index = treeView->index(y, x); QVERIFY(selectionModel->isSelected(index)); } } @@ -976,7 +976,7 @@ void tst_qquicktreeview::selectionBehaviorColumns() for (int x = 0; x < 3; ++x) { for (int y = 0; y < treeView->rows(); ++y) { - const auto index = treeView->modelIndex(y, x); + const auto index = treeView->index(y, x); QVERIFY(selectionModel->isSelected(index)); } } @@ -1033,7 +1033,7 @@ void tst_qquicktreeview::sortTreeModel() // is the same as in the view. That means that QQmlTreeModelToTableModel // and QSortFilterProxyModel are in sync. for (int row = 0; row < treeView->rows(); ++row) { - const auto index = treeView->modelIndex(row, 0); + const auto index = treeView->index(row, 0); const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString(); const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row)); QVERIFY(childFxItem); @@ -1049,7 +1049,7 @@ void tst_qquicktreeview::sortTreeModel() WAIT_UNTIL_POLISHED; for (int row = 0; row < treeView->rows(); ++row) { - const auto index = treeView->modelIndex(row, 0); + const auto index = treeView->index(row, 0); const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString(); const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row)); QVERIFY(childFxItem); @@ -1098,7 +1098,7 @@ void tst_qquicktreeview::sortTreeModelDynamic() // is the same as in the view. That means that QQmlTreeModelToTableModel // and QSortFilterProxyModel are in sync. for (int row = 0; row < treeView->rows(); ++row) { - const auto index = treeView->modelIndex(row, 0); + const auto index = treeView->index(row, 0); const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString(); const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row)); QVERIFY(childFxItem); @@ -1111,10 +1111,10 @@ void tst_qquicktreeview::sortTreeModelDynamic() // Now change the text in one of the items. This will trigger // a sort for only one of the parents in the model. - proxyModel.setData(treeView->modelIndex(row, 0), u"xxx"_qs, Qt::DisplayRole); + proxyModel.setData(treeView->index(row, 0), u"xxx"_qs, Qt::DisplayRole); for (int row = 0; row < treeView->rows(); ++row) { - const auto index = treeView->modelIndex(row, 0); + const auto index = treeView->index(row, 0); const QString modelDisplay = proxyModel.data(index, Qt::DisplayRole).toString(); const auto childFxItem = treeViewPrivate->loadedTableItem(QPoint(0, row)); QVERIFY(childFxItem); |