QT5.2中文乱码解决问题
总结: 之前Qt4写的代码用QTextCodec::codecForTr()来解决中文输入问题,为了防止错误的滥用,在Qt5中已经取消了这个方法。 Qt5VS2010 中文乱码问题的解决:【请记住VS2010必须打普定vs2010sp1,下面代码才有效】 #if _MSC_VER = 1600 #pragma execution_cha
总结:之前Qt4写的代码用QTextCodec::codecForTr()来解决中文输入问题,为了防止错误的滥用,在Qt5中已经取消了这个方法。
Qt5+VS2010中文乱码问题的解决:【请记住VS2010必须打普定vs2010sp1,下面代码才有效】
<p><span><span>#if _MSC_VER >= 1600</span> <span>#pragma</span><span> execution_character_set</span><span>(</span><span>"utf-8"</span><span>)</span> <span>#endif</span></span></p>
Qt+Visualstudio的中文乱码总结:
1、Qt4.7+VS2008,通过如下方法:
-
#include
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- // 以下部分解决中文乱码
- QTextCodec *codec = QTextCodec::codecForName("GBK");
- QTextCodec::setCodecForTr(codec);
- QTextCodec::setCodecForLocale(codec);
-
QTextCodec::setCodecForCStrings(codec);
}
2、Qt5.2+VS2010打入补丁VS2010sp1,也会支持 #pragma execution_character_set ("utf-8"),顺利解决qt中文乱码问题
在main函数之前加入:
- #if _MSC_VER >= 1600
- #pragma execution_character_set("utf-8")
- #endif
3、Qt5.2+ VS2012 不支持 #pragma execution_character_set ("utf-8") 导致utf-8编码的无法直接输出中文,qt首当其冲受害。 微软官方回应,再下一个版本中会修正这个问题:
由 Microsoft 在 2013/1/3 於 10:24 公佈
Hi Seek:
Thanks for reporting the issue.
A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.
Xiang Fan
Visual C++ Team
4、Qt5.2+VS2013支持 #pragma execution_character_set ("utf-8"),所以qt也赶紧发布了一个
Visual Studio Add-in 1.2.3 Alpha for Qt5 (supports versions 2008, 2010, 2012 and 2013, ess edition)
在main函数之前加入:
- #if _MSC_VER >= 1600
- #pragma execution_character_set("utf-8")
- #endif
5、回到 Qt5 的中文输出问题。
Qt默认支持 VS2010/MinGW/Gcc 等编译器,而它们现在都已经真正支持UTF8了。当然,VS2010 对UTF8的支持会入侵代码(#pragma execution_character_set("utf-8"))。 看看Qt官方论坛别人是怎么说的:
http://qt-project.org/forums/viewthread/17617
Nothing special need to do, it will works by default.
If the exec-charset of your your compiler is UTF-8.
简单的说,从Qt5开始,源代码就是默认UTF8编码的。
当然,VC2010编辑器对带BOM的UTF8也是认识,只可惜VC2010编译器根本承认它是UTF8!
在继续看官方论坛的回复:
You can write a simple example like this
- You can write a simple example like this
-
#include
-
#include
- #if _MSC_VER >= 1600
- #pragma execution_character_set("utf-8")
- #endif
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QLabel label("???ó??ń??");
- label.show();
- return a.exec();
- }
- If other people can reproduce your problem, you can file a bug.
If other people can reproduce your problem, you can file a bug.
较完整的解决方案(增加了Qt4/Qt5和非VC环境的判断):
- // Coding: UTF-8(BOM)
- #if defined(_MSC_VER) && (_MSC_VER >= 1600)
- # pragma execution_character_set("utf-8")
- #endif
-
#include
-
#include
-
#include
- int main(int argc, char* argv[])
- {
- QApplication app(argc, argv);
- #if QT_VERSION
- #if defined(_MSC_VER) && (_MSC_VER
- QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030-0"));
- #else
- QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
- #endif
- #endif
- QLabel *label = new QLabel(QObject::tr("你好!"));
- label->show();
- return app.exec();
- }
另外:Qt4/Qt5/Linux: 只要是默认的UTF8环境, 应该都没问题
其实这个问题不是Qt特有的, 追根溯源还是C/C++和编译器的问题.即使是支持UTF16的Java也同样难逃此问题。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

Quark browser software provides countless resource information, and the search results are the most accurate. It has a powerful built-in search engine. You can find the corresponding answers by entering keywords. The overall Internet experience is very good. Browsing sections in different fields are open. Search for whatever you search and see. You can find it immediately. All annoying advertising pop-ups are blocked. All annoying advertising pop-ups are blocked. You can freely switch between different browsing modes and surf the Internet. There is no lag at all in the process, and of course the translation function can be turned on. Languages of any country can be easily translated, and there are no problems in communication. Files and documents compatible with various formats can be opened and viewed. Now the editor is browsing for Quark online in detail. Server users bring the method of setting Chinese translation. 1. First click on the mobile phone desktop
