'Why does QStringLiteral returns a garbled string

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1