Re-Translating QStringLists
Unsolved
General and Desktop
-
In my code I have some string lists like e.g.:
QStringList OUTPUTLIST_FILTERS({ QCoreApplication::translate("StackingDlg", "File List (*.dssfilelist)", "IDS_LISTFILTER_OUTPUT"), QCoreApplication::translate("StackingDlg", "File List (*.txt)", "IDS_LISTFILTER_OUTPUT"), QCoreApplication::translate("StackingDlg", "All Files (*)", "IDS_LISTFILTER_OUTPUT") });
which are not currently members of the StackingDlg class, and when the user changes to a different language they need to be updated.
I have:
void StackingDlg::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { ui->retranslateUi(this); } Inherited::changeEvent(event); }
How should I change that to update those string lists?
Thanks
David -
Hi @Perdrix,
As
QCoreApplication::translate()
is a static function, those strings will not automatically re-translate, so you'll need to trigger re-translate yourself. Perhaps something like:const QStringList OUTPUTLIST_FILTER_SOURCES({ QStringLiteral("File List (*.dssfilelist)"), QStringLiteral("File List (*.txt)"), QStringLiteral("All Files (*)") }); QStringList OUTPUTLIST_FILTERS{}; // call this somewhere early to initialise. void translateFilterStrings() { OUTPUTLIST_FILTERS.clear(); for (const QString &filter: OUTPUTLIST_FILTER_SOURCES) { OUTPUTLIST_FILTERS.append(QCoreApplication::translate("StackingDlg", filter, "IDS_LISTFILTER_OUTPUT"); } Q_ASSERT(OUTPUTLIST_FILTERS.size() == OUTPUTLIST_FILTER_SOURCES.size()); } void StackingDlg::changeEvent(QEvent* event) { translateFilterStrings(); ... Inherited::changeEvent(event); }
Of course, once you're managing those translations within your
StackingDlg
, you might as well useStackingDlg::tr()
instead ofQCoreApplication::translate()
too.Cheers.