Examples: cleanup foreach usage

Replace deprecated foreach macro with range-based for loop

Change-Id: If919ba1d1d4acddfc1c5460ce7aebf8c49e3ac38
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-01-01 15:18:57 +01:00
parent 7847e6bc02
commit df39627fa3
12 changed files with 37 additions and 32 deletions

View File

@ -185,7 +185,9 @@ void MainWindow::find()
m_findMatches.clear(); m_findMatches.clear();
m_findIndex = 0; m_findIndex = 0;
foreach (const QStandardItem *item, m_model->findItems(value, Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive)) const QList<QStandardItem *> items =
m_model->findItems(value, Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive);
for (const QStandardItem *item : items)
m_findMatches.append(m_model->indexFromItem(item)); m_findMatches.append(m_model->indexFromItem(item));
statusBar()->showMessage(tr("%n mime types match \"%1\".", 0, m_findMatches.size()).arg(value)); statusBar()->showMessage(tr("%n mime types match \"%1\".", 0, m_findMatches.size()).arg(value));
updateFindActions(); updateFindActions();

View File

@ -185,7 +185,7 @@ void Game::write(QJsonObject &json) const
json["player"] = playerObject; json["player"] = playerObject;
QJsonArray levelArray; QJsonArray levelArray;
foreach (const Level level, mLevels) { for (const Level level : mLevels) {
QJsonObject levelObject; QJsonObject levelObject;
level.write(levelObject); level.write(levelObject);
levelArray.append(levelObject); levelArray.append(levelObject);

View File

@ -97,7 +97,7 @@ void Level::write(QJsonObject &json) const
{ {
json["name"] = mName; json["name"] = mName;
QJsonArray npcArray; QJsonArray npcArray;
foreach (const Character npc, mNpcs) { for (const Character npc : mNpcs) {
QJsonObject npcObject; QJsonObject npcObject;
npc.write(npcObject); npc.write(npcObject);
npcArray.append(npcObject); npcArray.append(npcObject);

View File

@ -89,9 +89,10 @@ Window::Window()
void Window::loadImage() void Window::loadImage()
{ {
QStringList formats; QStringList formats;
foreach (QByteArray format, QImageReader::supportedImageFormats()) const QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
for (const QByteArray &format : supportedFormats)
if (format.toLower() == format) if (format.toLower() == format)
formats.append("*." + format); formats.append(QLatin1String("*.") + QString::fromLatin1(format));
QString newPath = QFileDialog::getOpenFileName(this, tr("Open Image"), QString newPath = QFileDialog::getOpenFileName(this, tr("Open Image"),
path, tr("Image files (%1)").arg(formats.join(' '))); path, tr("Image files (%1)").arg(formats.join(' ')));

View File

@ -62,7 +62,8 @@ void method1()
qDebug() << "Error:" << reply.error().message(); qDebug() << "Error:" << reply.error().message();
exit(1); exit(1);
} }
foreach (QString name, reply.value()) const QStringList values = reply.value();
for (const QString &name : values)
qDebug() << name; qDebug() << name;
} }

View File

@ -162,7 +162,6 @@ void SlippyMap::handleNetworkData(QNetworkReply *reply)
{ {
QImage img; QImage img;
QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint(); QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint();
QUrl url = reply->url();
if (!reply->error()) if (!reply->error())
if (!img.load(reply, 0)) if (!img.load(reply, 0))
img = QImage(); img = QImage();
@ -173,10 +172,12 @@ void SlippyMap::handleNetworkData(QNetworkReply *reply)
emit updated(tileRect(tp)); emit updated(tileRect(tp));
// purge unused spaces // purge unused spaces
QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2); const QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2);
foreach(QPoint tp, m_tilePixmaps.keys()) for (auto it = m_tilePixmaps.keyBegin(); it != m_tilePixmaps.keyEnd(); ++it) {
if (!bound.contains(tp)) const QPoint &tp = *it;
m_tilePixmaps.remove(tp); if (!bound.contains(tp))
m_tilePixmaps.remove(tp);
}
download(); download();
} }

View File

@ -387,7 +387,7 @@ void Widget::renderWindowReady()
QList<QByteArray> extensionList = context->extensions().toList(); QList<QByteArray> extensionList = context->extensions().toList();
std::sort(extensionList.begin(), extensionList.end()); std::sort(extensionList.begin(), extensionList.end());
m_extensions->append(tr("Found %1 extensions:").arg(extensionList.count())); 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_extensions->append(QString::fromLatin1(ext));
m_output->moveCursor(QTextCursor::Start); m_output->moveCursor(QTextCursor::Start);

View File

@ -201,7 +201,7 @@ void GLWidget::paintEvent(QPaintEvent *event)
//! [10] //! [10]
QPainter painter(this); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
foreach (Bubble *bubble, bubbles) { for (Bubble *bubble : qAsConst(bubbles)) {
if (bubble->rect().intersects(event->rect())) if (bubble->rect().intersects(event->rect()))
bubble->drawBubble(&painter); bubble->drawBubble(&painter);
} }

View File

@ -385,10 +385,10 @@ void GLWidget::paintGL()
painter.endNativePainting(); painter.endNativePainting();
if (m_showBubbles) if (m_showBubbles) {
foreach (Bubble *bubble, m_bubbles) { for (Bubble *bubble : qAsConst(m_bubbles))
bubble->drawBubble(&painter); bubble->drawBubble(&painter);
} }
if (const int elapsed = m_time.elapsed()) { if (const int elapsed = m_time.elapsed()) {
QString framesPerSecond; QString framesPerSecond;

View File

@ -176,7 +176,7 @@ void MainWindow::timerUsageChanged(bool enabled)
m_timer->start(); m_timer->start();
} else { } else {
m_timer->stop(); m_timer->stop();
foreach (QOpenGLWidget *w, m_glWidgets) for (QOpenGLWidget *w : qAsConst(m_glWidgets))
w->update(); w->update();
} }
} }

