QtWidgets: replace some Q_FOREACH loops with C++11 range-for

This needs to be handled a bit carefully, because Qt containers
will detach upon being iterated over using range-for.

In the cases of this patch, that cannot happen, because all
containers are marked as const (either by this patch or before).

Separate patches will deal with other situations.

Range-for loops are much more efficient than foreach loops.

This patch shaves almost 3K of text size off an optimized Linux
AMD64 GCC 4.9 build.

Change-Id: I7b1d41db4d9b5db8b515cb75686dc5135177da68
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2015-11-30 12:19:31 +01:00
parent 2d3b04e4f6
commit b697518634
6 changed files with 34 additions and 33 deletions

View File

@ -1187,13 +1187,13 @@ QList<QUrl> QFileDialogPrivate::userSelectedFiles() const
const QModelIndexList selectedRows = qFileDialogUi->listView->selectionModel()->selectedRows();
files.reserve(selectedRows.size());
foreach (const QModelIndex &index, selectedRows)
for (const QModelIndex &index : selectedRows)
files.append(QUrl::fromLocalFile(index.data(QFileSystemModel::FilePathRole).toString()));
if (files.isEmpty() && !lineEdit()->text().isEmpty()) {
const QStringList typedFilesList = typedFiles();
files.reserve(typedFilesList.size());
foreach (const QString &path, typedFilesList)
for (const QString &path : typedFilesList)
files.append(QUrl::fromLocalFile(path));
}
@ -1257,7 +1257,7 @@ QStringList QFileDialog::selectedFiles() const
QStringList files;
const QList<QUrl> userSelectedFiles = d->userSelectedFiles();
files.reserve(userSelectedFiles.size());
foreach (const QUrl &file, userSelectedFiles)
for (const QUrl &file : userSelectedFiles)
files.append(file.toLocalFile());
if (files.isEmpty() && d->usingWidgets()) {
const FileMode fm = fileMode();
@ -1284,7 +1284,7 @@ QList<QUrl> QFileDialog::selectedUrls() const
QList<QUrl> urls;
const QStringList selectedFileList = selectedFiles();
urls.reserve(selectedFileList.size());
foreach (const QString &file, selectedFileList)
for (const QString &file : selectedFileList)
urls.append(QUrl::fromLocalFile(file));
return urls;
}
@ -1554,7 +1554,7 @@ void QFileDialog::setMimeTypeFilters(const QStringList &filters)
{
Q_D(QFileDialog);
QStringList nameFilters;
foreach (const QString &mimeType, filters) {
for (const QString &mimeType : filters) {
const QString text = nameFilterForMime(mimeType);
if (!text.isEmpty())
nameFilters.append(text);
@ -2218,7 +2218,7 @@ QStringList QFileDialog::getOpenFileNames(QWidget *parent,
const QList<QUrl> selectedUrls = getOpenFileUrls(parent, caption, QUrl::fromLocalFile(dir), filter, selectedFilter, options, schemes);
QStringList fileNames;
fileNames.reserve(selectedUrls.size());
foreach (const QUrl &url, selectedUrls)
for (const QUrl &url : selectedUrls)
fileNames << url.toLocalFile();
return fileNames;
}
@ -2696,7 +2696,7 @@ void QFileDialogPrivate::saveSettings()
QStringList historyUrls;
const QStringList history = q->history();
historyUrls.reserve(history.size());
foreach (const QString &path, history)
for (const QString &path : history)
historyUrls << QUrl::fromLocalFile(path).toString();
settings.setValue(QLatin1String("history"), historyUrls);
settings.setValue(QLatin1String("lastVisited"), lastVisitedDir()->toString());
@ -2730,7 +2730,8 @@ bool QFileDialogPrivate::restoreFromSettings()
return true;
QStringList history;
foreach (const QString &urlStr, settings.value(QLatin1String("history")).toStringList()) {
const auto urlStrings = settings.value(QLatin1String("history")).toStringList();
for (const QString &urlStr : urlStrings) {
QUrl url(urlStr);
if (url.isLocalFile())
history << url.toLocalFile();
@ -2998,7 +2999,8 @@ void QFileDialogPrivate::createWidgets()
q->setHistory(options->history());
if (options->initiallySelectedFiles().count() == 1)
q->selectFile(options->initiallySelectedFiles().first().fileName());
foreach (const QUrl &url, options->initiallySelectedFiles())
const auto initiallySelectedFiles = options->initiallySelectedFiles();
for (const QUrl &url : initiallySelectedFiles)
q->selectUrl(url);
lineEdit()->selectAll();
_q_updateOkButton();
@ -3755,7 +3757,7 @@ void QFileDialogPrivate::_q_emitUrlsSelected(const QList<QUrl> &files)
Q_Q(QFileDialog);
emit q->urlsSelected(files);
QStringList localFiles;
foreach (const QUrl &file, files)
for (const QUrl &file : files)
if (file.isLocalFile())
localFiles.append(file.toLocalFile());
if (!localFiles.isEmpty())

View File

@ -481,7 +481,8 @@ void QFontDialogPrivate::updateFamilies()
QFontDatabase fdb;
QStringList familyNames;
foreach (const QString &family, fdb.families(writingSystem)) {
const auto families = fdb.families(writingSystem);
for (const QString &family : families) {
if (fdb.isPrivateFamily(family))
continue;

View File

@ -602,10 +602,10 @@ QSimplexConstraint *GraphPath::constraint(const GraphPath &path) const
QString GraphPath::toString() const
{
QString string(QLatin1String("Path: "));
foreach(AnchorData *edge, positives)
for (AnchorData *edge : positives)
string += QString::fromLatin1(" (+++) %1").arg(edge->toString());
foreach(AnchorData *edge, negatives)
for (AnchorData *edge : negatives)
string += QString::fromLatin1(" (---) %1").arg(edge->toString());
return string;
@ -1932,8 +1932,7 @@ void QGraphicsAnchorLayoutPrivate::removeVertex(QGraphicsLayoutItem *item, Qt::A
if (AnchorVertex *v = internalVertex(item, edge)) {
Graph<AnchorVertex, AnchorData> &g = graph[edgeOrientation(edge)];
const QList<AnchorVertex *> allVertices = graph[edgeOrientation(edge)].adjacentVertices(v);
AnchorVertex *v2;
foreach (v2, allVertices) {
for (auto *v2 : allVertices) {
g.removeEdge(v, v2);
removeInternalVertex(item, edge);
removeInternalVertex(v2->m_item, v2->m_edge);
@ -2223,12 +2222,10 @@ bool QGraphicsAnchorLayoutPrivate::calculateTrunk(Orientation orientation, const
// Calculate and set the preferred size for the layout,
// from the edge sizes that were calculated above.
qreal pref(0.0);
foreach (const AnchorData *ad, path.positives) {
for (const AnchorData *ad : path.positives)
pref += ad->sizeAtPreferred;
}
foreach (const AnchorData *ad, path.negatives) {
for (const AnchorData *ad : path.negatives)
pref -= ad->sizeAtPreferred;
}
sizeHints[orientation][Qt::MinimumSize] = min;
sizeHints[orientation][Qt::PreferredSize] = pref;
@ -2368,8 +2365,8 @@ void QGraphicsAnchorLayoutPrivate::findPaths(Orientation orientation)
*/
void QGraphicsAnchorLayoutPrivate::constraintsFromPaths(Orientation orientation)
{
foreach (AnchorVertex *vertex, graphPaths[orientation].uniqueKeys())
{
const auto vertices = graphPaths[orientation].uniqueKeys();
for (AnchorVertex *vertex : vertices) {
int valueCount = graphPaths[orientation].count(vertex);
if (valueCount == 1)
continue;

View File

@ -469,7 +469,7 @@ void QAbstractScrollAreaPrivate::layoutChildren()
if ((vscrollOverlap > 0 && needv) || (hscrollOverlap > 0 && needh)) {
const QList<QHeaderView *> headers = q->findChildren<QHeaderView*>();
if (headers.count() <= 2) {
Q_FOREACH (const QHeaderView *header, headers) {
for (const QHeaderView *header : headers) {
const QRect geo = header->geometry();
if (header->orientation() == Qt::Vertical && header->isVisible() && QStyle::visualRect(opt.direction, opt.rect, geo).left() <= opt.rect.width() / 2)
vHeaderRight = QStyle::visualRect(opt.direction, opt.rect, geo).right();

View File

@ -408,7 +408,7 @@ void IconTiler::rearrange(QList<QWidget *> &widgets, const QRect &domain) const
int MinOverlapPlacer::accumulatedOverlap(const QRect &source, const QVector<QRect> &rects)
{
int accOverlap = 0;
foreach (const QRect &rect, rects) {
for (const QRect &rect : rects) {
QRect intersection = source.intersected(rect);
accOverlap += intersection.width() * intersection.height();
}
@ -425,7 +425,7 @@ QRect MinOverlapPlacer::findMinOverlapRect(const QVector<QRect> &source, const Q
{
int minAccOverlap = -1;
QRect minAccOverlapRect;
foreach (const QRect &srcRect, source) {
for (const QRect &srcRect : source) {
const int accOverlap = accumulatedOverlap(srcRect, rects);
if (accOverlap < minAccOverlap || minAccOverlap == -1) {
minAccOverlap = accOverlap;
@ -454,7 +454,7 @@ QVector<QRect> MinOverlapPlacer::getCandidatePlacements(const QSize &size, const
if (domain.bottom() - size.height() + 1 >= 0)
ylist << domain.bottom() - size.height() + 1;
foreach (const QRect &rect, rects) {
for (const QRect &rect : rects) {
xlist << rect.right() + 1;
ylist << rect.bottom() + 1;
}
@ -504,7 +504,7 @@ QVector<QRect> MinOverlapPlacer::findMaxOverlappers(const QRect &domain, const Q
result.reserve(source.size());
int maxOverlap = -1;
foreach (const QRect &srcRect, source) {
for (const QRect &srcRect : source) {
QRect intersection = domain.intersected(srcRect);
const int overlap = intersection.width() * intersection.height();
if (overlap >= maxOverlap || maxOverlap == -1) {
@ -549,7 +549,7 @@ QPoint MinOverlapPlacer::place(const QSize &size, const QVector<QRect> &rects,
{
if (size.isEmpty() || !domain.isValid())
return QPoint();
foreach (const QRect &rect, rects) {
for (const QRect &rect : rects) {
if (!rect.isValid())
return QPoint();
}
@ -1237,7 +1237,8 @@ void QMdiAreaPrivate::internalRaise(QMdiSubWindow *mdiChild) const
QMdiSubWindow *stackUnderChild = 0;
if (!windowStaysOnTop(mdiChild)) {
foreach (QObject *object, viewport->children()) {
const auto children = viewport->children(); // take a copy, as raising/stacking under changes the order
for (QObject *object : children) {
QMdiSubWindow *child = qobject_cast<QMdiSubWindow *>(object);
if (!child || !childWindows.contains(child))
continue;
@ -1392,7 +1393,7 @@ QMdiAreaPrivate::subWindowList(QMdiArea::WindowOrder order, bool reversed) const
list.prepend(child);
}
} else if (order == QMdiArea::StackingOrder) {
foreach (QObject *object, viewport->children()) {
for (QObject *object : viewport->children()) {
QMdiSubWindow *child = qobject_cast<QMdiSubWindow *>(object);
if (!child || !childWindows.contains(child))
continue;
@ -1741,7 +1742,7 @@ QSize QMdiArea::sizeHint() const
QSize desktopSize = QApplication::desktop()->size();
QSize size(desktopSize.width() * 2 / scaleFactor, desktopSize.height() * 2 / scaleFactor);
foreach (QMdiSubWindow *child, d_func()->childWindows) {
for (QMdiSubWindow *child : d_func()->childWindows) {
if (!sanityCheck(child, "QMdiArea::sizeHint"))
continue;
size = size.expandedTo(child->sizeHint());
@ -1759,7 +1760,7 @@ QSize QMdiArea::minimumSizeHint() const
style()->pixelMetric(QStyle::PM_TitleBarHeight, 0, this));
size = size.expandedTo(QAbstractScrollArea::minimumSizeHint());
if (!d->scrollBarsEnabled()) {
foreach (QMdiSubWindow *child, d->childWindows) {
for (QMdiSubWindow *child : d->childWindows) {
if (!sanityCheck(child, "QMdiArea::sizeHint"))
continue;
size = size.expandedTo(child->minimumSizeHint());

View File

@ -2206,8 +2206,8 @@ void QMdiSubWindowPrivate::setSizeGrip(QSizeGrip *newSizeGrip)
void QMdiSubWindowPrivate::setSizeGripVisible(bool visible) const
{
// See if we can find any size grips
QList<QSizeGrip *> sizeGrips = q_func()->findChildren<QSizeGrip *>();
foreach (QSizeGrip *grip, sizeGrips)
const QList<QSizeGrip *> sizeGrips = q_func()->findChildren<QSizeGrip *>();
for (QSizeGrip *grip : sizeGrips)
grip->setVisible(visible);
}