Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.


  • Lifetime Qt Champion

    @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?



  • @Christian-Ehrlicher

    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)


  • Lifetime Qt Champion

    @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.



  • @Christian-Ehrlicher

    I have everywhere UTF8 or I just don't know where I can change this that


  • Lifetime Qt Champion

    @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...



  • @Christian-Ehrlicher

    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.
    imgw.png


  • Lifetime Qt Champion

    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.



  • @Christian-Ehrlicher

    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();
         }
    
    

  • Lifetime Qt Champion

    @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



  • @Christian-Ehrlicher

    Same. Hmm, so I think the problem is with Item Map... Something broken, need to repair this.


  • Lifetime Qt Champion

    @naax said in QTextStream codec:

    so I think the problem is with Item Map

    Did you actually implement what I suggested?



  • @Christian-Ehrlicher

    Yes, all is correct here.

    ee.png

    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()]);
    
    

  • Lifetime Qt Champion

    Are you sure currentItem() is correct? Print it out (and use a temp variable for readability)



  • @Christian-Ehrlicher

    Yes, he is. Current Item Name is correct, but other variables no. Should be 79, not 97 like by first item

    eeq.png



  • 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.


Log in to reply