Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

qt - QFileDialog localization

QFileDialog is used in my code like following:

QFileDialog fileDlg;
fileDlg.setFileMode(QFileDialog::AnyFile);
fileDlg.setViewMode(QFileDialog::List);
fileDlg.setNameFilter("Excel Files(*.csv)");
fileDlg.setDefaultSuffix("csv");
fileDlg.setAcceptMode(QFileDialog::AcceptSave);
fileDlg.exec();

Unfortunately, this does not use text from the user's current locale. I would expect the save button to be "保存". Further, when I click on a dialog, the button's text is set to "Open", while it should be "打开" in my locale.

How can I provide localized strings to QFileDialog?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know this has been answered and accepted but the correct way is not to manually translate what Qt already provides but rather to load the translations included in Qt like this:

 /* load the system translations provided by Qt */
 QTranslator qtTranslator;
 qtTranslator.load("qt_" + QLocale::system().name(),
         QLibraryInfo::location(QLibraryInfo::TranslationsPath));
 app.installTranslator(&qtTranslator);

 /* load our custom translations for this application */
 QTranslator myappTranslator;
 myappTranslator.load("myapp_" + QLocale::system().name());
 app.installTranslator(&myappTranslator);

This way Qt will translate what it already knows (like it's own widgets) and will use the custom translations provided with the application for the rest.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...