#include "customtextedit.h" #include CustomTextEdit::CustomTextEdit(QObject *parent) : QTextEdit((QWidget*)parent) { maxLength = 0; connect(this,SIGNAL(textChanged()),this,SLOT(truncateText())); } void CustomTextEdit::setMaxLength(int length) { maxLength = length; QString curText = toPlainText(); if ((maxLength > 0) && (curText.length() > maxLength)) { curText = curText.left(maxLength); setPlainText(curText); } } int CustomTextEdit::getMaxLength() { return maxLength; } void CustomTextEdit::truncateText() { if(maxLength > 0) { QString curText = this->toPlainText(); if(curText.length() > maxLength) { this->setText(curText.left(maxLength)); this->moveCursor(QTextCursor::End); } } }