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>
This commit is contained in:
Marc Mutz 2016-01-26 14:38:54 +01:00
parent e1c2bfa53b
commit 85c2a128ef
9 changed files with 41 additions and 22 deletions

View File

@ -2016,8 +2016,8 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
if (viewportRect.intersects(rangeRect)) if (viewportRect.intersects(rangeRect))
selectionRegion += rangeRect; selectionRegion += rangeRect;
if (d->hasSpans()) { if (d->hasSpans()) {
foreach (QSpanCollection::Span *s, const auto spansInRect = d->spans.spansInRect(range.left(), range.top(), range.width(), range.height());
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())) { if (range.contains(s->top(), s->left(), range.parent())) {
const QRect &visualSpanRect = d->visualSpanRect(*s); const QRect &visualSpanRect = d->visualSpanRect(*s);
if (viewportRect.intersects(visualSpanRect)) if (viewportRect.intersects(visualSpanRect))

View File

@ -1718,9 +1718,11 @@ QString QApplicationPrivate::desktopStyleKey()
// first valid one. // first valid one.
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) { if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
const QStringList availableKeys = QStyleFactory::keys(); 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)) if (availableKeys.contains(style, Qt::CaseInsensitive))
return style; return style;
}
} }
return QString(); return QString();
} }
@ -2225,10 +2227,13 @@ QWidget *qt_tlw_for_window(QWindow *wnd)
else else
break; break;
} }
if (wnd) if (wnd) {
foreach (QWidget *tlw, qApp->topLevelWidgets()) const auto tlws = qApp->topLevelWidgets();
for (QWidget *tlw : tlws) {
if (tlw->windowHandle() == wnd) if (tlw->windowHandle() == wnd)
return tlw; return tlw;
}
}
return 0; return 0;
} }
@ -3942,7 +3947,8 @@ void QApplication::alert(QWidget *widget, int duration)
if (QWindow *window= QApplicationPrivate::windowForWidget(widget)) if (QWindow *window= QApplicationPrivate::windowForWidget(widget))
window->alert(duration); window->alert(duration);
} else { } else {
foreach (QWidget *topLevel, topLevelWidgets()) const auto topLevels = topLevelWidgets();
for (QWidget *topLevel : topLevels)
QApplication::alert(topLevel, duration); QApplication::alert(topLevel, duration);
} }
} }

View File

