|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include "customtextedit.h"
#include <QTextEdit>
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);
}
}
}
|