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

DoubleSpinBox

    XMLWordPrintable

Details

    Description

      https://doc.qt.io/qt-5.10/qml-qtquick-controls2-spinbox.html#details has an example of a spinbox that accepts floating point numbers, but it's not easy to extend. For example, it's not clear how to modify the code to allow entering small fractions (like 0.0001). Some answers on Stack Overflow come close, but it's hard to do it properly without entering C++.

      For example, code based on this answer almost works, except that realValue is never written to, and can't be written to without a bi-directional binding:

      import QtQuick 2.9
      import QtQuick.Controls 2.2
      
      import App 1.0
      
      ApplicationWindow {
          width: 800
          height: 800
          visible: true
      
          SpinBox {
              id: control
              from: 0
              editable: true
              stepSize: realStepSize * factor
              value: realValue * factor
              to : realTo * factor
              anchors.centerIn: parent
      
              property int decimals: 6
              property real factor: Math.pow(10, decimals)
              property real realValue: 0.0
              property real realFrom: 0.0
              property real realTo: 100.0
              property real realStepSize: .0001
      
              onValueModified: print(realValue)
      
              validator: DoubleValidator {
                  bottom: Math.min(control.from, control.to) * control.factor
                  top:  Math.max(control.from, control.to) * control.factor
              }
      
              textFromValue: function(value, locale) {
                  return Number(value * 1.0 / control.factor).toLocaleString(locale, 'f', control.decimals)
              }
      
              valueFromText: function(text, locale) {
                  return Number.fromLocaleString(locale, text) * factor
              }
          }
      }
      

      It would be much nicer if we could offer something out of the box.

      Attachments

        Issue Links

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

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              mitch_curtis Mitch Curtis
              Votes:
              24 Vote for this issue
              Watchers:
              17 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes