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

How is the QML floating point SpinBox example supposed to be used?



  • Nobody else seems to have trouble with this, so I'm sorry - but I don't understand how the floating point SpinBox example on this page is intended to be used.

    OnValueChanged gives correct values from the SpinBox, but when changing value from the outside (e.g. from a bound property), value is two decimal places off. The valueToText callback is apparently not invoked when value is changed directly. Setting realValue also won't update the SpinBox, so what is the intended usage of this example?



  • Hi @Yosemite ,you mean to tell that when you change the value from outside, you are not getting the decimal part?What values are you assigning to the spinbox.

    Note:-The spinbox values are basically integer values, they are not decimal, so if you give 220, it will be displayed as 2.20.

    spinbox.value = 220
    

    if value = 1, then displayed value = 0.01
    if value = 10, then displayed value = 0.10
    if value = 100, then displayed value = 1.00


  • Moderators

    hi @Yosemite

    from my understanding of that example, the idea is to interpret the int value - spin boxes operate on integers - as a floating point and interpret a floating point as an int.

    It's all only for the user. You yourself will have to operate on ints.

    But you can read the real value via the realValue property but setting it programmatically will have to be done via int*decimals.

    or you define your own "setter" function:

    function setRealValue( v ) {
        if (decimals != 0 )
           value = v * (decimals * 10)
        else
           value = v
    }
    


  • Hi @Shrinidhi-Upadhyaya and @J-Hilk , thank you for your responses, I love the thoughtfulness of the replies on this forum. The integer representation makes sense, I'm still confused on how to interface with it. It seems like it runs afoul of the way QML to C++ bindings work. Am I wrong about that?

    Here's where I'm coming from; I'm using this with a C++ class that has all of it's properties exposed with Q_PROPERTY. This particular one is:

    
        Q_PROPERTY(float value READ value WRITE setValue NOTIFY valueChanged)
    

    And in the QML class that has a reference to the C++ implementation I bound many properties thusly:

    // Value changed from QML         
    onValueChanged: implementation.frequency.value = value
    // Value changed from C++
    value: implementation.frequency.value
    

    I love this syntax, it is tidy and very maintainable. How would I use this floating point SpinBox to get something similar? Perhaps I can bind directly to the setter function @J-Hilk wrote and I just haven't figured it out.


  • Moderators

    @Yosemite the simplest would probably be the following

    // Value changed from QML         
    onRealValueChanged: implementation.frequency.value = realValue
    
    // Value changed from C++
    Connections {
         target: implementation.frequency
    
         //Either
         onValueChanged: mySpinBox.value = value * 100
         //Or with the "setter"
         onValueChanged: mySpinBox.value.setRealValue(value)
    }
    

Log in to reply