QtWidgets tests: port remaining users away from Q_FOREACH

These are all trivial: all are over (already or newly-made) const
local variables.

As a drive-by, replace a QList legacy left-shift-based- with
initializer_list-construction.

Pick-to: 6.6 6.5
Task-number: QTBUG-115803
Change-Id: I453e24272c4c4b7dce5b91a0bd04481d833c50bb
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Marc Mutz 2023-08-05 10:43:43 +02:00 committed by Ahmad Samir
parent 6f5f10bea8
commit 40afcb9d01
9 changed files with 36 additions and 25 deletions

View File

@ -565,9 +565,9 @@ void tst_QWizard::addPage()
#define CHECK_VISITED(wizard, list) \
do { \
QList<int> myList = list; \
const QList<int> myList = list; \
QCOMPARE((wizard).visitedIds(), myList); \
Q_FOREACH(int id, myList) \
for (int id : myList) \
QVERIFY((wizard).hasVisitedPage(id)); \
} while (0)
@ -1837,7 +1837,8 @@ public:
#define OPT(option, on) OptionInfo::instance().operation(option, on)
#define CLROPT(option) OPT(option, false)
#define SETOPT(option) OPT(option, true)
foreach (QWizard::WizardOption option, OptionInfo::instance().options()) {
const auto options = OptionInfo::instance().options();
for (QWizard::WizardOption option : options) {
setAllOptions[false] << CLROPT(option);
setAllOptions[true] << SETOPT(option);
}
@ -1924,7 +1925,8 @@ public:
testGroup.add() << pageOp << optionOps;
testGroup.createTestRows();
foreach (QWizard::WizardOption option, OptionInfo::instance().options()) {
const auto options = OptionInfo::instance().options();
for (QWizard::WizardOption option : options) {
QSharedPointer<Operation> optionOp = OPT(option, i == 1);
testGroup.reset("testAll 4.3");
testGroup.add() << optionOp << pageOp;
@ -1947,7 +1949,8 @@ public:
testGroup.add() << styleOp << optionOps;
testGroup.createTestRows();
foreach (QWizard::WizardOption option, OptionInfo::instance().options()) {
const auto options = OptionInfo::instance().options();
for (QWizard::WizardOption option : options) {
QSharedPointer<Operation> optionOp = OPT(option, i == 1);
testGroup.reset("testAll 5.3");
testGroup.add() << optionOp << styleOp;
@ -1986,7 +1989,8 @@ public:
testGroup.add() << styleOp << pageOp << optionOps;
testGroup.createTestRows();
foreach (QWizard::WizardOption option, OptionInfo::instance().options()) {
const auto options = OptionInfo::instance().options();
for (QWizard::WizardOption option : options) {
QSharedPointer<Operation> optionOp = OPT(option, i == 1);
testGroup.reset("testAll 6.5");
testGroup.add() << optionOp << pageOp << styleOp;
@ -2570,7 +2574,8 @@ void tst_QWizard::task161658_alignments()
wizard.show();
QVERIFY(QTest::qWaitForWindowExposed(&wizard));
foreach (QLabel *subtitleLabel, wizard.findChildren<QLabel *>()) {
const auto subtitleLabels = wizard.findChildren<QLabel *>();
for (QLabel *subtitleLabel : subtitleLabels) {
if (subtitleLabel->text().startsWith("SUBTITLE#")) {
QCOMPARE(lineEdit1.mapToGlobal(lineEdit1.contentsRect().bottomRight()).x(),
subtitleLabel->mapToGlobal(subtitleLabel->contentsRect().bottomRight()).x());

View File

@ -2476,7 +2476,8 @@ void tst_QGraphicsProxyWidget::tooltip_basic()
bool foundView = false;
bool foundTipLabel = false;
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
const auto widgets = QApplication::topLevelWidgets();
for (QWidget *widget : widgets) {
if (widget == &view)
foundView = true;
if (widget->inherits("QTipLabel"))

View File

@ -1049,9 +1049,9 @@ void tst_QGraphicsView::rotated_rubberBand()
sendMouseMove(view.viewport(), QPoint(midWidth + 2, view.viewport()->height()),
Qt::LeftButton, Qt::LeftButton);
QCOMPARE(scene.selectedItems().size(), dim);
foreach (const QGraphicsItem *item, scene.items()) {
const auto items = scene.items();
for (const QGraphicsItem *item : items)
QCOMPARE(item->isSelected(), item->data(0).toBool());
}
sendMouseRelease(view.viewport(), QPoint(), Qt::LeftButton);
}
@ -2739,7 +2739,8 @@ void tst_QGraphicsView::optimizationFlags_dontSavePainterState2()
rectB->setTransform(QTransform::fromTranslate(200, 200));
rectB->setPen(QPen(Qt::black, 0));
foreach (QGraphicsItem *item, scene.items())
const auto items = scene.items();
for (QGraphicsItem *item : items)
item->setOpacity(0.6);
CustomView view(&scene);
@ -3258,7 +3259,8 @@ protected:
{
++mouseMoves;
QGraphicsView::mouseMoveEvent(event);
foreach (QGraphicsItem *item, scene()->items()) {
const auto items = scene()->items();
for (QGraphicsItem *item : items) {
scene()->removeItem(item);
delete item;
}

View File

@ -58,7 +58,8 @@ private slots:
static inline int visibleTopLevelWidgetCount()
{
int result= 0;
foreach (const QWidget *topLevel, QApplication::topLevelWidgets()) {
const auto topLevels = QApplication::topLevelWidgets();
for (const QWidget *topLevel : topLevels) {
if (topLevel->isVisible())
++result;
}

View File

@ -117,7 +117,8 @@ void tst_QToolTip::keyEvent()
static QWidget *findWhatsThat()
{
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
const auto widgets = QApplication::topLevelWidgets();
for (QWidget *widget : widgets) {
if (widget->inherits("QWhatsThat"))
return widget;
}

View File

@ -4717,7 +4717,8 @@ void tst_QLineEdit::sideWidgets()
testWidget.move(300, 300);
testWidget.show();
QVERIFY(QTest::qWaitForWindowExposed(&testWidget));
foreach (QToolButton *button, lineEdit->findChildren<QToolButton *>())
const auto buttons = lineEdit->findChildren<QToolButton *>();
for (QToolButton *button : buttons)
QCOMPARE(button->cursor().shape(), Qt::ArrowCursor);
// Arbitrarily add/remove actions, trying to detect crashes. Add QTRY_VERIFY(false) to view the result.
delete label3Action;
@ -4732,7 +4733,8 @@ void tst_QLineEdit::sideWidgets()
template <class T> T *findAssociatedWidget(const QAction *a)
{
foreach (QObject *w, a->associatedObjects()) {
const auto associatedObjects = a->associatedObjects();
for (QObject *w : associatedObjects) {
if (T *result = qobject_cast<T *>(w))
return result;
}

View File

@ -1361,8 +1361,8 @@ void tst_QMenuBar::menubarSizeHint()
mb.setStyle(&style);
//this is a list of arbitrary strings so that we check the geometry
QStringList list = QStringList() << "trer" << "ezrfgtgvqd" << "sdgzgzerzerzer" << "eerzertz" << "er";
foreach(QString str, list)
const auto list = QStringList{"trer", "ezrfgtgvqd", "sdgzgzerzerzer", "eerzertz", "er"};
for (const QString &str : list)
mb.addAction(str);
const int panelWidth = style.pixelMetric(QStyle::PM_MenuBarPanelWidth);
@ -1373,7 +1373,8 @@ void tst_QMenuBar::menubarSizeHint()
centerOnScreen(&mb);
mb.show();
QRect result;
foreach(QAction *action, mb.actions()) {
const auto actions = mb.actions();
for (QAction *action : actions) {
const QRect actionRect = mb.actionGeometry(action);
if (!result.isNull()) //this is the first item
QCOMPARE(actionRect.left() - result.right() - 1, spacing);

View File

@ -995,7 +995,7 @@ void tst_QPlainTextEdit::copyAvailable_data()
//Tests the copyAvailable slot for several cases
void tst_QPlainTextEdit::copyAvailable()
{
QFETCH(pairListType,keystrokes);
QFETCH(const pairListType, keystrokes);
QFETCH(QList<bool>, copyAvailable);
QFETCH(QString, function);
@ -1008,9 +1008,8 @@ void tst_QPlainTextEdit::copyAvailable()
QSignalSpy spyCopyAvailabe(ed, SIGNAL(copyAvailable(bool)));
//Execute Keystrokes
foreach(keyPairType keyPair, keystrokes) {
for (keyPairType keyPair : keystrokes)
QTest::keyClick(ed, keyPair.first, keyPair.second );
}
//Execute ed->"function"
if (function == "cut")

View File

@ -1352,7 +1352,7 @@ void tst_QTextEdit::copyAvailable_data()
//Tests the copyAvailable slot for several cases
void tst_QTextEdit::copyAvailable()
{
QFETCH(pairListType,keystrokes);
QFETCH(const pairListType, keystrokes);
QFETCH(QList<bool>, copyAvailable);
QFETCH(QString, function);
@ -1365,9 +1365,8 @@ void tst_QTextEdit::copyAvailable()
QSignalSpy spyCopyAvailabe(ed, SIGNAL(copyAvailable(bool)));
//Execute Keystrokes
foreach(keyPairType keyPair, keystrokes) {
for (keyPairType keyPair : keystrokes)
QTest::keyClick(ed, keyPair.first, keyPair.second );
}
//Execute ed->"function"
if (function == "cut")