QML TextArea, process each character one at a time during input/typing
-
I have a text area (in QML) and i want to listen to every characters inputted on it, What i am trying to do is when a spacebar is pressed, I will perform an operation or processing such as replacing some characters on it. I have tried and listen to onTextChanged however, it may cause recursion as setting my TextArea.text to new value triggers a call to onTextChange event thus causing stack overflow.
I have tried to listen to Keys.onPressed event as well and listen to space key but the space key is processed as well and when i set a new text value, the space value is forcedly inserted. How and where should i handled and listen to these events?
(1) How can i listen to each inputted characters in QML text area, catch the space bar, process and replace the text with the new processed text? also ignore the last spacebar entry?
(2) how do i reject a chracter when inputted in the TextEdit or TextArea? say for example, i want to prevent spacebar input
in summary this is what i need in pseudocode
something input event per character :{ if(input is spacebar) { var newtext = processText(myTextArea.text); // process the text without the last space .. myTextArea.text = newText + " "; // append the space } }
-
@cebuger said in QML TextArea, process each character one at a time during input/typing:
I have a text area (in QML) and i want to listen to every characters inputted on it, What i am trying to do is when a spacebar is pressed, I will perform an operation or processing such as replacing some characters on it. I have tried and listen to onTextChanged however, it may cause recursion as setting my TextArea.text to new value triggers a call to onTextChange event thus causing stack overflow.
If monitoring textChanged is the route you want to go, recursion can be avoided through the use of a flag. Eg
TextArea { property bool editing: false onTextChanged: { if (editing) return; editing = true; text += "something"; editing = false; } }
I have tried to listen to Keys.onPressed event as well and listen to space key but the space key is processed as well and when i set a new text value, the space value is forcedly inserted. How and where should i handled and listen to these events?
An code sample would make it easier to offer relevant commentary. Both QtQuick.Controls 1 and QtQuick.Controls 2 have a TextArea, with different interfaces.
(1) How can i listen to each inputted characters in QML text area, catch the space bar, process and replace the text with the new processed text? also ignore the last spacebar entry?
(2) how do i reject a chracter when inputted in the TextEdit or TextArea? say for example, i want to prevent spacebar input
in summary this is what i need in pseudocode
something input event per character :{ if(input is spacebar) { var newtext = processText(myTextArea.text); // process the text without the last space .. myTextArea.text = newText + " "; // append the space } }
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.15 Window { TextArea { anchors.fill: parent Keys.onSpacePressed: { var pre = text.slice(0, cursorPosition); var post = text.slice(cursorPosition); text = pre + "<space>" + post; cursorPosition = pre.length + "<space>".length; } } }