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 to send a double through a TextInput
QtWS25 Last Chance

problem to send a double through a TextInput

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 1.3k Views
  • 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.
  • C Offline
    C Offline
    cosmoff
    wrote on last edited by
    #1

    Hello everyone,

    Like I said, I would like to retrieve a double from a TextInput but I retrieve only the rounded value.

    I show you my code :

    Rectangle{
                    color : "green"
                    height : parent.height/4
                    width : parent.width
    
                    TextInput{
    
                        id: idTextInput
                        inputMethodHints: Qt.ImhFormattedNumbersOnly
                        padding : 20
                        text: controller.value
                        font.pixelSize: 13
                        font.bold: false
                        color: "black"
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.horizontalCenter: parent.horizontalCenter
                        validator: DoubleValidator{bottom: 0 ;
                                                  top: 100;
                                                  decimals : 3
                                                  notation: DoubleValidator.StandardNotation}
    
    
                        onAccepted: {
                            controller.newValuefromTextInput( parseFloat(text) )
                            controller.value = parseFloat(idTextInput.text)
                            console.log("value  = " + text)
                        }
    
    
                    }
                }
    

    and the methode newValuefromTextInput from controller is :

    void Controller::newValuefromTextInput(double v)
    {
        qDebug() << "value = " << v ;
    }
    
    Do you have an idea of the problem?
    
    Thanks
    
    1 Reply Last reply
    0
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by Pradeep P N
      #2

      Hi @cosmoff

      Code looks fine, it do print the double value in the C++

      • QML
              TextInput {
                  id: idTextInput
      
                  focus: true
                  anchors.fill: parent
                  verticalAlignment: TextInput.AlignVCenter
                  font {
                      pointSize: 15;
                      bold: true;
                  }
      
                  validator: DoubleValidator {
                      top: 100; // 100 is the maximum acceptable value (More than 100 its not acceptable & onAccepted will not be called). 
                      bottom: 0;
                      decimals: 3;
                      notation: DoubleValidator.StandardNotation
                  }
      
                  onAccepted: {
                      console.log("value  = " + text)
                      cppObject.doubleValueQML(idTextInput.text)
      
                  }
              }
      
      • main.cpp
          engine.rootContext()->setContextProperty("cppObject", &cppClass);
      
      • MyClass.h & MyClass.cpp - I am using public slots function to QML
      public slots:
          void doubleValueQML(double value);
      
      
      void MyClass::doubleValueQML(double value)
      {
          qDebug() << Q_FUNC_INFO << value << endl;
      }
      

      Output:

      QML debugging is enabled. Only use this in a safe environment.
      qml: value  = 100.000
      void MyClass::doubleValueQML(double) 100 
      
      qml: value  = 99.99
      void MyClass::doubleValueQML(double) 99.99 
      
      qml: value  = 99.100
      void MyClass::doubleValueQML(double) 99.1 
      
      qml: value  = 99.001
      void MyClass::doubleValueQML(double) 99.001 
      
      qml: value  = 99.999
      void MyClass::doubleValueQML(double) 99.999 
      

      All the best.

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      C 1 Reply Last reply
      2
      • Pradeep P NP Pradeep P N

        Hi @cosmoff

        Code looks fine, it do print the double value in the C++

        • QML
                TextInput {
                    id: idTextInput
        
                    focus: true
                    anchors.fill: parent
                    verticalAlignment: TextInput.AlignVCenter
                    font {
                        pointSize: 15;
                        bold: true;
                    }
        
                    validator: DoubleValidator {
                        top: 100; // 100 is the maximum acceptable value (More than 100 its not acceptable & onAccepted will not be called). 
                        bottom: 0;
                        decimals: 3;
                        notation: DoubleValidator.StandardNotation
                    }
        
                    onAccepted: {
                        console.log("value  = " + text)
                        cppObject.doubleValueQML(idTextInput.text)
        
                    }
                }
        
        • main.cpp
            engine.rootContext()->setContextProperty("cppObject", &cppClass);
        
        • MyClass.h & MyClass.cpp - I am using public slots function to QML
        public slots:
            void doubleValueQML(double value);
        
        
        void MyClass::doubleValueQML(double value)
        {
            qDebug() << Q_FUNC_INFO << value << endl;
        }
        

        Output:

        QML debugging is enabled. Only use this in a safe environment.
        qml: value  = 100.000
        void MyClass::doubleValueQML(double) 100 
        
        qml: value  = 99.99
        void MyClass::doubleValueQML(double) 99.99 
        
        qml: value  = 99.100
        void MyClass::doubleValueQML(double) 99.1 
        
        qml: value  = 99.001
        void MyClass::doubleValueQML(double) 99.001 
        
        qml: value  = 99.999
        void MyClass::doubleValueQML(double) 99.999 
        

        All the best.

        C Offline
        C Offline
        cosmoff
        wrote on last edited by cosmoff
        #3

        @Pradeep-P-N

        thanks for your answer,

        I do not understand how you code can work because you insert a string in your function MyClass::doubleValueQML(double) which need a double. So I put a parseFloat() But like I said the function seems to round the value

        Pradeep P NP 1 Reply Last reply
        0
        • C cosmoff

          @Pradeep-P-N

          thanks for your answer,

          I do not understand how you code can work because you insert a string in your function MyClass::doubleValueQML(double) which need a double. So I put a parseFloat() But like I said the function seems to round the value

          Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on last edited by
          #4

          @cosmoff said in problem to send a double through a TextInput:

          parseFloat

          I tried both the ways, It works properly even with parseFloat(idTextInput.text)

                      onAccepted: {
                          console.log("value  = " + text)
                          cppObject.doubleValueQML(parseFloat(idTextInput.text))
                      }
          
          • Output:
          QML debugging is enabled. Only use this in a safe environment.
          qml: value  = 11.11
          void MySpinBoxValue::doubleValueQML(double) 11.11 
          
          qml: value  = 22.22
          void MySpinBoxValue::doubleValueQML(double) 22.22 
          
          qml: value  = 99.100
          void MySpinBoxValue::doubleValueQML(double) 99.1 
          
          qml: value  = 99.999
          void MySpinBoxValue::doubleValueQML(double) 99.999 
          

          0_1560758411195_fd5f4f0d-a198-4ec2-9cd6-9df150174953-image.png

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          C 1 Reply Last reply
          0
          • Pradeep P NP Pradeep P N

            @cosmoff said in problem to send a double through a TextInput:

            parseFloat

            I tried both the ways, It works properly even with parseFloat(idTextInput.text)

                        onAccepted: {
                            console.log("value  = " + text)
                            cppObject.doubleValueQML(parseFloat(idTextInput.text))
                        }
            
            • Output:
            QML debugging is enabled. Only use this in a safe environment.
            qml: value  = 11.11
            void MySpinBoxValue::doubleValueQML(double) 11.11 
            
            qml: value  = 22.22
            void MySpinBoxValue::doubleValueQML(double) 22.22 
            
            qml: value  = 99.100
            void MySpinBoxValue::doubleValueQML(double) 99.1 
            
            qml: value  = 99.999
            void MySpinBoxValue::doubleValueQML(double) 99.999 
            

            0_1560758411195_fd5f4f0d-a198-4ec2-9cd6-9df150174953-image.png

            C Offline
            C Offline
            cosmoff
            wrote on last edited by
            #5

            @Pradeep-P-N
            Yes I understood my problem. In fact, because of validator I cannot fill the textInput with the character ' . ' for the float like 70.5 but only 70,5 and so Qt does not understand the character ' , '

            Do you have an idea why my validator do not want the character ' . '

            J.HilkJ 1 Reply Last reply
            0
            • C cosmoff

              @Pradeep-P-N
              Yes I understood my problem. In fact, because of validator I cannot fill the textInput with the character ' . ' for the float like 70.5 but only 70,5 and so Qt does not understand the character ' , '

              Do you have an idea why my validator do not want the character ' . '

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @cosmoff said in problem to send a double through a TextInput:

              Do you have an idea why my validator do not want the character ' . '

              localization issues.

              I know of 3 ways to solve this.

              • setting the locale property appropriately: https://doc.qt.io/qt-5/qml-qtquick-doublevalidator.html#locale-prop
              • reimplementing valueFromText, with your own parser
              • replacing the , with in your onAccepted method
              onAccepted: {
                                      var modifiedText = text.replace(",",".")
                                      controller.newValuefromTextInput( parseFloat(modifiedText) )
                                      controller.value = parseFloat(modifiedText)
                                      console.log("value  = " + text)
                                  }
              

              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.

              C 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @cosmoff said in problem to send a double through a TextInput:

                Do you have an idea why my validator do not want the character ' . '

                localization issues.

                I know of 3 ways to solve this.

                • setting the locale property appropriately: https://doc.qt.io/qt-5/qml-qtquick-doublevalidator.html#locale-prop
                • reimplementing valueFromText, with your own parser
                • replacing the , with in your onAccepted method
                onAccepted: {
                                        var modifiedText = text.replace(",",".")
                                        controller.newValuefromTextInput( parseFloat(modifiedText) )
                                        controller.value = parseFloat(modifiedText)
                                        console.log("value  = " + text)
                                    }
                
                C Offline
                C Offline
                cosmoff
                wrote on last edited by
                #7

                @J.Hilk

                yes it works with : var modifiedText = text.replace(",",".")

                thanks a lot

                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