Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextStream codec
Forum Updated to NodeBB v4.3 + New Features

QTextStream codec

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • naaxN Offline
    naaxN Offline
    naax
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • naaxN Offline
      naaxN Offline
      naax
      wrote on last edited by
      #16

      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.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

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

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        naaxN 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

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

          naaxN Offline
          naaxN Offline
          naax
          wrote on last edited by
          #3

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

          Christian EhrlicherC 1 Reply Last reply
          0
          • naaxN naax

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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            naaxN 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

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

              naaxN Offline
              naaxN Offline
              naax
              wrote on last edited by
              #5

              @Christian-Ehrlicher

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

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                naaxN 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

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

                  naaxN Offline
                  naaxN Offline
                  naax
                  wrote on last edited by
                  #7

                  @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

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    naaxN 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      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.

                      naaxN Offline
                      naaxN Offline
                      naax
                      wrote on last edited by
                      #9

                      @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();
                           }
                      
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @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

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        naaxN 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @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

                          naaxN Offline
                          naaxN Offline
                          naax
                          wrote on last edited by
                          #11

                          @Christian-Ehrlicher

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

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @naax said in QTextStream codec:

                            so I think the problem is with Item Map

                            Did you actually implement what I suggested?

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            naaxN 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @naax said in QTextStream codec:

                              so I think the problem is with Item Map

                              Did you actually implement what I suggested?

                              naaxN Offline
                              naaxN Offline
                              naax
                              wrote on last edited by
                              #13

                              @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()]);
                              
                              
                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #14

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

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                naaxN 1 Reply Last reply
                                0
                                • Christian EhrlicherC Christian Ehrlicher

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

                                  naaxN Offline
                                  naaxN Offline
                                  naax
                                  wrote on last edited by
                                  #15

                                  @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

                                  1 Reply Last reply
                                  0
                                  • naaxN Offline
                                    naaxN Offline
                                    naax
                                    wrote on last edited by
                                    #16

                                    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.

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved