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