PDA

View Full Version : Shortcut depending on the language



ddonate
22nd December 2016, 11:32
Hi,

I have a QT application with a large text, and I would like to show a 'Search' box when user clicks the system shortcut for this functionality. But this shortcuts depends on the current system language. I mean for example, in Windows + English, Search = "CRTL + F", but in Windows + Spanish, Search = "CRTL + B".

How can I manage this issue and detect the good shorcut depending on the language? I would like to open my 'Search' box when the user presses CRTL+F (if system is in English), or when user presses CTRL+B (if system is in Spanish)

Thanks in advance,
Diego

^NyAw^
22nd December 2016, 13:35
Hi,

You can use QLocale for this:


QLocale qLocale = QLocale::system(); //Get the system locale
QLocale::Country qCountry = qLocale.country();
QLocale::Language qLanguage = qLocale.country();

//Here you can play with "qCountry" and "qLanguage" variables to change the shortcut of your QAction to the correct key combination. Think on that in you can have multiple languages into the same country(Spain have Spanish,Catalan,Basque,Galician,...)
qMyAction.setShortcut(...);

anda_skoa
22nd December 2016, 13:58
QKeySequence, which is usually used to define such keyboard combinations, e.g. on a QAction or a QShortcut, supports translations.

I.e. you can pass the string like "CTRL+F" through the Qt translation funcion tr() and it will be part of what a translator can adapt.

In that special case of search/find, there is also a predefined standard key, see "Find" in http://doc.qt.io/qt-5/qkeysequence.html#StandardKey-enum

Cheers,
_

ddonate
22nd December 2016, 16:47
Thanks a lot for the response. I forgot to say (sorry!!!) that my application has QML, I don't use QWidgets for GUI. I have tried:

Shortcut {
sequence: StandardKey.Find
context: Qt.ApplicationShortcut
onActivated: console.log("find...")
}

But 'onActivated' is only called with CTRL+F (not depending on the language)

anda_skoa
22nd December 2016, 18:50
Ok, strange.

Have you verified that the correct translation for Qt is loaded?

If it is and still does not work, try the translatable string approach


sequence: qsTr("CTRL+F")


Cheers,
_