Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Disable comma in QDoubleValidator
Qt 6.11 is out! See what's new in the release blog

Disable comma in QDoubleValidator

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Misty River
    wrote on last edited by
    #1

    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 ?

    raven-worxR D 2 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Not a direct answer but why not use a QDoubleSpinBox ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Misty River

        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 ?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @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);
        }
        }
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • M Misty River

          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 ?

          D Offline
          D Offline
          Devopia53
          wrote on last edited by
          #4

          @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);
          
          
          1 Reply Last reply
          1
          • JonBJ JonB referenced this topic on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved