diff options
18 files changed, 54 insertions, 49 deletions
diff --git a/examples/widgets/animation/stickman/animation.cpp b/examples/widgets/animation/stickman/animation.cpp index 94a92749bc6..5c2d1682af4 100644 --- a/examples/widgets/animation/stickman/animation.cpp +++ b/examples/widgets/animation/stickman/animation.cpp @@ -159,18 +159,16 @@ void Animation::save(QIODevice *device) const QDataStream stream(device); stream << m_name; stream << m_frames.size(); - foreach (Frame *frame, m_frames) { + for (const Frame *frame : qAsConst(m_frames)) { stream << frame->nodeCount(); - for (int i=0; i<frame->nodeCount(); ++i) + for (int i = 0; i < frame->nodeCount(); ++i) stream << frame->nodePos(i); } } void Animation::load(QIODevice *device) { - if (!m_frames.isEmpty()) - qDeleteAll(m_frames); - + qDeleteAll(m_frames); m_frames.clear(); QDataStream stream(device); diff --git a/examples/widgets/animation/sub-attaq/animationmanager.cpp b/examples/widgets/animation/sub-attaq/animationmanager.cpp index e3dc27f37f5..a611641613f 100644 --- a/examples/widgets/animation/sub-attaq/animationmanager.cpp +++ b/examples/widgets/animation/sub-attaq/animationmanager.cpp @@ -93,14 +93,14 @@ void AnimationManager::unregisterAllAnimations() void AnimationManager::pauseAll() { - foreach (QAbstractAnimation* animation, animations) { + for (QAbstractAnimation *animation : qAsConst(animations)) { if (animation->state() == QAbstractAnimation::Running) animation->pause(); } } void AnimationManager::resumeAll() { - foreach (QAbstractAnimation* animation, animations) { + for (QAbstractAnimation *animation : qAsConst(animations)) { if (animation->state() == QAbstractAnimation::Paused) animation->resume(); } diff --git a/examples/widgets/animation/sub-attaq/bomb.cpp b/examples/widgets/animation/sub-attaq/bomb.cpp index 76e4575293c..2b865137dd5 100644 --- a/examples/widgets/animation/sub-attaq/bomb.cpp +++ b/examples/widgets/animation/sub-attaq/bomb.cpp @@ -112,7 +112,9 @@ void Bomb::launch(Bomb::Direction direction) void Bomb::onAnimationLaunchValueChanged(const QVariant &) { - foreach (QGraphicsItem * item , collidingItems(Qt::IntersectsItemBoundingRect)) { + const QList<QGraphicsItem *> colItems = + collidingItems(Qt::IntersectsItemBoundingRect); + for (QGraphicsItem *item : colItems) { if (item->type() == SubMarine::Type) { SubMarine *s = static_cast<SubMarine *>(item); destroy(); diff --git a/examples/widgets/animation/sub-attaq/graphicsscene.cpp b/examples/widgets/animation/sub-attaq/graphicsscene.cpp index 3205cdc54d6..8f0dfc1357b 100644 --- a/examples/widgets/animation/sub-attaq/graphicsscene.cpp +++ b/examples/widgets/animation/sub-attaq/graphicsscene.cpp @@ -265,17 +265,17 @@ void GraphicsScene::onSubMarineExecutionFinished() void GraphicsScene::clearScene() { - foreach (SubMarine *sub, submarines) { + for (SubMarine *sub : qAsConst(submarines)) { sub->destroy(); sub->deleteLater(); } - foreach (Torpedo *torpedo, torpedos) { + for (Torpedo *torpedo : qAsConst(torpedos)) { torpedo->destroy(); torpedo->deleteLater(); } - foreach (Bomb *bomb, bombs) { + for (Bomb *bomb : qAsConst(bombs)) { bomb->destroy(); bomb->deleteLater(); } diff --git a/examples/widgets/animation/sub-attaq/torpedo.cpp b/examples/widgets/animation/sub-attaq/torpedo.cpp index 5f8ef2f2b84..92a38334520 100644 --- a/examples/widgets/animation/sub-attaq/torpedo.cpp +++ b/examples/widgets/animation/sub-attaq/torpedo.cpp @@ -111,7 +111,9 @@ void Torpedo::setCurrentSpeed(int speed) void Torpedo::onAnimationLaunchValueChanged(const QVariant &) { - foreach (QGraphicsItem *item , collidingItems(Qt::IntersectsItemBoundingRect)) { + const QList<QGraphicsItem *> colItems = + collidingItems(Qt::IntersectsItemBoundingRect); + for (QGraphicsItem *item : colItems) { if (Boat *b = qgraphicsitem_cast<Boat*>(item)) b->destroy(); } diff --git a/examples/widgets/desktop/screenshot/screenshot.cpp b/examples/widgets/desktop/screenshot/screenshot.cpp index 80f26ae2822..715e6c780ee 100644 --- a/examples/widgets/desktop/screenshot/screenshot.cpp +++ b/examples/widgets/desktop/screenshot/screenshot.cpp @@ -139,7 +139,8 @@ void Screenshot::saveScreenshot() fileDialog.setFileMode(QFileDialog::AnyFile); fileDialog.setDirectory(initialPath); QStringList mimeTypes; - foreach (const QByteArray &bf, QImageWriter::supportedMimeTypes()) + const QList<QByteArray> baMimeTypes = QImageWriter::supportedMimeTypes(); + for (const QByteArray &bf : baMimeTypes) mimeTypes.append(QLatin1String(bf)); fileDialog.setMimeTypeFilters(mimeTypes); fileDialog.selectMimeTypeFilter("image/" + format); diff --git a/examples/widgets/dialogs/standarddialogs/dialog.cpp b/examples/widgets/dialogs/standarddialogs/dialog.cpp index 4b59aab8872..1830b21e8f7 100644 --- a/examples/widgets/dialogs/standarddialogs/dialog.cpp +++ b/examples/widgets/dialogs/standarddialogs/dialog.cpp @@ -99,9 +99,10 @@ void DialogOptionsWidget::addSpacer() int DialogOptionsWidget::value() const { int result = 0; - foreach (const CheckBoxEntry &checkboxEntry, checkBoxEntries) + for (const CheckBoxEntry &checkboxEntry : qAsConst(checkBoxEntries)) { if (checkboxEntry.first->isChecked()) result |= checkboxEntry.second; + } return result; } diff --git a/examples/widgets/doc/src/diagramscene.qdoc b/examples/widgets/doc/src/diagramscene.qdoc index ca4876f2e8d..860dcc5cb93 100644 --- a/examples/widgets/doc/src/diagramscene.qdoc +++ b/examples/widgets/doc/src/diagramscene.qdoc @@ -589,7 +589,7 @@ \snippet graphicsview/diagramscene/diagramscene.cpp 14 The scene has single selection, i.e., only one item can be - selected at any given time. The foreach will then loop one time + selected at any given time. The for loop will then loop one time with the selected item or none if no item is selected. \c isItemChange() is used to check whether a selected item exists and also is of the specified diagram \a type. diff --git a/examples/widgets/doc/src/orderform.qdoc b/examples/widgets/doc/src/orderform.qdoc index de59fc402d5..5c63081a779 100644 --- a/examples/widgets/doc/src/orderform.qdoc +++ b/examples/widgets/doc/src/orderform.qdoc @@ -191,7 +191,7 @@ We then set the \c{cursor}'s position back to its last position in \c topFrame and fill in the customer's name (provided by the constructor) - and address - using a \c foreach loop to traverse the QString, \c address. + and address - using a range-based for loop to traverse the QString, \c address. \snippet richtext/orderform/mainwindow.cpp 4 diff --git a/examples/widgets/doc/src/plugandpaint.qdoc b/examples/widgets/doc/src/plugandpaint.qdoc index d3044860ab5..b37176da0e1 100644 --- a/examples/widgets/doc/src/plugandpaint.qdoc +++ b/examples/widgets/doc/src/plugandpaint.qdoc @@ -171,8 +171,8 @@ \snippet tools/plugandpaint/app/mainwindow.cpp 8 We use QDir::entryList() to get a list of all files in that - directory. Then we iterate over the result using \l foreach and - try to load the plugin using QPluginLoader. + directory. Then we iterate over the result using a range-based for loop + and try to load the plugin using QPluginLoader. The QObject provided by the plugin is accessible through QPluginLoader::instance(). If the dynamic library isn't a Qt diff --git a/examples/widgets/gestures/imagegestures/imagewidget.cpp b/examples/widgets/gestures/imagegestures/imagewidget.cpp index ca9affca70a..0a6b963559e 100644 --- a/examples/widgets/gestures/imagegestures/imagewidget.cpp +++ b/examples/widgets/gestures/imagegestures/imagewidget.cpp @@ -72,7 +72,7 @@ ImageWidget::ImageWidget(QWidget *parent) void ImageWidget::grabGestures(const QList<Qt::GestureType> &gestures) { //! [enable gestures] - foreach (Qt::GestureType gesture, gestures) + for (Qt::GestureType gesture : gestures) grabGesture(gesture); //! [enable gestures] } diff --git a/examples/widgets/layouts/dynamiclayouts/dialog.cpp b/examples/widgets/layouts/dynamiclayouts/dialog.cpp index f46053372a4..baf3b4cf1ee 100644 --- a/examples/widgets/layouts/dynamiclayouts/dialog.cpp +++ b/examples/widgets/layouts/dynamiclayouts/dialog.cpp @@ -104,7 +104,7 @@ void Dialog::rotateWidgets() { Q_ASSERT(rotatableWidgets.count() % 2 == 0); - foreach (QWidget *widget, rotatableWidgets) + for (QWidget *widget : qAsConst(rotatableWidgets)) rotatableLayout->removeWidget(widget); rotatableWidgets.enqueue(rotatableWidgets.dequeue()); diff --git a/examples/widgets/layouts/flowlayout/flowlayout.cpp b/examples/widgets/layouts/flowlayout/flowlayout.cpp index b7064d98b1e..53613b8b207 100644 --- a/examples/widgets/layouts/flowlayout/flowlayout.cpp +++ b/examples/widgets/layouts/flowlayout/flowlayout.cpp @@ -155,8 +155,7 @@ QSize FlowLayout::sizeHint() const QSize FlowLayout::minimumSize() const { QSize size; - QLayoutItem *item; - foreach (item, itemList) + for (const QLayoutItem *item : qAsConst(itemList)) size = size.expandedTo(item->minimumSize()); size += QSize(2*margin(), 2*margin()); @@ -176,9 +175,8 @@ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const //! [9] //! [10] - QLayoutItem *item; - foreach (item, itemList) { - QWidget *wid = item->widget(); + for (QLayoutItem *item : qAsConst(itemList)) { + const QWidget *wid = item->widget(); int spaceX = horizontalSpacing(); if (spaceX == -1) spaceX = wid->style()->layoutSpacing( diff --git a/examples/widgets/richtext/orderform/mainwindow.cpp b/examples/widgets/richtext/orderform/mainwindow.cpp index 291c37edf69..0b6a2cac6c9 100644 --- a/examples/widgets/richtext/orderform/mainwindow.cpp +++ b/examples/widgets/richtext/orderform/mainwindow.cpp @@ -126,8 +126,8 @@ void MainWindow::createLetter(const QString &name, const QString &address, cursor.setPosition(topFrame->lastPosition()); cursor.insertText(name, textFormat); - QString line; - foreach (line, address.split("\n")) { + const QStringList lines = address.split('\n'); + for (const QString &line : lines) { cursor.insertBlock(); cursor.insertText(line); } diff --git a/examples/widgets/richtext/syntaxhighlighter/highlighter.cpp b/examples/widgets/richtext/syntaxhighlighter/highlighter.cpp index 6c420c328ae..0c10eed6cf7 100644 --- a/examples/widgets/richtext/syntaxhighlighter/highlighter.cpp +++ b/examples/widgets/richtext/syntaxhighlighter/highlighter.cpp @@ -58,18 +58,19 @@ Highlighter::Highlighter(QTextDocument *parent) keywordFormat.setForeground(Qt::darkBlue); keywordFormat.setFontWeight(QFont::Bold); - QStringList keywordPatterns; - keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b" - << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b" - << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b" - << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b" - << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b" - << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b" - << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b" - << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b" - << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b" - << "\\bvoid\\b" << "\\bvolatile\\b" << "\\bbool\\b"; - foreach (const QString &pattern, keywordPatterns) { + const QString keywordPatterns[] = { + QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"), QStringLiteral("\\bconst\\b"), + QStringLiteral("\\bdouble\\b"), QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"), + QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"), QStringLiteral("\\bint\\b"), + QStringLiteral("\\blong\\b"), QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"), + QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"), QStringLiteral("\\bpublic\\b"), + QStringLiteral("\\bshort\\b"), QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"), + QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"), QStringLiteral("\\bstruct\\b"), + QStringLiteral("\\btemplate\\b"), QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"), + QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"), QStringLiteral("\\bvirtual\\b"), + QStringLiteral("\\bvoid\\b"), QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b") + }; + for (const QString &pattern : keywordPatterns) { rule.pattern = QRegularExpression(pattern); rule.format = keywordFormat; highlightingRules.append(rule); @@ -80,14 +81,14 @@ Highlighter::Highlighter(QTextDocument *parent) //! [2] classFormat.setFontWeight(QFont::Bold); classFormat.setForeground(Qt::darkMagenta); - rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b"); + rule.pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b")); rule.format = classFormat; highlightingRules.append(rule); //! [2] //! [3] singleLineCommentFormat.setForeground(Qt::red); - rule.pattern = QRegularExpression("//[^\n]*"); + rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*")); rule.format = singleLineCommentFormat; highlightingRules.append(rule); @@ -96,7 +97,7 @@ Highlighter::Highlighter(QTextDocument *parent) //! [4] quotationFormat.setForeground(Qt::darkGreen); - rule.pattern = QRegularExpression("\".*\""); + rule.pattern = QRegularExpression(QStringLiteral("\".*\"")); rule.format = quotationFormat; highlightingRules.append(rule); //! [4] @@ -104,21 +105,21 @@ Highlighter::Highlighter(QTextDocument *parent) //! [5] functionFormat.setFontItalic(true); functionFormat.setForeground(Qt::blue); - rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()"); + rule.pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()")); rule.format = functionFormat; highlightingRules.append(rule); //! [5] //! [6] - commentStartExpression = QRegularExpression("/\\*"); - commentEndExpression = QRegularExpression("\\*/"); + commentStartExpression = QRegularExpression(QStringLiteral("/\\*")); + commentEndExpression = QRegularExpression(QStringLiteral("\\*/")); } //! [6] //! [7] void Highlighter::highlightBlock(const QString &text) { - foreach (const HighlightingRule &rule, highlightingRules) { + for (const HighlightingRule &rule : qAsConst(highlightingRules)) { QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); while (matchIterator.hasNext()) { QRegularExpressionMatch match = matchIterator.next(); diff --git a/examples/widgets/richtext/textedit/textedit.cpp b/examples/widgets/richtext/textedit/textedit.cpp index 0d51ce61ad3..00ea0325dc5 100644 --- a/examples/widgets/richtext/textedit/textedit.cpp +++ b/examples/widgets/richtext/textedit/textedit.cpp @@ -377,7 +377,7 @@ void TextEdit::setupTextActions() comboSize->setEditable(true); const QList<int> standardSizes = QFontDatabase::standardSizes(); - foreach (int size, standardSizes) + for (int size : standardSizes) comboSize->addItem(QString::number(size)); comboSize->setCurrentIndex(standardSizes.indexOf(QApplication::font().pointSize())); diff --git a/examples/widgets/scroller/graphicsview/main.cpp b/examples/widgets/scroller/graphicsview/main.cpp index c33a3091eda..d6b2956d508 100644 --- a/examples/widgets/scroller/graphicsview/main.cpp +++ b/examples/widgets/scroller/graphicsview/main.cpp @@ -117,7 +117,8 @@ public: QRectF boundingRect() const override { QRectF rect; - foreach (QGraphicsItem *item, childItems()) + const auto items = childItems(); + for (const QGraphicsItem *item : items) rect |= item->boundingRect().translated(item->pos()); return rect; } diff --git a/examples/widgets/statemachine/rogue/window.cpp b/examples/widgets/statemachine/rogue/window.cpp index f91a6e522ed..a5363e27581 100644 --- a/examples/widgets/statemachine/rogue/window.cpp +++ b/examples/widgets/statemachine/rogue/window.cpp @@ -66,7 +66,8 @@ Window::Window() font = QFont("Monospace"); } else { - foreach (QString family, database.families()) { + const QStringList fontFamilies = database.families(); + for (const QString &family : fontFamilies ) { if (database.isFixedPitch(family)) { font = QFont(family); break; |