Results 1 to 5 of 5

Thread: problem in character encoding

  1. #1
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default problem in character encoding

    Hello,

    I have a problem with Turkish cgaracters while writing in an XML file. Although I tried using some encoding functions, it did not work. Turkish characters I am trying to write in the file are "çÇğĞıİöÖşŞüà ". For example, I try writing "Görev", however, the text written is "Görev".

    Could you please help me with this issue?

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

    Default Re: problem in character encoding

    Please show the code that you wrote. Also, pay attention to the following points:
    - When using string literals, avoid non-ASCII characters so that the behaviour of the program does not depend on the encoding of the .cpp file. Use escape sequences instead.
    - When viewing the contents of the file in a text editor, make sure you open it using the same encoding in which it was written.

  3. #3
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem in character encoding

    Hello,

    The code I wrote is as the following:

    QXmlStreamWriter *stream = new QXmlStreamWriter(file);
    stream->setAutoFormatting(true);
    stream->writeStartDocument();
    stream->writeStartElement("Main");
    stream->writeTextElement("Entry", entry);

    entry is the variable entered through a QLineEdit widget. I tried setting encoding as the following:

    QTextCodec *q;
    q = q->codecForName("ISO-8859-9");
    QTextCodec::setCodecForCStrings(q);

    or calling the function QString::fromUtf8() and some more. However, the text written in the file is problematic. Moreover, I try to read the xml file to be able to write it on the QLineEdit widget, in the same way, the text is damaged.

  4. #4
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem in character encoding

    Just the addition of the following code fixes the problem. I had made something wrong

    Qt Code:
    1. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: problem in character encoding

    That is strange. QTextCodec::setCodecForCStrings() sets the encoding to use to convert C strings and QByteArrays to QStrings. It applies to "Main" and "Entry" in your code. If the entry variable is a QString obtained from QLineEdit::text(), then it should not be affected.

Similar Threads

  1. Qt for Android: Character Encoding problems.
    By HeReSY in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2012, 21:48
  2. encoding problem
    By lexqqq in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2010, 23:41
  3. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 08:45
  4. Character encoding issues
    By yagabey in forum Qt Programming
    Replies: 11
    Last Post: 15th December 2008, 22:20
  5. QTableView encoding problem
    By SudaNix in forum Newbie
    Replies: 7
    Last Post: 26th February 2008, 14:19

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.