Trouble with selectedText()
-
I am building a basic numeric number pad with
Repeater
:Repeater { id: repeaterID model: [7,8,9,4,5,6,1,2,3] delegate: PushButton { height: 75 width: height text: modelData onClicked: { if(entryDisplay.selectedText.length > 0) { console.info("first") console.info(entryDisplay.selectedText.length) entryDisplay.text = modelData } else { console.info("second") console.info(entryDisplay.selectedText.length) entryDisplay.text += modelData } } } }
and as you can see I am attempting to create logic s/t if the string in the
TextInput
:TextInput { id: entryDisplay width: parent.width - 50 anchors.centerIn: display color: "black" text: Math.round(settingValue.value).toString() onActiveFocusChanged: { if(activeFocus) { selectAll(); } } }
is currently selected then if another button is pressed it will erase that string and replace it with the value of the button pressed. The string is selected when the component is created and when the user clicks somewhere in the actual text field. It's working but only on the first go around. Here is the output of the first three button presses:
qml: first qml: 1 qml: second qml: 0 qml: second qml: 0
now if I then click inside the text field the text gets selected like it is supposed to.. but... here is the output of the next button press:
qml: second qml: 0
in other words, it is not recognizing the selected text. The output should have been:
qml: first qml: 1
Any idea what I am doing wrong here?
-
Hi @Circuits , what is happening is that even if you click on the PushButton the TextInput is still in focus.
Just try to make this code change:-
onClicked: { console.log(entryDisplay.selectedText.length) if(entryDisplay.selectedText.length > 0) { console.info("first") console.info(entryDisplay.selectedText.length) entryDisplay.text = modelData } else { console.info("second") console.info(entryDisplay.selectedText.length) entryDisplay.text += modelData } //Just add the below line in your code (rootButton is PushButton) rootButton.forceActiveFocus() }
-
Unfortunately I still can't get it to work. I tried giving the
PushButton
an id called "theButton":Repeater { id: repeaterID model: [7,8,9,4,5,6,1,2,3] delegate: PushButton { id: theButton height: 75 width: height text: modelData onClicked: { console.log(entryDisplay.selectedText.length) if(entryDisplay.selectedText.length > 0) { console.info("first") console.info(entryDisplay.selectedText.length) entryDisplay.text = modelData } else { console.info("second") console.info(entryDisplay.selectedText.length) entryDisplay.text += modelData } theButton.forceActiveFocus() } } }
output first 3 button presses:
qml: 1 qml: first qml: 1 qml: 0 qml: second qml: 0 qml: 0 qml: second qml: 0
clicked inside of the text area to select the text and clicked another button:
qml: 0 qml: second qml: 0
I think I agree that this is a
focus
problem. Is there some way for me to print what hasfocus
? -
@Circuits said in Trouble with selectedText():
I think I agree that this is a focus problem. Is there some way for me to print what has focus?
Use
activeFocusItem
property change signal of root Window. It is also useful to set names to objects viaobjectName
property. -
Hi @Circuits , i just copy pasted your code its working fine for me.
-
@Shrinidhi-Upadhyaya This code?:
Repeater { id: repeaterID model: [7,8,9,4,5,6,1,2,3] delegate: PushButton { id: theButton height: 75 width: height text: modelData onClicked: { console.info(entryDisplay.selectedText.length) if(entryDisplay.selectedText.length > 0) { console.info("first") console.info(entryDisplay.selectedText.length) entryDisplay.text = modelData } else { console.info("second") console.info(entryDisplay.selectedText.length) entryDisplay.text += modelData } theButton.forceActiveFocus() } } }
I really can't understand why it's not working for me. I wish I was better with the QML debugger. Perhaps I will try re-writing the whole component. There must be a deeper problem.