PDA

View Full Version : QTextEdit - how to limit maximum length?



Henrikas[MI]
10th October 2006, 10:35
Is there any kind of validator or maximum length that I can assign to QTextEdit control? I cannot use QLineEdit, because I need a multi-line entry widget...

Qt 3.20

e8johan
10th October 2006, 13:33
You can always install an event filter choosing not to pass on additional characters if the length reaches your limit. Just make sure that you pass on movements (up, down, left, right, pgup, pgdn, home, end) and deletions (delete, backspace).

sudhansu
10th March 2010, 12:07
You can always install an event filter choosing not to pass on additional characters if the length reaches your limit. Just make sure that you pass on movements (up, down, left, right, pgup, pgdn, home, end) and deletions (delete, backspace).

How to do this programetically?

toutarrive
10th March 2010, 12:39
Connect the signal QTextEdit::textChanged () to a slot wherein you can limit the input to your needs.

Lykurg
10th March 2010, 12:46
Connect the signal QTextEdit::textChanged () to a slot wherein you can limit the input to your needs.

That's too late since you then don't know what was changed. You have to reimplement key***event or install an event filter.

toutarrive
10th March 2010, 13:24
The idea was to have something like this in the slot handling the textChanged() signal :


textedit.setPlainText(rightLengthText);


But this could lead to a recursivity issue with the textChanged() signal except if QObject::blockSignals ( bool block ) is enough for not triggerering the potentially recursive textChanged signal.

Instead of controlling the length of the input we limit the output length.

jwiedey
21st September 2010, 19:14
void on_edit_textChanged()
{
if (edit.text().length() > MAX_LENGTH) {
edit->textCursor().deletePreviousChar();
}
}

Lykurg
21st September 2010, 20:38
That also wont work. Just guess you are inserting a long text by CTRL+C or drop it in. For that you have to replace the if by while and that is nasty.

..or you have to select the previous characters and delete them. But still, that is not nice. Best would be to disallow the copying if it is to long. So still the solution of johan is the only "valid" one.