Solved Changing QtVirtualKeyboard Language Without Keyboard Button
-
Hi all,
Apologies if this is in the wrong forum but, I'm working on bringing in the QtVirtualKeyboard into my project. I'm using Qt version 5.8 and I'm trying to change the QtVirtualKeyboard language without using the built in change language button on the keyboard. I'm planning on having the project's language settings be configured in a settings menu that'll change text labels and also the keyboard.I've got to a point where I have brought in the QtVirtualKeyboard and have it pop up whenever I click on a QTextEdit field, however I'm not quite sure how to get a reference to the keyboard itself. I've implemented a function within the Keyboard.qml file that takes in an index and sets the keyboard locale to that of the index provided. If I could get a reference to the keyboard, I could call that function and pass in the desired index.
I've tried looking through the documentation to figure out how to do this but couldn't find anything. Is there a way to get a reference to the VirtualKeyboard to call my function or is there another way to do this that I'm not aware of?
Thank you,
Ryan=== Update ===
Solution is posted below -
Hi,
I haven't used it yet but isn't VirtualKeyboardSettings::activeLocales what you are looking for ?
-
Thank you for the response,
No, I'm trying to change the keyboard language from a .ui file where I have a dropdown to select what language I want the application to use (not only the keyboard). When the confirm button is clicked I want to be able to update the keyboard to change the language to the one I selected.
I've been unable to figure out how to directly reference the Keyboard object that gets brought up whenever I click on a text field since the way it gets loaded into the system is via 'qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));'.
I am figuring it must be loaded somewhere within the application and once I can reference it I can just grab it as a QObject* and call 'InvokeMethod(object, "myLanguageChangingFunction"), I'm just not sure where it is. -
Do you mean QGuiApplication::inputMethod ?
-
@SGaist That ended up sending me down the right path.
So it looks like you can set the langauge of the system on application startup if you use
QLocale::setDefault(<desiredLanguage>)
in which case the QtVirtualKeyboard defaults to the set language. This must be done before the QApplication initializesQApplication a(argc, argv)
. However, this didn't quite solve the problem of if I wanted to change the Language after the system was already up and running.So I figured I could use
QGuiApplication::inputMethod.setProperty("locale", <desiredLanguage>)
to change it after the fact. This doesn't work as it returns false as it couldn't (re)define the property. If there's a way to do it with this method I'd be curious to know as I think it'd be a better solution.I then figured out I could grab the individual text boxes and set their locale to the desired langaugeui->textEdit->setProperty("locale", <desiredLanguage>)
and this worked. The VirtualKeyboard loaded the correct language.Update:
My previous solution was wrong, I made a mistake and I wasn't really getting the language to change this way.
In the VirtualKeyboard documentation it is mentioned that the Keyboard is running as a top level window application in which to me meant there must be a way to access it directly.The way to get the top windows is to call
QGuiApplication::topLevelWindows()
. If you useqDebug()
to print the windows out you should see theQtVirtualKeyboard::InputView(<address>)
in the list.From here you can grab the InputView and its root object and then look for the "keyboard" child. Once you have the reference to the keyboard you can simply call the
changeLanguageInput
function to cycle through to the next language, or call your own function to set the language index to your desired language.Here's the sample code for this
QQuickView *topWindow = static_cast<QQuickView*>(QGuiApplication::topLevelWindows().last()); qDebug() << "Root Object\t" << topWindow->rootObject(); qDebug() << "Root Object Children\t" << topWindow->rootObject()->children(); QObject* keyboard = topWindow->rootObject()->findChild<QObject*>("keyboard"); QMetaObject::invokeMethod(keyboard, "changeInputLanguage", Q_ARG(QVariant, true));
Hopefully this helps anyone that was running into the same issues as me.
Solved.