0

I have a problem with using the QRegExValidator to validate a QLineEdit. It's supposed to only acept first and last name with a space between. I pretty much followed the example from here:

http://doc.qt.digia.com/4.6/qregexpvalidator.html

the code compiles and i can run the programm but I can't type any letter into the lineedit field. Why?

QString name_rx = "[A-z]+\\S[A-z]+";
QRegExp rx;
QValidator *validator =  new QRegExpValidator(rx, this); // this means mainwindow

rx.setPattern(name_rx);
ui->nameEdit->setValidator(validator);
ui->nameEdit->setMaxLength(32);

Is this is all it takes to check the input field?

AntonyG
  • 861
  • 9
  • 22
Serious Sammy
  • 99
  • 2
  • 11

2 Answers2

2

Two minor problems...

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

Another acceptable method would be:

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

And here it is added to the validator

QValidator *validator =  new QRegExpValidator(rx, this); // this means mainwindow

ui->nameEdit->setValidator(validator);
ui->nameEdit->setMaxLength(32);

Also a good example for what works, check out this page:

http://doc.qt.digia.com/4.6/tools-settingseditor-variantdelegate-cpp.html

It has all of these examples specifically for using QRegEx with QRegExValidator with line edits:

 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());

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • An [`[A-z]+` does not only match letters](http://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926). – Wiktor Stribiżew Sep 14 '16 at 13:05
  • @WiktorStribiżew Good call. It looks like it will also get `[\]^_``` going on the ascii table from A (65) to z (122). I'll put in a substitute expression shortly. – phyatt Sep 14 '16 at 13:24
  • ` [\]^_ ` and the backtick were the unwanted characters – phyatt Sep 14 '16 at 13:32
0

your pattern matches (for instance) this string

a\Sa

that is there must be a literal \ and an uppercase S between the names. I tried using the very handy example regexp, you can find it in standard examples released with QtCreator (Ctrl+1, then enter regexp in the search field...)

enter image description here

CapelliC
  • 59,646
  • 5
  • 47
  • 90