Results 1 to 9 of 9

Thread: Reading Unicode character from file and converting it in to QString of actual text

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Reading Unicode character from file and converting it in to QString of actual text

    Hi,

    I have to read unicode character of Indian Languages(Hindi/Telugu etc) from a file,
    then i have to convert it in to the QString of actual text/font of that language.

    While we are passing unicode manually it's working fine like following :

    Qt Code:
    1. QString str = QString::fromStdWString(L"\u0926\u0947\u0935\u0928\u093e\u0970");
    2. qDebug() << str; // It's printing actual font;
    To copy to clipboard, switch view to plain text mode 


    But while we are trying to read unicode from file then not able to move ahead,
    our requirement is like that :


    Qt Code:
    1. QString fileText;
    2. QFile file(argv[1]); //argv[1] is file name we are passing on command line.
    3. QTextStream out(&file);
    4. if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    5. {
    6. fileText=out.readAll();
    7. }
    8. file.close();
    9. qDebug()<<"data in the file:\n"<<fileText; // printing unicode character as it is in file;
    10.  
    11. // PROBLEM
    12.  
    13. std::wstring temp_text = fileText.toStdWString();
    14. QString test = QString::fromStdWString(temp_text); // Not Working
    15.  
    16. qDebug()<<"text:"<<test; // It's printing unicode character as, conversion not happening
    To copy to clipboard, switch view to plain text mode 

    My doubt is on fromStdWString() , In first case we are passing "L", what is this ?
    And how to paas this "L" in fromStdWString(temp_text);

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    Why do you want to convert the QString into a wstring just to reverse the conversion immediately again?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    Hi Anda,

    Thanks for your quick reply !

    At line No. 9,
    qDebug()<<"data in the file:\n"<<fileText; // printing unicode character as it is in file;

    Here we have to print data in actual hindi font, but it's printing unicode as it is in file.
    So after reading the unicode from file we have to print it in to Hindi(local) font.

    Can you guide me how to do ?

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    In the line
    Qt Code:
    1. QString str = QString::fromStdWString(L"\u0926\u0947\u0935\u0928\u093e\u0970");
    To copy to clipboard, switch view to plain text mode 
    the 'L' marks L"\u0926\u0947\u0935\u0928\u093e\u0970" as a wide string literal, i.e. a NUL-terminated string of wide characters (of type wchar_t), which are probably 16-bit wide on your system. In summary, this is probably a native UTF-16 string. The compiler then allocates a temporary std::wstring with this string literal, then builds a QString out of it. This works well because QString::fromStdWString() expects its argument to be encoded in UTF-16. By the way, you can eliminate the intermediate std::wstring by calling QString::fromWCharArray() instead.

    When reading from a file, things are a bit different. A file is made of bytes, not 16-bit wide characters; you need to tell QTextStream how to decode those bytes into Unicode characters. By default, QTextStream uses QTextCodec::codecForLocale(), i.e. the encoding associated with the system locale. This may or may not be what you want. You should call QTextStream::setCodec() to explicitly set the appropriate codec. You can obtain a codec with QTextCodec::codecForName(); for instance, QTextCodec::codecForName("UTF-8") returns a UTF-8 codec.

    Your first task should be to determine the encoding of the text file.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    Quote Originally Posted by gunturrohith View Post
    qDebug()<<"data in the file:\n"<<fileText; // printing unicode character as it is in file;

    Here we have to print data in actual hindi font, but it's printing unicode as it is in file.
    So after reading the unicode from file we have to print it in to Hindi(local) font.
    qDebug() is a debug stream, you might have more luck with writing into cout or using a QTextStream on top of the stdout file handle.

    For cout you would probably need to convert the QString to the local 8 bit encoding, using QString::toLocal8Bit()

    Cheers,
    _

  6. #6
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    Hi yeye_olive,

    Thanks for your replay.

    I didn't get clarity,can you provide any example for this one.

    Thanks

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    1. Find out how the text file is encoded.
    2. In your code, right after the line
      Qt Code:
      1. QTextStream out(&file);
      To copy to clipboard, switch view to plain text mode 
      add a line
      Qt Code:
      1. out.setCodec("replace this with the name of the encoding (see the documentation for QTextCodec)");
      To copy to clipboard, switch view to plain text mode 
      to set the encoding you determined in 1.

    Read the documentation for QTextCodec and QTextStream for details.

  8. #8
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    Hi yeye_olive,

    I already tried this one.But,I didn't get result

    QFile file(argv[1]); (Here i gave hindi unicode file)
    QTextStream out(&file);
    out.setCodec("UTF-16");
    out.setAutoDetectUnicode(true);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))

    {
    fileText=out.readAll();
    }
    file.close();
    qDebug()<<"data in the file:\n"<<fileText;

    it prints like"ç•œæŒ°ã€°ç•œæŒ°ã„°ç• œæŒ°ãˆ°ç•œæŒ°ã°" unknown code But i want original language of unicode

    Thanks.

  9. #9
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading Unicode character from file and converting it in to QString of actual tex

    I have no idea what you mean by "hindi unicode" or "original language of unicode". There is only one Unicode (to rule them all). If you do not understand what a text encoding is, then I am afraid I cannot help you. I cannot do anything for you unless you follow those steps in order:
    1. Read some material on text encodings to understand what they are.
    2. Determine what the encoding of your file is.
    3. Start coding.

Similar Threads

  1. Replies: 2
    Last Post: 13th November 2013, 08:03
  2. Read unicode text from file
    By Boron in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2012, 15:27
  3. Replies: 1
    Last Post: 12th October 2012, 21:32
  4. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  5. Reading a unicode names from a file???
    By darpan in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2006, 17:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.