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.
Related
I have an EditBox in a MFC-dialog. The user is supposed to enter a number. I'm trying to automatically add separators to the number while the user is inputting it:
When the number is more than 3 digits long, a separator is added between the hundreds and the thousands digit; a second one between the hundredthousands and the millions when it gets longer than 6 digits and so forth (so that 1234567 becomes 1,234,567 for example).
This is done in a function executed by ON_EN_CHANGE and basically works fine already. But the problem is that the caret position is set to the beginning of the EditBox once my function changes the length of the string in it, preventing continous typing.
I tried simulating the press of the end-key to send the caret to the end of the EditBox, which works as long as the user only enters a number from left to right. But it won't work when the user is trying to add, remove or edit digits in the middle of the number. I need the caret position at the exact spot of the number where it was before the user pressed a key.
I tried calculating the new caret position from the previous one (gotten with CEdit::GetSel()) and the previous length of the number:
OnEnChange()
{
int prevCursPos {HIWORD(m_editCtrl.GetSel())};
int prevStrLen {m_editCtrl.GetWindowTextLengthW()};
UpdateData(TRUE);
// Adding/Removing of separators as needed
UpdateData(FALSE);
int difference {m_editCtrl.GetWindowTextLengthW() - prevStrLen))};
if(difference > 0) // a separator has been added to the string
m_editCtrl.SetSel(-1, prevCursPos + 1);
}
However, the SetSel() function doesn't seem to have any effect. I also tried to send an EM_SETSEL message instead, but I couldn't figure out how to make that work either, the caret always resets itself to the beginning of the EditBox.
Does anyone have another idea on how to accomplish what I'm trying to do?
It is unclear how you are handling "/ Adding/Removing of separators as needed".
SetSel with the first parameter set to -1 will position the caret at the beginning of the string if the string changes after calling UpdateData(false)
Create CString type of the variable (m_csEdit for example) for this edit control and
int iLen = m_csDDx.GetLength();
m_editCtrl.SetSel(iLen, -1);
or
m_editCtrl.SetSel(iLen, iLen);
You can calculate the length differently from what I suggested.
Consider using masked edit control (maybe).
Forgot to mention. Use GetNumberFormatEx to format number with thousand delimiter.
I want to take input from the user in between the () in the given "cout" statement, in place of the '_'
cout<<"Warning! you are going to resize, which may result in loss of data are you sure you want to continue: Y/N(_)";
How may I be able to do it? Are there any escape sequence for doing it? Or any other way?
Use this code fragment:
scanf("(%s)",&str)
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 need to take time as user input in the form HH:MM and then validate it.
It needs to be a proper time in that certain format. Any good Ideas on how to do that?
I'm trying to make a function that will iterate through the string, validating each character, then convert them into numbers (or some kind of time stamp) so I can compare several strings to eachother.
I'm only using the std namespace.
Use boost::regex to match string and its parts (HH) and (MM) and use scanf to get hours and minutes.
It sounds more like an algorithm problem, I would:
1, check the length of the string if it's 5.
2, check if ':' is in the middle.
3, check HH is in the range.
4, check MM is in the range.
5, Convert it to the format which will bring convenience to you.
It may be overkill for this particular problem, but this kind of task is a great fit for a state machine. Basically, you'll want to read the input one character at a time, and each character can change the machine's state until you end up in a success or error state. For example:
First character
If not a number, change to error state
Otherwise store value and change to state 2
Second character
If not a number, change to error state
Otherwise multiply stored value by 10 and add second character. If the result is out of range, change to error state. Otherwise, change to state 3
Third character
If :, change to state 4, otherwise change to error state
Fourth character
Similar to First character, changing to state 5 upon success.
Fifth character
Similar to Second character, changing to state 6 upon success.
Success state
A winner is yuo!
Error state
Handle the error, duh.
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.