Nominate our 2022 Qt Champions!

TextEdit maximum text length

  • 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
    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.

  • 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.

  • Hmm, sounds interesting.

    Have to read about it...


  • 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.

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

  • no problem!

Log in to reply