Source code for Qt dynamic switching Language

Source: Internet
Author: User

This article describesQt dynamic SwitchingLanguage. When writing an international program, in many cases, it is selected during the system startup process.LanguageAnd then load the corresponding locationLanguageTranslate files to implementLanguage.

However, some users need to perform dynamic operations.Language Switching(Dynamic Language Switching), that is, after the program master> body is running, you need to change the Language options. The user does not wantLanguage SwitchingThe program is restarted or the window is closed.

QtThe international design provides this possibility. To achieve this kind of dynamicSwitch, You just need to use a little trick in the program, instead of callingQtThe tr code of the translation statement is concentrated in a function unrelated to form changes, such as retranslateStrings. ChangingLanguageYou can call this function.

The following is a demo program attached, which is only used to prove the function. The Code rigor is not considered too much.

 
 
  1. //dialog.cpp  
  2.  
  3. #include   
  4. #include "dialog.h"  
  5. Dialog::Dialog(QWidget *parent)  
  6. : QDialog(parent)  
  7. {  
  8.     label=new QLabel(this);  
  9.     okButton=new QPushButton(this);  
  10.     connect(okButton,SIGNAL(clicked()),this,SLOT(switchlang()));  
  11.     flag=0;  
  12.     retranslateStrings();  
  13.  
  14.     QVBoxLayout *mainLayout = new QVBoxLayout;  
  15.     mainLayout->addWidget(label);  
  16.     mainLayout->addWidget(okButton);  
  17.     setLayout(mainLayout);  
  18. }  
  19.  
  20. void Dialog::retranslateStrings()  
  21. {  
  22.     label->setText(tr("Name:"));  
  23.     okButton->setText(tr("Switch Language"));  
  24.     setWindowTitle(tr("Switch Language ..."));  
  25. }  
  26.  
  27. void Dialog::switchlang()  
  28. {  
  29.     QTranslator translator;  
  30.     if(0==flag){  
  31.         translator.load("switchlang_zh");  
  32.         flag=1;  
  33.     }else  
  34.     {  
  35.         flag=0;  
  36.     }  
  37.     qApp->installTranslator(&translator);  
  38.     retranslateStrings();  
  39. }  
  40. //dialog.h  
  41. #ifndef DIALOG_H  
  42. #define DIALOG_H  
  43. #include   
  44. class QLabel;  
  45. class QPushButton;  
  46.  
  47. //! [0]  
  48. class Dialog : public QDialog  
  49. {  
  50. Q_OBJECT  
  51. public:  
  52.     Dialog(QWidget *parent = 0);  
  53. private:  
  54.     QLabel *label;  
  55.     QPushButton *okButton;  
  56.     int flag;  
  57. private slots:  
  58.     void retranslateStrings();  
  59.     void switchlang();  
  60. };  
  61. //! [0]  
  62. #endif  
  63. //main.cpp  
  64. #include   
  65. #include "dialog.h"  
  66. int main(int argc, char *argv[])  
  67. {  
  68.     QApplication app(argc, argv);  
  69.     Dialog dialog;  
  70.     dialog.show();  
  71.     return app.exec();  
  72. }  
  73. //switchlang.pro  
  74. TEMPLATE = app 
  75. TARGET =  
  76. DEPENDPATH += .  
  77. INCLUDEPATH += .  
  78. # Input  
  79. HEADERS += dialog.h  
  80. SOURCES += dialog.cpp main.cpp  
  81. TRANSLATIONS+=switchlang_zh.ts 

Summary:

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.