编程语言
首页 > 编程语言> > Qt的多语言功能模块Linguist的VS样例程序

Qt的多语言功能模块Linguist的VS样例程序

作者:互联网

代码

main.cpp

#include "QtVSMultiLanguage.h"
#include <QtWidgets/QApplication>
#include "Linguist.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    CLinguist* lin = CLinguist::GetLinguistInstance();
    lin->ChangeLanguage(Language::zh_cn);


    QtVSMultiLanguage w;
    w.show();
    return a.exec();
}

 Linguist.h

// Linguist.h
#pragma once
/*
语言家,配置当前系统的语言
*/

#include <QObject>
#include <qtranslator.h>

enum Language
{
    zh_cn,
    en_us,
    undefined
};

class CLinguist :public QObject
{
    Q_OBJECT
private:
    CLinguist();

public:
    // 当前语言 简体中文、英文、未定义
    Language m_CurrentLanguage = Language::undefined;
    // 切换语言
    void ChangeLanguage(Language lan);
    static  CLinguist * GetLinguistInstance();
private:
    static CLinguist * linguist;
    QTranslator * m_trans;
signals:
    // 语言切换信号,通知当前系统语言已经被切换
    void LanguageChaned();
};

Linguist.cpp


#include "Linguist.h"
#include <QtCore/QCoreApplication>

CLinguist * CLinguist::linguist = nullptr;
CLinguist::CLinguist() :m_trans(new QTranslator)
{
}

CLinguist * CLinguist::GetLinguistInstance()
{
    if (nullptr == linguist)
    {
        linguist = new CLinguist;
    }

    return linguist;
}

void CLinguist::ChangeLanguage(Language lan)
{
    if (lan == this->m_CurrentLanguage || lan == Language::undefined) return;
    bool ret;
    switch (lan)
    {
    case zh_cn:
        if (nullptr != m_trans)
        {
            qApp->removeTranslator(m_trans);
        }
        ret = m_trans->load("qtvsmultilanguage_zh.qm");
        if (ret)
        {
            qApp->installTranslator(m_trans);
        }
        break;
    case en_us:
        if (nullptr != m_trans)
        {
            qApp->removeTranslator(m_trans);
        }
        ret = m_trans->load("qtvsmultilanguage_en.qm");
        if (ret)
        {
            qApp->installTranslator(m_trans);
        }
        break;
    default:
        break;
    }
    if (ret)
    {
        this->m_CurrentLanguage = lan;
        // 发出语言被切换的信号
        emit LanguageChaned();
    }
}

 QtVSMultiLanguage.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>QtVSMultiLanguageClass</class>
 <widget class="QMainWindow" name="QtVSMultiLanguageClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>653</width>
    <height>416</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>QtVSMultiLanguage</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QWidget" name="layoutWidget">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>20</y>
      <width>354</width>
      <height>185</height>
     </rect>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QLabel" name="UserNameLabel">
         <property name="text">
          <string>UserName:</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>28</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QTextEdit" name="UserNametextEdit"/>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <widget class="QLabel" name="PassWordLabel">
         <property name="text">
          <string>PassWord:</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer_2">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>28</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QTextEdit" name="PassWordtextEdit"/>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_3">
       <item>
        <widget class="QPushButton" name="SwitchpushButton">
         <property name="text">
          <string>SwitchLanguage</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer_3">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="LoginpushButton">
         <property name="text">
          <string>Login</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer_4">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="CancelpushButton">
         <property name="text">
          <string>Cancel</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </widget>
   <widget class="ForceLinePointWidget" name="PointInfoWidget">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>250</y>
      <width>440</width>
      <height>40</height>
     </rect>
    </property>
    <property name="toolTip">
     <string>The current time</string>
    </property>
    <property name="whatsThis">
     <string>The analog clock widget displays the current time.</string>
    </property>
    <property name="pointName">
     <string>FemurHeadCenter</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>653</width>
     <height>23</height>
    </rect>
   </property>
  </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"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>ForceLinePointWidget</class>
   <extends>QWidget</extends>
   <header>ForceLinePointWidget.h</header>
  </customwidget>
 </customwidgets>
 <resources>
  <include location="QtVSMultiLanguage.qrc"/>
 </resources>
 <connections/>
</ui>

结果

 

标签:CLinguist,include,Qt,Language,样例,ret,VS,trans,Linguist
来源: https://blog.csdn.net/beibeix2015/article/details/111663013