Important: Please read the Qt Code of Conduct -

Disable comma in QDoubleValidator

  • Hi, I have a QDoubleValidator that is set on a QLineEdit and I would like to disable the comma decimal separator.. I only want the user to be able to type the dot decimal like that 1.234 and not 1,234 ...

    I have set the Locale to QLocale::C on the validator but the user can still type the comma.... How can I do that ?

  • Lifetime Qt Champion


    Not a direct answer but why not use a QDoubleSpinBox ?

  • Moderators

    try this (untested):

    class MyValidator : public QDoubleValidator
    QValidator::State validate(QString & input, int & pos) const
       if( input.contains( QChar(',') )
             return Qvalidator::Invalid;
       return QDoubleValidator::validate(inout,pos);
    void fixup(QString & input) const
         input.remove(QChar(','), Qt::CaseInsensitive);

  • @Misty-River

    The Comma is a group separate character of locale.
    If not you need a comma, use the QLocale::RejectGroupSeparator option.

    Something like this:

    QLocale lo(QLocale::C);
    auto val = new QDoubleValidator(0, 1000000, 2, this);

Log in to reply