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. [Solved] QDoubleValidator, allow to enter ".00..." and to disable ",00"
QtWS25 Last Chance

[Solved] QDoubleValidator, allow to enter ".00..." and to disable ",00"

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.7k 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.
  • N Offline
    N Offline
    newe12
    wrote on last edited by newe12
    #1

    Hi!

    I am using QDoubleValidator which works fine, except two things:

    1. If someone would like to enter ".001", it is required to type "0."

    Isn't there a solution, where the user could just type ".001" and miss out the "0" before the point?

    I have:

    QValidator *validator = new QDoubleValidator(.0, 1.0, 10, lineedit);
     lineedit->setValidator(validator);
    
    1. It is also possible to enter ",00...". This option with a "comma" I would like to disable.

    Therefore, I would like to enable to enter a number with decimal places, that the user can

    1. directly start with "." (or optional with "0.") and
    2. to disable the possibility to enter a "comma" (e.g. "," and "0,").

    I'd appreciate any useful ideas!

    K R 2 Replies Last reply
    0
    • N newe12

      Hi!

      I am using QDoubleValidator which works fine, except two things:

      1. If someone would like to enter ".001", it is required to type "0."

      Isn't there a solution, where the user could just type ".001" and miss out the "0" before the point?

      I have:

      QValidator *validator = new QDoubleValidator(.0, 1.0, 10, lineedit);
       lineedit->setValidator(validator);
      
      1. It is also possible to enter ",00...". This option with a "comma" I would like to disable.

      Therefore, I would like to enable to enter a number with decimal places, that the user can

      1. directly start with "." (or optional with "0.") and
      2. to disable the possibility to enter a "comma" (e.g. "," and "0,").

      I'd appreciate any useful ideas!

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @newe12
      Probably QRegExpValidator provides the extended flexibility you need.
      Another possibility is the inputMask

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • N newe12

        Hi!

        I am using QDoubleValidator which works fine, except two things:

        1. If someone would like to enter ".001", it is required to type "0."

        Isn't there a solution, where the user could just type ".001" and miss out the "0" before the point?

        I have:

        QValidator *validator = new QDoubleValidator(.0, 1.0, 10, lineedit);
         lineedit->setValidator(validator);
        
        1. It is also possible to enter ",00...". This option with a "comma" I would like to disable.

        Therefore, I would like to enable to enter a number with decimal places, that the user can

        1. directly start with "." (or optional with "0.") and
        2. to disable the possibility to enter a "comma" (e.g. "," and "0,").

        I'd appreciate any useful ideas!

        R Offline
        R Offline
        roseicollis
        wrote on last edited by roseicollis
        #3

        @newe12 As koahnig says you need a QRegExpValidator. You have to be carefull with dot (.) so u have to scape it. I let u an example to see how it works:

         QRegExp rx("[\\.]\\d{0,30}"); // See that I don't put "." but "\\."
            QValidator *validator7 = new QRegExpValidator(rx, this);
            your_LineEdit->setValidator(validator7);
        

        In this case this will accept a dot and then from 0 to 30 numbers p.e: .0 / .123 / .12345678912346579 /

        1 Reply Last reply
        1
        • N Offline
          N Offline
          newe12
          wrote on last edited by newe12
          #4

          Hi @koahnig and @roseicollis!

          thank you very much!

          It nearly works:

          QRegExp rx("[\\.]\\d{0,10}");
          QValidator *validator = new QRegExpValidator(rx, this);
          lineedit->setValidator(validator);
          

          But, how can I enable to type additionally "0."?

          R 1 Reply Last reply
          0
          • N newe12

            Hi @koahnig and @roseicollis!

            thank you very much!

            It nearly works:

            QRegExp rx("[\\.]\\d{0,10}");
            QValidator *validator = new QRegExpValidator(rx, this);
            lineedit->setValidator(validator);
            

            But, how can I enable to type additionally "0."?

            R Offline
            R Offline
            roseicollis
            wrote on last edited by
            #5

            @newe12

            It was just an example to let you see how it works because I had so much problems with that issue too and all help was so apreciated. I'm not an expert on this and I don't know if this is best way but maybe you need something like this:

                QRegExp rx7("[0-9\\.\\0-9]{0,7}"); // 0,7 allows you to write from 0 to 7 numbers like 0.2 or 0.23568 or 123.56
            

            As I don't know exactly what u need u will have to play whith this on your own and find which is your best combination. Here you can read more about QRegExp: Documentation

            1 Reply Last reply
            1
            • N Offline
              N Offline
              newe12
              wrote on last edited by
              #6

              @roseicollis

              thank you very much!

              Well, nearly!

              If I use

              QRegExp rx("[0\\.]\\d{0,10}");
              

              I can enter ".xy" as intended, but also "0xy" as not intended, instead of "0.xy" as planned.

              Almost...

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                It's rather "0?\\.\\d{0,10}"
                Optional 0, mandatory point, then from 0 to 10 number

                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
                1
                • N Offline
                  N Offline
                  newe12
                  wrote on last edited by newe12
                  #8

                  Perfect! That's it! Solved!

                  @SGaist Thanks very much!

                  1 Reply Last reply
                  0

                  • Login

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