Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How is the QML floating point SpinBox example supposed to be used?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • YosemiteY Offline
    YosemiteY Offline
    Yosemite
    wrote on last edited by Yosemite
    #1

    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?

    J.HilkJ 1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      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

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      1 Reply Last reply
      2
      • YosemiteY Yosemite

        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?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        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
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • YosemiteY Offline
          YosemiteY Offline
          Yosemite
          wrote on last edited by
          #4

          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.

          J.HilkJ 1 Reply Last reply
          0
          • YosemiteY Yosemite

            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.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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)
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved