0

I have an application with buttons that have icons set for the various click states, no actual QPushButton text is set or displayed; it is all contained in the icon.

These icon files include text that requires translation, and have already been generated for each language and state. I am looking for a way to use the QTranslator or QAction class to automatically select which localized version of the button to use based on the selected main language.

For example:

BTN_Media_Browse->setIcon(QIcon(QPixmap(tr(":/Images/BTN_Media_Browse_Unpressed.png"))));

I have followed the instructions on the QT Wiki: Multi Language Application but the buttons do not show up in the generated translation (.ts) files. Having a switch case for each instance of its use based on the language is not ideal.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Nfettinger
  • 71
  • 7
  • Do you want to translate the entire text or just a part of it? – eyllanesc Aug 16 '17 at 20:32
  • I want to translate the location text. For example: `tr(":/Images/BTN_Media_Browse_Unpressed.png")` becomes `":/Images/BTN_Media_Browse_Unpressed_en.png"` or `":/Images/BTN_Media_Browse_Unpressed_de.png"` Edited for clarity. – Nfettinger Aug 16 '17 at 21:18
  • You could share your test code through github, drive, dropbox or similar. – eyllanesc Aug 16 '17 at 21:20
  • You can use .qrc file. See **The Qt Resource System** topic. For example: ` cut.jpg cut_fr.jpg ` – olya Aug 17 '17 at 05:32
  • This worked wonderfully. Please provide this as an answer so I can accept it. It might be worth adding a bit more clarification or a link for others who were confused like me. – Nfettinger Aug 18 '17 at 13:53

1 Answers1

0

To use different icons depending on the user's locale, you can add qresource section with lang attribute in your .qrc file.

For example:

<qresource>
    ...
    <file alias="Images/BTN_Media_Browse_Unpressed.png">Images/BTN_Media_Browse_Unpressed_en.png</file>
    ...
</qresource>
<qresource lang="de">
    ...
    <file alias="Images/BTN_Media_Browse_Unpressed.png">Images/BTN_Media_Browse_Unpressed_de.png</file>
    ...
</qresource>

Then you can use it with code like this:

BTN_Media_Browse->setIcon(QIcon(":/Images/BTN_Media_Browse_Unpressed.png"));
olya
  • 312
  • 1
  • 4