How Disable ScientificNotation on TextField
-
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 }
-
@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) } }
-
@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})