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. Problem with SpinBox and double values in QML
Forum Updated to NodeBB v4.3 + New Features

Problem with SpinBox and double values in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml
2 Posts 2 Posters 1.7k Views 1 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.
  • B Offline
    B Offline
    Babs
    wrote on last edited by Babs
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • B Babs

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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