Qt与国际化
Posted 随机
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt与国际化相关的知识,希望对你有一定的参考价值。
默默地EEer,原文地址:
http://www.cnblogs.com/hebaichuanyeah/p/5628012.html
Qt国际化可能是我最喜的Qt功能。
国际化是多语言切换支持。
Qt实现国际化需要满足两个条件
1.所有Qstring字符串用tr()修饰。
2.导入翻译文件。
改造上一篇文章线程的例子,添加国际化支持:
http://www.cnblogs.com/hebaichuanyeah/p/5618781.html
把所有字符串全部用tr()修饰,增加一个语言切换下拉选项。
TRANSLATIONS += zh.ts \\ en.ts
利用lupdate命令,或者QtCreator生成.ts的XML文件。
2.翻译修改.ts xml文件。修改格式如下:
3.运行lrelease命令生成二进制翻译文件.qm,并在程序中导入生成的.qm文件,即可自动翻译。
运行效果:
中文界面
英文界面
thread.h
#include "QThread" class Thread : public QThread { Q_OBJECT public: Thread(); // void setMessage(const Qstring &message); void setRun(); void stop(); bool getState(); protected: void run(); private: // QString messageStr; volatile bool enable; signals: void OutMessage(QString message); };
thread.cpp
#include "thread.h" Thread::Thread() { enable = true; } void Thread::run() { while(true) { if(enable) { this->OutMessage(tr("I\'m run : thread 1")); msleep(2100); } } } void Thread::stop() { enable = false; } void Thread::setRun() { enable = true; } bool Thread::getState() { return enable; }
mainwindow.cpp
#include "mainwindow.h" #include <QPainter> MainWindow :: MainWindow() { mainWindow = new QWidget; this->setCentralWidget(mainWindow); backThread = new Thread(); backThread->start(); languageCombo = new QComboBox(); languageCombo->addItem(tr("english")); languageCombo->addItem(tr("chinese")); languageCombo->setStyleSheet("font-size : 15px"); clearButton = new QPushButton(tr("clear")); clearButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); clearButton->setStyleSheet("font-size : 15px"); runStopButton = new QPushButton(tr("stop")); runStopButton->setStyleSheet("font-size : 15px"); messageEdit = new QTextEdit(); messageEdit->setReadOnly(true); messageEdit->setStyleSheet("font-size : 17px"); mainLayout = new QGridLayout; mainLayout->addWidget(messageEdit,0,0,5,5); mainLayout->addWidget(clearButton,1,5,1,1); mainLayout->addWidget(runStopButton,0,5,1,1); mainLayout->addWidget(languageCombo,2,5,1,1); mainWindow->setLayout(mainLayout); QObject::connect(clearButton,SIGNAL(clicked()),this,SLOT(clearMessage())); QObject::connect(runStopButton,SIGNAL(clicked()),this,SLOT(runStopThread())); QObject::connect(languageCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(languageChange(int))); QObject::connect(backThread,SIGNAL(OutMessage(QString)),this,SLOT(updateMessage(QString))); this->setWindowTitle(tr("thread test~")); this->setStyleSheet("font-size : 15px"); this->resize(QSize(350,135)); } MainWindow :: ~MainWindow() { mainWindow->deleteLater(); mainLayout->deleteLater(); messageEdit->deleteLater(); clearButton->deleteLater(); runStopButton->deleteLater(); } void MainWindow::updateMessage(QString message) { messageEdit->append(message); } void MainWindow::clearMessage() { messageEdit->clear(); } void MainWindow::runStopThread() { if(backThread->getState()) { backThread->stop(); runStopButton->setText(tr("run")); } else { backThread->setRun(); runStopButton->setText(tr("stop")); } } void MainWindow::languageChange(int index) { switch(index) { case 0: translator->load(QString("en.qm")); break; case 1: translator->load(QString("zh.qm")); break; } updateShow(); } void MainWindow::setTranslate(QTranslator * tran) { translator = tran; } void MainWindow::updateShow() { if(!backThread->getState()) { runStopButton->setText(tr("run")); } else { runStopButton->setText(tr("stop")); } setWindowTitle(tr("thread test~")); clearButton->setText(tr("clear")); } void MainWindow::keyPressEvent(QKeyEvent * key) { close(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include <QGridLayout> #include <QPushButton> #include <QTextEdit> #include <QDoubleValidator> #include <QComboBox> #include <QTranslator> #include "thread.h" #define PI 3.14592653589 class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); ~MainWindow(); void setTranslate(QTranslator * tran); private: QWidget* mainWindow; QGridLayout * mainLayout; QTextEdit * messageEdit; QPushButton * clearButton; QPushButton * runStopButton; Thread * backThread; QComboBox * languageCombo; QTranslator * translator; void updateShow(); void keyPressEvent(QKeyEvent * key); private slots: void updateMessage(QString message); void clearMessage(); void runStopThread(); void languageChange(int index); }; #endif
main.cpp
#include <QApplication> #include <QFont> #include <QTranslator> #include <QTextCodec> #include "mainwindow.h" int main(int argc ,char* argv[]) { QApplication app(argc,argv); QTranslator translator; translator.load(QString("zh.qm")); app.installTranslator(&translator); MainWindow myWindow; myWindow.setTranslate(&translator); myWindow.show(); return app.exec(); }
以上是关于Qt与国际化的主要内容,如果未能解决你的问题,请参考以下文章
ngx-translate实现国际化:this.translate.use()this.translate.get()this.translate.instant()onLangChange(代码片段
26.Qt Quick QML-RotationAnimationPathAnimationSmoothedAnimationBehaviorPauseAnimationSequential(代码片段
使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)
QObject提供了QMetaObject元类信息(相当于RTTI和反射),信号与连接,父子关系,调试信息,属性,事件,继承关系,窗口类型,线程属性,时间器,对象名称,国际化