BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
maxlengthtextedit.cpp
1 #include "gui/maxlengthtextedit.h"
2 
3 #include <QTimer>
4 
5 
6 MaxLengthTextEdit::MaxLengthTextEdit(QWidget *parent)
7  : QTextEdit(parent), m_maxLength(-1) {
8  connect(this, SIGNAL(textChanged()), this, SLOT(checkLength()));
9 }
10 
11 MaxLengthTextEdit::~MaxLengthTextEdit(){
12 }
13 
14 int MaxLengthTextEdit::maxLength() const {
15  return m_maxLength;
16 }
17 
18 void MaxLengthTextEdit::setMaxLength(int max_length) {
19  m_maxLength = max_length;
20 }
21 
22 void MaxLengthTextEdit::checkLength() {
23  QTimer::singleShot(0, this, SLOT(reallyCheckLength()));
24 }
25 
26 void MaxLengthTextEdit::reallyCheckLength() {
27  while (m_maxLength >= 0 && toPlainText().size() > m_maxLength) {
28  textCursor().deletePreviousChar();
29  }
30 }