View File

@ -79,9 +79,9 @@ int main(int argc, char **argv)
// create one window on each additional screen as well // create one window on each additional screen as well
QList<QScreen *> screens = app.screens();
QList<WindowPtr> windows; QList<WindowPtr> windows;
foreach (QScreen *screen, screens) { const QList<QScreen *> screens = app.screens();
for (QScreen *screen : screens) {
if (screen == app.primaryScreen()) if (screen == app.primaryScreen())
continue; continue;
WindowPtr window(new Window(screen)); WindowPtr window(new Window(screen));

View File

@ -50,7 +50,8 @@
#include <QtCore> #include <QtCore>
void parseHtmlFile(QTextStream &out, const QString &fileName) { void parseHtmlFile(QTextStream &out, const QString &fileName)
{
QFile file(fileName); QFile file(fileName);
out << "Analysis of HTML file: " << fileName << endl; out << "Analysis of HTML file: " << fileName << endl;
@ -71,11 +72,11 @@ void parseHtmlFile(QTextStream &out, const QString &fileName) {
while (!reader.atEnd()) { while (!reader.atEnd()) {
reader.readNext(); reader.readNext();
if (reader.isStartElement()) { if (reader.isStartElement()) {
if (reader.name() == "title") if (reader.name() == QLatin1String("title"))
title = reader.readElementText(); title = reader.readElementText();
else if(reader.name() == "a") else if (reader.name() == QLatin1String("a"))
links.append(reader.attributes().value("href").toString()); links.append(reader.attributes().value(QLatin1String("href")).toString());
else if(reader.name() == "p") else if (reader.name() == QLatin1String("p"))
++paragraphCount; ++paragraphCount;
} }
} }
@ -94,10 +95,10 @@ void parseHtmlFile(QTextStream &out, const QString &fileName) {
<< " Number of links: " << links.size() << endl << " Number of links: " << links.size() << endl
<< " Showing first few links:" << endl; << " Showing first few links:" << endl;
while(links.size() > 5) while (links.size() > 5)
links.removeLast(); links.removeLast();
foreach(QString link, links) for (const QString &link : qAsConst(links))
out << " " << link << endl; out << " " << link << endl;
out << endl << endl; out << endl << endl;
} }
@ -108,11 +109,10 @@ int main(int argc, char **argv)
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
// get a list of all html files in the current directory // get a list of all html files in the current directory
QStringList filter; const QStringList filter = { QStringLiteral("*.htm"),
filter << "*.htm"; QStringLiteral("*.html") };
filter << "*.html";
QStringList htmlFiles = QDir(":/").entryList(filter, QDir::Files); const QStringList htmlFiles = QDir(QStringLiteral(":/")).entryList(filter, QDir::Files);
QTextStream out(stdout); QTextStream out(stdout);
@ -122,8 +122,8 @@ int main(int argc, char **argv)
} }
// parse each html file and write the result to file/stream // parse each html file and write the result to file/stream
foreach(QString file, htmlFiles) for (const QString &file : htmlFiles)
parseHtmlFile(out, ":/" + file); parseHtmlFile(out, QStringLiteral(":/") + file);
return 0; return 0;
} }