版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lilili1010101010/article/details/84028340
QLineEdit *name_lineEdit = new QLineEdit;
QRegExp regx("^[\u4e00-\u9fa5a-zA-Z]+$"); //其中匹配中文[\u4e00-\u9fa5]
QValidator *validator = new QRegExpValidator(regx);
name_lineEdit->setValidator(validator);
上面这个正则表达式没有过滤掉中文符号,本想一个一个过滤,(如下),仍然无效
QRegExp regx("[^`~!@#$%^&*()-_+={}':;',\\[\\].<>/?¥……()——;’‘:“',。、?0-9]+$");
1
后响应QLineEdit 的textChanged的信号,在槽函数中过滤中文符号,成功!正则表达式无法过滤也因为编码问题。
connect(name_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotNameFilter(QString)));
void SearchCondWidget::slotNameFilter(QString s) //SearchCondWidget我的类
{
s.replace(QString::fromLocal8Bit("【"), "");
s.replace(QString::fromLocal8Bit("】"), "");
s.replace(QString::fromLocal8Bit("?"), "");
s.replace(QString::fromLocal8Bit("!"), "");
s.replace(QString::fromLocal8Bit("·"), "");
s.replace(QString::fromLocal8Bit("¥"), "");
s.replace(QString::fromLocal8Bit("……"), "");
s.replace(QString::fromLocal8Bit("("), "");
s.replace(QString::fromLocal8Bit(")"), "");
s.replace(QString::fromLocal8Bit("——"), "");
s.replace(QString::fromLocal8Bit("、"), "");
s.replace(QString::fromLocal8Bit(":"), "");
s.replace(QString::fromLocal8Bit(";"), "");
s.replace(QString::fromLocal8Bit("“"), ""); //引号两边分开过滤
s.replace(QString::fromLocal8Bit("”"), "");
s.replace(QString::fromLocal8Bit("’"), ""); //引号两边分开过滤
s.replace(QString::fromLocal8Bit("‘"), "");
s.replace(QString::fromLocal8Bit("《"), "");
s.replace(QString::fromLocal8Bit("》"), "");
s.replace(QString::fromLocal8Bit(","), "");
s.replace(QString::fromLocal8Bit("。"), "");
name_lineEdit->setText(s);
}
其中这个双引号(“”)和单引号(‘’)两边要分开过滤
---------------------
版权声明:本文为CSDN博主「哒哒的九九」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lilili1010101010/article/details/84028340