Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Validators in QML
Forum Update on Monday, May 27th 2025

Validators in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
13 Posts 3 Posters 2.4k 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.
  • S Offline
    S Offline
    Shubham Gupta
    wrote on last edited by
    #1

    @dheerendram
    I have one text box on which i want to store two things on certain conditions:

    1. Time (mm:ss)
    2. Integer values.

    So i need to apply validators for this. So how to achieve two validators in one text box.

        TextField{
                id:text_area
                font.pixelSize:fontSize
                text:btn_text
                color:btn_text_color
                anchors.centerIn: parent
                width:root.width*0.9
                height:root.height
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                inputMethodHints: Qt.ImhDigitsOnly
                validator: DoubleValidator{
                    top: top_value
                  bottom:bottom_value
                }
    

    }

    Shubham Gupta

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You can use conditional operator and define the validators with ids.

      e.g
      IntValidator {id:three;bottom: 11; top: 31;}
      IntValidator {id:four;bottom: 40; top: 50;}
      property int val:1

      validator:val===0?three:four

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shubham Gupta
        wrote on last edited by
        #3

        I used conditional operator like this:

        validator: val===0?RegExpValidator:DoubleValidator

        But it is showing an error.

        Shubham Gupta

        J.HilkJ 1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          show me what are these RegExpValidator:DoubleValidator ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          S 1 Reply Last reply
          0
          • S Shubham Gupta

            I used conditional operator like this:

            validator: val===0?RegExpValidator:DoubleValidator

            But it is showing an error.

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

            @Shubham-Gupta
            you'll have to read up on the Conditional Operator

            It reqiuers 2 expression after the ? your examply only has 1

            forget everything, it's to damn early in the morning.


            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Shubham Gupta
              wrote on last edited by
              #6

              What?
              I din get your words.

              Shubham Gupta

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                Please paste your code here. You need to use ternary operator. You may be doing in some mistake. Hence requested paste the code.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                0
                • dheerendraD dheerendra

                  show me what are these RegExpValidator:DoubleValidator ?

                  S Offline
                  S Offline
                  Shubham Gupta
                  wrote on last edited by
                  #8

                  @dheerendra

                  validator: val===0?DoubleValidator{
                                  top: top_value
                                  bottom:bottom_value}
                               : RegExpValidator{regExp: ""}
                  

                  I am trying to make regular exp for accepting time(mm:ss).
                  But its showing an error.

                  Shubham Gupta

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #9

                    what is the error ?

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Shubham Gupta
                      wrote on last edited by
                      #10

                      @dheerendra

                      Hi dheerendra
                      In qml there are three validators

                      1. IntValidator
                      2. DoubleValidator
                      3. RegExpValidator

                      If i want to store text like 2:30 (mm:ss). So which validator will work.
                      If RegExpValidor is used, so what will be the regular expression for this syntax (mm:ss).

                      Shubham Gupta

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by
                        #11

                        Use RegExpValidator. Did you check the google for regular expression for mm:ss ? It should help.

                        Dheerendra
                        @Community Service
                        Certified Qt Specialist
                        http://www.pthinks.com

                        S 1 Reply Last reply
                        0
                        • dheerendraD dheerendra

                          Use RegExpValidator. Did you check the google for regular expression for mm:ss ? It should help.

                          S Offline
                          S Offline
                          Shubham Gupta
                          wrote on last edited by
                          #12

                          @dheerendra

                          solved.
                          thanks

                          Shubham Gupta

                          1 Reply Last reply
                          0
                          • dheerendraD Offline
                            dheerendraD Offline
                            dheerendra
                            Qt Champions 2022
                            wrote on last edited by
                            #13

                            Good. Move the case to "Solved" state. It helps others as well.

                            Dheerendra
                            @Community Service
                            Certified Qt Specialist
                            http://www.pthinks.com

                            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