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 Disable ScientificNotation on TextField
QtWS25 Last Chance

How Disable ScientificNotation on TextField

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 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.
  • B Offline
    B Offline
    Bensuperpc
    wrote on last edited by Bensuperpc
    #1

    I would like to know how to disable ScientificNotation in a TextField, when I enter a large number (Integer), eg: "1000000", it is automatically transformed into "1e+05".
    This ignores RegularExpressionValidator, the textfield no longer works on the large numbers.

    I tried with validator: DoubleValidator.StandardNotation and other solutions but it doesn't work.

    I'm using Qt 6.2.4 with QtQuick 2.

    Part of my code:

    TextField {
    	id: minRangeValue
    	placeholderText: qsTr("Enter minimal range value")
    	text: myapp_ui.minRangeValue
    	selectByMouse: true
    	validator: RegularExpressionValidator {
    		regularExpression: /[0-9]+/
    	}
    }
    Binding {
    	target: myapp_ui
            // minRangeValue is uint64_t 
    	property: "minRangeValue"
    	value: minRangeValue.text
    }
    

    fffdfdsfsdfds.PNG

    L J.HilkJ 2 Replies Last reply
    0
    • B Bensuperpc

      I would like to know how to disable ScientificNotation in a TextField, when I enter a large number (Integer), eg: "1000000", it is automatically transformed into "1e+05".
      This ignores RegularExpressionValidator, the textfield no longer works on the large numbers.

      I tried with validator: DoubleValidator.StandardNotation and other solutions but it doesn't work.

      I'm using Qt 6.2.4 with QtQuick 2.

      Part of my code:

      TextField {
      	id: minRangeValue
      	placeholderText: qsTr("Enter minimal range value")
      	text: myapp_ui.minRangeValue
      	selectByMouse: true
      	validator: RegularExpressionValidator {
      		regularExpression: /[0-9]+/
      	}
      }
      Binding {
      	target: myapp_ui
              // minRangeValue is uint64_t 
      	property: "minRangeValue"
      	value: minRangeValue.text
      }
      

      fffdfdsfsdfds.PNG

      L Offline
      L Offline
      lemons
      wrote on last edited by lemons
      #2

      @Bensuperpc here an example explaining the validator and input text part.
      Note, that the text you set is not covered by the validator, but only the manual inputs. You could even set the text to "Hello World" with a DoubleValidator or IntValidator in place.

      Column {
          anchors.fill: parent
          anchors.margins: 20
          TextField {
              width: parent.width
              color: "red"
              // allows numbers and characters for scientific notation to be entered
              // allows decimal character of locale
              validator: DoubleValidator {
                  notation: DoubleValidator.ScientificNotation
              }
              // use the number type to display the number in the proper string format
              // https://doc.qt.io/qt-6/qml-qtqml-number.html
              // this also sets the proper decimal character for different languages
              text: Number(0.00000000000001).toLocaleString(Qt.locale(), 'f', 14)
              onTextChanged: {
                  if (!acceptableInput) {
                      console.debug("Input not accepted")
                      return
                  }
      
                  console.debug("Input accepted")
      
                  // get the proper number from the input string
                  let number = Number.fromLocaleString(Qt.locale(), text)
                  console.debug("number:", number)
              }
          }
          TextField {
              width: parent.width
              color: "blue"
              // does not let you enter e.g. "e"
              // allows decimal character of locale
              validator: DoubleValidator {
                  notation: DoubleValidator.StandardNotation
              }
              text: Number(0.00000000000001).toLocaleString(Qt.locale(), 'f', 14)
              onTextChanged: {
                  if (!acceptableInput) {
                      console.debug("Input not accepted")
                      return
                  }
      
                  console.debug("Input accepted")
      
                  // get the proper number from the input string
                  let number = Number.fromLocaleString(Qt.locale(), text)
                  console.debug("number:", number)
              }
          }
      }
      

      For IntValidator you can do it simlar:

      TextField {
          width: parent.width
          color: "red"
          validator: IntValidator {
              locale: Qt.locale().name
              bottom: 1
              top: 999999999
          }
          text: Number(100000000).toLocaleString(Qt.locale(), 'f', 0)
          onTextChanged: {
              if (!acceptableInput) {
                  console.debug("Input not accepted")
                  return
              }
      
              console.debug("Input accepted")
      
              // get the proper number from the input string
              let number = Number.fromLocaleString(Qt.locale(), text)
              console.debug("number:", number)
          }
      }
      
      1 Reply Last reply
      1
      • B Bensuperpc

        I would like to know how to disable ScientificNotation in a TextField, when I enter a large number (Integer), eg: "1000000", it is automatically transformed into "1e+05".
        This ignores RegularExpressionValidator, the textfield no longer works on the large numbers.

        I tried with validator: DoubleValidator.StandardNotation and other solutions but it doesn't work.

        I'm using Qt 6.2.4 with QtQuick 2.

        Part of my code:

        TextField {
        	id: minRangeValue
        	placeholderText: qsTr("Enter minimal range value")
        	text: myapp_ui.minRangeValue
        	selectByMouse: true
        	validator: RegularExpressionValidator {
        		regularExpression: /[0-9]+/
        	}
        }
        Binding {
        	target: myapp_ui
                // minRangeValue is uint64_t 
        	property: "minRangeValue"
        	value: minRangeValue.text
        }
        

        fffdfdsfsdfds.PNG

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

        @Bensuperpc do a proper full conversion to a string and don't rely on the automated stuff.

        than you can force the full length of the string:

        text: myapp_ui.minRangeValue.toLocaleString('fullwide', {useGrouping:false})
        

        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
        • B Offline
          B Offline
          Bensuperpc
          wrote on last edited by
          #4

          It works, thank you for your answers, @J-Hilk 's solution fits my need more, thank you anyway for your answer @lemons :)

          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