Qt之语言家的简单使用(一)(Qt翻译UI,Qt Linguist的使用,含源码+注释)

一、翻译程序示例图

下图包含两个窗口,先弹出一个对话框,然后点击按钮切换语言。
在这里插入图片描述

二、流程须知(个人理解)

  1. 想要使用翻译类生成文件,需要在pro文件中指定生成目录,如本文pro文件中的TRANSLATIONS = Language/linguist_cn.ts Language/linguist_en.ts,其中Language/为ts、qm文件包含目录,该文件夹存在于pro文件同级目录中(不设置包含目录则两种类型文件生成在pro同级目录中)。
  2. 代码中包含文本时需添加使用tr函数包含文本,使其在生成ts文件时能识别到;提示:tr函数使用方法建议单独查看
  3. 当编辑完成后后,通过下图方式生成ts文件(存在则覆盖);
    在这里插入图片描述
  4. 使用Qt Linguist打开ts文件,如下图方式编辑翻译,然后保存;
    在这里插入图片描述
  5. 然后如下图方式生成qm文件操作(存在则覆盖);
    (生成当前ts文件的qm文件)
    在这里插入图片描述

    (生成当前项目中所有ts文件的qm文件)
    在这里插入图片描述最后如代码中使用即可。

三、关于对话框中QDialogButtonBox翻译的操作

在ts文件中需要手动添加如下代码,这就是为什么我Qt Linguist打开ts文件会多一项“QPlatformTheme”的原因,具体原因请查看Qt QDialogButtonBox 英文翻译问题

 <context>
    <name>QPlatformTheme</name>
    <message>
        <location filename="../CTipDialog.ui"/>
        <source>OK</source>
        <translation>确定</translation>
    </message>
    <message>
        <source>Cancel</source>
        <translation>取消</translation>
    </message>
</context>

四、源码

CLinguistTest.h

#ifndef CLINGUISTTEST_H
#define CLINGUISTTEST_H

#include "CTipDialog.h"
#include <QMainWindow>
#include <QTranslator>

namespace Ui {
class CLinguistTest;
}

class CLinguistTest : public QMainWindow
{
    Q_OBJECT

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

private slots:
    // 语言切换槽函数
    void on_switchLanguageBtn_clicked();

private:
    Ui::CLinguistTest   *ui;

    bool                m_languageFlag; // 语言标记值(true为中文,false为英语)

    CTipDialog          *m_dialog;  // 对话框指针

    QTranslator         *m_translator;  // 翻译类对象指针
};

#endif // CLINGUISTTEST_H

CLinguistTest.cpp

#include "CLinguistTest.h"
#include "ui_CLinguistTest.h"
#include "CTipDialog.h"
#include <QApplication>
#include <QPushButton>

CLinguistTest::CLinguistTest(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CLinguistTest)
    , m_languageFlag(true)
    , m_translator(nullptr)
{
    ui->setupUi(this);

    // 创建对话框对象
    m_dialog = new CTipDialog;
    // 连接信号槽,弹出对话框
    connect(ui->showDialogBtn, SIGNAL(clicked()), m_dialog, SLOT(show()));

    // 发出信号,作用为初始化语言
    emit ui->switchLanguageBtn->clicked();
}

CLinguistTest::~CLinguistTest()
{
    delete m_dialog;        // 释放对话框的内存空间
    delete m_translator;    //释放翻译类对象的内存空间
    delete ui;              // 释放ui的内存空 间
}

void CLinguistTest::on_switchLanguageBtn_clicked()
{
    // 当翻译类对象不为空才进入
    if(nullptr != m_translator)
    {
        // 移除上次设置的翻译类对象
        qApp->removeTranslator(m_translator);
        // 释放翻译类对象空间
        delete  m_translator;
    }

    //! 创建时使用默认语言(中文)
    // 获取可执行程序地址,上跳两级目录,再拿到应用程序名,组成项目pro文件所在目录
    QString filePath = qApp->applicationDirPath() + "/../../" +qApp->applicationName();
    // 判断标记值组qm文件地址
    if(m_languageFlag)
    {
        filePath += "/Language/linguist_cn.qm";
    }
    else
    {
        filePath += "/Language/linguist_en.qm";
    }
    // 每次进入将标记值取反
    m_languageFlag = !m_languageFlag;

    // 创建翻译对象
    m_translator = new QTranslator;
    // 加载翻译文件
    m_translator->load(filePath);
    // 安装翻译文件
    qApp->installTranslator(m_translator);

    // 更新翻译
    ui->retranslateUi(this);
    m_dialog->retranslateUi();
}

CLinguistTest.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>CLinguistTest</class>
 <widget class="QMainWindow" name="CLinguistTest">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>290</width>
    <height>280</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Qt语言家测试</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QLabel" name="label">
      <property name="text">
       <string>一个标签</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QCheckBox" name="checkBox">
      <property name="text">
       <string>一个复选框</string>
      </property>
     </widget>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QPushButton" name="switchLanguageBtn">
        <property name="text">
         <string>中文</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="showDialogBtn">
        <property name="text">
         <string>弹出一个对话框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <widget class="QTextEdit" name="textEdit">
      <property name="placeholderText">
       <string>我是文本框</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>290</width>
     <height>23</height>
    </rect>
   </property>
   <widget class="QMenu" name="menumenu">
    <property name="title">
     <string>菜单</string>
    </property>
    <addaction name="action_1"/>
    <addaction name="action_2"/>
   </widget>
   <addaction name="menumenu"/>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="action_1">
   <property name="text">
    <string>选项1</string>
   </property>
  </action>
  <action name="action_2">
   <property name="text">
    <string>选项2</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

CTipDialog.h

#ifndef CTIPDIALOG_H
#define CTIPDIALOG_H

#include <QDialog>

namespace Ui {
class CTipDialog;
}

class CTipDialog : public QDialog
{
    Q_OBJECT

public:
    explicit CTipDialog(QWidget *parent = nullptr);
    ~CTipDialog();
    /**
     * @brief retranslateUi 调用函数更新翻译
     */
    void retranslateUi();

private:
    Ui::CTipDialog *ui;
};

#endif // CTIPDIALOG_H

CTipDialog.cpp

#include "CTipDialog.h"
#include "ui_CTipDialog.h"
#include <QAbstractButton>

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

CTipDialog::~CTipDialog()
{
    delete ui;
}

void CTipDialog::retranslateUi()
{
    // 更新
    ui->retranslateUi(this);
}

CTipDialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>CTipDialog</class>
 <widget class="QDialog" name="CTipDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>346</width>
    <height>187</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>提示对话框</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QLabel" name="label">
     <property name="text">
      <string>一个对话框</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QDialogButtonBox" name="buttonBox">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
     <property name="standardButtons">
      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>CTipDialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>CTipDialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

总结

本文翻译内容都是存在于UI文件中的,UI文件中的文本会自动识别到ts文件中(建议在UI文件中设置窗口标题);要是想代码中操作文本那就需要做其他操作,这就是为什么Qt语言家我打算写多个文章的原因,然后tr函数我个人也需要再搞清楚一点,内容将在(二)中展出。

相关文章

Qt之语言家的简单使用(二)(使用注意事项,含源码+注释)

友情提示——哪里看不懂可私哦,让我们一起互相进步吧
(创作不易,请留下一个免费的赞叭 谢谢 ^o^/)

注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除

  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lw向北.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值