PDA

View Full Version : problem in character encoding



fulbay
17th August 2011, 08:28
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?

yeye_olive
17th August 2011, 09:00
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.

fulbay
17th August 2011, 09:20
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.

fulbay
17th August 2011, 12:02
Just the addition of the following code fixes the problem. I had made something wrong :(


QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8"));

yeye_olive
17th August 2011, 13:29
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.