文檔聲明:
以下資料均屬于本人在學(xué)習(xí)過程中產(chǎn)出的學(xué)習(xí)筆記,如果錯誤或者遺漏之處搀继,請多多指正窘面。并且該文檔在后期會隨著學(xué)習(xí)的深入不斷補(bǔ)充完善翠语。感謝各位的參考查看叽躯。
筆記資料僅供學(xué)習(xí)交流使用,轉(zhuǎn)載請標(biāo)明出處肌括,謝謝配合点骑。
如果存在相關(guān)知識點(diǎn)的遺漏,可以在評論區(qū)留言谍夭,看到后將在第一時間更新黑滴。
作者:Aliven888
如何配置開發(fā)環(huán)境,可以參考這邊文章《VS2015 配置 Qt 開發(fā)編譯環(huán)境》紧索。
創(chuàng)建一個Qt 工程
? 首先袁辈,我們創(chuàng)建一個Qt的項(xiàng)目工程。這里我選擇的是Qt GUI Application珠漂,對話框繼承 QMainWindows 類型(因?yàn)閭€人覺得菜單欄切換語言語言比較方便)晚缩。
添加多語言翻譯文件
? 創(chuàng)建兩個多語言文件 Aliven_zh.ts(中文) 和 Aliven_en.ts(英文)。
編輯翻譯文件
? 編輯多語言文件媳危,并且發(fā)布荞彼,生成 Aliven_xx.qm 文件。
實(shí)現(xiàn)源碼
QtGuiApplication.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication.h"
#include <QTranslator>
#include <QCoreApplication>
#include <QMenu>
#include <QAction>
#include <QDir>
#include <QString>
#include <QMap>
class QtGuiApplication : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication(QWidget *parent = Q_NULLPTR);
~QtGuiApplication();
void init();
void Uninit();
private:
bool loadSysConfigFile(); //加載系統(tǒng)配置文件
bool saveSysConfigFile(); //保存系統(tǒng)配置文件
private:
//多語言翻譯
QTranslator *m_Translation; //加載翻譯文件對象
QString m_strLanguageType; //翻譯文件的類型待笑,zh:中文 en:英文
//菜單
QMenu *m_LanguageMenu;
QAction *m_LanguageEnAction; //英文
QAction *m_LanguageZhAction; //中文
private:
Ui::QtGuiApplicationClass ui;
};
QtGuiApplication.cpp
#include "QtGuiApplication.h"
//多語言文本字符串對象
static const char *c_strQtGuiApplication = "QtGuiApplication";
static const char *c_strTitle = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.title");
static const char *c_strCaption = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.caption");
static const char *c_strLanguage = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.language");
static const char *c_strChinese = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.chinese");
static const char *c_strEnglish = QT_TRANSLATE_NOOP("QtGuiApplication", "Aliven.QtGuiApplication.english");
QtGuiApplication::QtGuiApplication(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_strLanguageType = "zh"; //默認(rèn)值
}
QtGuiApplication::~QtGuiApplication()
{
Uninit();
}
void QtGuiApplication::init()
{
//加載配置文件
if(!loadSysConfigFile())
{
qDebug() << "read config file err.";
}
//加載多語言文件
m_Translation = new QTranslator(this);
QString strPath = QDir::currentPath() + "/TranslationFile/Aliven_" + m_strLanguageType + ".qm";
m_Translation->load(strPath);
qApp->installTranslator(m_Translation);
//初始化菜單 - 綁定信號與槽
m_LanguageMenu = new QMenu(qApp->translate(c_strQtGuiApplication, c_strLanguage), this);
m_LanguageEnAction = new QAction(qApp->translate(c_strQtGuiApplication, c_strEnglish), this);
m_LanguageZhAction = new QAction(qApp->translate(c_strQtGuiApplication, c_strChinese), this);
m_LanguageMenu->addAction(m_LanguageEnAction);
m_LanguageMenu->addAction(m_LanguageZhAction);
ui.menuBar->addMenu(m_LanguageMenu);
//設(shè)置屬性為可選中
m_LanguageEnAction->setCheckable(true);
m_LanguageZhAction->setCheckable(true);
if ("zh" != m_strLanguageType) //設(shè)置當(dāng)前選中狀態(tài)
{
m_LanguageEnAction->setChecked(true);
}
else
{
m_LanguageZhAction->setChecked(true);
}
connect(m_LanguageZhAction, &QAction::triggered, this, [=] {
m_strLanguageType = "zh";
m_LanguageEnAction->setChecked(false);
});
connect(m_LanguageEnAction, &QAction::triggered, this, [=] {
m_strLanguageType = "en";
m_LanguageZhAction->setChecked(false);
});
}
void QtGuiApplication::Uninit()
{
//退出前鸣皂,保存配置參數(shù)到文件中
SaveSysConfigFile();
//釋放資源
delete m_LanguageZhAction;
m_LanguageZhAction = nullptr;
delete m_LanguageEnAction;
m_LanguageEnAction = nullptr;
delete m_LanguageMenu;
m_LanguageMenu = nullptr;
delete m_Translation;
m_Translation = nullptr;
}
//加載系統(tǒng)配置文件
bool QtGuiApplication::loadSysConfigFile()
{
QString strPath = QDir::currentPath() + "/systemConfig.Aliven";
QMap<QString, QString> mapData;
QString strLine = "";
QString strKey = "";
QString strValue = "";
QStringList strList;
QFile file(strPath);
if (!file.open(QIODevice::ReadOnly))
{
return false;
}
QTextStream in(&file);
while (!in.atEnd())
{
strLine = in.readLine();
strList.clear();
strList = strLine.split("=");
mapData.insert(strList.value(0), strList.value(1));
}
file.close();
//獲取配置參數(shù)
if (!mapData.isEmpty())
{
m_strLanguageType = mapData.value("languageType");
}
return true;
}
//保存系統(tǒng)配置文件
bool QtGuiApplication::saveSysConfigFile()
{
QString strPath = QDir::currentPath() + "/systemConfig.Aliven";
QMap<QString, QString> mapData;
mapData.insert("languageType", m_strLanguageType);
QFile file(strPath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
return false;
}
QTextStream out(&file);
QMapIterator<QString, QString> it(mapData);
while (it.hasNext())
{
it.next();
out << it.key() << "=" << it.value() << "\n";
}
file.close();
return true;
}
多語言配置文件
systemConfig.Aliven
languageType=zh
效果圖
? 初次打開,默認(rèn)的是中文
? 選擇英文暮蹂,關(guān)閉后軟件后寞缝,再次打開,系統(tǒng)語言就成功切換成英文了仰泻。