@ -219,7 +219,8 @@ QGesture *QGestureManager::getState(QObject *object, QGestureRecognizer *recogni
} }
// check if the QGesture for this recognizer has already been created // 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) if (m_gestureToRecognizer.value(state) == recognizer)
return state; return state;
} }
@ -684,7 +685,8 @@ void QGestureManager::deliverEvents(const QSet<QGesture *> &gestures,
QApplication::sendEvent(receiver, &event); QApplication::sendEvent(receiver, &event);
bool eventAccepted = event.isAccepted(); bool eventAccepted = event.isAccepted();
foreach(QGesture *gesture, event.gestures()) { const auto eventGestures = event.gestures();
for (QGesture *gesture : eventGestures) {
if (eventAccepted || event.isAccepted(gesture)) { if (eventAccepted || event.isAccepted(gesture)) {
QWidget *w = event.m_targetWidgets.value(gesture->gestureType(), 0); QWidget *w = event.m_targetWidgets.value(gesture->gestureType(), 0);
Q_ASSERT(w); Q_ASSERT(w);
@ -710,7 +712,8 @@ void QGestureManager::deliverEvents(const QSet<QGesture *> &gestures,
QGestureEvent event(it.value()); QGestureEvent event(it.value());
QApplication::sendEvent(it.key(), &event); QApplication::sendEvent(it.key(), &event);
bool eventAccepted = event.isAccepted(); bool eventAccepted = event.isAccepted();
foreach (QGesture *gesture, event.gestures()) { const auto eventGestures = event.gestures();
for (QGesture *gesture : eventGestures) {
if (gesture->state() == Qt::GestureStarted && if (gesture->state() == Qt::GestureStarted &&
(eventAccepted || event.isAccepted(gesture))) { (eventAccepted || event.isAccepted(gesture))) {
QWidget *w = event.m_targetWidgets.value(gesture->gestureType(), 0); QWidget *w = event.m_targetWidgets.value(gesture->gestureType(), 0);

View File

@ -375,11 +375,9 @@ class QWhatsThisPrivate : public QObject
void QWhatsThisPrivate::notifyToplevels(QEvent *e) void QWhatsThisPrivate::notifyToplevels(QEvent *e)
{ {
QWidgetList toplevels = QApplication::topLevelWidgets(); const QWidgetList toplevels = QApplication::topLevelWidgets();
for (int i = 0; i < toplevels.count(); ++i) { for (auto *w : toplevels)
QWidget *w = toplevels.at(i);
QApplication::sendEvent(w, e); QApplication::sendEvent(w, e);
}
} }
QWhatsThisPrivate *QWhatsThisPrivate::instance = 0; QWhatsThisPrivate *QWhatsThisPrivate::instance = 0;

View File

@ -1417,7 +1417,8 @@ void QWidgetPrivate::create_sys(WId window, bool initializeWindow, bool destroyO
win = topData()->window; 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)) if (!qstrncmp(propertyName, "_q_platform_", 12))
win->setProperty(propertyName, q->property(propertyName)); win->setProperty(propertyName, q->property(propertyName));
} }
@ -11984,7 +11985,9 @@ QWidget *QWidgetPrivate::widgetInNavigationDirection(Direction direction)
QWidget *targetWidget = 0; QWidget *targetWidget = 0;
int shortestDistance = INT_MAX; 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())); const QRect targetCandidateRect = targetCandidate->rect().translated(targetCandidate->mapToGlobal(QPoint()));

View File

@ -406,10 +406,12 @@ HWND QWindowsXPStylePrivate::winId(const QWidget *widget)
return hwnd; return hwnd;
// Find top level with native window (there might be dialogs that do not have one). // 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 (toplevel->windowHandle() && toplevel->windowHandle()->handle())
if (const HWND topLevelHwnd = QApplicationPrivate::getHWNDForWidget(toplevel)) if (const HWND topLevelHwnd = QApplicationPrivate::getHWNDForWidget(toplevel))
return topLevelHwnd; return topLevelHwnd;
}
if (QDesktopWidget *desktop = qApp->desktop()) if (QDesktopWidget *desktop = qApp->desktop())
if (const HWND desktopHwnd = QApplicationPrivate::getHWNDForWidget(desktop)) if (const HWND desktopHwnd = QApplicationPrivate::getHWNDForWidget(desktop))

View File

@ -581,7 +581,8 @@ QGestureRecognizer::Result QFlickGestureRecognizer::recognize(QGesture *state,
// Check for an active scroller at globalPos // Check for an active scroller at globalPos
if (inputType == QScroller::InputPress) { if (inputType == QScroller::InputPress) {
foreach (QScroller *as, QScroller::activeScrollers()) { const auto activeScrollers = QScroller::activeScrollers();
for (QScroller *as : activeScrollers) {
if (as != scroller) { if (as != scroller) {
QRegion scrollerRegion; QRegion scrollerRegion;
@ -589,11 +590,13 @@ QGestureRecognizer::Result QFlickGestureRecognizer::recognize(QGesture *state,
scrollerRegion = QRect(w->mapToGlobal(QPoint(0, 0)), w->size()); scrollerRegion = QRect(w->mapToGlobal(QPoint(0, 0)), w->size());
#ifndef QT_NO_GRAPHICSVIEW #ifndef QT_NO_GRAPHICSVIEW
} else if (QGraphicsObject *go = qobject_cast<QGraphicsObject *>(as->target())) { } 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()); 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) scrollerRegion |= gv->mapFromScene(goBoundingRectMappedToScene)
.translated(gv->mapToGlobal(QPoint(0, 0))); .translated(gv->mapToGlobal(QPoint(0, 0)));
}
} }
#endif #endif
} }

View File

@ -964,7 +964,8 @@ bool QDialogButtonBox::event(QEvent *event)
break; 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) { if (pb->isDefault() && pb != firstAcceptButton) {
hasDefault = true; hasDefault = true;
break; break;

View File

@ -1270,14 +1270,17 @@ void QMenuPrivate::_q_platformMenuAboutToShow()
Q_Q(QMenu); Q_Q(QMenu);
#ifdef Q_OS_OSX #ifdef Q_OS_OSX
if (platformMenu) if (platformMenu) {
Q_FOREACH (QAction *action, q->actions()) const auto actions = q->actions();
for (QAction *action : actions) {
if (QWidget *widget = widgetItems.value(action)) if (QWidget *widget = widgetItems.value(action))
if (widget->parent() == q) { if (widget->parent() == q) {
QPlatformMenuItem *menuItem = platformMenu->menuItemForTag(reinterpret_cast<quintptr>(action)); QPlatformMenuItem *menuItem = platformMenu->menuItemForTag(reinterpret_cast<quintptr>(action));
moveWidgetToPlatformItem(widget, menuItem); moveWidgetToPlatformItem(widget, menuItem);
platformMenu->syncMenuItem(menuItem); platformMenu->syncMenuItem(menuItem);
} }
}
}
#endif #endif
emit q->aboutToShow(); emit q->aboutToShow();