QT4 Chinese garbled characters-general Linux technology-Linux programming and kernel information. The following is a detailed description. For beginners of Linux, go to advanced QT programming. However, the first Demo program encountered a Chinese Garbled text, a great sweat!
Environment:
1. RedHat AS5
2. QT4.4.0
3. LANG = "zh_CN.GB18030"
Program:
...
QTextCodec: setCodecForTr (QTextCodec: codecForName ("gb18030 "));
...
Label. setText (QObject: tr ("the same world, the same dream! "));
...
Result:
The title is displayed normally. Chinese characters of labels and buttons are garbled.
Search online for 3 ~ 4 days, summarized as follows:
1. Use setdefacodecodec to set;
QApp-> setdefacodecodec (QTextCodec: codecForName ("GBK "));
QLabel * label = new QLabel (tr ("Chinese label "));
Unfortunately, setdefacodecodec is a function of QT3, which is no longer supported by QT4.
2. Set the encoding of the QObject member function tr;
QTextCodec: setCodecForTr (QTextCodec: codecForName ("GBK "));
3. Use the fromLocal8Bit () function of QString;
QString str;
Str = str. fromLocal8Bit ("HAHAHA ");
Hello. setWindowTitle (str );
4. Use the toUnicode method of QTextCodec to display Chinese characters.
QLabel hello (QObject: tr ("hello"). toLocal8Bit ());
QTextCodec * codec = QTextCodec: codecForLocale ();
QString a = codec-> toUnicode ("Anshi manual ");
The above issues are not solved.
I still did not search for this question, and finally found the real answer in a blog: font.
"A strange phenomenon found in QT4 is that the Title" I Am a dialog box "in the dialog box can be correctly displayed, and the button is a small square. It seems that some system settings are incorrect, not character encoding. If it is a character encoding problem, it should be garbled rather than a small square. I suddenly thought that the font was mentioned on the Internet. I think it can be explained that the title bar and button text are not the same font, but the button font is not, so it is a small square, rather than Garbled text.
I searched the default font used for QT4 on the Internet. I did not find the default font. I read the source code of QT4 and found too many codes.
However, the setFont function caught my attention. I have seen this method on the Internet ."
Refer to the above content to modify the code:
...
QTextCodec: setCodecForTr (QTextCodec: codecForName ("gb18030 "));
QFont font ("Times", 12, QFont: Normal, FALSE );
App. setFont (font );
...
Label. setText (QObject: tr ("the same world, the same dream! "));
...
Successful! Chinese display is normal!
The solution to this problem is really too important. To tell the truth, it is really a little exhausting. Fortunately, I didn't give up. Yeah!
Note: when using the qt designer, the interface is still garbled. Use qtconfig to set font to Bitstream Charter to solve the garbled problem. At this time, I think the original program is also OK? Unexpectedly, there was no Garbled text ~ Oh MyGod!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.