Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Limit number of characters in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

[Solved]Limit number of characters in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 21.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    b1gsnak3
    wrote on 28 Jan 2013, 13:10 last edited by
    #1

    Well, I need to limit the number of characters in a QTextEdit. I found something that says to rewrite the keyPressEvent and enter a event filter. I was thinking of something like

    @if(textEdit->document()->characterCount() >= maxLen) {
    limit the editing only for delete, up, down, etc.
    } else {
    normal editing
    }
    @

    The problem is that I am not sure on how to implement this. On the else I just copy from the normal keyPressEvent. I know I can use QLineEdit but I need a multiline text editor. Also it will only contain plain text (no html subset).

    1 Reply Last reply
    1
    • B Offline
      B Offline
      b1gsnak3
      wrote on 28 Jan 2013, 15:27 last edited by
      #2

      found an example that works just fine:

      http://qt-project.org/faq/answer/how_can_i_get_set_the_max_length_of_a_qtextedit

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mar4eli
        wrote on 23 Sept 2015, 16:10 last edited by
        #3

        Sorry for necroposting, but i have a better way than reimplementing keyPress.

        void wClassArea::on_textEdit_textChanged()
        {
        if(ui->textEdit->toPlainText().length() > m_maxDescriptionLength)
        {
        int diff = ui->textEdit->toPlainText().length() - m_maxTextEditLength; //m_maxTextEditLength - just an integer
        QString newStr = ui->textEdit->toPlainText();
        newStr.chop(diff);
        ui->textEdit->setText(newStr);
        QTextCursor cursor(ui->textEdit->textCursor());
        cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        ui->textEdit->setTextCursor(cursor);
        }
        }

        There we do:

        1. Catch textChanged event.
        2. Compare length of the text in TextEdit buffer with our limit. It is vital, because when you paste even large text (millions of chars), you will get there only i time. I have checked this in the debug mode.
        3. Get text string from TextEdit.
        4. Remove all symbols over limit.
        5. Move cursor to the end.

        P.s. I just didn't want to inherit QTextEdit and change my old code.-)
        P.p.s. sorry for my English.

        1 Reply Last reply
        4

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved