Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
5.12.0
-
None
Description
The code snippet of the double SpinBox example on SpinBox QML Type contains a bug. The example uses a DoubleValidator as shown in the following code snippet
SpinBox { id: spinbox from: 0 value: 110 to: 100 * 100 stepSize: 100 anchors.centerIn: parent property int decimals: 2 property real realValue: value / 100 validator: DoubleValidator { bottom: Math.min(spinbox.from, spinbox.to) top: Math.max(spinbox.from, spinbox.to) } textFromValue: function(value, locale) { return Number(value / 100).toLocaleString(locale, 'f', spinbox.decimals) } valueFromText: function(text, locale) { return Number.fromLocaleString(locale, text) * 100 } }
This DoubleSpinBox implementation basically doesn't use double/real, but int to work around the limitation of the base SpinBox which only accepts int values for the from, to and value properties. This is why the DoubleSpinBox example shows how to circumvent that by overriding the textFromValue and valueFromText callbacks. Those callbacks either multiply by 100 or divide by 100 depending on which direction the callback is aiming for, either text to value or value to text. If it wants to set a double/real value on the SpinBox and needs to convert it to int before setting it or the other way around. The factor for the example is 100, because the decimals property is set to 2.
The DoubleValidator expects values of type real for its from and to properties, but in the example it gets the from and to property from the SpinBox which are of type int. This means after the Math.min() and Math.max() operation the value needs to be divided by 100. Otherwise the DoubleValidator isn't working properly.
Attachments
Issue Links
- depends on
-
QTBUG-67349 DoubleSpinBox
- Reported
- relates to
-
QTBUG-99268 Make SpinBox more flexible to allow double precision values
- Closed