Cleanup Widgets examples - foreach
Cleanup the Widget examples - replace foreach with range-based for loop in subdirectory tools, touch and tutorials Change-Id: I008d23b5993a18a3332fe9f5e5bca68cb0561066 Reviewed-by: Luca Beldi <v.ronin@yahoo.it> Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com> Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
parent
bec8173347
commit
1f2c23a7ca
@ -127,11 +127,10 @@ void MainWindow::about()
|
|||||||
|
|
||||||
void MainWindow::aboutToShowSaveAsMenu()
|
void MainWindow::aboutToShowSaveAsMenu()
|
||||||
{
|
{
|
||||||
QString currentText = textEdit->toPlainText();
|
const QString currentText = textEdit->toPlainText();
|
||||||
|
for (QAction *action : qAsConst(saveAsActs)) {
|
||||||
foreach (QAction *action, saveAsActs) {
|
const QByteArray codecName = action->data().toByteArray();
|
||||||
QByteArray codecName = action->data().toByteArray();
|
const QTextCodec *codec = QTextCodec::codecForName(codecName);
|
||||||
QTextCodec *codec = QTextCodec::codecForName(codecName);
|
|
||||||
action->setVisible(codec && codec->canEncode(currentText));
|
action->setVisible(codec && codec->canEncode(currentText));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,7 +141,8 @@ void MainWindow::findCodecs()
|
|||||||
QRegularExpression iso8859RegExp("^ISO[- ]8859-([0-9]+).*$");
|
QRegularExpression iso8859RegExp("^ISO[- ]8859-([0-9]+).*$");
|
||||||
QRegularExpressionMatch match;
|
QRegularExpressionMatch match;
|
||||||
|
|
||||||
foreach (int mib, QTextCodec::availableMibs()) {
|
const QList<int> mibs = QTextCodec::availableMibs();
|
||||||
|
for (int mib : mibs) {
|
||||||
QTextCodec *codec = QTextCodec::codecForMib(mib);
|
QTextCodec *codec = QTextCodec::codecForMib(mib);
|
||||||
|
|
||||||
QString sortKey = codec->name().toUpper();
|
QString sortKey = codec->name().toUpper();
|
||||||
@ -177,7 +177,7 @@ void MainWindow::createMenus()
|
|||||||
QMenu *saveAsMenu = fileMenu->addMenu(tr("&Save As"));
|
QMenu *saveAsMenu = fileMenu->addMenu(tr("&Save As"));
|
||||||
connect(saveAsMenu, &QMenu::aboutToShow,
|
connect(saveAsMenu, &QMenu::aboutToShow,
|
||||||
this, &MainWindow::aboutToShowSaveAsMenu);
|
this, &MainWindow::aboutToShowSaveAsMenu);
|
||||||
foreach (const QTextCodec *codec, codecs) {
|
for (const QTextCodec *codec : qAsConst(codecs)) {
|
||||||
const QByteArray name = codec->name();
|
const QByteArray name = codec->name();
|
||||||
QAction *action = saveAsMenu->addAction(tr("%1...").arg(QLatin1String(name)));
|
QAction *action = saveAsMenu->addAction(tr("%1...").arg(QLatin1String(name)));
|
||||||
action->setData(QVariant(name));
|
action->setData(QVariant(name));
|
||||||
|
@ -182,7 +182,7 @@ PreviewForm::PreviewForm(QWidget *parent)
|
|||||||
void PreviewForm::setCodecList(const QList<QTextCodec *> &list)
|
void PreviewForm::setCodecList(const QList<QTextCodec *> &list)
|
||||||
{
|
{
|
||||||
encodingComboBox->clear();
|
encodingComboBox->clear();
|
||||||
foreach (const QTextCodec *codec, list) {
|
for (const QTextCodec *codec : list) {
|
||||||
encodingComboBox->addItem(QLatin1String(codec->name()),
|
encodingComboBox->addItem(QLatin1String(codec->name()),
|
||||||
QVariant(codec->mibEnum()));
|
QVariant(codec->mibEnum()));
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,8 @@ bool EchoWindow::loadPlugin()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
pluginsDir.cd("plugins");
|
pluginsDir.cd("plugins");
|
||||||
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
|
const QStringList entries = pluginsDir.entryList(QDir::Files);
|
||||||
|
for (const QString &fileName : entries) {
|
||||||
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
|
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
|
||||||
QObject *plugin = pluginLoader.instance();
|
QObject *plugin = pluginLoader.instance();
|
||||||
if (plugin) {
|
if (plugin) {
|
||||||
|
@ -148,14 +148,14 @@ void LanguageChooser::checkBoxToggled()
|
|||||||
|
|
||||||
void LanguageChooser::showAll()
|
void LanguageChooser::showAll()
|
||||||
{
|
{
|
||||||
foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
|
for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it)
|
||||||
checkBox->setChecked(true);
|
(*it)->setChecked(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LanguageChooser::hideAll()
|
void LanguageChooser::hideAll()
|
||||||
{
|
{
|
||||||
foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
|
for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it)
|
||||||
checkBox->setChecked(false);
|
(*it)->setChecked(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList LanguageChooser::findQmFiles()
|
QStringList LanguageChooser::findQmFiles()
|
||||||
|
@ -170,7 +170,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent)
|
|||||||
{
|
{
|
||||||
int dividerIndex = 0;
|
int dividerIndex = 0;
|
||||||
|
|
||||||
foreach (QString group, settings->childGroups()) {
|
const QStringList childGroups = settings->childGroups();
|
||||||
|
for (const QString &group : childGroups) {
|
||||||
QTreeWidgetItem *child;
|
QTreeWidgetItem *child;
|
||||||
int childIndex = findChild(parent, group, dividerIndex);
|
int childIndex = findChild(parent, group, dividerIndex);
|
||||||
if (childIndex != -1) {
|
if (childIndex != -1) {
|
||||||
@ -190,7 +191,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent)
|
|||||||
settings->endGroup();
|
settings->endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (const QString &key, settings->childKeys()) {
|
const QStringList childKeys = settings->childKeys();
|
||||||
|
for (const QString &key : childKeys) {
|
||||||
QTreeWidgetItem *child;
|
QTreeWidgetItem *child;
|
||||||
int childIndex = findChild(parent, key, 0);
|
int childIndex = findChild(parent, key, 0);
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ int main(int argc, char **argv)
|
|||||||
QWidget window;
|
QWidget window;
|
||||||
Ui::Dials dialsUi;
|
Ui::Dials dialsUi;
|
||||||
dialsUi.setupUi(&window);
|
dialsUi.setupUi(&window);
|
||||||
QList<QAbstractSlider *> sliders = window.findChildren<QAbstractSlider *>();
|
const QList<QAbstractSlider *> sliders = window.findChildren<QAbstractSlider *>();
|
||||||
foreach (QAbstractSlider *slider, sliders)
|
for (QAbstractSlider *slider : sliders)
|
||||||
slider->setAttribute(Qt::WA_AcceptTouchEvents);
|
slider->setAttribute(Qt::WA_AcceptTouchEvents);
|
||||||
window.showMaximized();
|
window.showMaximized();
|
||||||
return app.exec();
|
return app.exec();
|
||||||
|
@ -129,7 +129,8 @@ void MainWindow::createActions()
|
|||||||
openAct->setShortcut(tr("Ctrl+O"));
|
openAct->setShortcut(tr("Ctrl+O"));
|
||||||
connect(openAct, &QAction::triggered, this, &MainWindow::open);
|
connect(openAct, &QAction::triggered, this, &MainWindow::open);
|
||||||
|
|
||||||
foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
|
const QList<QByteArray> imageFormats = QImageWriter::supportedImageFormats();
|
||||||
|
for (const QByteArray &format : imageFormats) {
|
||||||
QString text = tr("%1...").arg(QString(format).toUpper());
|
QString text = tr("%1...").arg(QString(format).toUpper());
|
||||||
|
|
||||||
QAction *action = new QAction(text, this);
|
QAction *action = new QAction(text, this);
|
||||||
@ -163,8 +164,7 @@ void MainWindow::createMenus()
|
|||||||
//! [15] //! [16]
|
//! [15] //! [16]
|
||||||
{
|
{
|
||||||
saveAsMenu = new QMenu(tr("&Save As"), this);
|
saveAsMenu = new QMenu(tr("&Save As"), this);
|
||||||
foreach (QAction *action, saveAsActs)
|
saveAsMenu->addActions(saveAsActs);
|
||||||
saveAsMenu->addAction(action);
|
|
||||||
|
|
||||||
fileMenu = new QMenu(tr("&File"), this);
|
fileMenu = new QMenu(tr("&File"), this);
|
||||||
fileMenu->addAction(openAct);
|
fileMenu->addAction(openAct);
|
||||||
|
@ -195,9 +195,9 @@ bool ScribbleArea::event(QEvent *event)
|
|||||||
case QEvent::TouchUpdate:
|
case QEvent::TouchUpdate:
|
||||||
case QEvent::TouchEnd:
|
case QEvent::TouchEnd:
|
||||||
{
|
{
|
||||||
QTouchEvent *touch = static_cast<QTouchEvent *>(event);
|
const QTouchEvent *touch = static_cast<QTouchEvent *>(event);
|
||||||
QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
|
const QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
|
||||||
foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) {
|
for (const QTouchEvent::TouchPoint &touchPoint : touchPoints) {
|
||||||
switch (touchPoint.state()) {
|
switch (touchPoint.state()) {
|
||||||
case Qt::TouchPointStationary:
|
case Qt::TouchPointStationary:
|
||||||
case Qt::TouchPointReleased:
|
case Qt::TouchPointReleased:
|
||||||
|
@ -163,7 +163,7 @@ void Mouse::timerEvent(QTimerEvent *)
|
|||||||
<< mapToScene(0, 0)
|
<< mapToScene(0, 0)
|
||||||
<< mapToScene(-30, -50)
|
<< mapToScene(-30, -50)
|
||||||
<< mapToScene(30, -50));
|
<< mapToScene(30, -50));
|
||||||
foreach (QGraphicsItem *item, dangerMice) {
|
for (QGraphicsItem *item : dangerMice) {
|
||||||
if (item == this)
|
if (item == this)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -76,25 +76,26 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
//! [set up the model]
|
//! [set up the model]
|
||||||
QStandardItemModel model;
|
QStandardItemModel model;
|
||||||
model.setHorizontalHeaderLabels(
|
model.setHorizontalHeaderLabels({ QApplication::translate("nestedlayouts", "Name"),
|
||||||
QStringList() << QApplication::translate("nestedlayouts", "Name")
|
QApplication::translate("nestedlayouts", "Office") });
|
||||||
<< QApplication::translate("nestedlayouts", "Office"));
|
|
||||||
|
|
||||||
QList<QStringList> rows = QList<QStringList>()
|
const QStringList rows[] = {
|
||||||
<< (QStringList() << "Verne Nilsen" << "123")
|
QStringList{ QStringLiteral("Verne Nilsen"), QStringLiteral("123") },
|
||||||
<< (QStringList() << "Carlos Tang" << "77")
|
QStringList{ QStringLiteral("Carlos Tang"), QStringLiteral("77") },
|
||||||
<< (QStringList() << "Bronwyn Hawcroft" << "119")
|
QStringList{ QStringLiteral("Bronwyn Hawcroft"), QStringLiteral("119") },
|
||||||
<< (QStringList() << "Alessandro Hanssen" << "32")
|
QStringList{ QStringLiteral("Alessandro Hanssen"), QStringLiteral("32") },
|
||||||
<< (QStringList() << "Andrew John Bakken" << "54")
|
QStringList{ QStringLiteral("Andrew John Bakken"), QStringLiteral("54") },
|
||||||
<< (QStringList() << "Vanessa Weatherley" << "85")
|
QStringList{ QStringLiteral("Vanessa Weatherley"), QStringLiteral("85") },
|
||||||
<< (QStringList() << "Rebecca Dickens" << "17")
|
QStringList{ QStringLiteral("Rebecca Dickens"), QStringLiteral("17") },
|
||||||
<< (QStringList() << "David Bradley" << "42")
|
QStringList{ QStringLiteral("David Bradley"), QStringLiteral("42") },
|
||||||
<< (QStringList() << "Knut Walters" << "25")
|
QStringList{ QStringLiteral("Knut Walters"), QStringLiteral("25") },
|
||||||
<< (QStringList() << "Andrea Jones" << "34");
|
QStringList{ QStringLiteral("Andrea Jones"), QStringLiteral("34") }
|
||||||
|
};
|
||||||
|
|
||||||
foreach (QStringList row, rows) {
|
QList<QStandardItem *> items;
|
||||||
QList<QStandardItem *> items;
|
for (const QStringList &row : rows) {
|
||||||
foreach (QString text, row)
|
items.clear();
|
||||||
|
for (const QString &text : row)
|
||||||
items.append(new QStandardItem(text));
|
items.append(new QStandardItem(text));
|
||||||
model.appendRow(items);
|
model.appendRow(items);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user