QTextEdit使用
#限制QTextEdit的字符数的最大输入量,包括中文字符数或自由字符的最大输入量(纯中文输入、自由字符输入)
##UI界面效果图
##textEdit.h
#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QMainWindow>
namespace Ui {
class textEdit;
}
class textEdit : public QMainWindow
{
Q_OBJECT
public:
explicit textEdit(QWidget *parent = 0);
~textEdit();
private slots:
void EmailTypeChanged(int index);
void FastCheckBoxClicked();
void InputTextChanged();
private:
Ui::textEdit *ui;
int m_iFastMaxLength_free;
int m_iNormalMaxLength_free;
int m_iFastMaxLength_chinese;
int m_iNormalMaxLength_chinese;
bool IsChinese(ushort usUnicode);
void CountNumber();
void CheckFreeEmail();
void CheckChineseEmail();
QString GetChineseFromContent(QString qstrContent);
};
#endif // TEXTEDIT_H
##textEdit.cpp
#include "textedit.h"
#include "ui_textedit.h"
#include "qtextcursor.h"
#include "qmessagebox.h"
textEdit::textEdit(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::textEdit)
{
ui->setupUi(this);
m_iFastMaxLength_free = 16;
m_iNormalMaxLength_free = 30;
m_iFastMaxLength_chinese = 10;
m_iNormalMaxLength_chinese = 20;
connect(ui->fast_checkBox, SIGNAL(clicked(bool)), this, SLOT(FastCheckBoxClicked()));
connect(ui->emailType_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(EmailTypeChanged(int)));
connect(ui->content_textEdit, SIGNAL(textChanged()), this, SLOT(InputTextChanged()));
}
textEdit::~textEdit()
{
delete ui;
}
void textEdit::InputTextChanged()
{
int index = ui->emailType_comboBox->currentIndex();
if(0 == index)
{
CheckFreeEmail();
}
else
{
CheckChineseEmail();
}
CountNumber();
}
void textEdit::EmailTypeChanged(int index)
{
QString qstrContent = ui->content_text