Results 1 to 8 of 8

Thread: QTextEdit - how to limit maximum length?

  1. #1
    Join Date
    Oct 2006
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default QTextEdit - how to limit maximum length?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - how to limit maximum length?

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

  3. #3
    Join Date
    Oct 2009
    Posts
    105
    Thanked 4 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTextEdit - how to limit maximum length?

    Quote Originally Posted by e8johan View Post
    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?

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit - how to limit maximum length?

    Connect the signal QTextEdit::textChanged () to a slot wherein you can limit the input to your needs.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextEdit - how to limit maximum length?

    Quote Originally Posted by toutarrive View Post
    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.

  6. #6
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit - how to limit maximum length?

    The idea was to have something like this in the slot handling the textChanged() signal :
    Qt Code:
    1. textedit.setPlainText(rightLengthText);
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by toutarrive; 10th March 2010 at 14:07.

  7. #7
    Join Date
    Sep 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit - how to limit maximum length?


    void on_edit_textChanged()
    {
    if (edit.text().length() > MAX_LENGTH) {
    edit->textCursor().deletePreviousChar();
    }
    }

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextEdit - how to limit maximum length?

    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.

Similar Threads

  1. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  2. qt4 & xp - QTextEdit length
    By incapacitant in forum Newbie
    Replies: 4
    Last Post: 6th March 2006, 15:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.