QTextEdit限制文本长度

2014-11-24 02:07:55 · 作者: · 浏览: 1
QString textContent = this->toPlainText();
int length = textContent.count();
int maxLength = maxLen; // 最大字符数
if(length > maxLength)
{
int position = this->textCursor().position();
QTextCursor textCursor = this->
textCursor();
textContent.remove(position - (length - maxLength), length - maxLength);
this->setText(textContent);
textCursor.setPosition(position - (length - maxLength));
this->setTextCursor(textCursor);
}