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.
Related
Let me clarify the question. I need to process keyboard input on the fly. So if the user presses something like the home button or the delete button I need to ignore the input. But any normal printable symbol I want to catch. I get the input in the text field() method of a QKeyEvent.
I've tried two regexps
The first one was:
QRegExp("[\\S]");
Which did not work as apparently pressing delete is considered a non-whitespace character.
The second one was
QRegExp("[\\w]");
This one was much better as it would only accept letters, number and underscore but no symbols such as + or *. Which I want to accept.
I was thinking of modifying the last expression to include any symbol in my keyboard. But how would I do that? Or how can I write an RegExp for what I want?
I'd like Alexa to be able to accept a variable-length list of English letters to my custom skill. It will allow users to search based on a string.
There's two steps to this:
Getting good representation for individual letters that Alexa can understand
Enumerating sample utterances with variable number of letters
For the first, one way would be to define a custom slot that has as its enumerated values of the English alphabet:
SLOT_LETTER
ay
bee
see
dee
ee
eff
gee
... etc
but that feels hacky. Does Amazon support any way to do this or is there a cleverer way?
I'd really rather not use NATO phonetic ("alpha bravo charlie" for "A-B-C") because it's a terrible user experience and very few people actually know them.
For the second issue (sample utterances), for AMAZON.LITERAL I want to define something like:
SpellIntent find me things starting with {first second|SLOT_LETTER}
SpellIntent find me things starting with {first second third|SLOT_LETTER}
SpellIntent find me things starting with {first second third fourth|SLOT_LETTER}
But I don't think Amazon will let you define a variable length LITERAL using a custom slot (since they are different "types")?
It isn't very well documented, but you can use "a.", "b.", "c." etc to represent the letter, as opposed to the sound. Create a custom slot and use these as the values. That should do you for the slot.
For the intent, create an intent with, say, five slots, all with the same slot type. Create five utterances against the intent, with one, two, three, four and five slots filled. When the user spells something, the intent will be invoked. Any slots the user did not specify will be null.
Having two slots in one intent not separated by a word often does not perform well. But try it and see. With a restricted vocabulary like this, it could do OK.
Lastly, if it has trouble distinguishing, say, "b." and "v.", you might try adding NATO call codes to your list. Alpha, Bravo, Charlie, etc. Then, in your processing, just take the first character of whatever value comes in for the slot.
You might enable the "Star Lanes" skill and experiment with the "Set Call Sign to X Y Z" intent. I do the above in that skill and it works fairly well.
I would avoid this kind of interface because it can be difficult for users to spell things, and difficult for Alexa to differentiate letters (b or v, perhaps). But if you want to try this, consider using the literal type and asking for letters in groups of three or four.
AV: "Spell the first four letters now"
User: "see ay are tee"
AV: "You spelled C, H, R, T. Would you like to add more letters, make a correction or search now?"
User: "Cancel. I'll just use my damn phone."
I need the user to a single letter 'A' plus 8 digits and no more. Eg. A00000000
Here is my code below kind of works but I want to limit the number of digits to 8. I am using MVC 4.
[RegularExpression("^[A][0-9]{8}$", ErrorMessage="Required format: A00000000")]
Although it does validate the user can keep typing forever. So there is no limit on the number of characters. It would be nice to stop the user from entering once it hits the maximum of 8 digits.
Thank you for reading.
It's not possible to limit the number of characters using regex alone, but you could use the maxlength attribute to specify the maximum number of characters allowed in your <input> element:
<input type="text" name="myInput" maxlength="9">
Unless you implement it yourself, a regex match can only operate on a string you have. And you won't have it before the user has typed it.
So if you want to prevent him from typing, you're going to have to do it before you get to the regex.
If you're trying to stop someone from typing more on something like say a textbox on a webpage, you would have to hook into the keypress event (keyup or keydown). Then once the validation fails, return "false" from the keypress event handler and it will stop the character from entering the textbox, or you may need to use preventDefault on the incoming event object (e.g. e.preventDefault()). If you're using jQuery, have a look at this webpage: https://api.jquery.com/keypress/ on how to set the event handler.
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]+")));
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.