From 1f2c23a7ca1699b345578aeb52fbd97b612e079a Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 7 Dec 2018 12:00:48 +0100 Subject: Cleanup Widgets examples - foreach Cleanup the Widget examples - replace foreach with range-based for loop in subdirectory tools, touch and tutorials Change-Id: I008d23b5993a18a3332fe9f5e5bca68cb0561066 Reviewed-by: Luca Beldi Reviewed-by: Sze Howe Koh Reviewed-by: Paul Wicking --- examples/widgets/tools/codecs/mainwindow.cpp | 14 ++++----- examples/widgets/tools/codecs/previewform.cpp | 2 +- .../tools/echoplugin/echowindow/echowindow.cpp | 3 +- examples/widgets/tools/i18n/languagechooser.cpp | 8 ++--- .../widgets/tools/settingseditor/settingstree.cpp | 6 ++-- examples/widgets/touch/dials/main.cpp | 4 +-- examples/widgets/touch/fingerpaint/mainwindow.cpp | 6 ++-- .../widgets/touch/fingerpaint/scribblearea.cpp | 6 ++-- examples/widgets/touch/pinchzoom/mouse.cpp | 2 +- .../tutorials/widgets/nestedlayouts/main.cpp | 35 +++++++++++----------- 10 files changed, 45 insertions(+), 41 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/tools/codecs/mainwindow.cpp b/examples/widgets/tools/codecs/mainwindow.cpp index 229c2ccfd4..53db9fe61f 100644 --- a/examples/widgets/tools/codecs/mainwindow.cpp +++ b/examples/widgets/tools/codecs/mainwindow.cpp @@ -127,11 +127,10 @@ void MainWindow::about() void MainWindow::aboutToShowSaveAsMenu() { - QString currentText = textEdit->toPlainText(); - - foreach (QAction *action, saveAsActs) { - QByteArray codecName = action->data().toByteArray(); - QTextCodec *codec = QTextCodec::codecForName(codecName); + const QString currentText = textEdit->toPlainText(); + for (QAction *action : qAsConst(saveAsActs)) { + const QByteArray codecName = action->data().toByteArray(); + const QTextCodec *codec = QTextCodec::codecForName(codecName); action->setVisible(codec && codec->canEncode(currentText)); } } @@ -142,7 +141,8 @@ void MainWindow::findCodecs() QRegularExpression iso8859RegExp("^ISO[- ]8859-([0-9]+).*$"); QRegularExpressionMatch match; - foreach (int mib, QTextCodec::availableMibs()) { + const QList mibs = QTextCodec::availableMibs(); + for (int mib : mibs) { QTextCodec *codec = QTextCodec::codecForMib(mib); QString sortKey = codec->name().toUpper(); @@ -177,7 +177,7 @@ void MainWindow::createMenus() QMenu *saveAsMenu = fileMenu->addMenu(tr("&Save As")); connect(saveAsMenu, &QMenu::aboutToShow, this, &MainWindow::aboutToShowSaveAsMenu); - foreach (const QTextCodec *codec, codecs) { + for (const QTextCodec *codec : qAsConst(codecs)) { const QByteArray name = codec->name(); QAction *action = saveAsMenu->addAction(tr("%1...").arg(QLatin1String(name))); action->setData(QVariant(name)); diff --git a/examples/widgets/tools/codecs/previewform.cpp b/examples/widgets/tools/codecs/previewform.cpp index d19b9c0833..206b5757cd 100644 --- a/examples/widgets/tools/codecs/previewform.cpp +++ b/examples/widgets/tools/codecs/previewform.cpp @@ -182,7 +182,7 @@ PreviewForm::PreviewForm(QWidget *parent) void PreviewForm::setCodecList(const QList &list) { encodingComboBox->clear(); - foreach (const QTextCodec *codec, list) { + for (const QTextCodec *codec : list) { encodingComboBox->addItem(QLatin1String(codec->name()), QVariant(codec->mibEnum())); } diff --git a/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp b/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp index 64b59d506e..6886a4cd88 100644 --- a/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp +++ b/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp @@ -113,7 +113,8 @@ bool EchoWindow::loadPlugin() } #endif pluginsDir.cd("plugins"); - foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { + const QStringList entries = pluginsDir.entryList(QDir::Files); + for (const QString &fileName : entries) { QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); QObject *plugin = pluginLoader.instance(); if (plugin) { diff --git a/examples/widgets/tools/i18n/languagechooser.cpp b/examples/widgets/tools/i18n/languagechooser.cpp index 58cf9d4047..f07d0ddee3 100644 --- a/examples/widgets/tools/i18n/languagechooser.cpp +++ b/examples/widgets/tools/i18n/languagechooser.cpp @@ -148,14 +148,14 @@ void LanguageChooser::checkBoxToggled() void LanguageChooser::showAll() { - foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys()) - checkBox->setChecked(true); + for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it) + (*it)->setChecked(true); } void LanguageChooser::hideAll() { - foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys()) - checkBox->setChecked(false); + for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it) + (*it)->setChecked(false); } QStringList LanguageChooser::findQmFiles() diff --git a/examples/widgets/tools/settingseditor/settingstree.cpp b/examples/widgets/tools/settingseditor/settingstree.cpp index 4ca843784e..8585792787 100644 --- a/examples/widgets/tools/settingseditor/settingstree.cpp +++ b/examples/widgets/tools/settingseditor/settingstree.cpp @@ -170,7 +170,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent) { int dividerIndex = 0; - foreach (QString group, settings->childGroups()) { + const QStringList childGroups = settings->childGroups(); + for (const QString &group : childGroups) { QTreeWidgetItem *child; int childIndex = findChild(parent, group, dividerIndex); if (childIndex != -1) { @@ -190,7 +191,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent) settings->endGroup(); } - foreach (const QString &key, settings->childKeys()) { + const QStringList childKeys = settings->childKeys(); + for (const QString &key : childKeys) { QTreeWidgetItem *child; int childIndex = findChild(parent, key, 0); diff --git a/examples/widgets/touch/dials/main.cpp b/examples/widgets/touch/dials/main.cpp index 071f485de3..059dfdc716 100644 --- a/examples/widgets/touch/dials/main.cpp +++ b/examples/widgets/touch/dials/main.cpp @@ -60,8 +60,8 @@ int main(int argc, char **argv) QWidget window; Ui::Dials dialsUi; dialsUi.setupUi(&window); - QList sliders = window.findChildren(); - foreach (QAbstractSlider *slider, sliders) + const QList sliders = window.findChildren(); + for (QAbstractSlider *slider : sliders) slider->setAttribute(Qt::WA_AcceptTouchEvents); window.showMaximized(); return app.exec(); diff --git a/examples/widgets/touch/fingerpaint/mainwindow.cpp b/examples/widgets/touch/fingerpaint/mainwindow.cpp index b0d91d25bf..2f7ec38d1a 100644 --- a/examples/widgets/touch/fingerpaint/mainwindow.cpp +++ b/examples/widgets/touch/fingerpaint/mainwindow.cpp @@ -129,7 +129,8 @@ void MainWindow::createActions() openAct->setShortcut(tr("Ctrl+O")); connect(openAct, &QAction::triggered, this, &MainWindow::open); - foreach (QByteArray format, QImageWriter::supportedImageFormats()) { + const QList imageFormats = QImageWriter::supportedImageFormats(); + for (const QByteArray &format : imageFormats) { QString text = tr("%1...").arg(QString(format).toUpper()); QAction *action = new QAction(text, this); @@ -163,8 +164,7 @@ void MainWindow::createMenus() //! [15] //! [16] { saveAsMenu = new QMenu(tr("&Save As"), this); - foreach (QAction *action, saveAsActs) - saveAsMenu->addAction(action); + saveAsMenu->addActions(saveAsActs); fileMenu = new QMenu(tr("&File"), this); fileMenu->addAction(openAct); diff --git a/examples/widgets/touch/fingerpaint/scribblearea.cpp b/examples/widgets/touch/fingerpaint/scribblearea.cpp index aa4e60c934..0b0c4476d9 100644 --- a/examples/widgets/touch/fingerpaint/scribblearea.cpp +++ b/examples/widgets/touch/fingerpaint/scribblearea.cpp @@ -195,9 +195,9 @@ bool ScribbleArea::event(QEvent *event) case QEvent::TouchUpdate: case QEvent::TouchEnd: { - QTouchEvent *touch = static_cast(event); - QList touchPoints = static_cast(event)->touchPoints(); - foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) { + const QTouchEvent *touch = static_cast(event); + const QList touchPoints = static_cast(event)->touchPoints(); + for (const QTouchEvent::TouchPoint &touchPoint : touchPoints) { switch (touchPoint.state()) { case Qt::TouchPointStationary: case Qt::TouchPointReleased: diff --git a/examples/widgets/touch/pinchzoom/mouse.cpp b/examples/widgets/touch/pinchzoom/mouse.cpp index 1e6814be13..1dfd7d749c 100644 --- a/examples/widgets/touch/pinchzoom/mouse.cpp +++ b/examples/widgets/touch/pinchzoom/mouse.cpp @@ -163,7 +163,7 @@ void Mouse::timerEvent(QTimerEvent *) << mapToScene(0, 0) << mapToScene(-30, -50) << mapToScene(30, -50)); - foreach (QGraphicsItem *item, dangerMice) { + for (QGraphicsItem *item : dangerMice) { if (item == this) continue; diff --git a/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp b/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp index 9acc74b469..8880dbe3d0 100644 --- a/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp +++ b/examples/widgets/tutorials/widgets/nestedlayouts/main.cpp @@ -76,25 +76,26 @@ int main(int argc, char *argv[]) //! [set up the model] QStandardItemModel model; - model.setHorizontalHeaderLabels( - QStringList() << QApplication::translate("nestedlayouts", "Name") - << QApplication::translate("nestedlayouts", "Office")); + model.setHorizontalHeaderLabels({ QApplication::translate("nestedlayouts", "Name"), + QApplication::translate("nestedlayouts", "Office") }); - QList rows = QList() - << (QStringList() << "Verne Nilsen" << "123") - << (QStringList() << "Carlos Tang" << "77") - << (QStringList() << "Bronwyn Hawcroft" << "119") - << (QStringList() << "Alessandro Hanssen" << "32") - << (QStringList() << "Andrew John Bakken" << "54") - << (QStringList() << "Vanessa Weatherley" << "85") - << (QStringList() << "Rebecca Dickens" << "17") - << (QStringList() << "David Bradley" << "42") - << (QStringList() << "Knut Walters" << "25") - << (QStringList() << "Andrea Jones" << "34"); + const QStringList rows[] = { + QStringList{ QStringLiteral("Verne Nilsen"), QStringLiteral("123") }, + QStringList{ QStringLiteral("Carlos Tang"), QStringLiteral("77") }, + QStringList{ QStringLiteral("Bronwyn Hawcroft"), QStringLiteral("119") }, + QStringList{ QStringLiteral("Alessandro Hanssen"), QStringLiteral("32") }, + QStringList{ QStringLiteral("Andrew John Bakken"), QStringLiteral("54") }, + QStringList{ QStringLiteral("Vanessa Weatherley"), QStringLiteral("85") }, + QStringList{ QStringLiteral("Rebecca Dickens"), QStringLiteral("17") }, + QStringList{ QStringLiteral("David Bradley"), QStringLiteral("42") }, + QStringList{ QStringLiteral("Knut Walters"), QStringLiteral("25") }, + QStringList{ QStringLiteral("Andrea Jones"), QStringLiteral("34") } + }; - foreach (QStringList row, rows) { - QList items; - foreach (QString text, row) + QList items; + for (const QStringList &row : rows) { + items.clear(); + for (const QString &text : row) items.append(new QStandardItem(text)); model.appendRow(items); } -- cgit v1.2.3