简体   繁体   English

如何基于QTranslator更改QPushButton的图标字符串

[英]How to change a QPushButton's Icon String based on QTranslator

I have an application with buttons that have icons set for the various click states, no actual QPushButton text is set or displayed; 我有一个带有按钮的应用程序,该按钮的图标设置为各种单击状态,没有设置或显示任何实际的QPushButton文本; 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. 我正在寻找一种使用QTranslator或QAction类基于所选的主要语言自动选择要使用的按钮的本地化版本的方法。

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. 我已按照QT Wiki:多语言应用程序上的说明进行操作,但是按钮未显示在生成的翻译(.ts)文件中。 Having a switch case for each instance of its use based on the language is not ideal. 根据语言为每个使用实例设置一个开关盒是不理想的。

To use different icons depending on the user's locale, you can add qresource section with lang attribute in your .qrc file. 要使用这取决于用户的区域设置不同的图标,你可以添加qresourcelang在.qrc文件属性。

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM