Fortune* Example: convert simple for-loop to ranged-for

Ends up shortening the loop, making it easier to read at a glance.

Pick-to: 6.5
Task-number: QTBUG-108875
Change-Id: Ia12a994259b00e9b57f2de48124be9cb38553bf5
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Mårten Nordheim 2023-01-13 16:56:05 +01:00
parent 45717db7a5
commit 605eec7b55
4 changed files with 20 additions and 22 deletions

View File

@ -14,12 +14,11 @@ BlockingClient::BlockingClient(QWidget *parent)
// find out which IP to connect to
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
const QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
for (const QHostAddress &entry : ipAddressesList) {
if (entry != QHostAddress::LocalHost && entry.toIPv4Address()) {
ipAddress = entry.toString();
break;
}
}

View File

@ -28,16 +28,16 @@ Client::Client(QWidget *parent)
if (name != QLatin1String("localhost"))
hostCombo->addItem(QString("localhost"));
// find out IP addresses of this machine
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
const QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// add non-localhost addresses
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (!ipAddressesList.at(i).isLoopback())
hostCombo->addItem(ipAddressesList.at(i).toString());
for (const QHostAddress &entry : ipAddressesList) {
if (!entry.isLoopback())
hostCombo->addItem(entry.toString());
}
// add localhost addresses
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i).isLoopback())
hostCombo->addItem(ipAddressesList.at(i).toString());
for (const QHostAddress &entry : ipAddressesList) {
if (entry.isLoopback())
hostCombo->addItem(entry.toString());
}
portLineEdit->setValidator(new QIntValidator(1, 65535, this));

View File

@ -72,12 +72,11 @@ void Server::initServer()
}
//! [0]
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
const QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
for (const QHostAddress &entry : ipAddressesList) {
if (entry != QHostAddress::LocalHost && entry.toIPv4Address()) {
ipAddress = entry.toString();
break;
}
}

View File

@ -26,12 +26,12 @@ Dialog::Dialog(QWidget *parent)
}
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
const QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
for (const QHostAddress &entry : ipAddressesList) {
if (entry != QHostAddress::LocalHost && entry.toIPv4Address()) {
ipAddress = entry.toString();
break;
}
}