TextField focus behaves strangely
-
Hello all, I have a question about the focus handling of TextFields (in QtQuick 2.14).
With the activeFocusOnPress-property set to true I observe the following behaviour:
When I click into the TextField field it will always lose focus and then immediately gain focus again. I was expecting that it only gains focus if it is not already focused. I observed this behaviour by adding a counter and then putting this into the onActiveFocusChanged-call:
onActiveFocusChanged: { if (activeFocus){ countFocusChanged++ text = countFocusChanged }
Now, when I click the TextField again and again, I can see this counter increase. I tested something similar to make shure that the onActiveFocusChanged gets called each time with activeFocus being false before it switches back to true.
Also, if I highligh text, the moment I release the Mouse the TextField loses focus, the highlighting is lost and the keyboard (that is open while TextField is focused) is closed. With activeFocusOnPress set to false, Text Highlighting works as expected but I can obiously not click outside of the TextField to remove focus (which is something that I need). I assume that the release of the mouse-Button triggers the same behaviour as described above as the keyboard-closing indicates the loss of the focus in the TextField.
Is this expected behavior? If so, is there a way to focus the TextField only if it does not allready have focus and make it loose focus when I click outside of it without using a MouseArea to control the focus-property?
I would be thankful for any hints and pointers you can give me.
-
Hello all, I have a question about the focus handling of TextFields (in QtQuick 2.14).
With the activeFocusOnPress-property set to true I observe the following behaviour:
When I click into the TextField field it will always lose focus and then immediately gain focus again. I was expecting that it only gains focus if it is not already focused. I observed this behaviour by adding a counter and then putting this into the onActiveFocusChanged-call:
onActiveFocusChanged: { if (activeFocus){ countFocusChanged++ text = countFocusChanged }
Now, when I click the TextField again and again, I can see this counter increase. I tested something similar to make shure that the onActiveFocusChanged gets called each time with activeFocus being false before it switches back to true.
Also, if I highligh text, the moment I release the Mouse the TextField loses focus, the highlighting is lost and the keyboard (that is open while TextField is focused) is closed. With activeFocusOnPress set to false, Text Highlighting works as expected but I can obiously not click outside of the TextField to remove focus (which is something that I need). I assume that the release of the mouse-Button triggers the same behaviour as described above as the keyboard-closing indicates the loss of the focus in the TextField.
Is this expected behavior? If so, is there a way to focus the TextField only if it does not allready have focus and make it loose focus when I click outside of it without using a MouseArea to control the focus-property?
I would be thankful for any hints and pointers you can give me.
So I figured out a way to set the focus manually with a TapHandler and setting activeFocusOnPress = false. Unlike the MouseArea a TapHandler will propagate the ClickEvent to the TextField, allowing me to Highlight Text and Move the Courser by Clicking. This is something that I want and that is (to my knowledge) not possible with MouseAreas as they will catch the MouseEvents without propagating them to underlying Elements.
Unfortunatily this does not solve my problem because with activeFocusOnPress = false the virtual Keyboard will not open at all, even if I manipulate the focus-property of my TextField manually. I do see the coursor blinking in it and can type with my physical keyboard just fine, but for some reason the virtual keyboard will only react to focus changes on my TextField when activeFocusOnPress = true.
I found this old bug-report describing exactly this behaviour on Android:
https://bugreports.qt.io/browse/QTBUG-45324?focusedCommentId=277556Is this a known bug with the activeFocusOnPress?
-
What if you call forceActiveFocus ?
-
@GrecKo Thanks for the suggestion, but unfortunatily that does not change anything. Wether I use focus = true or forceActiveFocus, I can type into the TextField using my real keyboard, so the focus seems to be set. But the virtual keyboard does not open. Only with activeFocusOnPress = true will it react to focus changes.
It may be of interest to note that I am using an Input-Panel wrapped in a Popup as keyboard. I did not find any slots in the InputPanel that can be used to trigger the keyboard manually. In the Documentation I always find statements like "Once properly installed, the virtual keyboard can be opened by clicking on a text input field." (https://doc.qt.io/qt-5/qtvirtualkeyboard-user-guide.html)
I guess Qt does not want me to manually control the behaviour of the keyboard? A while back there was a openSoftwareInputPanel()-function but it seems they removed it. Maybe something like this would have been helpful here..
-
After some more digging I was able to fix most of my problems. Both the the focus problem on re-clicking the TextField and the problem with the text-highlighting were caused by the closePolicy of the PopUp I wrapped my InputPanel into.
The close-policy was set to CloseOnReleaseOutsideParent, which caused the Popup to close as soon as I released the mouse-Button when highlighting text (which is a release outside of the Popup-Parent). I set the close-Policy to CloseOnPressOutsideParent and now highlighting works fine.
I still observe the TextField refocusing each time I click it which makes sense since this is a click outside of the PopUp-parent-element. The keyboard closes, the TextField looses focus and only after that the click into the TextField is handeled causing it to gain focus again and re-opening the Keyboard.
I guess I could set the PopUp-close-policy to NoAutoClose to get rid of the re-focusing but that would mean major refactorings in my application as I would need to go to each element that uses the TextField and handle clicks outisde of it mynually (With TapHandlers or even goodold MouseAreas). For now I am satisfied with my solution.
I was not able to figure out why the keyboard stops reacting to focus changes when I set activeFocusOnPress = false in my TextField. No Idea whats going on there.
-