Solved QTextStream support unicode?
-
Hi i'm using qtextstream but when i write in file like:
QFile mFile(Filename); if(!mFile.open(QFile::WriteOnly | QIODevice::Text | QIODevice::Append)) { //error } QTextStream out(&mFile); out << s ;
And write a symbol it doesnt write me. Support unicode?
-
@Jeronimo Try to set codec using http://doc.qt.io/qt-5/qtextstream.html#setCodec as described in the documentation. And you should call setGenerateByteOrderMark(true) as described in documentation, so BOM is written into the file.
-
@Jeronimo
Please clear if I am wrong, you wanted converted of 's' to a symbol or write only 's' in the file? -
@Jeronimo Can you please explain better what happens?
What symbols do you want to write and what is written? -
@Jeronimo If you read documentation (http://doc.qt.io/qt-5/qtextstream.html) you will find:
"Internally, QTextStream uses a Unicode based buffer, and QTextCodec is used by QTextStream to automatically support different character sets. By default, QTextCodec::codecForLocale() is used for reading and writing, but you can also set the codec by calling setCodec(). Automatic Unicode detection is also supported. When this feature is enabled (the default behavior), QTextStream will detect the UTF-16 or the UTF-32 BOM (Byte Order Mark) and switch to the appropriate UTF codec when reading. QTextStream does not write a BOM by default, but you can enable this by calling setGenerateByteOrderMark(true). When QTextStream operates on a QString directly, the codec is disabled."
So, yes it supports Unicode. -
@jsulm But why when i write like in arabic show me interrogation ??
I'm doing this:
void Write(QString Filename, QString s) { QFile mFile(Filename); if(!mFile.open(QFile::WriteOnly | QIODevice::Text | QIODevice::Append)) { //error } QTextStream out(&mFile); out << s ; mFile.flush(); mFile.close(); } Write("hola","ي")
But all time show me interrogations like ????
This only happen me with languages like arabic and russian other's not like english but idont know why take this error. -
@jsulm this happen to with qdebug i can write in other language like russian etc.
-
@Jeronimo Try to set codec using http://doc.qt.io/qt-5/qtextstream.html#setCodec as described in the documentation. And you should call setGenerateByteOrderMark(true) as described in documentation, so BOM is written into the file.
-
@jsulm works fine thxx..!!
-
@Jeronimo Unicode can be tricky compared to ASCII :-)