From df39627fa33392a71ab79aadaa57e5c5e650e79e Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 1 Jan 2019 15:18:57 +0100 Subject: Examples: cleanup foreach usage Replace deprecated foreach macro with range-based for loop Change-Id: If919ba1d1d4acddfc1c5460ce7aebf8c49e3ac38 Reviewed-by: Paul Wicking --- .../mimetypes/mimetypebrowser/mainwindow.cpp | 4 +++- examples/corelib/serialization/savegame/game.cpp | 2 +- examples/corelib/serialization/savegame/level.cpp | 2 +- .../corelib/threads/queuedcustomtype/window.cpp | 5 +++-- examples/dbus/listnames/listnames.cpp | 3 ++- examples/embedded/lightmaps/slippymap.cpp | 11 ++++----- examples/opengl/contextinfo/widget.cpp | 2 +- examples/opengl/legacy/overpainting/glwidget.cpp | 2 +- examples/opengl/qopenglwidget/glwidget.cpp | 6 ++--- examples/opengl/qopenglwidget/mainwindow.cpp | 2 +- examples/qpa/windows/main.cpp | 4 ++-- examples/xml/htmlinfo/main.cpp | 26 +++++++++++----------- 12 files changed, 37 insertions(+), 32 deletions(-) diff --git a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp index bc7ec17d1c..2e5c8069b8 100644 --- a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp +++ b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp @@ -185,7 +185,9 @@ void MainWindow::find() m_findMatches.clear(); m_findIndex = 0; - foreach (const QStandardItem *item, m_model->findItems(value, Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive)) + const QList items = + m_model->findItems(value, Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive); + for (const QStandardItem *item : items) m_findMatches.append(m_model->indexFromItem(item)); statusBar()->showMessage(tr("%n mime types match \"%1\".", 0, m_findMatches.size()).arg(value)); updateFindActions(); diff --git a/examples/corelib/serialization/savegame/game.cpp b/examples/corelib/serialization/savegame/game.cpp index 4caec71a03..226f6fda11 100644 --- a/examples/corelib/serialization/savegame/game.cpp +++ b/examples/corelib/serialization/savegame/game.cpp @@ -185,7 +185,7 @@ void Game::write(QJsonObject &json) const json["player"] = playerObject; QJsonArray levelArray; - foreach (const Level level, mLevels) { + for (const Level level : mLevels) { QJsonObject levelObject; level.write(levelObject); levelArray.append(levelObject); diff --git a/examples/corelib/serialization/savegame/level.cpp b/examples/corelib/serialization/savegame/level.cpp index 8eda107f46..c7adc6c8ff 100644 --- a/examples/corelib/serialization/savegame/level.cpp +++ b/examples/corelib/serialization/savegame/level.cpp @@ -97,7 +97,7 @@ void Level::write(QJsonObject &json) const { json["name"] = mName; QJsonArray npcArray; - foreach (const Character npc, mNpcs) { + for (const Character npc : mNpcs) { QJsonObject npcObject; npc.write(npcObject); npcArray.append(npcObject); diff --git a/examples/corelib/threads/queuedcustomtype/window.cpp b/examples/corelib/threads/queuedcustomtype/window.cpp index 2cefba1e17..0d3f80aba4 100644 --- a/examples/corelib/threads/queuedcustomtype/window.cpp +++ b/examples/corelib/threads/queuedcustomtype/window.cpp @@ -89,9 +89,10 @@ Window::Window() void Window::loadImage() { QStringList formats; - foreach (QByteArray format, QImageReader::supportedImageFormats()) + const QList supportedFormats = QImageReader::supportedImageFormats(); + for (const QByteArray &format : supportedFormats) if (format.toLower() == format) - formats.append("*." + format); + formats.append(QLatin1String("*.") + QString::fromLatin1(format)); QString newPath = QFileDialog::getOpenFileName(this, tr("Open Image"), path, tr("Image files (%1)").arg(formats.join(' '))); diff --git a/examples/dbus/listnames/listnames.cpp b/examples/dbus/listnames/listnames.cpp index c0afec062d..50203da73d 100644 --- a/examples/dbus/listnames/listnames.cpp +++ b/examples/dbus/listnames/listnames.cpp @@ -62,7 +62,8 @@ void method1() qDebug() << "Error:" << reply.error().message(); exit(1); } - foreach (QString name, reply.value()) + const QStringList values = reply.value(); + for (const QString &name : values) qDebug() << name; } diff --git a/examples/embedded/lightmaps/slippymap.cpp b/examples/embedded/lightmaps/slippymap.cpp index ff43261700..da003981ff 100644 --- a/examples/embedded/lightmaps/slippymap.cpp +++ b/examples/embedded/lightmaps/slippymap.cpp @@ -162,7 +162,6 @@ void SlippyMap::handleNetworkData(QNetworkReply *reply) { QImage img; QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint(); - QUrl url = reply->url(); if (!reply->error()) if (!img.load(reply, 0)) img = QImage(); @@ -173,10 +172,12 @@ void SlippyMap::handleNetworkData(QNetworkReply *reply) emit updated(tileRect(tp)); // purge unused spaces - QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2); - foreach(QPoint tp, m_tilePixmaps.keys()) - if (!bound.contains(tp)) - m_tilePixmaps.remove(tp); + const QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2); + for (auto it = m_tilePixmaps.keyBegin(); it != m_tilePixmaps.keyEnd(); ++it) { + const QPoint &tp = *it; + if (!bound.contains(tp)) + m_tilePixmaps.remove(tp); + } download(); } diff --git a/examples/opengl/contextinfo/widget.cpp b/examples/opengl/contextinfo/widget.cpp index a5d9e98bf8..b1b7076503 100644 --- a/examples/opengl/contextinfo/widget.cpp +++ b/examples/opengl/contextinfo/widget.cpp @@ -387,7 +387,7 @@ void Widget::renderWindowReady() QList extensionList = context->extensions().toList(); std::sort(extensionList.begin(), extensionList.end()); m_extensions->append(tr("Found %1 extensions:").arg(extensionList.count())); - Q_FOREACH (const QByteArray &ext, extensionList) + for (const QByteArray &ext : qAsConst(extensionList)) m_extensions->append(QString::fromLatin1(ext)); m_output->moveCursor(QTextCursor::Start); diff --git a/examples/opengl/legacy/overpainting/glwidget.cpp b/examples/opengl/legacy/overpainting/glwidget.cpp index 1ec7bd731c..f98d043c5c 100644 --- a/examples/opengl/legacy/overpainting/glwidget.cpp +++ b/examples/opengl/legacy/overpainting/glwidget.cpp @@ -201,7 +201,7 @@ void GLWidget::paintEvent(QPaintEvent *event) //! [10] QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - foreach (Bubble *bubble, bubbles) { + for (Bubble *bubble : qAsConst(bubbles)) { if (bubble->rect().intersects(event->rect())) bubble->drawBubble(&painter); } diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp index 3fe919f94b..946b2bec67 100644 --- a/examples/opengl/qopenglwidget/glwidget.cpp +++ b/examples/opengl/qopenglwidget/glwidget.cpp @@ -385,10 +385,10 @@ void GLWidget::paintGL() painter.endNativePainting(); - if (m_showBubbles) - foreach (Bubble *bubble, m_bubbles) { + if (m_showBubbles) { + for (Bubble *bubble : qAsConst(m_bubbles)) bubble->drawBubble(&painter); - } + } if (const int elapsed = m_time.elapsed()) { QString framesPerSecond; diff --git a/examples/opengl/qopenglwidget/mainwindow.cpp b/examples/opengl/qopenglwidget/mainwindow.cpp index 4bd123628f..6fab3df79e 100644 --- a/examples/opengl/qopenglwidget/mainwindow.cpp +++ b/examples/opengl/qopenglwidget/mainwindow.cpp @@ -176,7 +176,7 @@ void MainWindow::timerUsageChanged(bool enabled) m_timer->start(); } else { m_timer->stop(); - foreach (QOpenGLWidget *w, m_glWidgets) + for (QOpenGLWidget *w : qAsConst(m_glWidgets)) w->update(); } } diff --git a/examples/qpa/windows/main.cpp b/examples/qpa/windows/main.cpp index 9d22d146d8..80f44ae0dc 100644 --- a/examples/qpa/windows/main.cpp +++ b/examples/qpa/windows/main.cpp @@ -79,9 +79,9 @@ int main(int argc, char **argv) // create one window on each additional screen as well - QList screens = app.screens(); QList windows; - foreach (QScreen *screen, screens) { + const QList screens = app.screens(); + for (QScreen *screen : screens) { if (screen == app.primaryScreen()) continue; WindowPtr window(new Window(screen)); diff --git a/examples/xml/htmlinfo/main.cpp b/examples/xml/htmlinfo/main.cpp index 6591c3ac91..22bf36f33c 100644 --- a/examples/xml/htmlinfo/main.cpp +++ b/examples/xml/htmlinfo/main.cpp @@ -50,7 +50,8 @@ #include -void parseHtmlFile(QTextStream &out, const QString &fileName) { +void parseHtmlFile(QTextStream &out, const QString &fileName) +{ QFile file(fileName); out << "Analysis of HTML file: " << fileName << endl; @@ -71,11 +72,11 @@ void parseHtmlFile(QTextStream &out, const QString &fileName) { while (!reader.atEnd()) { reader.readNext(); if (reader.isStartElement()) { - if (reader.name() == "title") + if (reader.name() == QLatin1String("title")) title = reader.readElementText(); - else if(reader.name() == "a") - links.append(reader.attributes().value("href").toString()); - else if(reader.name() == "p") + else if (reader.name() == QLatin1String("a")) + links.append(reader.attributes().value(QLatin1String("href")).toString()); + else if (reader.name() == QLatin1String("p")) ++paragraphCount; } } @@ -94,10 +95,10 @@ void parseHtmlFile(QTextStream &out, const QString &fileName) { << " Number of links: " << links.size() << endl << " Showing first few links:" << endl; - while(links.size() > 5) + while (links.size() > 5) links.removeLast(); - foreach(QString link, links) + for (const QString &link : qAsConst(links)) out << " " << link << endl; out << endl << endl; } @@ -108,11 +109,10 @@ int main(int argc, char **argv) QCoreApplication app(argc, argv); // get a list of all html files in the current directory - QStringList filter; - filter << "*.htm"; - filter << "*.html"; + const QStringList filter = { QStringLiteral("*.htm"), + QStringLiteral("*.html") }; - QStringList htmlFiles = QDir(":/").entryList(filter, QDir::Files); + const QStringList htmlFiles = QDir(QStringLiteral(":/")).entryList(filter, QDir::Files); QTextStream out(stdout); @@ -122,8 +122,8 @@ int main(int argc, char **argv) } // parse each html file and write the result to file/stream - foreach(QString file, htmlFiles) - parseHtmlFile(out, ":/" + file); + for (const QString &file : htmlFiles) + parseHtmlFile(out, QStringLiteral(":/") + file); return 0; } -- cgit v1.2.3