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. Prevent user from typing invalid values in QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Prevent user from typing invalid values in QLineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 621 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.
  • W Offline
    W Offline
    wakiki
    wrote on 17 Oct 2022, 08:51 last edited by wakiki
    #1

    I'm trying to to use QDoubleValidator to prevent user from typing invalid values, or values which are out of range, in QLineEdit.
    Im not sure if validators suppose to block entering wrong values while typing or only when the user press "Enter" or release focus.
    (docu says: ...The user may change the content to any Intermediate value during editing, but will be prevented from editing the text to a value that v validates as Invalid. ..)
    I need a solution which prevents the user from typing any invalid values.
    I tried something like this:

    _validator(new QDoubleValidator(0.0, 10.0, 5, this))
    _validator->setNotation(QDoubleValidator::StandardNotation);
    _validator->setLocale(QLocale::C);
    setValidator(_validator);
    

    But i can still enter numbers up to 99.
    Decimal check seems to work and hasAcceptableInput() works also correctly.
    It returns false for values higher than 10.0, when i add signal/slot connection for textChanged().

    J 1 Reply Last reply 17 Oct 2022, 08:57
    0
    • W wakiki
      17 Oct 2022, 08:51

      I'm trying to to use QDoubleValidator to prevent user from typing invalid values, or values which are out of range, in QLineEdit.
      Im not sure if validators suppose to block entering wrong values while typing or only when the user press "Enter" or release focus.
      (docu says: ...The user may change the content to any Intermediate value during editing, but will be prevented from editing the text to a value that v validates as Invalid. ..)
      I need a solution which prevents the user from typing any invalid values.
      I tried something like this:

      _validator(new QDoubleValidator(0.0, 10.0, 5, this))
      _validator->setNotation(QDoubleValidator::StandardNotation);
      _validator->setLocale(QLocale::C);
      setValidator(_validator);
      

      But i can still enter numbers up to 99.
      Decimal check seems to work and hasAcceptableInput() works also correctly.
      It returns false for values higher than 10.0, when i add signal/slot connection for textChanged().

      J Offline
      J Offline
      JonB
      wrote on 17 Oct 2022, 08:57 last edited by
      #2

      @wakiki
      Hello and welcome.

      Since you seem to be accepting floating point numbers from the user, before you proceed with QLineEdit did you see QDoubleSpinBox as your starting point?

      W 1 Reply Last reply 18 Oct 2022, 09:33
      4
      • J JonB
        17 Oct 2022, 08:57

        @wakiki
        Hello and welcome.

        Since you seem to be accepting floating point numbers from the user, before you proceed with QLineEdit did you see QDoubleSpinBox as your starting point?

        W Offline
        W Offline
        wakiki
        wrote on 18 Oct 2022, 09:33 last edited by
        #3

        @JonB
        I would rather avoid changing the widget type, but i will check that.
        Is it possible to show a string in QDoubleSpinBox? I want to add something like ".sec" behind the number.

        J 1 Reply Last reply 18 Oct 2022, 09:47
        0
        • W wakiki
          18 Oct 2022, 09:33

          @JonB
          I would rather avoid changing the widget type, but i will check that.
          Is it possible to show a string in QDoubleSpinBox? I want to add something like ".sec" behind the number.

          J Offline
          J Offline
          JonB
          wrote on 18 Oct 2022, 09:47 last edited by JonB
          #4

          @wakiki said in Prevent user from typing invalid values in QLineEdit:

          I would rather avoid changing the widget type, but i will check that.

          Odd. Why do you want a QLineEdit for editing a number when it is not designed for that, QDoubleSpinBox is and has range validation built into it?

          Is it possible to show a string in QDoubleSpinBox? I want to add something like ".sec" behind the number.

          How do you do this at present with a QLineEdit which has a QDoubleValidator? If the user types in .sec it won't validate as a number? Or do you mean you put .sec somewhere outside the widget (e.g. in a QLabel), in which case it does not affect which widget you use? Or, QDoubleSpinBox has an optional prefix/suffix, is that what you are wanting for your .sec?

          Meanwhile, QDoubleValidator::validate() says

          Returns Intermediate if input contains a double that is outside the range or is in the wrong format; e.g. is empty.

          so I am guessing that is what you get when you enter 99.

          1 Reply Last reply
          1
          • W Offline
            W Offline
            wakiki
            wrote on 19 Oct 2022, 15:23 last edited by wakiki
            #5

            Currently my QLineEdit has no validator at all. Its handled after editing is finished. I guess you are right,
            QDoubleSpinBox with a suffix would be the best solution in this case.

            We used QLineEdit for all editable double parameters so far. I don't know why, but live typing validation was not a required feature,
            so i will stick with QLineEdit.

            J 1 Reply Last reply 19 Oct 2022, 17:22
            0
            • W wakiki
              19 Oct 2022, 15:23

              Currently my QLineEdit has no validator at all. Its handled after editing is finished. I guess you are right,
              QDoubleSpinBox with a suffix would be the best solution in this case.

              We used QLineEdit for all editable double parameters so far. I don't know why, but live typing validation was not a required feature,
              so i will stick with QLineEdit.

              J Offline
              J Offline
              JonB
              wrote on 19 Oct 2022, 17:22 last edited by
              #6

              @wakiki
              Fair enough. I think you will find the validator prevents the user from typing some things --- for example invalid characters or a string which is too long --- but it won't for example prevent things being typed which could be changed or adjusted to be right. I'm not sure what the rules are. It will just return Intermediate for the verification.

              1 Reply Last reply
              0

              1/6

              17 Oct 2022, 08:51

              • Login

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