From 874f8ef3b9fb9d80a2b05c264ca48464e31257e2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 10 Jul 2019 21:21:17 +0200 Subject: Eradicate Java-style iterators and Q_FOREACH and mark the module free of them ... and QLinkedList. Java-style iterators are scheduled to be deprecated, or at the very least banned from use in Qt's own implementation. Ditto Q_FOREACH. Ditto QLinkedList. Change-Id: I92eb5c22762b63cba45f8eaf717c1b7d458fcda4 Reviewed-by: Volker Hilsheimer Reviewed-by: Andy Shaw Reviewed-by: Mitch Curtis --- .qmake.conf | 2 ++ .../controls/tableview/src/sortfilterproxymodel.cpp | 18 +++--------------- .../controls/texteditor/src/documenthandler.cpp | 3 ++- .../tst_qquicktreemodeladaptor.cpp | 16 ++++++++-------- tests/auto/shared/testmodel.h | 3 +-- tests/benchmarks/objectcount/tst_objectcount.cpp | 4 ++-- 6 files changed, 18 insertions(+), 28 deletions(-) diff --git a/.qmake.conf b/.qmake.conf index 92f4428e..eb6b6a3b 100644 --- a/.qmake.conf +++ b/.qmake.conf @@ -2,4 +2,6 @@ load(qt_build_config) CONFIG += warning_clean android|ios|qnx|isEmpty(QT.widgets.name): CONFIG += no_desktop +DEFINES += QT_NO_FOREACH QT_NO_JAVA_STYLE_ITERATORS QT_NO_LINKED_LIST + MODULE_VERSION = 5.14.0 diff --git a/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp b/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp index b93641a9..1e47f23e 100644 --- a/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp +++ b/examples/quickcontrols/controls/tableview/src/sortfilterproxymodel.cpp @@ -132,11 +132,8 @@ QJSValue SortFilterProxyModel::get(int idx) const QJSValue value = engine->newObject(); if (idx >= 0 && idx < count()) { QHash roles = roleNames(); - QHashIterator it(roles); - while (it.hasNext()) { - it.next(); + for (auto it = roles.cbegin(), end = roles.cend(); it != end; ++it) value.setProperty(QString::fromUtf8(it.value()), data(index(idx, 0), it.key()).toString()); - } } return value; } @@ -156,14 +153,7 @@ void SortFilterProxyModel::componentComplete() int SortFilterProxyModel::roleKey(const QByteArray &role) const { - QHash roles = roleNames(); - QHashIterator it(roles); - while (it.hasNext()) { - it.next(); - if (it.value() == role) - return it.key(); - } - return -1; + return roleNames().key(role, -1); } QHash SortFilterProxyModel::roleNames() const @@ -181,9 +171,7 @@ bool SortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &so QAbstractItemModel *model = sourceModel(); if (filterRole().isEmpty()) { QHash roles = roleNames(); - QHashIterator it(roles); - while (it.hasNext()) { - it.next(); + for (auto it = roles.cbegin(), end = roles.cend(); it != end; ++it) { QModelIndex sourceIndex = model->index(sourceRow, 0, sourceParent); QString key = model->data(sourceIndex, it.key()).toString(); if (key.contains(rx)) diff --git a/examples/quickcontrols/controls/texteditor/src/documenthandler.cpp b/examples/quickcontrols/controls/texteditor/src/documenthandler.cpp index 69da88f0..ac9f5bd4 100644 --- a/examples/quickcontrols/controls/texteditor/src/documenthandler.cpp +++ b/examples/quickcontrols/controls/texteditor/src/documenthandler.cpp @@ -345,7 +345,8 @@ QStringList DocumentHandler::defaultFontSizes() const // uhm... this is quite ugly QStringList sizes; QFontDatabase db; - foreach (int size, db.standardSizes()) + const auto standardSizes = db.standardSizes(); + for (int size : standardSizes) sizes.append(QString::number(size)); return sizes; } diff --git a/tests/auto/qquicktreemodeladaptor/tst_qquicktreemodeladaptor.cpp b/tests/auto/qquicktreemodeladaptor/tst_qquicktreemodeladaptor.cpp index 0fec548d..c035c676 100644 --- a/tests/auto/qquicktreemodeladaptor/tst_qquicktreemodeladaptor.cpp +++ b/tests/auto/qquicktreemodeladaptor/tst_qquicktreemodeladaptor.cpp @@ -1181,8 +1181,8 @@ void tst_QQuickTreeModelAdaptor::reparentOnSameRow() // at least DepthRole and ModeIndexRole changes should have happened for the affected row bool depthChanged = false; bool modelIndexChanged = false; - QList > &changes = dataChangedSpy; - foreach (QList change, changes) { + const QList > &changes = dataChangedSpy; + for (const QList &change : changes) { if (change.at(0) == movedIndex) { if (change.at(2).value >().contains(QQuickTreeModelAdaptor1::DepthRole)) depthChanged = true; @@ -1258,7 +1258,7 @@ void tst_QQuickTreeModelAdaptor::selectionForRowRange() QCOMPARE(sel.count(), 2); // We don't know in which order the selection ranges are // being added, so we iterate and try to find what we expect. - foreach (const QItemSelectionRange &range, sel) { + for (const QItemSelectionRange &range : sel) { if (range.topLeft() == model.index(0, 0)) QCOMPARE(QModelIndex(range.bottomRight()), model.index(0, 0)); else if (range.topLeft() == model.index(0, 0, parent)) @@ -1275,7 +1275,7 @@ void tst_QQuickTreeModelAdaptor::selectionForRowRange() QCOMPARE(sel.count(), 2); // We don't know in which order the selection ranges are // being added, so we iterate and try to find what we expect. - foreach (const QItemSelectionRange &range, sel) { + for (const QItemSelectionRange &range : sel) { if (range.topLeft() == model.index(0, 0)) QCOMPARE(QModelIndex(range.bottomRight()), model.index(4, 0)); else if (range.topLeft() == model.index(0, 0, parent)) @@ -1296,7 +1296,7 @@ void tst_QQuickTreeModelAdaptor::selectionForRowRange() QCOMPARE(sel.count(), 3); // We don't know in which order the selection ranges are // being added, so we iterate and try to find what we expect. - foreach (const QItemSelectionRange &range, sel) { + for (const QItemSelectionRange &range : sel) { if (range.topLeft() == model.index(0, 0)) QCOMPARE(QModelIndex(range.bottomRight()), model.index(4, 0)); else if (range.topLeft() == model.index(0, 0, parent)) @@ -1319,7 +1319,7 @@ void tst_QQuickTreeModelAdaptor::selectionForRowRange() QCOMPARE(sel.count(), 3); // We don't know in which order the selection ranges are // being added, so we iterate and try to find what we expect. - foreach (const QItemSelectionRange &range, sel) { + for (const QItemSelectionRange &range : sel) { if (range.topLeft() == model.index(0, 0)) QCOMPARE(QModelIndex(range.bottomRight()), model.index(0, 0)); else if (range.topLeft() == model.index(0, 0, parent)) @@ -1338,7 +1338,7 @@ void tst_QQuickTreeModelAdaptor::selectionForRowRange() QCOMPARE(sel.count(), 4); // We don't know in which order the selection ranges are // being added, so we iterate and try to find what we expect. - foreach (const QItemSelectionRange &range, sel) { + for (const QItemSelectionRange &range : sel) { if (range.topLeft() == model.index(0, 0)) QCOMPARE(QModelIndex(range.bottomRight()), model.index(ModelRowCount - 1, 0)); else if (range.topLeft() == model.index(0, 0, parent)) @@ -1359,7 +1359,7 @@ void tst_QQuickTreeModelAdaptor::selectionForRowRange() QCOMPARE(sel.count(), 4); // We don't know in which order the selection ranges are // being added, so we iterate and try to find what we expect. - foreach (const QItemSelectionRange &range, sel) { + for (const QItemSelectionRange &range : sel) { if (range.topLeft() == model.index(1, 0)) QCOMPARE(QModelIndex(range.bottomRight()), model.index(1, 0)); else if (range.topLeft() == model.index(1, 0, parent)) diff --git a/tests/auto/shared/testmodel.h b/tests/auto/shared/testmodel.h index 6eaab74a..b1d9308e 100644 --- a/tests/auto/shared/testmodel.h +++ b/tests/auto/shared/testmodel.h @@ -289,8 +289,7 @@ public: ~Node() { - foreach (Node *n, children) - delete n; + qDeleteAll(children); } void addRows(int row, int count) diff --git a/tests/benchmarks/objectcount/tst_objectcount.cpp b/tests/benchmarks/objectcount/tst_objectcount.cpp index c5797160..996b4b66 100644 --- a/tests/benchmarks/objectcount/tst_objectcount.cpp +++ b/tests/benchmarks/objectcount/tst_objectcount.cpp @@ -83,7 +83,7 @@ static void printItems(const QList &items) std::cout << " QQuickItems: " << items.count() << " (total of QObjects: " << qt_qobjects->count() << ")" << std::endl; if (qt_verbose) { - foreach (QObject *object, *qt_qobjects) + for (QObject *object : qAsConst(*qt_qobjects)) qInfo() << "\t" << object; } } @@ -101,7 +101,7 @@ void tst_ObjectCount::controls() QVERIFY2(object.data(), qPrintable(component.errorString())); QList items; - foreach (QObject *object, *qt_qobjects()) { + for (QObject *object : qAsConst(*qt_qobjects)) { QQuickItem *item = qobject_cast(object); if (item) items += item; -- cgit v1.2.1