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. QML TextInput element's validation
Forum Updated to NodeBB v4.3 + New Features

QML TextInput element's validation

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 8.5k 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.
  • P Offline
    P Offline
    Punit_Tak
    wrote on 15 Jun 2012, 11:11 last edited by
    #1

    Hello Folks,
    I am facing a problem while using validator property with TextInput. I am able to validate input but i want that if user is entering data which is out of validation range she will be warned with a popup.
    Here is my code:

    @TextInput {
    id: inputBox
    objectName: "InputBox"
    horizontalAlignment: TextInput.AlignHCenter
    color: "black"
    font.family: "Arial"
    anchors.fill: parent
    anchors.leftMargin: 5
    anchors.rightMargin: 5
    anchors.topMargin: 5
    font.pixelSize:13
    maximumLength: 4
    validator: DoubleValidator{bottom: -5.0; top: 5.0; decimals: 1; notation: DoubleValidator.StandardNotation }
    }@

    Here i am restricting user to enter only values from -5.0 to 5.0. If user is entering any values out of this range a popup should appear.

    Thanks in advance.
    Let me know if more details are required or problem is not clear to you.

    Regards,
    Punit

    edit: corrected code little bit

    Punit Tak

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DigitalPioneer
      wrote on 16 Jun 2012, 17:41 last edited by
      #2

      I think your problem here is that when a validator is set, the user cannot enter illegal values. Pressing the keys will have no effect. If you're looking for reactive validator, you'll need to handle the textChanged signal with your own validator like so:

      @
      TextInput {
      id: inputBox
      objectName: "InputBox"
      horizontalAlignment: TextInput.AlignHCenter
      color: "black"
      font.family: "Arial"
      anchors.fill: parent
      anchors.leftMargin: 5
      anchors.rightMargin: 5
      anchors.topMargin: 5
      font.pixelSize:13
      maximumLength: 4

          onTextChanged: {
              if (parseFloat(text) < -5 || parseFloat(text) > 5) {
                  console.log("Invalid input");
                  //Display whatever warning here
                  inputBox.color = "red";
              } else {
                  console.log("Input good");
                  //Hide warning here
                  inputBox.color = "black";
              }
          }
      }
      

      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Punit_Tak
        wrote on 17 Jun 2012, 21:40 last edited by
        #3

        Thanks a lot DigitalPioneer.. it worked..Thank you very much!!! :)

        Punit Tak

        1 Reply Last reply
        0

        1/3

        15 Jun 2012, 11:11

        • Login

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