From 26f7edb09eaf66590de9a3603489464fd868eedc Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 7 Dec 2018 11:59:02 +0100 Subject: Cleanup Widgets examples - foreach Cleanup the Widgets examples - replace foreach with range-based for loop in graphicsview subdirectory Change-Id: I9093b3ae89d73d0b860d29929943881c90074bce Reviewed-by: Luca Beldi Reviewed-by: Paul Wicking --- examples/widgets/graphicsview/elasticnodes/graphwidget.cpp | 10 ++++++---- examples/widgets/graphicsview/elasticnodes/node.cpp | 7 ++++--- 2 files changed, 10 insertions(+), 7 deletions(-) (limited to 'examples/widgets/graphicsview/elasticnodes') diff --git a/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp b/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp index 81928a4ee1..6b817b2a21 100644 --- a/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp +++ b/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp @@ -164,16 +164,17 @@ void GraphWidget::timerEvent(QTimerEvent *event) Q_UNUSED(event); QList nodes; - foreach (QGraphicsItem *item, scene()->items()) { + const QList items = scene()->items(); + for (QGraphicsItem *item : items) { if (Node *node = qgraphicsitem_cast(item)) nodes << node; } - foreach (Node *node, nodes) + for (Node *node : qAsConst(nodes)) node->calculateForces(); bool itemsMoved = false; - foreach (Node *node, nodes) { + for (Node *node : qAsConst(nodes)) { if (node->advancePosition()) itemsMoved = true; } @@ -246,7 +247,8 @@ void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::shuffle() { - foreach (QGraphicsItem *item, scene()->items()) { + const QList items = scene()->items(); + for (QGraphicsItem *item : items) { if (qgraphicsitem_cast(item)) item->setPos(-150 + QRandomGenerator::global()->bounded(300), -150 + QRandomGenerator::global()->bounded(300)); } diff --git a/examples/widgets/graphicsview/elasticnodes/node.cpp b/examples/widgets/graphicsview/elasticnodes/node.cpp index 71737c984b..d4fa3d2b4f 100644 --- a/examples/widgets/graphicsview/elasticnodes/node.cpp +++ b/examples/widgets/graphicsview/elasticnodes/node.cpp @@ -94,7 +94,8 @@ void Node::calculateForces() // Sum up all forces pushing this item away qreal xvel = 0; qreal yvel = 0; - foreach (QGraphicsItem *item, scene()->items()) { + const QList items = scene()->items(); + for (QGraphicsItem *item : items) { Node *node = qgraphicsitem_cast(item); if (!node) continue; @@ -113,7 +114,7 @@ void Node::calculateForces() //! [4] // Now subtract all forces pulling items together double weight = (edgeList.size() + 1) * 10; - foreach (Edge *edge, edgeList) { + for (const Edge *edge : qAsConst(edgeList)) { QPointF vec; if (edge->sourceNode() == this) vec = mapToItem(edge->destNode(), 0, 0); @@ -194,7 +195,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { switch (change) { case ItemPositionHasChanged: - foreach (Edge *edge, edgeList) + for (Edge *edge : qAsConst(edgeList)) edge->adjust(); graph->itemMoved(); break; -- cgit v1.2.3