foreach() ??
-
I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks
localAdapters = QBluetoothLocalDevice::allDevices();
count = localAdapters.count();
ui->chat_10->clear();qDebug()<< " localAdapters count " << QString::number(count); for(int index = 0; index < count; index++) { qDebug()<< " local adapter address" << localAdapters.at(index).address().toString(); ui->chat\_10->append( localAdapters.at(index).address().toString()); qDebug() << " local adapater name " << localAdapters.at(index).name(); ui->chat\_10->append( localAdapters.at(index).name()); }
-
I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks
localAdapters = QBluetoothLocalDevice::allDevices();
count = localAdapters.count();
ui->chat_10->clear();qDebug()<< " localAdapters count " << QString::number(count); for(int index = 0; index < count; index++) { qDebug()<< " local adapter address" << localAdapters.at(index).address().toString(); ui->chat\_10->append( localAdapters.at(index).address().toString()); qDebug() << " local adapater name " << localAdapters.at(index).name(); ui->chat\_10->append( localAdapters.at(index).name()); }
This should work:
for(auto& adapter : localAdapters)
{
qDebug()<< " local adapter address" << adapter.address().toString();
ui->chat_10->append( adapter.address().toString());
qDebug() << " local adapater name " << adapter.name();
ui->chat_10->append( adapter.name());
}Mircea
-
I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks
localAdapters = QBluetoothLocalDevice::allDevices();
count = localAdapters.count();
ui->chat_10->clear();qDebug()<< " localAdapters count " << QString::number(count); for(int index = 0; index < count; index++) { qDebug()<< " local adapter address" << localAdapters.at(index).address().toString(); ui->chat\_10->append( localAdapters.at(index).address().toString()); qDebug() << " local adapater name " << localAdapters.at(index).name(); ui->chat\_10->append( localAdapters.at(index).name()); }
Try (not tested)
auto localAdapters = QBluetoothLocalDevice::allDevices();
ui->chat_10->clear();
qDebug()<< " localAdapters count " << QString::number(localAdapters.count());
for (const auto & device : localAdapters)
{
qDebug()<< " local adapter address" << device.address().toString();
ui->chat_10->append( device.address().toString());
qDebug() << " local adapater name " << device.name();
ui->chat_10->append( device.name());
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
I like to learn how to use "foreach". I have posted similar request on another forum and got "RTFM" Could somebody REWRiTE the attached using "foreach" ? ..and a simple explanation of code used ( called "comments" ) would be nice. I need to start with "foreach" before goto "new style" of "for". Thanks
localAdapters = QBluetoothLocalDevice::allDevices();
count = localAdapters.count();
ui->chat_10->clear();qDebug()<< " localAdapters count " << QString::number(count); for(int index = 0; index < count; index++) { qDebug()<< " local adapter address" << localAdapters.at(index).address().toString(); ui->chat\_10->append( localAdapters.at(index).address().toString()); qDebug() << " local adapater name " << localAdapters.at(index).name(); ui->chat\_10->append( localAdapters.at(index).name()); }
Here is an example of how the code you provided can be rewritten using the foreach loop:
Copy code
localAdapters = QBluetoothLocalDevice::allDevices();
count = localAdapters.count();
ui->chat_10->clear();qDebug()<< " localAdapters count " << QString::number(count);
// foreach loop to iterate through the local adapters
foreach (QBluetoothLocalDevice localAdapter, localAdapters) {
qDebug()<< " local adapter address" << localAdapter.address().toString();
ui->chat_10->append( localAdapter.address().toString());
qDebug() << " local adapater name " << localAdapter.name();
ui->chat_10->append( localAdapter.name());
}In this example, the foreach loop is used to iterate through each QBluetoothLocalDevice object in the localAdapters list. The variable localAdapter is declared as the loop variable and will take on the value of each object in the list as the loop iterates through it. The foreach loop is equivalent to the for loop. The code inside the loop is the same as the original code, it is just that the variable localAdapter is used instead of the variable localAdapters.at(index).
In general, foreach is used to iterate over the items in a container, such as a list or an array. It is a simpler and more readable way to write a loop than a traditional for loop.
-
Try (not tested)
auto localAdapters = QBluetoothLocalDevice::allDevices();
ui->chat_10->clear();
qDebug()<< " localAdapters count " << QString::number(localAdapters.count());
for (const auto & device : localAdapters)
{
qDebug()<< " local adapter address" << device.address().toString();
ui->chat_10->append( device.address().toString());
qDebug() << " local adapater name " << device.name();
ui->chat_10->append( device.name());
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
Somebody send me a private message to which I am unable to reply... I want to make sure he /she knows I got it and appreciate it. I did rewrite few lines of code using both "foreach" and "for" , works much gooder then the old "indexing" stuff... Thanks
Better is subjective. For example if you modify the existing code to the following line then you should continue to use the existing solution
qDebug()<< "index=" << i << " local adapter address" << localAdapters.at(index).address().toString();