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. Limit the number of input characters in TextArea

Limit the number of input characters in TextArea

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 3.1k Views
  • 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.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by
    #1

    Hi,
    How to limit the number of input characters in TextArea?
    there is one feature in TextInput name maximumLength for that how about TextArea?

    Thanks,

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There is nothing built-in, but you can implement the limit with something like this:

      TextArea {
        onTextChanged: if (length > MyLimit) remove(MyLimit, length);
      }
      

      (Z(:^

      AlienA 1 Reply Last reply
      4
      • sierdzioS sierdzio

        There is nothing built-in, but you can implement the limit with something like this:

        TextArea {
          onTextChanged: if (length > MyLimit) remove(MyLimit, length);
        }
        
        AlienA Offline
        AlienA Offline
        Alien
        wrote on last edited by Alien
        #3

        Dear @sierdzio ,
        Thanks it works nicely.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          :-) you're welcome, happy coding!

          (Z(:^

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AlexChris
            wrote on last edited by
            #5

            Indeed, I support using this way:
            onTextChanged: {
            remove((cursorPosition-(length-maxlen)),cursorPosition)
            }
            it can prevent user from inserting characters in the middle part of the text.
            Howerer, when "ctrl + z" is typed, there're still problems.

            1 Reply Last reply
            0
            • mmjvoxM Offline
              mmjvoxM Offline
              mmjvox
              wrote on last edited by mmjvox
              #6

              I think this is better:

              property int maximumLength: -1;
              property string lastValidText: "";
                      onTextChanged: {
                          if (text.length > maximumLength  && maximumLength>-1) {
                              var currentPosition = cursorPosition-1;
                              text = lastValidText;
                              cursorPosition = currentPosition;
                          } else {
                              lastValidText = text;
                          }
                      }
              
              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