1

I have a QLineEdit and I am using it for a measurement conversion application. In that QLineEdit I have to use only integer values, so I used the QDoubleValidator.

q_LineEdit->setValidator(new QDoubleValidator(this));

Now I want the QLineEdit to accept only " and / characters to it, as well as the integers, as it is required for the conversion application. How can I make my QLineEdit accept it while using a QDoubleValidator?

Note: I want my QLineEdit to accept something like this (eg. 70“1/2).

Note: The QLineEdit should not accept any other characters other than " and /.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
New Moon
  • 787
  • 6
  • 21
  • 35

1 Answers1

4

At last I figured out the answer by myself. It's very simple. Just use QRegExpValidator. Here's my piece of code:

QRegExp rx("(|\"|/|\\.|[0-9]){30}");
m_LineEdit->setValidator(new QRegExpValidator(rx, this));
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
New Moon
  • 787
  • 6
  • 21
  • 35
  • 1
    You could also use QRegExp or QRegularExpression to fix precisely what is accepted or not in your QLabel. The regexp you provide allows '"""70""1/21/5"12"/51/"5"65//' as a valid value ;) `QRegExp rx("[0-9]{2}\"[0-9]\/[0-9]");` is probably better, you just have to fix how many numbers you accept at each step – Antwane Dec 13 '13 at 10:29