[solved] Translation on the fly crashes
-
Hello everyone.
I am developping an application in French and need to translate it into English aswell.
It was working fine till yesterday when I had a new idea about the way to change the language. At first I was using a menu bar to translate the application, then I wanted to do it through a new widget.The former case worked perfectly but the latter one failed so I decided to remove it. The problem is... translation through the menu bar doesn't work anymore now.
I have a MainWindow composed of a mainWidget and this mainwidget is used to display "other" widgets by using buttons.
Anyway, here is how I implemented the translation :@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
translator = new QTranslator(0);
QApplication::instance()->installTranslator(translator);connect(item1, SIGNAL(triggered()), this, (SLOT(changeToFrench()))); connect(item2, SIGNAL(triggered()), this, (SLOT(changeToEnglish())));@
@void MainWindow::changeToFrench()
{
if(!translator->isEmpty())
QApplication::instance()->removeTranslator(translator);
}void MainWindow::changeToEnglish()
{
translator->load("dcl_en",QApplication::applicationDirPath());
}@@void MainWindow::changeEvent(QEvent *e)
{
if(e->type() == QEvent::LanguageChange)
{
ui->retranslateUi(this);
retranslateUi();
}
QMainWindow::changeEvent(e);
}void MainWindow::retranslateUi()
{
menu1->setTitle(tr("Langue"));
menu2->setTitle(tr("A propos..."));
item4->setText(tr("Paramètres système"));
item1->setText("Français");
item2->setText("English");
item3->setText(tr("A propos..."));
}@The translation works fine when I am in the main menu but if I wanna change the language when I am within an "other widget" (a widget called ui_identity for instance) it crashes with exit code -1073741819.
Here is the code in "ui_identity" :
@void ui_identity::changeEvent(QEvent *e)
{
if(e->type() == QEvent::LanguageChange)
{
ui->retranslateUi(this);
}
}@Any ideas ?
-
Hi,
What does a run trough the debugger tells you ?
Are you sure all the pointers your are using are valid ?
-
Are you sure translator is valid ?
-
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
translator = new QTranslator(0);@I can translate the whole program while being on MainWindow. As soon as I go into a widget, I can no longer translate the program. It crashes :s
-
That's in the MainWindow, how do you do it in your widget ?
-
I emit a signal from the widget which is then caught by the main window :
WIDGET :
@void ui_sys_settings::languageToEn()
{
emit toEnglish();
}void ui_sys_settings::languageToFr()
{
emit toFrench();
}@MAINWINDOW :
@connect(sysSettings,SIGNAL(toEnglish()),this,SLOT(changeToEnglish()));
connect(sysSettings,SIGNAL(toFrench()),this,SLOT(changeToFrench()));void MainWindow::changeToFrench()
{
if(!translator->isEmpty())
qApp->removeTranslator(translator);
}void MainWindow::changeToEnglish()
{
if(!translator->isEmpty())
qApp->removeTranslator(translator);translator->load("dcl_en",QApplication::applicationDirPath()); qApp->installTranslator(translator);
}@
-
Are you sure you need to re-install the translator ?
-
Strange...
Could you post/share a minimal compilable example that shows the problem ? -
Rather the minimal code that reproduce the crash
-
Indeed, but with only pieces of code, I can't try to reproduce the crash.
-
You can also use a service like "pastbin":http://pastebin.com/