July 22, 2011

michal.k michal.k
Lab Rat
70 posts

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:

  1.             onTextChanged: {
  2.                 if (!readOnly && text.length > 10)
  3.                 {
  4.                     var cpos = cursorPosition
  5.                     text = text.slice(0, 10 - text.length)
  6.                     if (cpos >= text.length)
  7.                         cursorPosition = text.length                    
  8.                     else
  9.                         cursorPosition = cpos
  10.                 }
  11.                 if (paintedHeight > height)
  12.                 {
  13.                     parent.height = paintedHeight+6
  14.                 }
  15.             }

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.

5 replies

July 22, 2011

Andre Andre
Robot Herder
6296 posts

Why not subclass 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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 22, 2011

michal.k michal.k
Lab Rat
70 posts

Hmm, sounds interesting.

Have to read about it…

Thanks!

July 22, 2011

michal.k michal.k
Lab Rat
70 posts

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.

July 22, 2011

Andre Andre
Robot Herder
6296 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 22, 2011

michal.k michal.k
Lab Rat
70 posts

no problem!

 
  ‹‹ Image, destroy and refresh (SOLVED)      Circular references in Qml : Should they be flagged or not? ››

You must log in to post a reply. Not a member yet? Register here!