Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does QStringLiteral returns a garbled string

Tags:

c++

unicode

qt

I'm programming a Chinese software, and embed some of the strings in the source file. To reduce runtime overhead (well, actually this is premature optimization, because the speed difference is negligible, but nonetheless, I'm curious), when I use QStringLiteral with Chinese characters such as

QString s = QStringLiteral("中文");

The string is garbled when displayed on screen; but if I simply use

QString s = "中文";

It works fine. But the latter constructs the string at runtime rather than compile time, so it is a little bit slower. Can some one tell me how to fix this?

My source file coding is in UTF8 without BOM; compiler is MSVC 2010 Express with SP1. I also tried

#pragma execution_character_set("utf-8")

to no avail.

like image 452
Siyuan Ren Avatar asked Apr 26 '14 01:04

Siyuan Ren


1 Answers

OK, I solved it myself. I need to save the file with byte order mark, and then MSVC correctly recognized that it is encoded in UTF-8 and everything works fine.


Update: on newer MSVC we can set the compiler option /utf-8 and not bother with the BOM.

like image 129
Siyuan Ren Avatar answered Oct 12 '22 09:10

Siyuan Ren