Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML TextArea, process each character one at a time during input/typing
Forum Updated to NodeBB v4.3 + New Features

QML TextArea, process each character one at a time during input/typing

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 810 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cebuger
    wrote on last edited by
    #1

    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
           }
    }
    
    jeremy_kJ 1 Reply Last reply
    0
    • C cebuger

      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
             }
      }
      
      jeremy_kJ Online
      jeremy_kJ Online
      jeremy_k
      wrote on last edited by
      #2

      @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;
              }
          }
      }
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved