QApplication 的 installTranslator 方法不更改语言
我重新定义了 QApplication 类并添加了 switchLanguage 插槽,我想在按 but1 时切换语言。 main_ru.qm 和 main_de.qm 正常,100% 工作。但是当我按下按钮时语言不会改变
// main.cpp
#include <QtGui>
#include "qmyapp.h"
int main( int argc, char** argv )
{
QMyApp app( argc, argv );
QTranslator translator;
// задаём файл перевода и директорию
translator.load( "main_ru.qm", "." );
app.installTranslator( &translator );
QWidget wgt;
QLabel *lbl = new QLabel( QObject::tr( "Hello" ) );
QPushButton *but1 = new QPushButton( "1" );
QVBoxLayout *vbox = new QVBoxLayout;
QObject::connect( but1, SIGNAL( clicked() ), &app, SLOT( switchLanguage() ) );
vbox->addWidget( lbl );
vbox->addWidget( but1 );
wgt.setLayout( vbox );
wgt.show();
return app.exec();
}
// qmyapp.h
#ifndef QMYAPP_H
#define QMYAPP_H
#include <QApplication>
#include <QtGui>
class QMyApp : public QApplication
{
Q_OBJECT
public:
QMyApp(int &argc, char **argv ) : QApplication ( argc, argv ) {}
public slots:
void switchLanguage( )
{
QTranslator* translator = new QTranslator();
translator->load( "main_de.qm", "." );
this->installTranslator( translator );
}
};
#endif // QMYAPP_H
I redefined QApplication class and add switchLanguage slot and i want to switch language when i press but1. main_ru.qm and main_de.qm is normal, 100% working. But language don't change when i press button
// main.cpp
#include <QtGui>
#include "qmyapp.h"
int main( int argc, char** argv )
{
QMyApp app( argc, argv );
QTranslator translator;
// задаём файл перевода и директорию
translator.load( "main_ru.qm", "." );
app.installTranslator( &translator );
QWidget wgt;
QLabel *lbl = new QLabel( QObject::tr( "Hello" ) );
QPushButton *but1 = new QPushButton( "1" );
QVBoxLayout *vbox = new QVBoxLayout;
QObject::connect( but1, SIGNAL( clicked() ), &app, SLOT( switchLanguage() ) );
vbox->addWidget( lbl );
vbox->addWidget( but1 );
wgt.setLayout( vbox );
wgt.show();
return app.exec();
}
// qmyapp.h
#ifndef QMYAPP_H
#define QMYAPP_H
#include <QApplication>
#include <QtGui>
class QMyApp : public QApplication
{
Q_OBJECT
public:
QMyApp(int &argc, char **argv ) : QApplication ( argc, argv ) {}
public slots:
void switchLanguage( )
{
QTranslator* translator = new QTranslator();
translator->load( "main_de.qm", "." );
this->installTranslator( translator );
}
};
#endif // QMYAPP_H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
安装新翻译后需要再次调用translate。因此,在调用
switchLangauge()
后,您需要在之前使用过的任何上下文中再次调用QObject::tr()
。You need to call translate again after installing new translations. So after you call
switchLangauge()
you need to callQObject::tr()
once again in whatever context it was used before.