一、限制字母数字输入
#include <QRegExpValidator>
QRegExp regExp("[a-fA-F0-9 ]{0,40}");
ui->lE_AESKey->setValidator(new QRegExpValidator(regExp,this));//限制输入
ui->lE_AESKey->setPlaceholderText("16字节");//提示
ui->lE_AESKey->setMaxLength(40);//限制长度
二、使用正则表达式清除空格 (输入时lineEdit每四位自动加空格)
void MainWindow::on_lE_AESKey_textChanged(const QString &arg1)
{
if(arg1[arg1.length()-1] == " ")
return;
QString Temp = arg1;
Temp.remove(QRegExp("\\s"));
int mo = Temp.length()/4;
for(int i = 1; i<=mo; i++)
{
if((4*i)+(i-1) != Temp.length())
{
Temp.insert(((4*i)+(i-1))," ");
}
}
ui->lE_AESKey->setText(Temp);
}
三、判断字符中是否包含中文
QString str;
bool b = str.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
if(b)
{
//存在中文
}