Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-74224

Mistake in the double SpinBox example



    • Bug
    • Resolution: Fixed
    • P2: Important
    • 6.6
    • 5.12.0
    • Documentation
    • None


      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.


        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.



              docteam Qt Documentation Team
              henning Henning Gr√ľndl
              0 Vote for this issue
              2 Start watching this issue



                Gerrit Reviews

                  There are no open Gerrit changes