Home > other >  QTextEdit show Chinese garbled
QTextEdit show Chinese garbled

Time:10-03

Code list as follows: copying code QFile inputFile (tr (" :/input. TXT ")); InputFile. Open (QIODevice: : ReadOnly); QTextStream (in & amp; InputFile); QString line=in readAll (); InputFile. Close ();//a QString info=tr (" Chinese ");//UI - & gt; TextEdit - & gt; SetPlainText (info); The UI - & gt; TextEdit - & gt; SetPlainText (tr (line. ToStdString () c_str ())); I'm from input. TXT (utf-8) read from the file content, if direct UI - & gt; TextEdit - & gt; SetPlainText (line); Will appear garbled, and if the above info="Chinese" will be garbled, tr will not, UI - & gt; TextEdit - & gt; SetPlainText (tr (line. ToStdString () c_str ())); Will not gibberish, but such writing isn't too much trouble, could you tell me, what's a little bit more simple writing, at least don't need so many steps, thank you

CodePudding user response:

Always remember to save trouble, don't in the code used in Chinese if you determine the TXT is try to impose on textstream set encoding utf8 words?

CodePudding user response:

Use the tr, inside and don't use Chinese... Try to use Chinese QString: : fromLocal8Bit (" Chinese English "); Or a QString: : fromUtf8 (in readAll ());

CodePudding user response:

Read more QT coding issues and look more like that

CodePudding user response:

File read, if can confirm file encoding can specify the codec

CodePudding user response:

In the main () to add the following line of code: QTextCodec: : setCodecForTr (QTextCodec: : codecForLocale ());

CodePudding user response:

Void UiUtil: : enableUtf8 () {QTextCodec * codec=QTextCodec: : codecForName (" UTF8 "); If (codec) {QTextCodec: : setCodecForTr (codec); QTextCodec: : setCodecForLocale (codec); QTextCodec: : setCodecForCStrings (codec); }}

CodePudding user response:

Main function used in the above code, let Qt application by default use utf-8 display character, "to save trouble don't use Chinese in your code", do not take with it, ha ha

CodePudding user response:

References to the seventh floor jorneyr 10:53 published in 2011-11-21, the main function is used in the above code, let Qt application by default use utf-8 display character, "want to save trouble, don't use Chinese in your code", do not take with it, ha ha ha ha, whether you agree or not agree, the 5 and 6 floor setCodecForXXX usage there are a variety of problems!! Also use error (or work only in certain circumstances)

CodePudding user response:

CSDN urine sex really poor, copy to come over from other web site's content, I found the original website, also delete my post, say my hair advertisement? Advertise your younger sister ah,
  • Related