Solved QTextStream codec
-
Also, I have this .txt file created myself in Windows. I'm adding here items manually for first, and I'm working in my program to add/load/save function for the future.
-
QString::toDouble() takes a bool which shows you if it could be correctly converted. Also you should print out the strings before trying to convert them to numbers.
-
So? I think no, because of still the same problem. Not working
QFile file(":/save/MealOne.txt"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); in.setCodec("UTF-8"); while(!in.atEnd()) { QString line = in.readLine(); QString line2 = in.readLine(); QString line3 = in.readLine(); QString line4 = in.readLine(); QString line5 = in.readLine(); ui->mealOneLeft->addItem(line); ItemKcals.push_back(line2.toDouble()); ItemProteins.push_back(line3.toDouble()); ItemFats.push_back(line4.toDouble()); ItemCarbons.push_back(line5.toDouble()); ItemMap.insert(line, ItemMap.size()); } file.close(); }
-
@naax said in QTextStream codec:
So?
You should check the conversion to your number! And you should print out the values with e.g. qDebug() or to a custom debug label or something else
-
Same. Hmm, so I think the problem is with Item Map... Something broken, need to repair this.
-
@naax said in QTextStream codec:
so I think the problem is with Item Map
Did you actually implement what I suggested?
-
Yes, all is correct here.
So the problem is when I'm "Adding" items to other QListWidget. These same variables are not correct reading from ItemMap
//Add Kcals[1]+=ItemKcals.at(ItemMap[ui->mealTwoLeft->currentItem()->text()]); Proteins[1]+=ItemProteins.at(ItemMap[ui->mealTwoLeft->currentItem()->text()]); Fats[1]+=ItemFats.at(ItemMap[ui->mealTwoLeft->currentItem()->text()]); Carbons[1]+=ItemCarbons.at(ItemMap[ui->mealTwoLeft->currentItem()->text()]);
-
Are you sure currentItem() is correct? Print it out (and use a temp variable for readability)
-
Yes, he is. Current Item Name is correct, but other variables no. Should be 79, not 97 like by first item
-
I resolved it.
The problem was with QMap. I used the same keys in two QListWidget. That's why my variables were not correct. Thx anyway for the help, I was sure that is a problem with the codec.