The creation and use example of QT translation file in VS2010

Source: Internet
Author: User

Operating system: WINXP

QT library:4.8.5

ide:vs2010.

Basic principle

In order to use multiple languages, you have to do two things:

1, put the string you want to translate into TR (), such as QString str=tr ("Legal");

2, at the beginning, load the prepared translation document (. QM).

Use the tools provided by QT to make the translation files and load them in the program.

1, the production of TS files

QT internationalization requires TS files, and we can create TS files in the following ways


2, Lupdate


3. Run QT linguist, open the *.ts file generated in the previous step, translate it after translation lrelease

4. Add the newly generated QM file to the QRC file:

Double-click the QRC file and configure it with the following configuration, mainly for the load () function


Attach the code before the modification:

#include <Qpushbutton>

#include <QApplication>

#include <Qwidget>

#include <QTextCodec>

#include <QObject>

#include <QTranslator>

int main (int argc, char *argv[])

{

Qapplication app (argc, argv);

Qwidget windows;

Windows.setminimumsize (300,200);

Windows.setmaximumsize (300,200);

Qpushbutton Qpushbutton_test1 (Qobject::tr ("Test1"), &windows);

Qpushbutton_test1.setgeometry (20,20,100,30);

Qpushbutton Qpushbutton_test2 (Qobject::tr ("Test2"), &windows);

Qpushbutton_test2.setgeometry (20,60,100,30);

Qpushbutton Qpushbutton_test3 (Qobject::tr ("Test3"), &windows);

Qpushbutton_test3.setgeometry (20,100,100,30);

Windows.show ();

return App.exec ();

}

Modified code:

#include <Qpushbutton>

#include <QApplication>

#include <Qwidget>

#include <QTextCodec>

#include <QObject>

#include <QTranslator>

int main (int argc, char *argv[])

{

Qapplication app (argc, argv);

/* Create a new Translation object and load the local QM file * *

Qtranslator *translator=new qtranslator (0);

Translator->load (":/test_zh.qm");

App.installtranslator (translator);

Qwidget windows;

Windows.setminimumsize (300,200);

Windows.setmaximumsize (300,200);

Qpushbutton Qpushbutton_test1 (Qobject::tr ("Test1"), &windows);

Qpushbutton_test1.setgeometry (20,20,100,30);

Qpushbutton Qpushbutton_test2 (Qobject::tr ("Test2"), &windows);

Qpushbutton_test2.setgeometry (20,60,100,30);

Qpushbutton Qpushbutton_test3 (Qobject::tr ("Test3"), &windows);

Qpushbutton_test3.setgeometry (20,100,100,30);

Windows.show ();

return App.exec ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.