August 2, 2011

kbt90 kbt90
Lab Rat
37 posts

[Solved] Using QRegExp with QLineEdit

 

Hello –

I am would like to use a QRegExp on a QLineEdit. I haven’t used regular expressions before, but have read a number of tutorials. I am unable to get the behavior I am trying to acheive.

I would like to be able to enter uppercase and lowercase letters, as well as digits. At least one character must be entered, but no more than 8. The following is the code I have:

  1. QRegExp rx ("[A-Za-z0-9]{1,8}");
  2. lineEdit->setValidator (new QRegExpValidator (rx, this));
  3.  
  4. if (lineEdit->hasAcceptableInput ()) {
  5.  buttonBox->button (QDialogButtonBox::Ok)->setEnabled (true);  
  6. } else {
  7.  buttonBox->button (QDialogButtonBox::Ok)->setEnabled (false);
  8. }

I am unable to enter any characters after I enter the 8th one and I am unable to enter any characters that are not allowed in the QLineEdit. For example, if I enter ‘abcdefghi’ only ‘abcdefgh’ shows up in the QLineEdit and OK remains enabled. I would like for the i to show up, but then for OK to become disabled. Likewise, if I type in ‘abcd$efgh’ only ‘abcdefgh’ shows up when I would like for all entered characters to show up and, if it is not acceptable input, for the OK button to become disabled. Is this behavior achievable and, if so, how?

Thanks,
Kate

4 replies

August 2, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

Hi Kate,

the validator ensures, that no invalid input can be entered.
If you want to achieve this behavior, you ahve to do it outside, without a validator.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 2, 2011

jaszczomb jaszczomb
Lab Rat
16 posts

As long as QRegExp rx (”[A-Za-z0-9]{1,8}”); is set {1,8} you will not be able to input more characters.
Here is what the code should look like if I didn’t misunderstood you:

  1. QRegExp rx ("[A-Za-z0-9]");
  2. lineEdit->setValidator (new QRegExpValidator (rx, this));
  3.  
  4. if ((lineEdit->hasAcceptableInput ())&&lineEdit->text().length()<9) {
  5.  buttonBox->button (QDialogButtonBox::Ok)->setEnabled (true);
  6. } else {
  7.  buttonBox->button (QDialogButtonBox::Ok)->setEnabled (false);
  8. }

I didn’t compile it but you should understand my idea with it.

August 2, 2011

kbt90 kbt90
Lab Rat
37 posts

Super – thanks for the replies and the explanations.

Kate

August 2, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

This only does part of it.
if you type ‘abcd$efgh’ only ‘abcdefgh’ will show up, which was also not liked.

you could go that way:

  1.     // remove this to enable all characters input: lineEdit->setValidator (new QRegExpValidator (rx, this));
  2.  
  3.     QRegExp rx ("[A-Za-z0-9]{1,8}");
  4.     if (rx.exactMatch(lineEdit->text()))
  5.     {
  6.         buttonBox->button (QDialogButtonBox::Ok)->setEnabled (true);
  7.     }
  8.     else
  9.     {
  10.         buttonBox->button (QDialogButtonBox::Ok)->setEnabled (false);
  11.     }

The code was hacked directly to devnet

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ [SOLVED][QHttp] qhttp->get("/index.html?text=AAA",file) Doesn’t work correctly      [Solved] activateWindow only works after there has been a mouse click on the window. ››

You must log in to post a reply. Not a member yet? Register here!