Results 1 to 5 of 5

Thread: Text edit and maxLength

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Text edit and maxLength

    Can text edit widget have maxLength like the line edit widget ?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Text edit and maxLength

    No, but you can count the characters in it when the textChanged() signal is emitted and disable the input if the count reaches a certain limit.

    Regards

  3. The following user says thank you to marcel for this useful post:

    vieraci (29th April 2007)

  4. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Text edit and maxLength

    Quote Originally Posted by marcel View Post
    No, but you can count the characters in it when the textChanged() signal is emitted and disable the input if the count reaches a certain limit.

    Regards
    The other option would be to filter the event on TextEdit and checking QString(textEdit->text).length() to certain MAX limit.

    Thanks

  5. #4
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: Text edit and maxLength

    Just for convenience, here is an example of a QTextEdit child class implementing the missing maxLength feature.

    Regards,
    Oscar

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include "TextEdit.hpp"
    4.  
    5. TextEdit::TextEdit() : QPlainTextEdit() {
    6. connect(this, SIGNAL(textChanged()), this, SLOT(myTextChanged()));
    7. }
    8.  
    9. TextEdit::TextEdit(int maxChar) : QPlainTextEdit() {
    10. this->maxChar = maxChar;
    11. connect(this, SIGNAL(textChanged()), this, SLOT(myTextChanged()));
    12. }
    13.  
    14. int TextEdit::getMaxChar() {
    15. return maxChar;
    16. }
    17.  
    18. void TextEdit::setMaxChar(int maxChar) {
    19. this->maxChar = maxChar;
    20. }
    21.  
    22. void TextEdit::myTextChanged() {
    23. if (QPlainTextEdit::toPlainText().length()>maxChar) {
    24. QPlainTextEdit::setPlainText(QPlainTextEdit::toPlainText().left(QPlainTextEdit::toPlainText().length()-1));
    25. QPlainTextEdit::moveCursor(QTextCursor::End);
    26. QMessageBox::information(NULL, QString::fromUtf8("Warning"),
    27. QString::fromUtf8("Warning: no more then ") + QString::number(maxChar) + QString::fromUtf8(" characters in this field"),
    28. QString::fromUtf8("Ok"));
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by oscar; 19th September 2008 at 18:17.

  6. #5
    Join Date
    Nov 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Text edit and maxLength

    It works!

    int maximum = 500;
    if (ui.textEdit->toPlainText().length() > maximum) {
    ui.textEdit->textCursor().deletePreviousChar();
    }

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.