Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with SpinBox and double values in QML



  • Hello,
    I'm working on a QML project. I have a problem with double values in my QML. I expose a C++ variable to my QML.

                                                SpinBox{
                                                    id:spinPal
                                                    minimumValue:  0
                                                    maximumValue: 10000
    
                                                    decimals: 2
                                                    onValueChanged:  {
                                                        plan.addValue(spinPal.value,index)
                                                        console.log("value added")
                                                    }
                                                }
    

    I set the value to 9.18 however it takes the value 9.
    Can anyone help?


  • Qt Champions 2019

    @Babs SpinBox is for integers, not floating point numbers. But you can use floating point numbers as shown in the documentation: https://doc.qt.io/qt-5/qml-qtquick-controls2-spinbox.html

    In the same manner, SpinBox can be customized to accept floating point numbers:
    
    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
        }
    }
    

Log in to reply