1

I need a spinbox which accept dots and also commas as a decimal separator.

I've changed the Locale settings: self.setLocale(QLocale(QLocale.C))

Because of that, my spinbox accept a dot as a decimal separator. A comma also shows up on screen when the comma button is pressed, but it disappears after editing.

I've redefined the keyPressEvent. Now, when I use the comma button, it simulates the dot key and calls the original keyPressEvent of the QDoubleSpinBox. But nothing happens on the spinbox screen, and I don't know what I missed.

Does anyone see the problem?

class MyDoubleSpinBox(QDoubleSpinBox):

    def __init__(self):

        super().__init__()

        self.setValue(0.00)
        self.setSingleStep(0.01)
        self.setMaximum(1000)

    def keyPressEvent(self, in_event):  # 46 = dot; 44 = comma:
        if in_event.key() == 44:
            self.keyPressEvent(QKeyEvent(QEvent.KeyPress, 46, Qt.NoModifier, 0, 0, 0))

        else:
            print(in_event.key()) # it's working (46 even if I press comma)
            QDoubleSpinBox.keyPressEvent(self, in_event)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Nelli
  • 11
  • 2
  • I don't understand why you want to modify the keyPressEvent – eyllanesc Aug 14 '19 at 11:01
  • I couldn't find a way to use comma and dot too, so I want to 'fool' the spinbox and pretend dot key event, even if the user press the comma button. (If you could suggest a way to use both separator, it would be much better. I've red somewhere that I could rewrite the validate function but I don't know how.) – Nelli Aug 14 '19 at 13:58
  • This issue here can be seen as "thousands/group" separator. That should be given a checkbox option so it can get disabled and thus automatically accepts both separators. Perhaps you can set the keyboard country setting for the doublespinbox as a loophole? – ZF007 May 12 '20 at 16:25

1 Answers1

-1

You should override validate() and valueFromText() methods instead, see https://stackoverflow.com/a/72054836/12108865

kpsylock
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 18 '22 at 02:44