码迷,mamicode.com
首页 > 其他好文 > 详细

Qt与国际化

时间:2016-06-29 20:35:46      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

默默地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()修饰,增加一个语言切换下拉选项。

 
实现多语言支持,需要导入翻译文件,如何生成翻译文件?
1.在Qt工程文件中加入以下代码,代表生成两个翻译文件。
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与国际化

标签:

原文地址:http://www.cnblogs.com/hebaichuanyeah/p/5628012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!