Cleanup Widgets examples - foreach

Cleanup the Widgets examples - replace foreach with range-based for loop
in the remaining directories

Change-Id: I321e6c0f414401a1ae4fb65762b97d894b725afa
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
This commit is contained in:
Christian Ehrlicher 2018-12-07 12:01:42 +01:00
parent 26f7edb09e
commit 34e4a8ef6d
18 changed files with 54 additions and 49 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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