兩個小問題...
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());
希望有所幫助。
當你寫信具有因它不匹配的正則表達式了,也是一個空格小寫'\\ s' –