8

When I type in a QLineEdit, the virtual keyboard starts with uppercase. Even if I set it to lowercase, it returns to uppercase as soon as I've typed a single character. That is, every time I type a character, the keyboard is reset to uppercase again.

This happens even on a freshly created project (I just put a line edit and run it).

I found a forum thread about the same issue - https://groups.google.com/forum/#!topic/android-qt/QMFZmkACAIA.

I'm using Qt/C++ (not QML).

Edit: Just tested it on a new QML project, the bug is there too. I also found a thread posted about it for QML - https://groups.google.com/forum/#!msg/android-qt/BzGDGoLNtYc/TdtOX9MW3vIJ.

Edit 2: I tested with inputMethodHints(), and the only one that had effect was ImhNoAutoUppercase. But then it still started with an uppercase char, and when pressing the back button (to delete the last character), the keyboard will switch to uppercase again, even if you've typed several letters. After the first letter it switches to lowercase, and if you don't press the back button it works mostly OK.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • I hope this link helps, but I'm not familiar with it. Sorry I couldn't be more help: http://grokbase.com/t/gg/android-qt/12akn19zck/qlineedit-issue – Zac Wimer Sep 06 '14 at 15:32

1 Answers1

2

Edit: A somewhat good workaround is setting ImhNoAutoUppercase, the first letter is still capitalized, but at least the next letters you type will be lowercase.

Original answer: In Android, this would be set using inputType on the EditText in the xml of the layout file for the Activity/Fragment (screen/page you are looking at). Can you access and edit the layout file directly for Android?

Are you using setInputMask() to control the input type? It may be that forcing lowercase (or switching of case conversion) gives the option to use upper or lower case. I guess what is being set in the Android layout xml file is inputType="textCapSentences" or something similar ( https://developer.android.com/training/keyboard-input/style.html ).

UPDATE: You mention that the issue is fixed in 5.4. This looks like the commit that would fix it. I would suggest just implementing the fixes shown here. https://qt.gitorious.org/qt/qtbase/commit/2b3f293d892c5268bd2a07ed17fa9fc5adacbd76

You mention you are happy to edit the Qt source code. I think the error may be in this part of src/org/qtproject/qt5/android/QtActivityDelegate.java

        if ((inputHints & ImhUppercaseOnly) != 0) {
            initialCapsMode |= android.text.TextUtils.CAP_MODE_CHARACTERS;
            inputType |= android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
        } else if ((inputHints & ImhLowercaseOnly) == 0 && (inputHints & ImhNoAutoUppercase) == 0) {
            initialCapsMode |= android.text.TextUtils.CAP_MODE_SENTENCES;
            inputType |= android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; 
        }

Should be changed to:

        if ((inputHints & ImhUppercaseOnly) != 0) {
            initialCapsMode |= android.text.TextUtils.CAP_MODE_CHARACTERS;
            inputType |= android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
        } else if ((inputHints & ImhLowercaseOnly) == 0 && (inputHints & ImhNoAutoUppercase) == 0) {
            //initialCapsMode |= android.text.TextUtils.CAP_MODE_SENTENCES; // not sure what to set here - could try 0 or null if commenting out line doesn't work
            inputType |= android.text.InputType.TYPE_CLASS_TEXT; 
        }

If this doesn't fix it, I'd suggest searching the source code for android.text.InputType.TYPE_TEXT_FLAG_CAP or android.text.TextUtils.CAP_MODE and replacing them by trial and error.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
TTransmit
  • 3,270
  • 2
  • 28
  • 43
  • This happens on a freshly created project with just a line edit. – sashoalm Jan 14 '15 at 14:10
  • Does using setInputMask stop the first character being uppercase? How about setting a QValidator? – TTransmit Jan 14 '15 at 14:15
  • I'm a bit busy right now but I'll try to test your suggestions today or tomorrow. – sashoalm Jan 15 '15 at 10:09
  • I have an AndroidManifest.xml file, which looks like this - http://pastebin.com/CadhLGEi. Is this where I should put the inputType? – sashoalm Jan 16 '15 at 10:15
  • To change the inputType in xml in Android you would normally work in res/layouts or styles.xml. If AndroidManifest.xml is the only xml you can access then that approach won't work. setInputMask and QValidator would be set in C++ for the QLineEdit in question I think. You may just have lucked out as someone I know asked me if I want to take a Qt job yesterday, which gives me a reason to get Qt set up. I can see the problem in the Qt Android source code. I'll need to get Qt set up to test what I think is the solution. – TTransmit Jan 17 '15 at 11:26
  • I just got a chance to try it on 5.4 and saw that it was fixed but I updated my answer which can hopefully help you. – TTransmit Jan 17 '15 at 18:50
  • About setInputMask, wouldn't that approach limit what keys can be entered? I'd rather not limit them, I still want uppercase letters to be possible to enter. I'll try your new suggestions now. – sashoalm Jan 18 '15 at 11:04
  • I tried setInputMask. It wasn't clear but it may be operating separately to and after the Android EditText and keyboard so it may not work for what you are wanting. – TTransmit Jan 18 '15 at 11:17
  • There is no QtActivityDelegate.java anywhere in Qt's install dir or in the build directories. This is strange, maybe it's precompiled and put inside the .jar files already? – sashoalm Jan 18 '15 at 15:25
  • I found QtActivityDelegate.java, but it's in the src/ directory, and it seems it's already compiled inside the .jar files that Qt uses. If I try to put a modified QtActivityDelegate.java in my $ANDROID_PACKAGE_SOURCE_DIR, I get this error - http://pastebin.com/D9BS3XUu – sashoalm Jan 18 '15 at 16:11
  • The error suggests the system is unhappy with there being two versions of QtActivityDelegate.java: your version and the version in the jar. This would mean creating your own version of the jar (or importing it as a project in Eclipse or Android Studio). I'm not sure of the process for Qt is they may have a process for doing this using a build tool for contributors. They have a mailing list ( http://lists.qt-project.org/mailman/listinfo/android-development ), someone there may be able to help you - including with fixing the issue you are having with capitalisation. – TTransmit Jan 18 '15 at 18:40
  • Well, thanks for the help, that's pretty much it, I'll stick with the ImhNoAutoUppercase workaround, it's not too bad with it. I don't think there's a way to fix this issue without some serious recompiling of Qt itself. It's too bad that Qt 5.4 has this annoying "feature" that prevents me from using it - https://stackoverflow.com/questions/27463772/qguiapplication-stops-the-event-loop-when-phone-is-locked-when-compiled-with-qt – sashoalm Jan 18 '15 at 18:57
  • Updated link to the commit (given that Qt no longer uses gitorious) : https://code.qt.io/cgit/qt/qtbase.git/commit/?id=2b3f293d892c5268bd2a07ed17fa9fc5adacbd76 – David Faure Mar 01 '20 at 23:30
  • With Qt 5.9, ImhNoAutoUppercase works as intended, it all remains lowercase. Everything uppercase is still a weird default behaviour, but at least the setting works. – David Faure Mar 01 '20 at 23:53