【QT】QT多语言切换


该例程使用的是Qt内置提供的多语言切换方法,优点是无需重启便能动态切换界面语言

1.创建任意一个项目

2. 利用lupdate(language update)工具生成.ts文件

ts(Translation Sourcefile)是XML格式的翻译源文件

2.1 在工程中的.pro文件中指定.ts文件要存放的位置

TRANSLATIONS += res/language/zh_CN.ts \
                res/language/en_US.ts

如下图:
在这里插入图片描述
注意:该路径需要自己在工程文件夹中新建(可自定义)
在这里插入图片描述

2.2 选择工具–》外部–》Qt语言家–》更新翻译

在这里插入图片描述
有如下信息输出说明生成成功:

在这里插入图片描述

若不添加会报如下警告:

在这里插入图片描述

3. 利用 lrelease(Language Release)将 .ts 文件翻译成指定语言的 .qm 文件

.qm 文件是 Qt message Qt 信息文件

3.1 启动对应版本的 linguist.exe(语言处理专家) 程序(在开始菜单与Qt-creator同级的应用)

在这里插入图片描述

3.2 点击工具栏的“打开”按钮(或者点击菜单“文件 --》打开”),选择打开

在这里插入图片描述

3.3 根据上一步选择的目标语言是zn_CN.ts ,所以目标语言是中文即可(选择.ts文件是什么语言目标语言就是对应的该语言)

在这里插入图片描述

3.4 按如下步骤将所有文本翻译成对应语言(我这里是中文)

在这里插入图片描述

3.5 所有翻译完成后点击保存再发布

在这里插入图片描述

3.6 点击菜单栏的 文件 --》发布

在这里插入图片描述

3.7 在.ts文件夹下生成的.qm文件

在这里插入图片描述

3.8 该翻译文件完成,点击菜单栏的文件–》关闭 关闭该文件,再打开其它要翻译的文件

3.9 按上述步骤即可完成其它需要翻译的语言

4. 在项目中添加 加载 qm 文件、安装翻译器 qTranslator、翻译后刷新界面的代码

if (m_qTranslator == nullptr) {
    m_qTranslator = new QTranslator(this);
}
m_qTranslator ->load(":/res/language/en_US.qm");
qApp->installTranslator(m_qTranslator );
ui->retranslateUi(this);

示例代码:可直接放入工程测试

// mainwindow 登录界面

// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTranslator>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_btn_ch_clicked();

    void on_btn_en_clicked();

private:
    Ui::MainWindow *ui;
    QTranslator *m_qTranslator = nullptr;
};
#endif // MAINWINDOW_H

// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "info_win.h"
#include <QString>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
// 登录按钮的槽函数
void MainWindow::on_pushButton_clicked()
{
    // 获取输入的用户名和密码
    QString account = ui->lineEdit->text();
    QString pswd = ui->lineEdit_2->text();

    if(account == "aaa" && pswd == "123")
    {
        //创建第二个界面对象,必须传递this指针(把第一个界面的地址传给第二个界面)
        info_win *info_w = new info_win(this);
        qDebug()<<"第一界面的地址是"<<this;
        // 显示第二个界面
        info_w->show();
        //隐藏第一个界面
        /*
            隐藏:把界面转入到系统后台运行,这个界面对象依然存在
            关闭:界面释放掉,对象不存在了
        */
        this->hide();
    }
    else
    {
        qDebug()<<"账号或密码错误,重新输入";
    }
}

void MainWindow::on_btn_ch_clicked()
{
    if (nullptr == m_qTranslator) {
        m_qTranslator = new QTranslator(this);
    }
    m_qTranslator->load(":/res/language/zh_CN.qm");
    qApp->installTranslator(m_qTranslator);
    ui->retranslateUi(this);
}

void MainWindow::on_btn_en_clicked()
{
    if (nullptr == m_qTranslator) {
        m_qTranslator = new QTranslator(this);
    }
    m_qTranslator->load(":/res/language/en_US.qm");
    qApp->installTranslator(m_qTranslator);
    ui->retranslateUi(this);
}

// info_win 信息界面

// info_win.cpp
#ifndef INFO_WIN_H
#define INFO_WIN_H

#include <QMainWindow>

namespace Ui {
class info_win;
}

class info_win : public QMainWindow
{
    Q_OBJECT

public:
    explicit info_win(QWidget *parent = nullptr);
    ~info_win();

private slots:
    void on_btn_back_clicked();

private:
    Ui::info_win *ui;
    QWidget *save_mainwin;
};

#endif // INFO_WIN_H

// info_win.cpp
#include "info_win.h"
#include "ui_info_win.h"
#include <QDebug>
#include <QWidget>

info_win::info_win(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::info_win)
{
    ui->setupUi(this);
    save_mainwin = parent;
    qDebug()<<"传递过来的参数是第一个界面的地址:"<<parent;
    ui->label_dymText->setText(tr("手动设置文本"));
}

info_win::~info_win()
{
    delete ui;
}
//返回上一级界面按钮的槽函数(第一个界面,登录界面)
void info_win::on_btn_back_clicked()
{
    //得到上一级界面的地址
    //思路1:当初创建第二个界面的时候,把第一个界面的地址通过构造函数传递过来
    //      定义的私有成员save_mainwin就是用来保存传递过来的第一个界面的地址
    // 显示上级界面
//    save_mainwin->show();

    //思路2:QT提供了一个函数,叫做parentWidget(),该函数用来返回上一级界面地址
    QWidget *main_win = this->parentWidget();
    main_win->show();
    qDebug()<<"main_win界面地址:"<<main_win;

    //关闭当前(第二个界面)界面
    delete this;    // this->close();
}

// .ui 文件
// mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>250</x>
      <y>20</y>
      <width>351</width>
      <height>81</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>欢迎登录</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>140</y>
      <width>121</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>账号:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>190</y>
      <width>131</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>密码:</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>150</y>
      <width>131</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>260</y>
      <width>151</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>登录</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>200</y>
      <width>131</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
   </widget>
   <widget class="QPushButton" name="btn_ch">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>390</y>
      <width>141</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>中文</string>
    </property>
   </widget>
   <widget class="QPushButton" name="btn_en">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>390</y>
      <width>201</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>英文</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

// info_win.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>info_win</class>
 <widget class="QMainWindow" name="info_win">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>140</y>
      <width>100</width>
      <height>40</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>睡觉</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>210</y>
      <width>171</width>
      <height>40</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>打游戏</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_4">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>90</y>
      <width>100</width>
      <height>40</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>吃饭</string>
    </property>
   </widget>
   <widget class="QPushButton" name="btn_back">
    <property name="geometry">
     <rect>
      <x>260</x>
      <y>60</y>
      <width>381</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>楷体</family>
      <pointsize>20</pointsize>
     </font>
    </property>
    <property name="text">
     <string>返回登录界面</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>300</y>
      <width>91</width>
      <height>61</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>cat</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>390</y>
      <width>91</width>
      <height>61</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>dog</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>300</y>
      <width>91</width>
      <height>61</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>food</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_dymText">
    <property name="geometry">
     <rect>
      <x>250</x>
      <y>400</y>
      <width>321</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

代码框架:

在这里插入图片描述
在代码中设置的文本需要 tr()函数,标记为可翻译的
在这里插入图片描述
修改完后可在Qt-creator做发布(lrelease),会将所有在Qt预言家(linguist.exe)中翻译的.ts文件全部生成为.qm文件

在这里插入图片描述

现象:

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值