打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How can I dynamically switch between languages in my application
userphoto

2010.04.17

关注

How can I dynamically switch between languages in my application using e.g a QComboBox?

 

Answer:


In order to achieve this, you can create a function, called e.g retranslateUi() that sets the user visible text for your widgets. It is not possible to switch between the languages without having such a function that gets called everytime the language is changed and that returns a translated version of the text by wrapping it with tr().

Now you can connect the triggered(const QString &) signal of your QComboBox to a slot which will load and install the correct translator. Finally you also need to reimplement the changeEvent() to check for a LanguageChange event and call retranslateUi() when it occurs .

See the documentation:

http://doc.trolltech.com/4.3/qevent.html#Type-enum

See the following example for a demonstration:

#include <QtGui>class MainWindow : public QMainWindow{    Q_OBJECTpublic:    MainWindow()    {        translator1 = new QTranslator(this);        translator2 = new QTranslator(this);        menu = new QMenu(this);        menuBar()->addMenu(menu);        fileAction = new QAction(this);        menu->addAction(fileAction);        QComboBox *combo = new QComboBox(this);        setCentralWidget(combo);        combo->addItem("fr");        combo->addItem("en");        combo->addItem("sp");        connect(combo, SIGNAL(activated(const QString &)), this, SLOT(changeMyLanguage(const QString &)));        retranslateUi();    }    void retranslateUi()    {        menu->setTitle(tr("File"));        fileAction->setText(tr("First action"));      }    void changeEvent ( QEvent * event )    {        if (event->type() == QEvent::LanguageChange) {            retranslateUi();        }        QMainWindow::changeEvent(event);    }    public slots:        void changeMyLanguage(const QString & string)        {            if(string == QString("fr")) {                translator1->load("t1_fr", ".");                qApp->installTranslator(translator1);            }            if(string == QString("sp")) {                translator2->load("t1_sp", ".");                qApp->installTranslator(translator2);            }            if(string == QString("en")){                qApp->removeTranslator(translator1);                qApp->removeTranslator(translator2);            }        }private:    QAction *fileAction;    QMenu *menu;    QTranslator *translator1;    QTranslator *translator2;};#include "main.moc"int main(int argc, char **argv){    QApplication a(argc, argv);    MainWindow window;    window.resize(50, 50);    window.show();    return a.exec();}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
支持多种语言的MFC程序
英语专业本科毕业论文参考题目
Qt实现动态切换语言
gitignore排除忽略目录
QT COMBOX
Qt5+VS2015编程实例:下拉列表框QComboBox控件使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服