From 0516487237145ad41b2fd13ecb5f63ba4325c02f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 30 Nov 2015 12:19:31 +0100 Subject: 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 Reviewed-by: Lars Knoll --- src/widgets/dialogs/qmessagebox.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/widgets/dialogs/qmessagebox.cpp') diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index 76a9de0f1c..704883b5c6 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -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 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(); -- cgit v1.2.3