I create a small test interface consisting of two buttons, a table and a combobox to change the language. All without Designer.
botonRedo = new QPushButton(QIcon("../Prueba/iconos/rehacer.png"),tr("Rehacer"));
botonUndo = new QPushButton(QIcon("../Prueba/iconos/deshacer.png"),tr("Deshacer"));
comboLenguajes= new QComboBox;
QStringList listaIdiomas;
listaIdiomas<<tr("Español")<<tr("Ingles");
comboLenguajes->addItems(listaIdiomas);
Object::connect(comboLenguajes,SIGNAL(currentIndexChanged(int)),this,SLOT(CambiarIdioma(int)));
The test widget has a table with this header:
cabecera<<tr("Amigo")<<tr("Ciudad")<<tr("Color")<<tr("Sexo");
The function (slot) that the combobox calls is this:
void Widget::CambiarIdioma(int indice)
{
QString filename = "/home/Otras/programacion/Qt/MVC/Prueba/Prueba/prueba_en";
if (indice==1)
{
listaTranslator.load(filename);
qApp->installTranslator(&listaTranslator);
}
else
{
qApp->removeTranslator(&listaTranslator);
}
}
And finally I have created this event:
void Widget::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange)
{
CambiarTraduccion();
}
QWidget::changeEvent(event);
}
Calling this function:
void Widget::CambiarTraduccion()
{
botonRedo->setText(tr("redo"));
botonUndo->setText(tr("undo"));
QStringList nombresIngles;
nombresIngles<<tr("Name")<<tr("City")<<tr("Colour")<<tr("Sex");
modelo->TraducirCabecera(nombresIngles);
}
The translation is created on the other hand with QLinguist, which has created the corresponding * .qm file.
The fact is that when I change the language first (Spanish) through e combobox, the thing works and everything happens in English, but when I put it back in Spanish it does not work. I also believe that I must have some conceptual problem, because I do not know why I have to translate and include the translation file, when I then have to create an ad hoc function to include the translations. Something is missing me.