QT5常見問題二:應用程序中文亂碼解決方法,總結

QT5的中文亂碼解決辦法的實驗環境如下:
環境:Windows10企業版
開發工具:VS2015企業版,Qt5.6.2_x64,add-in
程序:VS2015創建了一個默認的QDialog Gui應用程序,主界面放置了一個QLabeL,UI界面和添加代碼如下。
UI界面
QT5常見問題二:應用程序中文亂碼解決方法,總結
添加代碼如下:
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

this->setWindowTitle("測試中文亂碼 作者:夾谷 QQ:863858950");
ui.label->setText("尋找中文亂碼解決的好辦法!");

}
程序運行結果:
QT5常見問題二:應用程序中文亂碼解決方法,總結

解決方法總結如下:
方法一:
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

this->setWindowTitle(QString::fromLocal8Bit("測試中文亂碼 作者:夾谷 QQ:863858950"));
ui.label->setText(QString::fromLocal8Bit("尋找中文亂碼解決的好辦法!"));

}
結果:
QT5常見問題二:應用程序中文亂碼解決方法,總結
方法二:
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

this->setWindowTitle(QStringLiteral("測試中文亂碼 作者:夾谷 QQ:863858950"));
ui.label->setText(QStringLiteral("尋找中文亂碼解決的好辦法!"));

}
結果:
QT5常見問題二:應用程序中文亂碼解決方法,總結
方法三:
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

this->setWindowTitle(QString::fromWCharArray(L"測試中文亂碼 作者:夾谷 QQ:863858950"));
ui.label->setText(QString::fromWCharArray(L"尋找中文亂碼解決的好辦法!"));

}
結果:
QT5常見問題二:應用程序中文亂碼解決方法,總結
方法四:
在頭文件聲明處聲明:#pragma execution_character_set("utf-8")
#pragma once
#pragma execution_character_set("utf-8")
#include <QtWidgets/QDialog>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QDialog
{
Q_OBJECT

public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);

private:
Ui::QtGuiApplication1Class ui;
};
注意:有可能報錯誤---warning C4068: 未知的雜注,這是由於VS安裝不完全(我曾在VS2010中遇到過此問題)
結果:
QT5常見問題二:應用程序中文亂碼解決方法,總結

自己在使用QT5進行開發的過程中,最開始主要是基於以上四種方法進行解決。但是當遇到中文國際化問題的時候,通過以上4中辦法雖然解決了中文亂碼問題,但是無法有效進行中文國際化。在翻譯文件中的依然會有中文亂碼題,該如何解決呢?下一篇文章揭曉筆者使用的方法,也許你已經使用了哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章