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

    Hi,

    Not a direct answer but why not use a QDoubleSpinBox ?


  • Moderators

    @Misty-River
    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
    {
         QDoubleValidator::fixup(input);
         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);
    lo.setNumberOptions(QLocale::RejectGroupSeparator);
    
    auto val = new QDoubleValidator(0, 1000000, 2, this);
    val->setLocale(lo);
    lineEdit->setValidator(val);
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.