I want the QLineEdit to accept only numbers without any decimal.e.g it should accept '456' but not '456.3434'.i.e. it should not allow decimal at all. Can anybody give some pointers that how can i do that.
I tried to use QIntValidator, but it still allows to enter decimal point, and when i convert the text from QLinEdit it returns zero (as it documentation says, if the conversion fails it will return zero).
I also tried to use QRegExpValidator( QRegExp("[0-9]"), but it allows only one number. There is no limit for the maximum number, how do i specify the QRegExp with minimum as 0 and maximum as undefined, if QRegExpValidator is the only way to achieve it ?
Thank you
You might try the following validator:
QLineEdit le;
le.setValidator(new QRegExpValidator(QRegExp("[0-9]+")));
le.show();
UPDATE
To allow input in exponential form you can try this:
le.setValidator(new QRegExpValidator( QRegExp("[0-9]+e[0-9]+")));
Related
I have a UI with a QLineEdit which only accepts float/double values with the help of a QDoubleValidator.
It only accepts values if I enter a leading number like: 0.234. But I'd prefer to be able to enter values directly without a leading number like .234. Unfortunately the QDoubleValidator does not accept a leading point. Is there any way to archive my goal with the help of the validator, or do I have to check every entered character myself? I'm using Qt 5.9.1 on Windows10.
QDoubleValidator* doubleValidator = new QDoubleValidator();
QLineEdit* lineEdit = new QLineEdit(frame);
lineEdit->setValidator(doubleValidator);
vbox->addWidget(lineEdit);
Unfortunately, QDoubleValidator is pretty limited, but you can use QRegExpValidator to get what you want with a regex describing a number, depending on the notation you expect.
// non-scientific floating-point numbers
QRegExp rx("[-+]?[0-9]*\\.?[0-9]+");
QRegExpValidator v(rx, 0);
QString s;
s = ".123";
v.validate(s, 0); // Returns Acceptable
This is much more extendable, and allows you to abstract it to any condition, with basic regular expression knowledge.
I have tried searching for this problem in various ways, but have found nothing relevant, so perhaps there is a simple way around this which I simply haven't found. If so, my apologies in advance.
I am using a number of QSpinBoxes in my app, and they have a behaviour which I find quite frustrating, namely that if you insert the cursor and try to type, it is blocked and nothing is inserted. If I then delete a character, then I can type one character, and so on.
Example:
Spinbox shows: 0.0000
I insert the cursor after the '.' and want to type '5', i.e. it would then have 0.50000. This is blocked, since the spinbox is full, given the constraints I have set on limits and precision. I have to press Delete to remove one '0', so it says 0.000 and then I can type my '5'.
How can I let the spinbox accept all valid input while typing, and only worry about truncating the value at the end?
You could probably subclass the QDoubleSpinBox and re-implement its validate method to allow numbers with more decimal points.
Then capture the editingFinished signal to truncate the entered value.
I'm using a QDoubleValidator for my QLineEdit. The application locale (set in QtCreator) is QLocale::German.
Now when I enter a valid double (either using a dot or a comma as decimal separator) writing to the textedit as well as converting the string to a float works perfectly fine. But the validator also lets me write stuff with multiple decimal separators. Strings like 123.567,890 or ,,03.4... get validated but can't get converted into a float.
Is there a way to tell QDoubleValidator to only validate real numbers and not just strings without alphabetical characters?
I basically want to have a validator, that only validates strings, that can get converted to floats
using either the default locale or the german locale.
I have not used the QDoubleValidator so far but I could achieve such behaviour by using a QRegExpValidator:
QRegExpValidator* rxv = new QRegExpValidator(QRegExp("[+-]?\\d*[\\.,]?\\d+"), this);
lineedit->setValidator(rxv);
If you want only convert your content into the float and you don't want locale specifications, you can use QRegExpValidator with next deep regexp.
ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")));
How in qt constrain what can be typed in lineedit? I would i.e. want user to enter just digits but not letters.
Check the setValidator function and the inputMask property. If you need something more complex than just numbers it is very easy to subclass QValidator and use it.
I am making an currency change program where I would be providing exact change to the input amount, for example a value of 23 would be one 20 dollars and 3 one dollar bills
I want to restrict the user to input the value only till 2 decimal places. For example: the valid inputs are
20, 20.4, 23.44 but an invalid input would be 20.523 or 20.000.
How can I do this is C/C++.
I read about one function that is setprecision but that is not what I want, setprecision allows to display the value till that decimal point, it still doesn't stop the user from entering any value.
Is there any way to do this?
Read the amount from the user as a string, either character by character or the entire line, and then check its format, and then convert it.
It's generally easier to let the user type whatever they want followed by the program rejecting the input if it isn't valid rather than restricting what they can type on a keystroke basis.
For keystroke analysis you would need a state machine with 4 states, which we can call Number, Numberdot, Numberdotone, and Numberdottwo. Your code would have to make the proper transitions for all keystrokes, including the arrow keys to move the cursor to some arbitrary place and the Backspace key. That's a lot of work.
With input validation, all you have to do is check the input using a regular expression, e.g. ^(([0-9]+) | ([0-9]+.[0-9]) | ([0-9]+.[0-9][0-9])$. This assumes that "20." is not valid. Then if it's invalid you tell the user and make them do it again.
I do not believe that there is any way to set the library to do this for you. Because of that you're going to have to do the work yourself.
There are may ways you can do this, but the only true way to handle restricting the input is to control reading it in yourself.
In this case you would loop on keyboard input, for ever keystroke you would have to decided if it can be accepted in the context of the past input, then display it. That is, if there is a decimal point you would only accept to more numbers. This also allows you to limit input to numbers and decimal places as well, not to mention input length.
The down side is you will have to handle all the editing commands. Even bare bones you would need to support delete and enter.
This is rather a task for the GUI you are using, than for core C/C++. Depending on your GUI/Web Toolkit you can give more or less detailed rules how data can or can not be entered.
If you are writing a normal GUI application you can control and modify the entered keys (in C or C++).
In a WEB application you can do similar things using javascript.
The best solution would be when all illegal input is impossible.