Solved QTextStream codec
-
Hello.
I have one problem with the load function in my program. I got polish characters in QListWidget items, loaded from my .txt file.
I used UTF8 codec and i have polish characters, but other variables like kcals, proteins, fats, carbons are same for all items.
For example 5 items in QListWidget. First is Banan (97 kcals etc.) and this (97 kcals etc. are same for the all four items too.
If i comment my setting codec option, than all works again (Every single item has right variables, but polish characters again are not showing correctly)QFile file(":/save/MealOne.txt"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); //in.setCodec("UTF-8"); // Now items variables are ok, but polish characters not working. while(!in.atEnd()) { ``` QString line = in.readLine(); ui->mealOneLeft->addItem(line); QString line2 = in.readLine(); double lineKcals = line2.toDouble(); QString line3 = in.readLine(); double lineProteins = line3.toDouble(); QString line4 = in.readLine(); double lineFats = line4.toDouble(); QString line5 = in.readLine(); double lineCarbons = line5.toDouble(); ItemName.push_back(line); ItemKcals.push_back(lineKcals); ItemProteins.push_back(lineProteins); ItemFats.push_back(lineFats); ItemCarbons.push_back(lineCarbons); ItemMap.insert(line, ItemMap.size()); } file.close(); }
I tried this:
QString line = in.readLine().toUtf8; ui->mealOneLeft->addItem(line);
but not working. Any ideas?
-
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. -
@naax said in QTextStream codec:
but not working.
In which way does it not work? How do you check that it's wrong? Are you sure your file is utf-8 encoded?
-
Polish characters are not showing correctly. Other variables works.
Also, I think so ( don't blame me please, because I'm not sure that works so)If I'm using
in.setCodec("UTF-8");
than works this codec for all "in", so I wanted to readLine convert to utf8 as I said
QString line = in.readLine().toUtf8; ui->mealOneLeft->addItem(line);
and this should work (yes?) (Showing polish characters and this all other variables should show correctly)
-
@naax said in QTextStream codec:
QString line = in.readLine().toUtf8;
This is wrong. You convert a QString to utf8 and then back into a QString by using the system locale.
Make sure your input file is correctly encoded - I'm pretty sure it's not utf-8 encoded. -
I have everywhere UTF8 or I just don't know where I can change this that
-
@naax said in QTextStream codec:
or I just don't know where I can change this that
Since you don't show us the code where you create your text file we can't be sure that it's encoding is really utf-8...
-
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.