Important: Please read the Qt Code of Conduct -

QML TextInput and QuickView focus issues

  • Hi all,

    I'm having an issue with explicitly setting the focus upon a TextInput which is created within a qquickview. This may take some explanation so apologies if this is verbose and confusing. I will try my best to explain it in as simple a way as possible. Hopefully someone will be able to help.

    I have a qml overlay which is used to display the distance I am moving objects within a 3D scene in my application. This movement can be carried out by dragging objects or using the arrow keys. When the movement occurs I have a text element which displays a key ("Distance") then a TextInput element which displays the current distance moved. The TextInput can then be clicked within and edited and the distance of the object being moved will then update. All of this is working as expected.

    What I would like to do is have the TextInput automatically update as soon as a user presses a numerical key which would then remove the requirement for the user to click within the TextInput (thus giving it focus). At the moment this is achieved by handling the keyEvent within my application, and then updating the distance value. I then have the text input focus set to "true" when the distance value changes which should then have the focus within the TextInput and then allow the TextInput catch any further key presses.

    This implementation works as expected when I move my objects using the mouse and then press a numerical key. The focus seems to stay within the Text Input and I can enter numerical values of more than 1 digit (eg. 1029.45).

    However, when I use the arrow keys to move the object, then press the numerical keys to update the distance I am only able to enter 1 number whcih is consistently overwritten. It seems that the focus is not being held within the TextInput and because of this each time I press a number key it is like I am pressing it for the first time.

    Can anyone shed any light on how I can explicitly specify that the focus is maintained within the TextInput? Or give any pointers towards why this would be behaving this way. I have tried experimenting with FocusScope but it didn't have any affect.

    I realise that my description might not be ideal and it is always better to see the code in action to get a real grasp of the problem but hopefully I have explained enough.

    Thanks in advance.

    qml code snippet to follow:

    import QtQuick 2.5
    Item {
        id: root
    	Rectangle {		
    		x: 10
    		y: 10 
    		width: 250
    		height: 30
    		visible: true
    		color: "white"
                    // this is a debug text output 
    		Text {
    			anchors.fill: parent
    			id: debugText		
    			text: "nothing to say"
    			horizontalAlignment: Text.AlignLeft
    			verticalAlignment: Text.AlignTop					
    	Item {
    		id: dataWidget
                    x: dataWidgetController.geometry.x
    		y: dataWidgetController.geometry.y
    		z: 99999.9
    		width: dataWidgetController.geometry.width
                    height: dataWidgetController.geometry.height
    		visible: dataWidgetController.visible;
    		Rectangle {
    		        id: rectangle
       		        anchors.fill: parent
    				color: "white"
    				Rectangle {
    					x: parent.x + 1
    					y: parent.y + 1
    					width: parent.width - 2
    					height: parent.height - 2
    					color: "white"
    					border.color: "black"
    					border.width: 1
    					Row { 
    						id: row
    						anchors.fill : parent
    						spacing : 0
    						Text {
    							id: keyText
    							text: dataWidgetController.keyText
    							width: (parent.width/2) - 4
    							height: parent.height
    							color: "black"
    							font.pixelSize: 11
    							horizontalAlignment: Text.AlignLeft
    							verticalAlignment: Text.AlignVCenter				
    							id: valueField
    							text: dataWidgetController.valueText
    							width: keyText.width
    							height: keyText.height
    							font.pixelSize: 11
    							horizontalAlignment: Text.AlignRight
    							verticalAlignment: Text.AlignVCenter		
    							selectByMouse: true
    							focus: dataWidgetController.valueFieldFocus
    							cursorVisible: dataWidgetController.cursorVisible
    							Keys.onPressed: keyPressTimer.restart() 
    							onAccepted: dataWidgetController.editingFinished(keyText.text, valueField.text);						
    							onEditingFinished:dataWidgetController.editingFinished(keyText.text, valueField.text);		
    							onActiveFocusChanged : {
    								if( focus ) 
    									debugText.text = "focus in textInput"
    									debugText.text = "no focus here?"
    							id: keyPressTimer
    							interval: 1000
    							running: true
    							repeat: false
    							onTriggered: dataWidgetController.editingPaused(keyText.text, valueField.text);						

  • Does anyone have any ideas with this?