1.建立一个QWidget 工程

-
修改 .pro 文件
添加了两句, 后面可生成两个 xxx.ts
- 编写代码, 对需要转换成国际文字的内容全用
QLabel* pTestLabel;
pTestLabel = new QLabel;
pTestLabel->setText(tr("这里的显示的文字会转换"));
4.生成 xxx.ts文件

我的项目会生成两个 ***.ts
-
***.ts去转换成 ***.qm
打开 linguist,
这东西在/opt/Qt.x.x.x/Qt.x.x.x/gcc_64/bin里面(windos系统,直接在"开始"找qt就有了)
保存生成 ***.qm文件

6.加载 ***.qm
QTranslator pTranslate = new QTranslator(this);
pTranslate->load("执行文件,当前目录下的 ***.qm");
- 安装翻译
qApp->installTranslator(pTranslate);
步骤3.(重新执行一遍,Text的内容就会转到对应 ***.qm指定的文字)
pTestLabel->setText(tr("这里的显示的文字会转换"));
**如果你是有ui的.
直接ui->retranslateUi(this)一键把你的ui里面的文字重新设置一遍,这个操作你也可以自己写个函数封装
Demo:
main.cpp
#include "widget.h"
#include <QApplication>
#include "translatewidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
// TranslateWidget w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QTranslator>
#include "translatewidget.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public:
void CreateControl();
void InitControl();
void ConnectControl();
void UpSetText();
public slots:
void OnChangeLanguage();
void OnTranslate();
private:
QLabel* m_pLoginLabel;
QLabel* m_pPasswordLabel;
QLineEdit* m_pLoginLineEdit;
QLineEdit* m_pPasswordLineEdit;
QPushButton* m_pLoginButton;
QPushButton* m_pCancelButton;
QPushButton* m_pLanguageButton;
QHBoxLayout* m_pLoginLabelHLayout;
QHBoxLayout* m_pPasswordLabelHLayout;
QHBoxLayout* m_pLoginButtonHLayout;
QVBoxLayout* m_pMainVLayout;
TranslateWidget* m_ptranslateWidget;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setGeometry(200,200,400, 200);
CreateControl();
InitControl();
ConnectControl();
}
Widget::~Widget()
{
}
void Widget::CreateControl()
{
m_ptranslateWidget = new TranslateWidget();
m_pLoginLabel = new QLabel();
m_pPasswordLabel = new QLabel();
m_pLoginLineEdit = new QLineEdit();
m_pPasswordLineEdit = new QLineEdit();
m_pLoginButton = new QPushButton();
m_pCancelButton = new QPushButton();
m_pLanguageButton = new QPushButton();
m_pLoginLabelHLayout = new QHBoxLayout();
m_pPasswordLabelHLayout = new QHBoxLayout();
m_pLoginButtonHLayout = new QHBoxLayout();
m_pMainVLayout = new QVBoxLayout(this);
}
void Widget::InitControl()
{
m_ptranslateWidget->setGeometry(0,0,300,80);
m_ptranslateWidget->hide();
m_pLoginLabel->setText(tr("用户: "));
m_pPasswordLabel->setText(tr("密码: "));
m_pLoginButton->setText(tr("登录"));
m_pCancelButton->setText(tr("取消"));
m_pLanguageButton->setText(tr("语言切换"));
m_pLoginLabelHLayout->addWidget(m_pLoginLabel);
m_pLoginLabelHLayout->addWidget(m_pLoginLineEdit);
m_pPasswordLabelHLayout->addWidget(m_pPasswordLabel);
m_pPasswordLabelHLayout->addWidget(m_pPasswordLineEdit);
m_pLoginButtonHLayout->addStretch();
m_pLoginButtonHLayout->addWidget(m_pLoginButton);
m_pLoginButtonHLayout->addWidget(m_pCancelButton);
m_pMainVLayout->addWidget(m_pLanguageButton);
m_pMainVLayout->addLayout(m_pLoginLabelHLayout);
m_pMainVLayout->addLayout(m_pPasswordLabelHLayout);
m_pMainVLayout->addLayout(m_pLoginButtonHLayout);
setLayout(m_pMainVLayout);
}
void Widget::ConnectControl()
{
connect(m_pLanguageButton, SIGNAL(clicked()), this, SLOT(OnChangeLanguage()));
connect(m_ptranslateWidget, SIGNAL(SignalTranslate()), this, SLOT(OnTranslate()));
}
void Widget::UpSetText()
{
m_pLoginLabel->setText(tr("用户: "));
m_pPasswordLabel->setText(tr("密码: "));
m_pLoginButton->setText(tr("登录"));
m_pCancelButton->setText(tr("取消"));
m_pLanguageButton->setText(tr("语言切换"));
qDebug("UpSetText");
}
void Widget::OnChangeLanguage()
{
m_ptranslateWidget->show();
}
void Widget::OnTranslate()
{
UpSetText();
}
tranlatewidget.h
#ifndef TRANSLATEWIDGET_H
#define TRANSLATEWIDGET_H
#include <QWidget>
#include <QApplication>
#include <QTranslator>
#include <QRadioButton>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QLabel>
class TranslateWidget : public QWidget
{
Q_OBJECT
public:
explicit TranslateWidget(QWidget *parent = nullptr);
public:
void CreateControl();
void InitControl();
void ConnectControl();
void UpSetText();
signals:
void SignalTranslate();
public slots:
void OnTranslate(int p_pID);
private:
QTranslator* m_pTranslate;
QLabel* m_pLanguageLabel;
QRadioButton* m_pEnglishButton;
QRadioButton* m_pChineseBUtton;
QButtonGroup* m_pLanguageButtonGroup;
QHBoxLayout* m_pLanguageLabelHLayout;
};
#endif // TRANSLATEWIDGET_H
translatewidget.cpp
#include "translatewidget.h"
TranslateWidget::TranslateWidget(QWidget *parent) : QWidget(parent)
{
// setGeometry(0,0,400, 80);
CreateControl();
InitControl();
ConnectControl();
}
void TranslateWidget::CreateControl()
{
m_pTranslate = new QTranslator(this);
m_pLanguageLabel = new QLabel();
m_pEnglishButton = new QRadioButton();
m_pChineseBUtton = new QRadioButton();
m_pLanguageButtonGroup = new QButtonGroup();
m_pLanguageLabelHLayout = new QHBoxLayout(this);
}
void TranslateWidget::InitControl()
{
m_pLanguageLabel->setText(tr("语言: "));
m_pEnglishButton->setText("Englisg");
m_pChineseBUtton->setText("中文");
m_pChineseBUtton->setChecked(true);
m_pLanguageButtonGroup->addButton(m_pChineseBUtton, 0);
m_pLanguageButtonGroup->addButton(m_pEnglishButton, 1);
m_pLanguageLabelHLayout->addStretch();
m_pLanguageLabelHLayout->addWidget(m_pLanguageLabel);
m_pLanguageLabelHLayout->addWidget(m_pChineseBUtton);
m_pLanguageLabelHLayout->addWidget(m_pEnglishButton);
hide();
setLayout(m_pLanguageLabelHLayout);
}
void TranslateWidget::ConnectControl()
{
connect(m_pLanguageButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(OnTranslate(int)));
}
void TranslateWidget::UpSetText()
{
m_pLanguageLabel->setText(tr("语言: "));
m_pEnglishButton->setText("Englisg");
m_pChineseBUtton->setText("中文");
}
void TranslateWidget::OnTranslate(int p_pID)
{
switch (p_pID) {
case 0:
m_pTranslate->load("Translate_CN.qm");
break;
case 1:
m_pTranslate->load("Translate_EN.qm");
break;
default:
break;
}
qApp->installTranslator(m_pTranslate);
SignalTranslate();
UpSetText();
}
网友评论