3

I need a QLineEdit which must represent a range. F.E. (1,2] , and for this representation I want to set a validation checker for user not to write other symbols. In this case I have char + int + char + int + char as shown in example below. Does Qt have any feature to handle this? Thanks in advance.

Tay2510
  • 5,748
  • 7
  • 39
  • 58
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
  • In my opinion an input field that doesn't allow you to TYPE some chars is an horrible idea (close to trapping mouse movement inside a rect). Note that many users cannot type without looking at the keyboard and therefore they will type the forbidden chars and later they will be simply surprised they're not present, retyping the whole thing again (still without looking the screen). MUCH better is to show a message close to the field if the content is invalid. – 6502 Mar 25 '14 at 06:43

4 Answers4

6

You can use Qt's Input Validator feature to achieve this goal.

The following snippet will restrict the input on a line edit as you specified.

QRegExp re("^[[,(]{1,1}(0|[1-9]{1,1}[0-9]{0,9})[,]{1,1}(0|[1-9]{1,1}[0-9]{0,9})[],)]{1,1}$");
QRegExpValidator *validator = new QRegExpValidator(re, this);
ui->lineEdit->setValidator(validator);

Edit Updated the regex

  • in this case i can write 00000,2222, but you must not allow writing more then one leading 0. the right one is "^[[,(]{1,1}(0|[1-9]{1,1}[0-9]{0,9})[,]{1,1}(0|[1-9]{1,1}[0-9]{0,9})[],)]{1,1}$" – Eduard Rostomyan Mar 25 '14 at 15:07
  • Do you need to `delete` the validator later on? – Tomáš Zato Dec 17 '15 at 10:34
  • @TomášZato It's not shown in the snippet, but the "this" argument is setting the *parent argument, which would enable automatic resource cleanup when the parent is a QObject (see http://stackoverflow.com/a/3902325/798377) –  Dec 17 '15 at 15:57
2
QRegExp expr("^[[,(]{1,1}(0|[1-9]{1,1}[0-9]{0,9})[,]{1,1}(0|[1-9]{1,1}[0-9]{0,9})[],)]{1,1}$");

This is what I wanted! I must allow more then one leading 0-s.

Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
1

It is not possible to write a regexp accepting only valid ranges, the reason is that you can check the syntax but not the numeric value (unless e regexp engine has some extensions). The difference between

[1234,5678)

and

[5678,1234)

is not in the syntax (what regexps are about), but in the semantics (where regexps are not that powerful).

For checking just the syntax a regexp could be

\[\d+,\d+\)

or, if you also allow other types of interval boundary conditions:

[\[)]\d+,\d+[\])]
6502
  • 112,025
  • 15
  • 165
  • 265
0

I would recommend not allowing all chars but only the needed ones. Example:

QRegExp("[\\\\\\(\\)\\{\\}]\\d[\\\\\\(\\)\\{\\}]\\d[\\\\\\(\\)\\{\\}]");

I'll explain:

[] these contain the matchin characters for your char: \\ (this is actually matching the \ sign, as you need to escape it once for your Regular Expression \ and once more for Qt String makes it \\), \( is for opening bracket and so on. You can add all chars you would like to be matched. A good help is the Regular Expression Cheat Sheet for this.

\d is matching a single digit, if you want to have more than one digit you could use \d+ for at least one or \d{3} for exactly 3 digits. (+ 1 or more, ? 0 or 1, * 0 or more)

Another example would be:

QRegExp("[\\\\\\(\\)\\{\\}]\\d[,\\.]\\d[\\\\\\(\\)\\{\\}]");

for having the center character to be a . or a , sign.

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38