Unsolved How to restrict QSpinBox value to 1 to 100?
-
I have tried setting ui->spinBox->setRange(1,100).But the spinBox is accepting 0.I dont want my spinBox object to take 0.I also tried setMinimum(1).Its not working.Why is this improper behaviour happening?I also tried setting the QSpinBox object's range in designer mode.It is still taking 0.Please suggest
Typo:- *setMinimum(1) @JonB
-
I dont want my spinBox object to take 0.I also tried setMinimum(0).Its not working.
If you do not want it to accept 0, why would you
setMinimum(0)
? -
Hi,
The input is "allowed" however the value is not changed to 0 and will go back to the last valid input made.
0 is a "valid" input because you have to be able to use it for number like 10 and 100.
-
How to restrict a spinBox to not enter 0's?
@JonB -
-
I think he want to set a validate on spinbox to avoid invalid number on view, so,
probably we want to implement a regExp like[1-9][0-9][0-9]
-
@KillerSmath
How would your regexp allow a value like, say,23
? How would it prevent, say,999
? -
I have the feeling, we had this question recently 🤔
@Sriu1
if you really want to restrict the input of leading 0's you probably will have to do this yourself by, for example, overwriting QAbstractSpinBox::validate -
@J.Hilk
A while back I posted (https://forum.qt.io/topic/85305/validation-behaviour-too-confusing-for-end-user) how I feel so unfamiliar about the way Qt validators work when typing into a line edit, which would apply to a spin box's edit too.For example, if your validator does indeed forbid a leading 0:
- Type a
6
. - Type a
0
. - You realize you meant
50
rather than60
. - So you try to delete the leading
6
, to then type in a5
. - But you can't! As you delete the leading
6
, the number now starts with a0
, so the validator prevents the delete. The user gets no feedback as to why. - To achieve the change, the user must first delete the
0
, followed by the6
, before he can then type in50
.
This is most unnatural, and regrettably my users are so confused I had to remove the validator.... So OP beware!
- Type a
-
@JonB said in How to restrict QSpinBox value to 1 to 100?:
@KillerSmath
How would your regexp allow a value like, say,23
? How would it prevent, say,999
?I just said a hypothetical example.
-
@KillerSmath
Oh, OK, some posters here will type in whatever you offer and then complain if it doesn't work as-is! -
@JonB said in How to restrict QSpinBox value to 1 to 100?:
@KillerSmath
Oh, OK, some posters here will type in whatever you offer and then complain if it doesn't work as-is!an easy mode to write this regular expression is
0|100|[1-9][0-9]?
-
@KillerSmath
Yep. For completeness, since the OP states he wants to disallow0
, if he wants to copy this it should be100|[1-9][0-9]?
. -
@JonB said in How to restrict QSpinBox value to 1 to 100?:
@KillerSmath
Yep. For completeness, since the OP states he wants to disallow0
, if he wants to copy this it should be100|[1-9][0-9]?
.Yep, thank you, i forgot this part. xD
-
To be correct:
^[1-9][0-9]?$|^100$
. Otherwise you'll be matching "numbers in numbers" -
@SGaist said in How to restrict QSpinBox value to 1 to 100?:
To be correct:
^[1-9][0-9]?$|^100$
. Otherwise you'll be matching "numbers in numbers"Yes, use the delimiters to apply this regular expression on full string.
-
To be correct:
^[1-9][0-9]?$|^100$
. Otherwise you'll be matching "numbers in numbers"But for
QAbstractSpinBox::validate()
I assume it uses http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details, which states:QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors; in other words, it always attempts to do an exact match.
?
-
@JonB said in How to restrict QSpinBox value to 1 to 100?:
To be correct:
^[1-9][0-9]?$|^100$
. Otherwise you'll be matching "numbers in numbers"But for
QAbstractSpinBox::validate()
I assume it uses http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details, which states:QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors; in other words, it always attempts to do an exact match.
?
Yes, i found the Qt reposity and i noticed
QAbstractSpinBox::validate()
returns for defaultQValidator::Acceptable
without verification.if the input is not validated to QValidator::Acceptable when Return is pressed or interpretText() is called.
-
@KillerSmath
Not sure your comment is against what I meant?
I was asking @SGaist why it is necessary to put in his^
&$
in the reg exp given that the docs for http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details (seem to me to) state that it will do full match anyway? UnlessQAbstractSpinBox::validate()
does not useQRegularExpressionValidator
? -
@JonB That was for direct
QRegularExpression
use.