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. TextEdit maximum text length
Forum Updated to NodeBB v4.3 + New Features

TextEdit maximum text length

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 8.9k 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.
  • M Offline
    M Offline
    MichK
    wrote on last edited by
    #1

    Does anyone know how to make text in TextEdit element constrained to specified number of characters?
    I am using following approach:

    @ onTextChanged: {
    if (!readOnly && text.length > 10)
    {
    var cpos = cursorPosition
    text = text.slice(0, 10 - text.length)
    if (cpos >= text.length)
    cursorPosition = text.length
    else
    cursorPosition = cpos
    }
    if (paintedHeight > height)
    {
    parent.height = paintedHeight+6
    }
    }
    @

    It works, but on my C7 automatic removing of a last char(after exceeding 10 chars) is not immediate.
    Also when writing in the middle of text sometimes input is set to upper-case letters instead of lower-case.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Why not subclass [[Doc:QValidator]], and check the length of the input string there? Simply re-implement validate() and fixup. validate() would just return Acceptable for strings shorter or equal than the required length, and Invalid for longer strings. fixup() would simply truncate the input text.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MichK
        wrote on last edited by
        #3

        Hmm, sounds interesting.

        Have to read about it...

        Thanks!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MichK
          wrote on last edited by
          #4

          Unfortunately I don't find way to use it in my case, because I need multi line editable text element.
          TextInput has validator property, but is single-line.
          TextEdit does not have validator property and I don't know how I could use QValidator in it.

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

            Ah, sorry about that! Also, I missed you were working in Quick (not your fault, you were posting in the right forum).

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichK
              wrote on last edited by
              #6

              no problem!

              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