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. QString shows garbage characeter
Forum Updated to NodeBB v4.3 + New Features

QString shows garbage characeter

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.2k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    neel2818
    wrote on last edited by
    #1

    Hi All,

    I am reading the following text from the text file and setText that string to label i am reading the text into QString and after reading i am setting the text. It is showing garbage character after applying the setText.

    Below is my code.

    @QFile file("C:/Users/my/Desktop/Issues.txt");

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            return;
    }
    
    QString line(file.readLine());    
    label->setText(line);
    file.close();@
    

    ###############################
    Below is the file content.

    TEMP (°C)
    ###############################

    But the same code if i do label->setText("TEMP (°C)") then is working fine.

    Output of the above code is garbage character when we read the content from file ?
    Here ° (celcius) character is displayed but garbage character will come. Is there any reason why garbage character is coming when we reading the this special character (not entered through keypad) ?

    Thanks,
    Neel

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lycis
      wrote on last edited by
      #2

      It's very likely that this is because of the file encoding. Maybe the file content is not encdoded in ASCII but in UTF-8 or some other encoding.

      Maybe it is sufficient to call "QString::toAscii()":http://doc-snapshot.qt-project.org/4.8/qstring.html#toAscii to convert the QString to a usable format.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        d2uriel
        wrote on last edited by
        #3

        If that's not going to do the trick try using "QTextStream":http://qt-project.org/doc/qt-4.8/qtextstream.html for reading and set the appropriate text codec.

        "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

        1 Reply Last reply
        0
        • N Offline
          N Offline
          neel2818
          wrote on last edited by
          #4

          I also tried with below conversion to UTF-8

          QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

          But it didn't help me. It showing garbage character (question mark symbol for every character).

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lycis
            wrote on last edited by
            #5

            Are you sure that the file content is stored as UTF-88? Did you try to use a QTextStream for accessing the data?

            Especially when using special characters (e.g. everything un-ASCII) it is important to use the correct character encoding.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              neel2818
              wrote on last edited by
              #6

              I have tried with QTextStream also. Below is the code.

              @FILE *fp;
              fp = fopen("C:/Users/my/Desktop/Issues.txt","r");

              QTextStream stream(fp);
              QString line;
              line = stream.readLine();
              
              label->setText(line);@
              
              1 Reply Last reply
              0
              • D Offline
                D Offline
                d2uriel
                wrote on last edited by
                #7

                In the above code you didn't set the codec. Try doing that!

                "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  neel2818
                  wrote on last edited by
                  #8

                  Now it is showing the correct string. But following is my observation.

                  when i write the below code then characters are displaying garbage and when i comment below code then characters are displaying correctly.

                  @QTextCodec *codec = QTextCodec::codecForName("UTF-8");
                  QTextCodec::setCodecForCStrings(codec);
                  QTextCodec::setCodecForTr(codec);@

                  Can you please tell me why this strange behaviour happens ?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    d2uriel
                    wrote on last edited by
                    #9

                    You need to post some more also the context of the code you posted. So give us more lines of your code so we can see what are you doing before and after this little snippet.

                    "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      neel2818
                      wrote on last edited by
                      #10

                      I got the issue. One application written the file in ANSI format that is why it is not able to decode it.
                      Anyway thanks for your support.

                      One more thing i want to know is if i already had applied codec for UTF-8 and if i want to apply the codec for another ISO then can i apply two times after one already set ?

                      @
                      QTextCodec *codec = QTextCodec::codecForName("UTF-8");
                      QTextCodec::setCodecForCStrings(codec);
                      QTextCodec::setCodecForTr(codec);
                      @

                      after above code i want to apply same with ISO type. can i set ? i am not able to set ? How to do that ?

                      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