两个小问题...
QString name_rx = "([A-Z]|[a-z])+\\s([A-Z]|[a-z])+";// Note, \\s not \\S
QRegExp rx;
rx.setPattern(name_rx);// Note, happens BEFORE use in the validator
另一种可接受的方法是:
QString name_rx = "[a-z]+\\s[a-z]";// Note, \\s not \\S
QRegExp rx;
rx.setCaseSensitivity(Qt::CaseInsensitive);
rx.setPattern(name_rx);// Note, happens BEFORE use in the validator
在这里它被添加到验证器
QValidator *validator = new QRegExpValidator(rx, this); // this means mainwindow
ui->nameEdit->setValidator(validator);
ui->nameEdit->setMaxLength(32);
也是一个很好的例子,看看这个页面:
http://doc.qt.digia.com/4.6/tools-settingseditor-variantdelegate-cpp.html
它具有所有这些示例,专门用于QRegEx
与QRegExValidator
行编辑一起使用:
boolExp.setPattern("true|false");
boolExp.setCaseSensitivity(Qt::CaseInsensitive);
byteArrayExp.setPattern("[\\x00-\\xff]*");
charExp.setPattern(".");
colorExp.setPattern("\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)");
doubleExp.setPattern("");
pointExp.setPattern("\\((-?[0-9]*),(-?[0-9]*)\\)");
rectExp.setPattern("\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)");
signedIntegerExp.setPattern("-?[0-9]*");
sizeExp = pointExp;
unsignedIntegerExp.setPattern("[0-9]*");
dateExp.setPattern("([0-9]{,4})-([0-9]{,2})-([0-9]{,2})");
timeExp.setPattern("([0-9]{,2}):([0-9]{,2}):([0-9]{,2})");
dateTimeExp.setPattern(dateExp.pattern() + "T" + timeExp.pattern());
希望有帮助。