Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Changing QtVirtualKeyboard Language Without Keyboard Button

Changing QtVirtualKeyboard Language Without Keyboard Button

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rgilio
    wrote on last edited by rgilio
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I haven't used it yet but isn't VirtualKeyboardSettings::activeLocales what you are looking for ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rgilio
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean QGuiApplication::inputMethod ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R Offline
            R Offline
            rgilio
            wrote on last edited by rgilio
            #5

            @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 initializes QApplication 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 langauge ui->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 use qDebug() to print the windows out you should see the QtVirtualKeyboard::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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved