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
QtWS25 Last Chance

Disable comma in QDoubleValidator

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 8.2k Views
  • 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 2 Jun 2016, 19:07 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 ?

    R D 2 Replies Last reply 3 Jun 2016, 07:10
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Jun 2016, 20:44 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
        2 Jun 2016, 19:07

        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 ?

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 3 Jun 2016, 07:10 last edited by raven-worx 6 Mar 2016, 07:13
        #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
          2 Jun 2016, 19:07

          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 3 Jun 2016, 07:55 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
          • J JonB referenced this topic on 5 Jun 2024, 12:57

          3/4

          3 Jun 2016, 07:10

          • Login

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