Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to restrict QSpinBox value to 1 to 100?
Forum Updated to NodeBB v4.3 + New Features

How to restrict QSpinBox value to 1 to 100?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
20 Posts 5 Posters 3.3k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi,

    The input is "allowed" however the value is not changed to 0 and will go back to the last valid input made.

    0 is a "valid" input because you have to be able to use it for number like 10 and 100.

    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
    • S Offline
      S Offline
      Sriu1
      wrote on last edited by
      #4

      How to restrict a spinBox to not enter 0's?
      @JonB

      JonBJ J.HilkJ 2 Replies Last reply
      0
      • S Sriu1

        How to restrict a spinBox to not enter 0's?
        @JonB

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #5

        @Sriu1
        If you really mean "I don't want to be able to type the 0 character physically into the box", then as @SGaist says that would mean your user could not enter, say, 10. Which seems like an unlikely requirement.

        1 Reply Last reply
        0
        • KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by
          #6

          I think he want to set a validate on spinbox to avoid invalid number on view, so,
          probably we want to implement a regExp like [1-9][0-9][0-9]

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          JonBJ 1 Reply Last reply
          0
          • KillerSmathK KillerSmath

            I think he want to set a validate on spinbox to avoid invalid number on view, so,
            probably we want to implement a regExp like [1-9][0-9][0-9]

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #7

            @KillerSmath
            How would your regexp allow a value like, say, 23? How would it prevent, say, 999?

            KillerSmathK 1 Reply Last reply
            0
            • S Sriu1

              How to restrict a spinBox to not enter 0's?
              @JonB

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #8

              I have the feeling, we had this question recently 🤔

              @Sriu1
              if you really want to restrict the input of leading 0's you probably will have to do this yourself by, for example, overwriting QAbstractSpinBox::validate


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                I have the feeling, we had this question recently 🤔

                @Sriu1
                if you really want to restrict the input of leading 0's you probably will have to do this yourself by, for example, overwriting QAbstractSpinBox::validate

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #9

                @J.Hilk
                A while back I posted (https://forum.qt.io/topic/85305/validation-behaviour-too-confusing-for-end-user) how I feel so unfamiliar about the way Qt validators work when typing into a line edit, which would apply to a spin box's edit too.

                For example, if your validator does indeed forbid a leading 0:

                1. Type a 6.
                2. Type a 0.
                3. You realize you meant 50 rather than 60.
                4. So you try to delete the leading 6, to then type in a 5.
                5. But you can't! As you delete the leading 6, the number now starts with a 0, so the validator prevents the delete. The user gets no feedback as to why.
                6. To achieve the change, the user must first delete the 0, followed by the 6, before he can then type in 50.

                This is most unnatural, and regrettably my users are so confused I had to remove the validator.... So OP beware!

                1 Reply Last reply
                0
                • JonBJ JonB

                  @KillerSmath
                  How would your regexp allow a value like, say, 23? How would it prevent, say, 999?

                  KillerSmathK Offline
                  KillerSmathK Offline
                  KillerSmath
                  wrote on last edited by
                  #10

                  @JonB said in How to restrict QSpinBox value to 1 to 100?:

                  @KillerSmath
                  How would your regexp allow a value like, say, 23? How would it prevent, say, 999?

                  I just said a hypothetical example.

                  @Computer Science Student - Brazil
                  Web Developer and Researcher
                  “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                  JonBJ 1 Reply Last reply
                  1
                  • KillerSmathK KillerSmath

                    @JonB said in How to restrict QSpinBox value to 1 to 100?:

                    @KillerSmath
                    How would your regexp allow a value like, say, 23? How would it prevent, say, 999?

                    I just said a hypothetical example.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #11

                    @KillerSmath
                    Oh, OK, some posters here will type in whatever you offer and then complain if it doesn't work as-is!

                    KillerSmathK 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @KillerSmath
                      Oh, OK, some posters here will type in whatever you offer and then complain if it doesn't work as-is!

                      KillerSmathK Offline
                      KillerSmathK Offline
                      KillerSmath
                      wrote on last edited by
                      #12

                      @JonB said in How to restrict QSpinBox value to 1 to 100?:

                      @KillerSmath
                      Oh, OK, some posters here will type in whatever you offer and then complain if it doesn't work as-is!

                      an easy mode to write this regular expression is 0|100|[1-9][0-9]?

                      @Computer Science Student - Brazil
                      Web Developer and Researcher
                      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                      JonBJ 1 Reply Last reply
                      2
                      • KillerSmathK KillerSmath

                        @JonB said in How to restrict QSpinBox value to 1 to 100?:

                        @KillerSmath
                        Oh, OK, some posters here will type in whatever you offer and then complain if it doesn't work as-is!

                        an easy mode to write this regular expression is 0|100|[1-9][0-9]?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #13

                        @KillerSmath
                        Yep. For completeness, since the OP states he wants to disallow 0, if he wants to copy this it should be 100|[1-9][0-9]?.

                        KillerSmathK 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @KillerSmath
                          Yep. For completeness, since the OP states he wants to disallow 0, if he wants to copy this it should be 100|[1-9][0-9]?.

                          KillerSmathK Offline
                          KillerSmathK Offline
                          KillerSmath
                          wrote on last edited by
                          #14

                          @JonB said in How to restrict QSpinBox value to 1 to 100?:

                          @KillerSmath
                          Yep. For completeness, since the OP states he wants to disallow 0, if he wants to copy this it should be 100|[1-9][0-9]?.

                          Yep, thank you, i forgot this part. xD

                          @Computer Science Student - Brazil
                          Web Developer and Researcher
                          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

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

                            To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

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

                            KillerSmathK JonBJ 2 Replies Last reply
                            2
                            • SGaistS SGaist

                              To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                              KillerSmathK Offline
                              KillerSmathK Offline
                              KillerSmath
                              wrote on last edited by KillerSmath
                              #16

                              @SGaist said in How to restrict QSpinBox value to 1 to 100?:

                              To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                              Yes, use the delimiters to apply this regular expression on full string.

                              @Computer Science Student - Brazil
                              Web Developer and Researcher
                              “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #17

                                @SGaist

                                To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                                But for QAbstractSpinBox::validate() I assume it uses http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details, which states:

                                QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors; in other words, it always attempts to do an exact match.

                                ?

                                KillerSmathK 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @SGaist

                                  To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                                  But for QAbstractSpinBox::validate() I assume it uses http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details, which states:

                                  QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors; in other words, it always attempts to do an exact match.

                                  ?

                                  KillerSmathK Offline
                                  KillerSmathK Offline
                                  KillerSmath
                                  wrote on last edited by KillerSmath
                                  #18

                                  @JonB said in How to restrict QSpinBox value to 1 to 100?:

                                  @SGaist

                                  To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                                  But for QAbstractSpinBox::validate() I assume it uses http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details, which states:

                                  QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors; in other words, it always attempts to do an exact match.

                                  ?

                                  Yes, i found the Qt reposity and i noticed QAbstractSpinBox::validate() returns for default QValidator::Acceptable without verification.

                                  if the input is not validated to QValidator::Acceptable when Return is pressed or interpretText() is called.

                                  QAbstractSpinBox.cpp Repository

                                  @Computer Science Student - Brazil
                                  Web Developer and Researcher
                                  “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                                  JonBJ 1 Reply Last reply
                                  0
                                  • KillerSmathK KillerSmath

                                    @JonB said in How to restrict QSpinBox value to 1 to 100?:

                                    @SGaist

                                    To be correct: ^[1-9][0-9]?$|^100$. Otherwise you'll be matching "numbers in numbers"

                                    But for QAbstractSpinBox::validate() I assume it uses http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details, which states:

                                    QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors; in other words, it always attempts to do an exact match.

                                    ?

                                    Yes, i found the Qt reposity and i noticed QAbstractSpinBox::validate() returns for default QValidator::Acceptable without verification.

                                    if the input is not validated to QValidator::Acceptable when Return is pressed or interpretText() is called.

                                    QAbstractSpinBox.cpp Repository

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #19

                                    @KillerSmath
                                    Not sure your comment is against what I meant?
                                    I was asking @SGaist why it is necessary to put in his ^ & $ in the reg exp given that the docs for http://doc.qt.io/qt-5/qregularexpressionvalidator.html#details (seem to me to) state that it will do full match anyway? Unless QAbstractSpinBox::validate() does not use QRegularExpressionValidator ?

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

                                      @JonB That was for direct QRegularExpression use.

                                      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

                                      • Login

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