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 <v.ronin@yahoo.it>
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Christian Ehrlicher 2018-12-07 12:00:48 +01:00
parent bec8173347
commit 1f2c23a7ca
10 changed files with 45 additions and 41 deletions

View File

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

View File

@ -182,7 +182,7 @@ PreviewForm::PreviewForm(QWidget *parent)
void PreviewForm::setCodecList(const QList<QTextCodec *> &list)
{
encodingComboBox->clear();
foreach (const QTextCodec *codec, list) {
for (const QTextCodec *codec : list) {
encodingComboBox->addItem(QLatin1String(codec->name()),
QVariant(codec->mibEnum()));
}

View File

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

View File

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

View File

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

View File

@ -60,8 +60,8 @@ int main(int argc, char **argv)
QWidget window;
Ui::Dials dialsUi;
dialsUi.setupUi(&window);
QList<QAbstractSlider *> sliders = window.findChildren<QAbstractSlider *>();
foreach (QAbstractSlider *slider, sliders)
const QList<QAbstractSlider *> sliders = window.findChildren<QAbstractSlider *>();
for (QAbstractSlider *slider : sliders)
slider->setAttribute(Qt::WA_AcceptTouchEvents);
window.showMaximized();
return app.exec();

View File

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

View File

@ -195,9 +195,9 @@ bool ScribbleArea::event(QEvent *event)
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QTouchEvent *touch = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) {
const QTouchEvent *touch = static_cast<QTouchEvent *>(event);
const QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
for (const QTouchEvent::TouchPoint &touchPoint : touchPoints) {
switch (touchPoint.state()) {
case Qt::TouchPointStationary:
case Qt::TouchPointReleased:

View File

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

View File

@ -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<QStringList> rows = QList<QStringList>()
<< (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<QStandardItem *> items;
foreach (QString text, row)
QList<QStandardItem *> items;
for (const QStringList &row : rows) {
items.clear();
for (const QString &text : row)
items.append(new QStandardItem(text));
model.appendRow(items);
}