QT的输入框(lineEdit)中如何判断输入的内容没有符号如(空格、!、~、=、+、-、等)
使用正则表达式,qt有QRegExp这个类
我估计你的意思应该是阻止用户输入特殊字符
代码如下
QLineEdit *edit=new QLineEdit(this);
QRegExpValidator *vialitor=new QRegExpValidator(QRegExp("[^!~=-+\\s]*"));
edit->setValidator(vialitor);
edit->show();
bool isValidInput(const QString &input) {
QRegExp regx("[^0-9]"); // 匹配非数字字符
if (regx.exactMatch(input))
return false; // 如果输入内容包含非数字字符,则返回false
else
return true; // 否则返回true
}