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_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));
statusBar()->showMessage(tr("%n mime types match \"%1\".", 0, m_findMatches.size()).arg(value));
updateFindActions();

View File

@ -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);

View File

@ -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);

View File

@ -89,9 +89,10 @@ Window::Window()
void Window::loadImage()
{
QStringList formats;
foreach (QByteArray format, QImageReader::supportedImageFormats())
const QList<QByteArray> 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(' ')));

View File

@ -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;
}

View File

@ -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();
}

View File

@ -387,7 +387,7 @@ void Widget::renderWindowReady()
QList<QByteArray> 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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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();
}
}

View File

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

View File

@ -50,7 +50,8 @@
#include <QtCore>
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;
}