How to make the sign-key (+/-) to negate a number?

If I add inputMethodHints: Qt.ImhDigitsOnly in a TextField{}, the numeric key pad opens. But the sign key +/- does not change the sign of the number. It only adds a ‘+’ at the current cursor position or ‘-’ with a double click.

Is this a bug, a feature request, or is there something like EnterKey.onClicked that could be used?

Using onTextChanged for changing the sign of the number does not sound optimal.

Well, i don’t think that’s its intended use, so i can’t imagine there being special handling for it ready to be hooked in. I don’t understand what you mean with suggesting current behavior could be a bug… So, some hack, like what you describe, is probably the only way.

I guess the problem is different semantics of the key(s) used in a numeric keypad: in a traditional calculator keypad, there must be separate keys for addition, subtraction, and “toggle sign of current numeric value”.

But the numeric keyboard in general is not necessarily a “calculator input” keyboard like that.

1 Like

Yes, TextField could be used for inputing something else than a numeric value, even though the characters were only numbers and minus signs.

Quite complicated. So not a bug, and perhaps I don’t make it a feature request. Quite much work needed.

Here’s what I have in the onTextChanged-event, if anyone else is in need.

        // changes needed only, if the sign is not the first character
        if (text.indexOf("+", 1) > 0 ||
               text.indexOf("-", 1) > 0) {
          var nbr, sign, strs, txt;
          // the sign after the latest +/-
          if (text.charAt(0) === "-") {
              sign = 1
              txt = text.substring(1)
           } else {
               sign = -1
               if (text.charAt(0) === "+") {
                   txt = text.substring(1)
               } else {
                   txt = text
               }
           }
           if (txt.indexOf("+") > 0) {
               strs = txt.split("+")
           } else {
               strs = txt.split("-")
           }

           if (strs.length >= 2) {
               txt = strs[0] + strs[1]
           } else if (strs.length === 1) {
               txt = strs[0]
           }
           if (!isNaN(txt*1.0)) {
               _editor.text = sign*txt*1.0
           } else {
               if (sign < 0) {
                   _editor.text = "-"
               } else {
                   _editor.text = ""
               }
           }
       }

Not simple either.

I guess a ‘proper’ solution would be to add a new input method hint to Qt.

Something like Qt::ImhArithmetic and have the vkb present a calculator-like layout then.