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

This needs to be handled a bit carefully, because Qt containers
will detach upon being iteratoed 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.

Apart from being more readable, range-for loops are also the
most efficient for loop.

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

Change-Id: I53810c7b25420b4fd449d20c90c07503c5e76a66
Reviewed-by: Edward Welbourne <edward.welbourne@theqtcompany.com>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2015-11-30 12:19:31 +01:00
parent b697518634
commit 0516487237
22 changed files with 138 additions and 170 deletions

View File

@ -53,10 +53,9 @@ QT_BEGIN_NAMESPACE
static QList<QWidget*> childWidgets(const QWidget *widget)
{
QList<QObject*> list = widget->children();
QList<QWidget*> widgets;
for (int i = 0; i < list.size(); ++i) {
QWidget *w = qobject_cast<QWidget *>(list.at(i));
for (QObject *o : widget->children()) {
QWidget *w = qobject_cast<QWidget *>(o);
if (w && !w->isWindow()
&& !qobject_cast<QFocusFrame*>(w)
#if !defined(QT_NO_MENU)
@ -77,9 +76,8 @@ static QString buddyString(const QWidget *widget)
if (!parent)
return QString();
#ifndef QT_NO_SHORTCUT
QObjectList ol = parent->children();
for (int i = 0; i < ol.size(); ++i) {
QLabel *label = qobject_cast<QLabel*>(ol.at(i));
for (QObject *o : parent->children()) {
QLabel *label = qobject_cast<QLabel*>(o);
if (label && label->buddy() == widget)
return label->text();
}
@ -302,8 +300,8 @@ QAccessibleWidget::relations(QAccessible::Relation match /*= QAccessible::AllRel
// ideally we would go through all objects and check, but that
// will be too expensive
const QList<QWidget*> kids = childWidgets(parent);
for (int i = 0; i < kids.count(); ++i) {
if (QLabel *labelSibling = qobject_cast<QLabel*>(kids.at(i))) {
for (QWidget *kid : kids) {
if (QLabel *labelSibling = qobject_cast<QLabel*>(kid)) {
if (labelSibling->buddy() == widget()) {
QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(labelSibling);
rels.append(qMakePair(iface, rel));

View File

@ -71,10 +71,9 @@ QList<QWidget*> childWidgets(const QWidget *widget)
{
if (widget == 0)
return QList<QWidget*>();
QList<QObject*> list = widget->children();
QList<QWidget*> widgets;
for (int i = 0; i < list.size(); ++i) {
QWidget *w = qobject_cast<QWidget *>(list.at(i));
for (QObject *o : widget->children()) {
QWidget *w = qobject_cast<QWidget *>(o);
if (!w)
continue;
QString objectName = w->objectName();
@ -1056,10 +1055,9 @@ QAccessibleInterface *QAccessibleMainWindow::childAt(int x, int y) const
if (!QRect(gp.x(), gp.y(), w->width(), w->height()).contains(x, y))
return 0;
QWidgetList kids = childWidgets(mainWindow());
const QWidgetList kids = childWidgets(mainWindow());
QPoint rp = mainWindow()->mapFromGlobal(QPoint(x, y));
for (int i = 0; i < kids.size(); ++i) {
QWidget *child = kids.at(i);
for (QWidget *child : kids) {
if (!child->isWindow() && !child->isHidden() && child->geometry().contains(rp)) {
return QAccessible::queryAccessibleInterface(child);
}

View File

@ -570,8 +570,8 @@ QAccessibleGroupBox::relations(QAccessible::Relation match /* = QAccessible::All
if ((match & QAccessible::Labelled) && (!groupBox()->title().isEmpty())) {
const QList<QWidget*> kids = childWidgets(widget());
for (int i = 0; i < kids.count(); ++i) {
QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(kids.at(i));
for (QWidget *kid : kids) {
QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(kid);
if (iface)
rels.append(qMakePair(iface, QAccessible::Relation(QAccessible::Labelled)));
}

View File

@ -2587,7 +2587,7 @@ void QFileDialog::accept()
return;
}
QStringList files = selectedFiles();
const QStringList files = selectedFiles();
if (files.isEmpty())
return;
QString lineEditText = d->lineEdit()->text();
@ -2657,10 +2657,10 @@ void QFileDialog::accept()
case ExistingFile:
case ExistingFiles:
for (int i = 0; i < files.count(); ++i) {
QFileInfo info(files.at(i));
for (const auto &file : files) {
QFileInfo info(file);
if (!info.exists())
info = QFileInfo(d->getEnvironmentVariable(files.at(i)));
info = QFileInfo(d->getEnvironmentVariable(file));
if (!info.exists()) {
#ifndef QT_NO_MESSAGEBOX
QString message = tr("%1\nFile not found.\nPlease verify the "
@ -3444,15 +3444,13 @@ void QFileDialogPrivate::_q_autoCompleteFileName(const QString &text)
return;
}
QStringList multipleFiles = typedFiles();
const QStringList multipleFiles = typedFiles();
if (multipleFiles.count() > 0) {
QModelIndexList oldFiles = qFileDialogUi->listView->selectionModel()->selectedRows();
QModelIndexList newFiles;
for (int i = 0; i < multipleFiles.count(); ++i) {
QModelIndex idx = model->index(multipleFiles.at(i));
if (oldFiles.contains(idx))
oldFiles.removeAll(idx);
else
for (const auto &file : multipleFiles) {
QModelIndex idx = model->index(file);
if (oldFiles.removeAll(idx) == 0)
newFiles.append(idx);
}
for (int i = 0; i < newFiles.count(); ++i)
@ -3479,7 +3477,7 @@ void QFileDialogPrivate::_q_updateOkButton()
bool enableButton = true;
bool isOpenDirectory = false;
QStringList files = q->selectedFiles();
const QStringList files = q->selectedFiles();
QString lineEditText = lineEdit()->text();
if (lineEditText.startsWith(QLatin1String("//")) || lineEditText.startsWith(QLatin1Char('\\'))) {
@ -3538,10 +3536,10 @@ void QFileDialogPrivate::_q_updateOkButton()
}
case QFileDialog::ExistingFile:
case QFileDialog::ExistingFiles:
for (int i = 0; i < files.count(); ++i) {
QModelIndex idx = model->index(files.at(i));
for (const auto &file : files) {
QModelIndex idx = model->index(file);
if (!idx.isValid())
idx = model->index(getEnvironmentVariable(files.at(i)));
idx = model->index(getEnvironmentVariable(file));
if (!idx.isValid()) {
enableButton = false;
break;
@ -3682,14 +3680,14 @@ void QFileDialogPrivate::_q_useNameFilter(int index)
void QFileDialogPrivate::_q_selectionChanged()
{
const QFileDialog::FileMode fileMode = q_func()->fileMode();
QModelIndexList indexes = qFileDialogUi->listView->selectionModel()->selectedRows();
const QModelIndexList indexes = qFileDialogUi->listView->selectionModel()->selectedRows();
bool stripDirs = (fileMode != QFileDialog::DirectoryOnly && fileMode != QFileDialog::Directory);
QStringList allFiles;
for (int i = 0; i < indexes.count(); ++i) {
if (stripDirs && model->isDir(mapToSource(indexes.at(i))))
for (const auto &index : indexes) {
if (stripDirs && model->isDir(mapToSource(index)))
continue;
allFiles.append(indexes.at(i).data().toString());
allFiles.append(index.data().toString());
}
if (allFiles.count() > 1)
for (int i = 0; i < allFiles.count(); ++i) {

View File

@ -282,8 +282,8 @@ void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &fil
infoList = QDir::drives();
} else {
infoList.reserve(files.count());
for (int i = 0; i < files.count(); ++i)
infoList << QFileInfo(files.at(i));
for (const auto &file : files)
infoList << QFileInfo(file);
}
for (int i = infoList.count() - 1; i >= 0; --i) {
QString driveName = translateDriveName(infoList.at(i));

View File

@ -1562,10 +1562,9 @@ void QFileSystemModel::setNameFilters(const QStringList &filters)
d->bypassFilters.clear();
// We guarantee that rootPath will stick around
QPersistentModelIndex root(index(rootPath()));
QModelIndexList persistantList = persistentIndexList();
for (int i = 0; i < persistantList.count(); ++i) {
QFileSystemModelPrivate::QFileSystemNode *node;
node = d->node(persistantList.at(i));
const QModelIndexList persistentList = persistentIndexList();
for (const auto &persistentIndex : persistentList) {
QFileSystemModelPrivate::QFileSystemNode *node = d->node(persistentIndex);
while (node) {
if (d->bypassFilters.contains(node))
break;
@ -1579,9 +1578,8 @@ void QFileSystemModel::setNameFilters(const QStringList &filters)
d->nameFilters.clear();
const Qt::CaseSensitivity caseSensitive =
(filter() & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive;
for (int i = 0; i < filters.size(); ++i) {
d->nameFilters << QRegExp(filters.at(i), caseSensitive, QRegExp::Wildcard);
}
for (const auto &filter : filters)
d->nameFilters << QRegExp(filter, caseSensitive, QRegExp::Wildcard);
d->forceSort = true;
d->delayedSort();
#endif
@ -1734,9 +1732,9 @@ void QFileSystemModelPrivate::addVisibleFiles(QFileSystemNode *parentNode, const
if (parentNode->dirtyChildrenIndex == -1)
parentNode->dirtyChildrenIndex = parentNode->visibleChildren.count();
for (int i = 0; i < newFiles.count(); ++i) {
parentNode->visibleChildren.append(newFiles.at(i));
parentNode->children.value(newFiles.at(i))->isVisible = true;
for (const auto &newFile : newFiles) {
parentNode->visibleChildren.append(newFile);
parentNode->children.value(newFile)->isVisible = true;
}
if (!indexHidden)
q->endInsertRows();
@ -1779,10 +1777,10 @@ void QFileSystemModelPrivate::_q_fileSystemChanged(const QString &path, const QV
QStringList newFiles;
QFileSystemModelPrivate::QFileSystemNode *parentNode = node(path, false);
QModelIndex parentIndex = index(parentNode);
for (int i = 0; i < updates.count(); ++i) {
QString fileName = updates.at(i).first;
for (const auto &update : updates) {
QString fileName = update.first;
Q_ASSERT(!fileName.isEmpty());
QExtendedInformation info = fileInfoGatherer.getInfo(updates.at(i).second);
QExtendedInformation info = fileInfoGatherer.getInfo(update.second);
bool previouslyHere = parentNode->children.contains(fileName);
if (!previouslyHere) {
addNode(parentNode, fileName, info.fileInfo());
@ -1971,8 +1969,8 @@ bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const
// Check the name regularexpression filters
if (!(node->isDir() && (filters & QDir::AllDirs))) {
for (int i = 0; i < nameFilters.size(); ++i) {
QRegExp copy = nameFilters.at(i);
for (const auto &nameFilter : nameFilters) {
QRegExp copy = nameFilter;
if (copy.exactMatch(node->fileName))
return true;
}

View File

@ -1047,26 +1047,26 @@ void QMessageBoxPrivate::detectEscapeButton()
}
// if the message box has one RejectRole button, make it the escape button
for (int i = 0; i < buttons.count(); i++) {
if (buttonBox->buttonRole(buttons.at(i)) == QDialogButtonBox::RejectRole) {
for (auto *button : buttons) {
if (buttonBox->buttonRole(button) == QDialogButtonBox::RejectRole) {
if (detectedEscapeButton) { // already detected!
detectedEscapeButton = 0;
break;
}
detectedEscapeButton = buttons.at(i);
detectedEscapeButton = button;
}
}
if (detectedEscapeButton)
return;
// if the message box has one NoRole button, make it the escape button
for (int i = 0; i < buttons.count(); i++) {
if (buttonBox->buttonRole(buttons.at(i)) == QDialogButtonBox::NoRole) {
for (auto *button : buttons) {
if (buttonBox->buttonRole(button) == QDialogButtonBox::NoRole) {
if (detectedEscapeButton) { // already detected!
detectedEscapeButton = 0;
break;
}
detectedEscapeButton = buttons.at(i);
detectedEscapeButton = button;
}
}
}
@ -1506,8 +1506,7 @@ void QMessageBox::keyPressEvent(QKeyEvent *e)
int key = e->key() & ~Qt::MODIFIER_MASK;
if (key) {
const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
for (int i = 0; i < buttons.count(); ++i) {
QAbstractButton *pb = buttons.at(i);
for (auto *pb : buttons) {
QKeySequence shortcut = pb->shortcut();
if (!shortcut.isEmpty() && key == int(shortcut[0] & ~Qt::MODIFIER_MASK)) {
pb->animateClick();

View File

@ -103,9 +103,9 @@ Qt::ItemFlags QUrlModel::flags(const QModelIndex &index) const
QMimeData *QUrlModel::mimeData(const QModelIndexList &indexes) const
{
QList<QUrl> list;
for (int i = 0; i < indexes.count(); ++i) {
if (indexes.at(i).column() == 0)
list.append(indexes.at(i).data(UrlRole).toUrl());
for (const auto &index : indexes) {
if (index.column() == 0)
list.append(index.data(UrlRole).toUrl());
}
QMimeData *data = new QMimeData();
data->setUrls(list);
@ -125,8 +125,8 @@ bool QUrlModel::canDrop(QDragEnterEvent *event)
return false;
const QList<QUrl> list = event->mimeData()->urls();
for (int i = 0; i < list.count(); ++i) {
QModelIndex idx = fileSystemModel->index(list.at(0).toLocalFile());
for (const auto &url : list) {
const QModelIndex idx = fileSystemModel->index(url.toLocalFile());
if (!fileSystemModel->isDir(idx))
return false;
}

View File

@ -3622,9 +3622,8 @@ void QWizardPage::cleanupPage()
{
Q_D(QWizardPage);
if (d->wizard) {
QVector<QWizardField> &fields = d->wizard->d_func()->fields;
for (int i = 0; i < fields.count(); ++i) {
const QWizardField &field = fields.at(i);
const QVector<QWizardField> &fields = d->wizard->d_func()->fields;
for (const auto &field : fields) {
if (field.page == this)
field.object->setProperty(field.property, field.initialValue);
}

View File

@ -222,9 +222,8 @@ public:
QSet<Vertex *> setOfVertices = vertices();
for (typename QSet<Vertex*>::const_iterator it = setOfVertices.begin(); it != setOfVertices.end(); ++it) {
Vertex *v = *it;
QList<Vertex*> adjacents = adjacentVertices(v);
for (int i = 0; i < adjacents.count(); ++i) {
Vertex *v1 = adjacents.at(i);
const QList<Vertex*> adjacents = adjacentVertices(v);
for (auto *v1 : adjacents) {
EdgeData *data = edgeData(v, v1);
bool forward = data->from == v;
if (forward) {

View File

@ -4351,8 +4351,7 @@ QItemViewPaintPairs QAbstractItemViewPrivate::draggablePaintPairs(const QModelIn
QRect &rect = *r;
const QRect viewportRect = viewport->rect();
QItemViewPaintPairs ret;
for (int i = 0; i < indexes.count(); ++i) {
const QModelIndex &index = indexes.at(i);
for (const auto &index : indexes) {
const QRect current = q->visualRect(index);
if (current.intersects(viewportRect)) {
QItemViewPaintPair p = { current, index };

View File

@ -177,9 +177,10 @@ public:
// Whether the data can actually be dropped will be checked in drag move.
if (event->type() == QEvent::DragEnter && (event->dropAction() & model->supportedDropActions())) {
const QStringList modelTypes = model->mimeTypes();
for (int i = 0; i < modelTypes.count(); ++i)
if (mime->hasFormat(modelTypes.at(i)))
for (const auto &modelType : modelTypes) {
if (mime->hasFormat(modelType))
return true;
}
}
QModelIndex index;

View File

@ -169,8 +169,8 @@ void QDirModelPrivate::invalidate()
const QDirNode *current = nodes.pop();
current->stat = false;
const QVector<QDirNode> &children = current->children;
for (int i = 0; i < children.count(); ++i)
nodes.push(&children.at(i));
for (const auto &child : children)
nodes.push(&child);
}
}

View File

@ -2958,8 +2958,7 @@ QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) c
int right = 0;
int rangeLeft, rangeRight;
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange r = selection.at(i);
for (const auto &r : selection) {
if (r.parent().isValid() || !r.isValid())
continue; // we only know about toplevel items and we don't want invalid ranges
// FIXME an item inside the range may be the leftmost or rightmost
@ -2992,8 +2991,7 @@ QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) c
int bottom = 0;
int rangeTop, rangeBottom;
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange r = selection.at(i);
for (const auto &r : selection) {
if (r.parent().isValid() || !r.isValid())
continue; // we only know about toplevel items
// FIXME an item inside the range may be the leftmost or rightmost

View File

@ -327,8 +327,8 @@ public:
inline int headerLength() const { // for debugging
int len = 0;
for (int i = 0; i < sectionItems.count(); ++i)
len += sectionItems.at(i).size;
for (const auto &section : sectionItems)
len += section.size;
return len;
}

View File

@ -651,8 +651,7 @@ QItemViewPaintPairs QListViewPrivate::draggablePaintPairs(const QModelIndexList
const QRect viewportRect = viewport->rect();
QItemViewPaintPairs ret;
const QSet<QModelIndex> visibleIndexes = intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset())).toList().toSet();
for (int i = 0; i < indexes.count(); ++i) {
const QModelIndex &index = indexes.at(i);
for (const auto &index : indexes) {
if (visibleIndexes.contains(index)) {
const QRect current = q->visualRect(index);
QItemViewPaintPair p = { current, index };
@ -1398,16 +1397,16 @@ QRegion QListView::visualRegionForSelection(const QItemSelection &selection) con
int c = d->column;
QRegion selectionRegion;
const QRect &viewportRect = d->viewport->rect();
for (int i = 0; i < selection.count(); ++i) {
if (!selection.at(i).isValid())
for (const auto &elem : selection) {
if (!elem.isValid())
continue;
QModelIndex parent = selection.at(i).topLeft().parent();
QModelIndex parent = elem.topLeft().parent();
//we only display the children of the root in a listview
//we're not interested in the other model indexes
if (parent != d->root)
continue;
int t = selection.at(i).topLeft().row();
int b = selection.at(i).bottomRight().row();
int t = elem.topLeft().row();
int b = elem.bottomRight().row();
if (d->viewMode == IconMode || d->isWrapping()) { // in non-static mode, we have to go through all selected items
for (int r = t; r <= b; ++r) {
const QRect &rect = visualRect(d->model->index(r, c, parent));
@ -2771,9 +2770,8 @@ bool QIconModeViewBase::filterDropEvent(QDropEvent *e)
}
QPoint start = dd->pressedPosition;
QPoint delta = (dd->movement == QListView::Snap ? snapToGrid(end) - snapToGrid(start) : end - start);
QList<QModelIndex> indexes = dd->selectionModel->selectedIndexes();
for (int i = 0; i < indexes.count(); ++i) {
QModelIndex index = indexes.at(i);
const QList<QModelIndex> indexes = dd->selectionModel->selectedIndexes();
for (const auto &index : indexes) {
QRect rect = dd->rectForIndex(index);
viewport()->update(dd->mapToViewport(rect, false));
QPoint dest = rect.topLeft() + delta;

View File

@ -1370,9 +1370,6 @@ void QTableView::paintEvent(QPaintEvent *event)
uint x = horizontalHeader->length() - horizontalHeader->offset() - (rightToLeft ? 0 : 1);
uint y = verticalHeader->length() - verticalHeader->offset() - 1;
const QRegion region = event->region().translated(offset);
const QVector<QRect> rects = region.rects();
//firstVisualRow is the visual index of the first visible row. lastVisualRow is the visual index of the last visible Row.
//same goes for ...VisualColumn
int firstVisualRow = qMax(verticalHeader->visualIndexAt(0),0);
@ -1391,13 +1388,15 @@ void QTableView::paintEvent(QPaintEvent *event)
QBitArray drawn((lastVisualRow - firstVisualRow + 1) * (lastVisualColumn - firstVisualColumn + 1));
const QRegion region = event->region().translated(offset);
if (d->hasSpans()) {
d->drawAndClipSpans(region, &painter, option, &drawn,
firstVisualRow, lastVisualRow, firstVisualColumn, lastVisualColumn);
}
for (int i = 0; i < rects.size(); ++i) {
QRect dirtyArea = rects.at(i);
const QVector<QRect> rects = region.rects();
for (auto dirtyArea : rects) {
dirtyArea.setBottom(qMin(dirtyArea.bottom(), int(y)));
if (rightToLeft) {
dirtyArea.setLeft(qMax(dirtyArea.left(), d->viewport->width() - int(x)));
@ -1952,8 +1951,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
bool horizontalMoved = horizontalHeader()->sectionsMoved();
if ((verticalMoved && horizontalMoved) || (d->hasSpans() && (verticalMoved || horizontalMoved))) {
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange range = selection.at(i);
for (const auto &range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
for (int r = range.top(); r <= range.bottom(); ++r)
@ -1964,8 +1962,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
}
}
} else if (horizontalMoved) {
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange range = selection.at(i);
for (const auto &range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
int top = rowViewportPosition(range.top());
@ -1980,8 +1977,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
}
}
} else if (verticalMoved) {
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange range = selection.at(i);
for (const auto &range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
int left = columnViewportPosition(range.left());
@ -1997,8 +1993,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
}
} else { // nothing moved
const int gridAdjust = showGrid() ? 1 : 0;
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange range = selection.at(i);
for (auto range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
d->trimHiddenSelections(&range);

View File

@ -1388,9 +1388,10 @@ void QTableWidgetItem::setData(int role, const QVariant &value)
QVariant QTableWidgetItem::data(int role) const
{
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
for (int i = 0; i < values.count(); ++i)
if (values.at(i).role == role)
return values.at(i).value;
for (const auto &value : values) {
if (value.role == role)
return value.value;
}
return QVariant();
}
@ -2359,10 +2360,9 @@ QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
QList<QTableWidgetItem*> QTableWidget::selectedItems() const
{
Q_D(const QTableWidget);
QModelIndexList indexes = selectionModel()->selectedIndexes();
const QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QTableWidgetItem*> items;
for (int i = 0; i < indexes.count(); ++i) {
QModelIndex index = indexes.at(i);
for (const auto &index : indexes) {
if (isIndexHidden(index))
continue;
QTableWidgetItem *item = d->tableModel()->item(index);
@ -2682,22 +2682,21 @@ void QTableWidget::dropEvent(QDropEvent *event) {
int col = -1;
int row = -1;
if (d->dropOn(event, &row, &col, &topIndex)) {
QModelIndexList indexes = selectedIndexes();
const QModelIndexList indexes = selectedIndexes();
int top = INT_MAX;
int left = INT_MAX;
for (int i = 0; i < indexes.count(); ++i) {
top = qMin(indexes.at(i).row(), top);
left = qMin(indexes.at(i).column(), left);
for (const auto &index : indexes) {
top = qMin(index.row(), top);
left = qMin(index.column(), left);
}
QList<QTableWidgetItem *> taken;
const int indexesCount = indexes.count();
taken.reserve(indexesCount);
for (int i = 0; i < indexesCount; ++i)
taken.append(takeItem(indexes.at(i).row(), indexes.at(i).column()));
for (const auto &index : indexes)
taken.append(takeItem(index.row(), index.column()));
for (int i = 0; i < indexes.count(); ++i) {
QModelIndex index = indexes.at(i);
for (const auto &index : indexes) {
int r = index.row() - top + topIndex.row();
int c = index.column() - left + topIndex.column();
setItem(r, c, taken.takeFirst());

View File

@ -2393,8 +2393,7 @@ QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) con
QRegion selectionRegion;
const QRect &viewportRect = d->viewport->rect();
for (int i = 0; i < selection.count(); ++i) {
QItemSelectionRange range = selection.at(i);
for (const auto &range : selection) {
if (!range.isValid())
continue;
QModelIndex parent = range.parent();

View File

@ -725,9 +725,9 @@ QMimeData *QTreeModel::internalMimeData() const
QMimeData *QTreeModel::mimeData(const QModelIndexList &indexes) const
{
QList<QTreeWidgetItem*> items;
for (int i = 0; i < indexes.count(); ++i) {
if (indexes.at(i).column() == 0) // only one item per row
items << item(indexes.at(i));
for (const auto &index : indexes) {
if (index.column() == 0) // only one item per row
items << item(index);
}
// cachedIndexes is a little hack to avoid copying from QModelIndexList to
@ -1735,7 +1735,7 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
default:
if (column < values.count()) {
bool found = false;
QVector<QWidgetItemData> column_values = values.at(column);
const QVector<QWidgetItemData> column_values = values.at(column);
for (int i = 0; i < column_values.count(); ++i) {
if (column_values.at(i).role == role) {
if (column_values.at(i).value == value)
@ -1785,9 +1785,10 @@ QVariant QTreeWidgetItem::data(int column, int role) const
default:
if (column >= 0 && column < values.size()) {
const QVector<QWidgetItemData> &column_values = values.at(column);
for (int i = 0; i < column_values.count(); ++i)
if (column_values.at(i).role == role)
return column_values.at(i).value;
for (const auto &column_value : column_values) {
if (column_value.role == role)
return column_value.value;
}
}
}
return QVariant();
@ -2136,8 +2137,8 @@ QVariant QTreeWidgetItem::childrenCheckState(int column) const
return QVariant();
bool checkedChildren = false;
bool uncheckedChildren = false;
for (int i = 0; i < children.count(); ++i) {
QVariant value = children.at(i)->data(column, Qt::CheckStateRole);
for (const auto *child : children) {
QVariant value = child->data(column, Qt::CheckStateRole);
if (!value.isValid())
return QVariant();
@ -3018,13 +3019,13 @@ void QTreeWidget::setItemSelected(const QTreeWidgetItem *item, bool select)
QList<QTreeWidgetItem*> QTreeWidget::selectedItems() const
{
Q_D(const QTreeWidget);
QModelIndexList indexes = selectionModel()->selectedIndexes();
const QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QTreeWidgetItem*> items;
items.reserve(indexes.count());
QSet<QTreeWidgetItem *> seen;
seen.reserve(indexes.count());
for (int i = 0; i < indexes.count(); ++i) {
QTreeWidgetItem *item = d->item(indexes.at(i));
for (const auto &index : indexes) {
QTreeWidgetItem *item = d->item(index);
if (isItemHidden(item) || seen.contains(item))
continue;
seen.insert(item);
@ -3278,8 +3279,7 @@ QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
Q_D(const QTreeWidget);
if (d->treeModel()->cachedIndexes.isEmpty()) {
QList<QModelIndex> indexes;
for (int i = 0; i < items.count(); ++i) {
QTreeWidgetItem *item = items.at(i);
for (const auto *item : items) {
if (Q_UNLIKELY(!item)) {
qWarning("QTreeWidget::mimeData: Null-item passed");
return 0;
@ -3382,12 +3382,12 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
int col = -1;
int row = -1;
if (d->dropOn(event, &row, &col, &topIndex)) {
QList<QModelIndex> idxs = selectedIndexes();
const QList<QModelIndex> idxs = selectedIndexes();
QList<QPersistentModelIndex> indexes;
const int indexesCount = idxs.count();
indexes.reserve(indexesCount);
for (int i = 0; i < indexesCount; i++)
indexes.append(idxs.at(i));
for (const auto &idx : idxs)
indexes.append(idx);
if (indexes.contains(topIndex))
return;
@ -3397,12 +3397,12 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
// Remove the items
QList<QTreeWidgetItem *> taken;
for (int i = 0; i < indexes.count(); ++i) {
QTreeWidgetItem *parent = itemFromIndex(indexes.at(i));
for (const auto &index : indexes) {
QTreeWidgetItem *parent = itemFromIndex(index);
if (!parent || !parent->parent()) {
taken.append(takeTopLevelItem(indexes.at(i).row()));
taken.append(takeTopLevelItem(index.row()));
} else {
taken.append(parent->parent()->takeChild(indexes.at(i).row()));
taken.append(parent->parent()->takeChild(index.row()));
}
}

View File

@ -1125,9 +1125,9 @@ QStyle *QApplication::style()
QStyle *&app_style = QApplicationPrivate::app_style;
app_style = QStyleFactory::create(style);
if (!app_style) {
QStringList styles = QStyleFactory::keys();
for (int i = 0; i < styles.size(); ++i) {
if ((app_style = QStyleFactory::create(styles.at(i))))
const QStringList styles = QStyleFactory::keys();
for (const auto &style : styles) {
if ((app_style = QStyleFactory::create(style)))
break;
}
}
@ -1742,8 +1742,7 @@ void QApplicationPrivate::notifyWindowIconChanged()
QWindowList windowList = QGuiApplication::topLevelWindows();
// send to all top-level QWidgets
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
for (auto *w : list) {
windowList.removeOne(w->windowHandle());
QCoreApplication::sendEvent(w, &ev);
}
@ -1906,9 +1905,8 @@ bool QApplicationPrivate::tryCloseAllWidgetWindows(QWindowList *processedWindows
}
retry:
QWidgetList list = QApplication::topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
const QWidgetList list = QApplication::topLevelWidgets();
for (auto *w : list) {
if (w->isVisible() && w->windowType() != Qt::Desktop &&
!w->testAttribute(Qt::WA_DontShowOnScreen) && !w->data->is_closing) {
QWindow *window = w->windowHandle();
@ -1995,9 +1993,8 @@ bool QApplication::event(QEvent *e)
ce->accept();
closeAllWindows();
QWidgetList list = topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
const QWidgetList list = topLevelWidgets();
for (auto *w : list) {
if (w->isVisible() && !(w->windowType() == Qt::Desktop) && !(w->windowType() == Qt::Popup) &&
(!(w->windowType() == Qt::Dialog) || !w->parentWidget())) {
ce->ignore();
@ -2011,9 +2008,8 @@ bool QApplication::event(QEvent *e)
} else if (e->type() == QEvent::LocaleChange) {
// on Windows the event propagation is taken care by the
// WM_SETTINGCHANGE event handler.
QWidgetList list = topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
const QWidgetList list = topLevelWidgets();
for (auto *w : list) {
if (!(w->windowType() == Qt::Desktop)) {
if (!w->testAttribute(Qt::WA_SetLocale))
w->d_func()->setLocale_helper(QLocale(), true);
@ -2057,9 +2053,8 @@ bool QApplication::event(QEvent *e)
}
if(e->type() == QEvent::LanguageChange) {
QWidgetList list = topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
const QWidgetList list = topLevelWidgets();
for (auto *w : list) {
if (!(w->windowType() == Qt::Desktop))
postEvent(w, new QEvent(QEvent::LanguageChange));
}
@ -2085,8 +2080,7 @@ void QApplicationPrivate::notifyLayoutDirectionChange()
QWindowList windowList = QGuiApplication::topLevelWindows();
// send to all top-level QWidgets
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
for (auto *w : list) {
windowList.removeAll(w->windowHandle());
QEvent ev(QEvent::ApplicationLayoutDirectionChange);
QCoreApplication::sendEvent(w, &ev);
@ -2137,9 +2131,8 @@ void QApplication::setActiveWindow(QWidget* act)
if (QApplicationPrivate::active_window) {
if (style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, QApplicationPrivate::active_window)) {
QWidgetList list = topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
const QWidgetList list = topLevelWidgets();
for (auto *w : list) {
if (w->isVisible() && w->isActiveWindow())
toBeDeactivated.append(w);
}
@ -2160,9 +2153,8 @@ void QApplication::setActiveWindow(QWidget* act)
if (QApplicationPrivate::active_window) {
if (style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, QApplicationPrivate::active_window)) {
QWidgetList list = topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
QWidget *w = list.at(i);
const QWidgetList list = topLevelWidgets();
for (auto *w : list) {
if (w->isVisible() && w->isActiveWindow())
toBeActivated.append(w);
}

View File

@ -653,9 +653,7 @@ void QGroupBox::setChecked(bool b)
void QGroupBoxPrivate::_q_setChildrenEnabled(bool b)
{
Q_Q(QGroupBox);
QObjectList childList = q->children();
for (int i = 0; i < childList.size(); ++i) {
QObject *o = childList.at(i);
for (QObject *o : q->children()) {
if (o->isWidgetType()) {
QWidget *w = static_cast<QWidget *>(o);
if (b) {