How to get first element of QList
-
@suslucoder said in How to get first element of QList:
@JKSH if you can look at my last question, the complete code is there. My mind so confused
You need to learn how to use loops.
Add more debug messages to your code; it might help you see what's happening:
qDebug() << "I am creating an empty list now."; QList<int> list; qDebug() << "My list contains" << list.count() << "items."; qDebug() << "My whole list is" << list; qDebug() << "I am putting an integer into my list now."; list << inner_data[1].toString().toInt(); qDebug() << "My list contains" << list.count() << "items."; qDebug() << "My whole list is" << list;
-
@suslucoder Did you run the code that I posted? What did you discover?
-
@JKSH Yes i run it. I realize that, it add the elements into list by one by.
I move to the definition of list to my header.run this
list << inner_data[1].toString().toInt(); qDebug() << "My list contains" << list.count() << "items."; qDebug() << "My whole list is" << list;
and achieve this, by i didnt solve why
My list contains 1 items. My whole list is (14) My list contains 2 items. My whole list is (14, 0) My list contains 3 items. My whole list is (14, 0, 1) My list contains 4 items. My whole list is (14, 0, 1, 13)
-
@suslucoder said in How to get first element of QList:
and achieve this
Good job. Now try
list.at(0)
again. -
@JKSH it gives me the first item but 4 times.
Why? I didnt understand this. Becauuse if my inner_data[1] size is 4?
How can i solve it?I expect that giving just this
My list contains 4 items. My whole list is (14, 0, 1, 13)
-
@suslucoder
ok, please post the whole function, from opening{
to closing}
-
@suslucoder said in How to get first element of QList:
@JKSH it gives me the first item but 4 times.
Why?
Because you called
list.at(0)
4 times, in a loop.Loops are used to run code repeatedly. If you don't want a line of code to run repeatedly, then you must put that line outside the loop.
How much do you understand for-loops?
-
void MainWindow::Okuma() { QFile file("readJson/deneme.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); QByteArray data = file.readAll(); QJsonDocument doc = QJsonDocument::fromJson(data); QJsonObject root = doc.object(); QJsonArray tlmtArray = root.value("Telemetri Verileri").toArray(); for(int i = 0; i < tlmtArray.size(); ++i) { QJsonObject obj = tlmtArray[i].toObject(); QJsonArray gps_array = obj.value("GPS").toArray(); for(int j = 0; j < gps_array.size(); ++j) { QJsonObject gps_obj = gps_array[j].toObject(); for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit) { // qDebug() << cit.key().toStdU16String() << ": ("; QJsonArray inner_data = cit.value().toArray(); if(inner_data[0] == "float") { // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : ""); } if (inner_data[0] == "integer") { // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : ""); list << inner_data[1].toString().toInt(); // qDebug() << "My list contains" << list.count() << "items."; // qDebug() << "My whole list is" << list; qDebug() << list.at(0); } } } }
}
-
@suslucoder ok, list magically appears, so I assume its a class member, the formatting is horrible,but my guess this is most likely to do copy & past issues in the forum.
Place your qDebug here, and it should work fine
void Okuma () { QFile file("readJson/deneme.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); QByteArray data = file.readAll(); QJsonDocument doc = QJsonDocument::fromJson(data); QJsonObject root = doc.object(); QJsonArray tlmtArray = root.value("Telemetri Verileri").toArray(); for(int i = 0; i < tlmtArray.size(); ++i) { QJsonObject obj = tlmtArray[i].toObject(); QJsonArray gps_array = obj.value("GPS").toArray(); for(int j = 0; j < gps_array.size(); ++j) { QJsonObject gps_obj = gps_array[j].toObject(); for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit) { QJsonArray inner_data = cit.value().toArray(); if(inner_data[0] == "float") { // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : ""); } if (inner_data[0] == "integer") { // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : ""); list << inner_data[1].toString().toInt(); // qDebug() << "My list contains" << list.count() << "items."; // qDebug() << "My whole list is" << list; } } } } if(!list.isEmpty()) qDebug() << list.at(0); else qDebug() << "List has no elements" }
-
@suslucoder said in How to get first element of QList:
QList<int> index;
try this way
QList<int> index; index.push_back(10); index.push_back(20); index.push_back(30); index.push_back(40); index.push_back(50); qDebug() << index.at(0); /// Answer Is 10
-
@Ketan__Patel__0011 said in How to get first element of QList:
try this way
QList<int> index; index.push_back(10); index.push_back(20); index.push_back(30); index.push_back(40); index.push_back(50); qDebug() << index.at(0); /// Answer Is 10
OP already figured out the function for obtaining the first element: https://forum.qt.io/post/650999 Their current problem is how to avoid calling it inside a loop.