summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2016-01-26 14:38:54 +0100
committerMarc Mutz <marc.mutz@kdab.com>2016-02-28 10:50:18 +0000
commit85c2a128ef8d1b5fbddf551691bccc0770e558ba (patch)
tree99580041fa8c0e60d2e91451d1a7531055803d3d /src
parente1c2bfa53b7549a1283f5b52974ea90b6a2b4659 (diff)
QtWidgets: eradicate Q_FOREACH loops [rvalues]
... by replacing them with C++11 range-for loops. This is the simplest of the patch series: Q_FOREACH took a copy, so we do, too. Except we don't, since we're just catching the return value that comes out of the function (RVO). We can't feed the rvalues into range-for, because they are non-const and would thus detach. Saves 2.2KiB in test size on optimized GCC 5.3 Linux AMD64 builds. Change-Id: I914aa20fe65577b2e32ea7ea89d51a8d003a57ba Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src')
-rw-r--r--src/widgets/itemviews/qtableview.cpp4
-rw-r--r--src/widgets/kernel/qapplication.cpp14
-rw-r--r--src/widgets/kernel/qgesturemanager.cpp9
-rw-r--r--src/widgets/kernel/qwhatsthis.cpp6
-rw-r--r--src/widgets/kernel/qwidget.cpp7
-rw-r--r--src/widgets/styles/qwindowsxpstyle.cpp4
-rw-r--r--src/widgets/util/qflickgesture.cpp9
-rw-r--r--src/widgets/widgets/qdialogbuttonbox.cpp3
-rw-r--r--src/widgets/widgets/qmenu.cpp7
9 files changed, 41 insertions, 22 deletions
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp
index 714f09e893..e045d60fc8 100644
--- a/src/widgets/itemviews/qtableview.cpp
+++ b/src/widgets/itemviews/qtableview.cpp
@@ -2016,8 +2016,8 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
if (viewportRect.intersects(rangeRect))
selectionRegion += rangeRect;
if (d->hasSpans()) {
- foreach (QSpanCollection::Span *s,
- d->spans.spansInRect(range.left(), range.top(), range.width(), range.height())) {
+ const auto spansInRect = d->spans.spansInRect(range.left(), range.top(), range.width(), range.height());
+ for (QSpanCollection::Span *s : spansInRect) {
if (range.contains(s->top(), s->left(), range.parent())) {
const QRect &visualSpanRect = d->visualSpanRect(*s);
if (viewportRect.intersects(visualSpanRect))
diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp
index eee91fea47..a0a363ba2b 100644
--- a/src/widgets/kernel/qapplication.cpp
+++ b/src/widgets/kernel/qapplication.cpp
@@ -1718,9 +1718,11 @@ QString QApplicationPrivate::desktopStyleKey()
// first valid one.
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
const QStringList availableKeys = QStyleFactory::keys();
- foreach (const QString &style, theme->themeHint(QPlatformTheme::StyleNames).toStringList())
+ const auto styles = theme->themeHint(QPlatformTheme::StyleNames).toStringList();
+ for (const QString &style : styles) {
if (availableKeys.contains(style, Qt::CaseInsensitive))
return style;
+ }
}
return QString();
}
@@ -2225,10 +2227,13 @@ QWidget *qt_tlw_for_window(QWindow *wnd)
else
break;
}
- if (wnd)
- foreach (QWidget *tlw, qApp->topLevelWidgets())
+ if (wnd) {
+ const auto tlws = qApp->topLevelWidgets();
+ for (QWidget *tlw : tlws) {
if (tlw->windowHandle() == wnd)
return tlw;
+ }
+ }
return 0;
}
@@ -3942,7 +3947,8 @@ void QApplication::alert(QWidget *widget, int duration)
if (QWindow *window= QApplicationPrivate::windowForWidget(widget))
window->alert(duration);
} else {
- foreach (QWidget *topLevel, topLevelWidgets())
+ const auto topLevels = topLevelWidgets();
+ for (QWidget *topLevel : topLevels)
QApplication::alert(topLevel, duration);
}
}
diff --git a/src/widgets/kernel/qgesturemanager.cpp b/src/widgets/kernel/qgesturemanager.cpp
index 4c85be33f5..9a3e9291bf 100644
--- a/src/widgets/kernel/qgesturemanager.cpp
+++ b/src/widgets/kernel/qgesturemanager.cpp
@@ -219,7 +219,8 @@ QGesture *QGestureManager::getState(QObject *object, QGestureRecognizer *recogni
}
// check if the QGesture for this recognizer has already been created
- foreach (QGesture *state, m_objectGestures.value(QGestureManager::ObjectGesture(object, type))) {
+ const auto states = m_objectGestures.value(QGestureManager::ObjectGesture(object, type));
+ for (QGesture *state : states) {
if (m_gestureToRecognizer.value(state) == recognizer)
return state;
}
@@ -684,7 +685,8 @@ void QGestureManager::deliverEvents(const QSet<QGesture *> &gestures,
QApplication::sendEvent(receiver, &event);
bool eventAccepted = event.isAccepted();
- foreach(QGesture *gesture, event.gestures()) {
+ const auto eventGestures = event.gestures();
+ for (QGesture *gesture : eventGestures) {
if (eventAccepted || event.isAccepted(gesture)) {
QWidget *w = event.m_targetWidgets.value(gesture->gestureType(), 0);
Q_ASSERT(w);
@@ -710,7 +712,8 @@ void QGestureManager::deliverEvents(const QSet<QGesture *> &gestures,
QGestureEvent event(it.value());
QApplication::sendEvent(it.key(), &event);
bool eventAccepted = event.isAccepted();
- foreach (QGesture *gesture, event.gestures()) {
+ const auto eventGestures = event.gestures();
+ for (QGesture *gesture : eventGestures) {
if (gesture->state() == Qt::GestureStarted &&
(eventAccepted || event.isAccepted(gesture))) {
QWidget *w = event.m_targetWidgets.value(gesture->gestureType(), 0);
diff --git a/src/widgets/kernel/qwhatsthis.cpp b/src/widgets/kernel/qwhatsthis.cpp
index b69ca54159..66b622911a 100644
--- a/src/widgets/kernel/qwhatsthis.cpp
+++ b/src/widgets/kernel/qwhatsthis.cpp
@@ -375,11 +375,9 @@ class QWhatsThisPrivate : public QObject
void QWhatsThisPrivate::notifyToplevels(QEvent *e)
{
- QWidgetList toplevels = QApplication::topLevelWidgets();
- for (int i = 0; i < toplevels.count(); ++i) {
- QWidget *w = toplevels.at(i);
+ const QWidgetList toplevels = QApplication::topLevelWidgets();
+ for (auto *w : toplevels)
QApplication::sendEvent(w, e);
- }
}
QWhatsThisPrivate *QWhatsThisPrivate::instance = 0;
diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp
index de832d667d..ec5a68eeef 100644
--- a/src/widgets/kernel/qwidget.cpp
+++ b/src/widgets/kernel/qwidget.cpp
@@ -1417,7 +1417,8 @@ void QWidgetPrivate::create_sys(WId window, bool initializeWindow, bool destroyO
win = topData()->window;
}
- foreach (const QByteArray &propertyName, q->dynamicPropertyNames()) {
+ const auto dynamicPropertyNames = q->dynamicPropertyNames();
+ for (const QByteArray &propertyName : dynamicPropertyNames) {
if (!qstrncmp(propertyName, "_q_platform_", 12))
win->setProperty(propertyName, q->property(propertyName));
}
@@ -11984,7 +11985,9 @@ QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
QWidget *targetWidget = 0;
int shortestDistance = INT_MAX;
- foreach(QWidget *targetCandidate, QApplication::allWidgets()) {
+
+ const auto targetCandidates = QApplication::allWidgets();
+ for (QWidget *targetCandidate : targetCandidates) {
const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));
diff --git a/src/widgets/styles/qwindowsxpstyle.cpp b/src/widgets/styles/qwindowsxpstyle.cpp
index cda7dff509..c33e4167c1 100644
--- a/src/widgets/styles/qwindowsxpstyle.cpp
+++ b/src/widgets/styles/qwindowsxpstyle.cpp
@@ -406,10 +406,12 @@ HWND QWindowsXPStylePrivate::winId(const QWidget *widget)
return hwnd;
// Find top level with native window (there might be dialogs that do not have one).
- foreach (const QWidget *toplevel, QApplication::topLevelWidgets())
+ const auto topLevels = QApplication::topLevelWidgets();
+ for (const QWidget *toplevel : topLevels) {
if (toplevel->windowHandle() && toplevel->windowHandle()->handle())
if (const HWND topLevelHwnd = QApplicationPrivate::getHWNDForWidget(toplevel))
return topLevelHwnd;
+ }
if (QDesktopWidget *desktop = qApp->desktop())
if (const HWND desktopHwnd = QApplicationPrivate::getHWNDForWidget(desktop))
diff --git a/src/widgets/util/qflickgesture.cpp b/src/widgets/util/qflickgesture.cpp
index 3d1971db8e..933db1711b 100644
--- a/src/widgets/util/qflickgesture.cpp
+++ b/src/widgets/util/qflickgesture.cpp
@@ -581,7 +581,8 @@ QGestureRecognizer::Result QFlickGestureRecognizer::recognize(QGesture *state,
// Check for an active scroller at globalPos
if (inputType == QScroller::InputPress) {
- foreach (QScroller *as, QScroller::activeScrollers()) {
+ const auto activeScrollers = QScroller::activeScrollers();
+ for (QScroller *as : activeScrollers) {
if (as != scroller) {
QRegion scrollerRegion;
@@ -589,11 +590,13 @@ QGestureRecognizer::Result QFlickGestureRecognizer::recognize(QGesture *state,
scrollerRegion = QRect(w->mapToGlobal(QPoint(0, 0)), w->size());
#ifndef QT_NO_GRAPHICSVIEW
} else if (QGraphicsObject *go = qobject_cast<QGraphicsObject *>(as->target())) {
- if (go->scene()) {
+ if (const auto *scene = go->scene()) {
const auto goBoundingRectMappedToScene = go->mapToScene(go->boundingRect());
- foreach (QGraphicsView *gv, go->scene()->views())
+ const auto views = scene->views();
+ for (QGraphicsView *gv : views) {
scrollerRegion |= gv->mapFromScene(goBoundingRectMappedToScene)
.translated(gv->mapToGlobal(QPoint(0, 0)));
+ }
}
#endif
}
diff --git a/src/widgets/widgets/qdialogbuttonbox.cpp b/src/widgets/widgets/qdialogbuttonbox.cpp
index 6fc8cb5e26..2b68c308ec 100644
--- a/src/widgets/widgets/qdialogbuttonbox.cpp
+++ b/src/widgets/widgets/qdialogbuttonbox.cpp
@@ -964,7 +964,8 @@ bool QDialogButtonBox::event(QEvent *event)
break;
}
- foreach (QPushButton *pb, (dialog ? dialog : this)->findChildren<QPushButton *>()) {
+ const auto pbs = (dialog ? dialog : this)->findChildren<QPushButton *>();
+ for (QPushButton *pb : pbs) {
if (pb->isDefault() && pb != firstAcceptButton) {
hasDefault = true;
break;
diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp
index c07d48a513..1b9e31968a 100644
--- a/src/widgets/widgets/qmenu.cpp
+++ b/src/widgets/widgets/qmenu.cpp
@@ -1270,14 +1270,17 @@ void QMenuPrivate::_q_platformMenuAboutToShow()
Q_Q(QMenu);
#ifdef Q_OS_OSX
- if (platformMenu)
- Q_FOREACH (QAction *action, q->actions())
+ if (platformMenu) {
+ const auto actions = q->actions();
+ for (QAction *action : actions) {
if (QWidget *widget = widgetItems.value(action))
if (widget->parent() == q) {
QPlatformMenuItem *menuItem = platformMenu->menuItemForTag(reinterpret_cast<quintptr>(action));
moveWidgetToPlatformItem(widget, menuItem);
platformMenu->syncMenuItem(menuItem);
}
+ }
+ }
#endif
emit q->aboutToShow();