QDoubleSpinBox and decimal place

  • I have a QDoubleSpinBox on a form, the inputMethodHints is set to ImhFormattedNumbersOnly. In my class I have the keyPrevvEvent and I can see when the dot is pressed the event is received, however for some reason unknown to me, the control doesn't accept the dot, so if I type in 12.3 I only see 123.

    Why ?

  • Yes, the regular expression will restrict what it can and cannot accept and the keyPressEvent handler will receive the dot and insert it into the line edit control.

  • Lifetime Qt Champion


    What locale are you using ? Did you check if it does the same using , rather that . ?

  • Just checked in the Qt Creator, locale: English, UnitedKingdom

    I'm using a smartCard reader ACR890, it is only has the . on the keypad.

    I just tried changing to UnitedStates, same problem.

  • Lifetime Qt Champion

    Just for fast test, i tried to change it here
    alt text
    and it gladly accepts the "." now. ( and rejects ",")

    I would check what keycode ACR890 sends for that key.

  • I've tried that, it doesn't fix it for me...the interesting thing is that the keyPressEvent does get the dot event when pressed and it shows a key value of 16777268, if I press any of the number keys, none of these are displayed in the keyPressEvent routine but they are excepted by the control.

  • Lifetime Qt Champion

    What keyboard are you using ?

  • Its the ACR890, a card reader:


    I've tried several locale's now including UnitedKingdom, UnitedStates and HongKong, the problem exists with all of those.

  • Lifetime Qt Champion

    Does it happen on the device itself or your getting inputs from the device to your computer ?

  • Sorry, I don't really understand what you are asking, I'm using Qt Creator to deploy the application to the target, I'm using the device directly when I push the following:


    On the display I see only:


    If I monitor in Qt Creator with a breakpoint in keyPressEvent, only pressing:


    Results in a call to the routine, pressing any of the numbers does not. The routine receives and event with the key returned as:


  • Lifetime Qt Champion

    ok so it is on the device.
    But are you saying when you type 12.3, in keyhandler the dot is seen but never 1,2,3
    but still they show up in the widget? (but not . even it was seen) ?

  • Thats exactly what I'm saying.

  • Lifetime Qt Champion

    That almost seems like magic.
    I wonder how keys come in then?
    Its a completely plain QDoubleSpinBox, no overrides or event filters?

  • No, nothing I just dropped it in from the palette on the left, changed the geometry and font size and that is all.

    Looking at the object in Qt Creator, to be precise, this is what I've changed:

        geometry          [(10,40), 221 x 51]
        font              A [Ubuntu, 24]
        alignment         AlignHCenter, AlignVCenter  
        maximum           99999999.990000
        singleStep        0.010000

    In the class prototype:

        void keyPressEvent(QKeyEvent* pEvent);
        private slots:
            void on_spbxAmount_valueChanged(const QString& strText);

    The implementation for the above:

        void clsSaleForm::keyPressEvent(QKeyEvent* pEvent) {
                int intKey = pEvent->key();
                qDebug() << intKey;
        void clsSaleForm::on_spbxAmount_valueChanged(const QString& strText) {
                bool blnNothing = (strText.isEmpty() || strText.toDouble() == 0);

    I just tried to replicate this behaviour on my iMAC, created with the same settings and it worked perfectly, so it must be an issue with the version of Qt on the Ubuntu build.

    On my iMAC I'm using:

        Qt Creator 4.9.1
        Based on Qt 5.12.3 (Clang 10.0 (Apple), 64 bit)
        Built on May 26 2019 21:14:16
        From revision 168e91b618

    And on the laptop running Ubuntu 14.04:

        Qt Creator 3.0.1
        Based on Qt 5.2.1 (GCC 4.8.2, 32 bit)
        Built on Apr 9 2014 at 09:14:10

    Sadly I cannot upgrade the Ubuntu Qt as the SDK I'm using is part of the ACR SDK and there doesn't appear to be an update.

  • Lifetime Qt Champion

    Ah, you overrode forms keypress, not the QDoubleSpinBox's.
    thats why you saw . and not 1,2,3.

    Are you 100% sure you set locale on the QDoubleSpinBox since its
    not shown in modifications?

  • Yes, 100% and I've been back to check it several times. I am currently implementing a solution not using the QDoubleSpinBox but just a regular QLineEdit with regular expression.

  • Lifetime Qt Champion

    ok. just checking
    so a normal QLineEdit will get the "." ?

  • Yes, the regular expression will restrict what it can and cannot accept and the keyPressEvent handler will receive the dot and insert it into the line edit control.

Log in to reply