Unsolved Overwriting QDoubleSpinBox's validate() does not prohibit input
-
@qwasder85 said in Overwriting QDoubleSpinBox's validate() does not prohibit input:
setRegularExpression
First thought: in the examples e.g. https://doc.qt.io/qt-5/qregularexpressionvalidator.html#details you will see their reg exs are not anchored. Indeed it states:
QRegularExpressionValidator
automatically wraps the regular expression in the\\A
and\\z
anchors; in other words, it always attempts to do an exact match.Try removing your leading/trailing
^
&$
, any better? Because if the Qt code really does what it states, it will be demanding you type in^
&$
:) -
@JonB Thank you. But no, unfortunately, makes no difference.
-
@qwasder85
Hmm, I'm surprised that is not the issue?! I agree the reg ex looks reasonable when I scan it.- Put in
qDebug()
to show the text it is validating against. - Break it down. Start with, say, just
.
(dot) as the expression. How does that behave? Build from there. I have used validators and they worked as document for me; but these were onQLineEdit
s, notQSpinBox
es. The way Qt validators work is they should actually stop you typing unacceptable characters in, that does not seem to be happening to you?
Maybe, being a spin box, it's not interested in your regular expression and just does it's own stuff. When you say "correct inputs produce 0", does that include a legitimate floating point number in your own locale?
1
would be a good example :) - Put in
-
@qwasder85 If I remember correctly, the validating is not done during input (for a QDoubleSpinBox) but on editingFinished(). So when you press enter or the spin box looses active focus.
-
@J-Hilk Hi, I figured that and tried a different approach. I have a custom QLineEdit control that CAN validate on input changed. If I use this control alone instead of the SpinBox, it works.
If I now pass this control to the QSpinBox's setLineEdit()-function, it stops working again.
There seems to be an additional abstraction layer between the SpinBox's input field and the QLineEdit behind it (maybe because the SpinBox can add pre/suffixes to the field?). -
@qwasder85 maybe, I'm not overly familiar with the QCombobox source code 😕
-
@qwasder85 said in Overwriting QDoubleSpinBox's validate() does not prohibit input:
The RegEx permits both positive and negative numbers with a limitation to the number of digits on either side of the decimal separator, which can be a dot or comma.
You don't need a validator for this, you can just use
QDoubleSpinBox::setRange
to limit the upper/lower bounds,QDoubleSpinBox::setDecimals
to set how many decimal places to accept finallyQDoubleSpinBox::locale()
takes care of chosing the appropriate decimal separator as suggeted by the system of the userP.S.
A decimal numeber with 64 digits is HUGE and won't fit in any non-specially-devised storage type -
@VRonin My spinbox needs to be able to handle commas AND dots as decimal separator. That's the crux. :D
-
Okay, seems that I've figured it out. Get this:
When you set a suffix for the QSpinBox, the custom validation stops working. Because, apparently, the spinbox tries to validate the value WITH suffix (which is clearly wrong).
If you check the cleanText()-function, it returns the value WITH suffix. Even though the documentation specifically states that the returned value excludes pre/suffix!Thanks Qt, very cool!
-
@qwasder85 time for a bug report :)