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. Convert QString into UTF-8 string

Convert QString into UTF-8 string

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.9k 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.
  • G Offline
    G Offline
    goli
    wrote on last edited by
    #1

    I have a file with utf-8 strings (hebrew). when i read the data from the file into Qstring argumet, i get incorrect string.
    I tried to use QString::fromUtf8() function, but it didn't work.
    Also when i use QTextCodec::codecForName("UTF-8"), i get the same result, and the data is incorrect.

    Does anyone know how can i get the correct values into the QString argument?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DSav
      wrote on last edited by
      #2

      Could you show us how you are loading a file?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goli
        wrote on last edited by
        #3

        open the file:

        @
        QFile myFile;
        myFile.setFileName("file.txt");

        // Open files
        if(! myFile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
        qDebug() << "open file Error\n";
        return;
        }
        @

        then i read the data into QString :
        @
        QString line = m_myFile.readLine();
        @

        the dada is not correct.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DSav
          wrote on last edited by
          #4

          The following simple test just loaded a correct string with cyrillic chars into string variable:

          @
          #include <QtCore>
          #include <QFile>
          #include <QDebug>

          int main(int argc, char *argv[])
          {
          Q_UNUSED(argc)
          Q_UNUSED(argv)

          QFile file;
          file.setFileName("file.txt");
          
          // Open files
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              qDebug() << "Open file error\n";
              return 0;
          }
          
          QString string = QString::fromUtf8(file.readLine());
          
          return 0;
          

          }
          @

          It works ok for both utf-8 file with or without Unicode BOM for me.

          Try to check it out, will it work fine for you?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DSav
            wrote on last edited by
            #5

            BTW, how do you make sure the data is incorrect: inspect a variable via debugger or outputting a value into some widget?

            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