Translation and QStringList
-
Helllo
I am currently trying my hand at a small program with QT and have now encountered the following problem:
I have created a translation file and specified what I want to translate in the code using "tr("")". In QT Linguist I can also translate these entries successfully.
Loading the translation also works perfectly.
However, I have a QStringList to fill a QTableWidget with the header information. There I have also specified the "tr("")", which is also displayed in QLinguist.
However, no translation is displayed in the window even after recompiling.
To rule out a direct error, I simply translated a QLabel and set it directly in the code like this: ui.qLabel->setText(tr("Hello"));
I then "translated" this in QLinguist using lrelease and created the *qm file again and tried it out. This translation is displayed to me.
I have the following as QStringList:QStringList headerLabels; headerLabels << tr("Order ID") << tr("Order Number") << tr("Line or machine") << tr("Order priority") << tr("Storage location") << tr("Article name") << tr("Article number") << tr("Article Quantity"); headerLabels << tr("Ordered first name") << tr("Orderer last name") << tr("More information") << tr("Cost unit") << tr("Order date");
and set it with
ui.QTableWidghet->setHorizontalHeaderLabels(headerLabels);
What else could be the reason that the translation is not displayed like this?
Many thanks for the answers.I Use QT 6.6.1
Sevi
-
Helllo
I am currently trying my hand at a small program with QT and have now encountered the following problem:
I have created a translation file and specified what I want to translate in the code using "tr("")". In QT Linguist I can also translate these entries successfully.
Loading the translation also works perfectly.
However, I have a QStringList to fill a QTableWidget with the header information. There I have also specified the "tr("")", which is also displayed in QLinguist.
However, no translation is displayed in the window even after recompiling.
To rule out a direct error, I simply translated a QLabel and set it directly in the code like this: ui.qLabel->setText(tr("Hello"));
I then "translated" this in QLinguist using lrelease and created the *qm file again and tried it out. This translation is displayed to me.
I have the following as QStringList:QStringList headerLabels; headerLabels << tr("Order ID") << tr("Order Number") << tr("Line or machine") << tr("Order priority") << tr("Storage location") << tr("Article name") << tr("Article number") << tr("Article Quantity"); headerLabels << tr("Ordered first name") << tr("Orderer last name") << tr("More information") << tr("Cost unit") << tr("Order date");
and set it with
ui.QTableWidghet->setHorizontalHeaderLabels(headerLabels);
What else could be the reason that the translation is not displayed like this?
Many thanks for the answers.I Use QT 6.6.1
Sevi
@Sevi
I know nothing about "Linguist" or ".qm" files. But if you are suggesting that in, say,QString s = tr("..."); QStringList sl << tr("...");
the
tr()
somehow works in the first statement but not in theQStringList
statement that is simply not possible.Start with a
qDebug() << headerLabels
to confirm what is in there has been translated before you do anything with it. -
@Sevi
I know nothing about "Linguist" or ".qm" files. But if you are suggesting that in, say,QString s = tr("..."); QStringList sl << tr("...");
the
tr()
somehow works in the first statement but not in theQStringList
statement that is simply not possible.Start with a
qDebug() << headerLabels
to confirm what is in there has been translated before you do anything with it.... and then check that you don't override your header labels later on somewhere else.
-
... and then check that you don't override your header labels later on somewhere else.
@Christian-Ehrlicher ...which is the most likely situation, if one had to guess, assuming the OP sees them revert to something...
-
@Sevi
I know nothing about "Linguist" or ".qm" files. But if you are suggesting that in, say,QString s = tr("..."); QStringList sl << tr("...");
the
tr()
somehow works in the first statement but not in theQStringList
statement that is simply not possible.Start with a
qDebug() << headerLabels
to confirm what is in there has been translated before you do anything with it.I think I have found the main problem. I called the translation file once in the class header and thought that would be enough, apparently not. I now call it in the main function and the QStringList is now translated and displayed correctly.
However, the error seems to persist somehow.
I have the following in it now:ui.lb_openCourierOrders->setText(tr("Hello Test")); qDebug() << tr("Hello Test");
I get this as the output of qDebug():
"This is a bigger test or something"
so it seems to be translated, but this change is not displayed.
Could it be that I have chosen the wrong setting for the label or something? -
I think I have found the main problem. I called the translation file once in the class header and thought that would be enough, apparently not. I now call it in the main function and the QStringList is now translated and displayed correctly.
However, the error seems to persist somehow.
I have the following in it now:ui.lb_openCourierOrders->setText(tr("Hello Test")); qDebug() << tr("Hello Test");
I get this as the output of qDebug():
"This is a bigger test or something"
so it seems to be translated, but this change is not displayed.
Could it be that I have chosen the wrong setting for the label or something?@Sevi
No that is not possible.tr()
is not magic, it's just a function call which takes a string and returns a new, translated string. And if you calllabel->setText(tr(....))
then it will set the label's text to the translated string. Again suggest you look at your code carefully, sounds like you do stuff after a translated string has been set which changes e.g. what is in the label. -
@Sevi
No that is not possible.tr()
is not magic, it's just a function call which takes a string and returns a new, translated string. And if you calllabel->setText(tr(....))
then it will set the label's text to the translated string. Again suggest you look at your code carefully, sounds like you do stuff after a translated string has been set which changes e.g. what is in the label.@JonB
I realize that. Hence the question of whether you could possibly "adjust" something in the QWidget or QLabel settings so that it no longer works.
I created a new window and used a label there and everything works there...so the problem with the other window must be with something else that I still have to find.Thanks for the quick answers though.
-
@JonB
I realize that. Hence the question of whether you could possibly "adjust" something in the QWidget or QLabel settings so that it no longer works.
I created a new window and used a label there and everything works there...so the problem with the other window must be with something else that I still have to find.Thanks for the quick answers though.
@Sevi said in Translation and QStringList:
whether you could possibly "adjust" something in the QWidget or QLabel settings so that it no longer works.
No, or else I would have said so. And they don't know about translating anyway, that's the point of passing a
tr()
ed string, that is where the translation takes place. You are then unconditionally setting its text to the string passed.Again, assuming the
tr()
ed string you are passing is correct, either you are callingui.lb_openCourierOrders->setText()
again later or you are recreating the original widget or something.Unfortunately, changing the text of a
QLabel
does not emit any signal, which could have helped spot where else this might be happening from. You'll have to look through your code, or use a debugger. Start by searching all files forlb_openCourierOrders->setText(
! -
Hi,
Where and when exactly are you setting up your translator ?