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. QLineEdit to accept comma seperated integers and validate
Forum Updated to NodeBB v4.3 + New Features

QLineEdit to accept comma seperated integers and validate

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 2.8k 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
    Shines
    wrote on last edited by
    #1

    How to write the code in Qt c++ for custom validator, which can parse numbers, comma separated numbers and range of numbers , which will validate strings like:
    "1", "1,3,4", "1,2,8-12" "4-8" etc.

    JonBJ 1 Reply Last reply
    0
    • S Shines

      How to write the code in Qt c++ for custom validator, which can parse numbers, comma separated numbers and range of numbers , which will validate strings like:
      "1", "1,3,4", "1,2,8-12" "4-8" etc.

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

      @Shines
      So this sounds like a comma-separated list of elements, where each element is either a single number or a number followed by a hyphen followed by another number (though you may say one side or the other of the hyphen can optionally be omitted).

      You could play with a regular expression. \d is a single digit. So something based on

      ((\d+|\d+-\d+),?)*
      

      You can use somewhere like https://regex101.com/ to experiment.

      If it all gets too complicated for a regular expression write C++ code to split on commas (QString::split()) and parse each element for what you want.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Shines
        wrote on last edited by
        #3

        @JonB said in QLineEdit to accept comma seperated integers and validate:

        You can use somewhere like https://regex101.com/ to experiment.
        If it all gets too complicated for a regular expression write C++ code to split on commas (QString::split()) and parse each element for what you want.

        I need to accept input and need to validate if the accepted input is within the permissible range of integers eg: 0 -256. not sure if input mask / validation works

        JonBJ 1 Reply Last reply
        0
        • S Shines

          @JonB said in QLineEdit to accept comma seperated integers and validate:

          You can use somewhere like https://regex101.com/ to experiment.
          If it all gets too complicated for a regular expression write C++ code to split on commas (QString::split()) and parse each element for what you want.

          I need to accept input and need to validate if the accepted input is within the permissible range of integers eg: 0 -256. not sure if input mask / validation works

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

          @Shines
          Although technically it is possible to write regular expressions which check the value of numbers it is a pain. I would recommend writing C++ code for this kind of case.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @Shines
            Although technically it is possible to write regular expressions which check the value of numbers it is a pain. I would recommend writing C++ code for this kind of case.

            S Offline
            S Offline
            Shines
            wrote on last edited by
            #5

            @JonB
            I actually have QLineEdit. This QLineEdit is supposed to accept the input with above conditions. I may probably have to use QInputmask and validator. How to implement it

            JonBJ 1 Reply Last reply
            0
            • S Shines

              @JonB
              I actually have QLineEdit. This QLineEdit is supposed to accept the input with above conditions. I may probably have to use QInputmask and validator. How to implement it

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

              @Shines
              For validator: void QLineEdit::setValidator(const QValidator *v), and void QIntValidator::setRange(int bottom, int top). But I don't see how you will use this for other than one item, once you have e.g. 1,2,8-12 you would have to write your own derived from QValidator.

              Equally for input mask, you may be able to use it to restrict which characters the user can type (digits, commas, hyphens) but not the whole required format.

              You might like to look at (how big & scary) https://stackoverflow.com/a/29160735 answer shows...!

              1 Reply Last reply
              1
              • S Offline
                S Offline
                Shines
                wrote on last edited by
                #7

                @JonB
                I have below code
                QObject::connect(lineEdit, editingFinished, [lineEdit, this](const QString& text)
                {
                std::vector<int> valList = ParseRange(text); //accepts text in ** 1,2,8-12** format and parses it and returns only integers
                for (auto value : valList)
                {
                QValidator* validator = new QIntValidator(this);
                lineEdit->setValidator(validator); //But once this line is executed, lineEdit disallows comma and hyphen
                checkIfValueIsValid();
                }
                });

                How to set mask/validator for lineEdit to allow only integers, comma and hyphen and disallow all othr characters

                JonBJ 1 Reply Last reply
                0
                • S Shines

                  @JonB
                  I have below code
                  QObject::connect(lineEdit, editingFinished, [lineEdit, this](const QString& text)
                  {
                  std::vector<int> valList = ParseRange(text); //accepts text in ** 1,2,8-12** format and parses it and returns only integers
                  for (auto value : valList)
                  {
                  QValidator* validator = new QIntValidator(this);
                  lineEdit->setValidator(validator); //But once this line is executed, lineEdit disallows comma and hyphen
                  checkIfValueIsValid();
                  }
                  });

                  How to set mask/validator for lineEdit to allow only integers, comma and hyphen and disallow all othr characters

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

                  @Shines
                  Of course, since QIntValidator validates for a single integer. That won't accept your ,s or -s for multiple/ranges. I said:

                  But I don't see how you will use this for other than one item, once you have e.g. 1,2,8-12 you would have to write your own derived from QValidator.

                  Also

                  std::vector<int> valList = ParseRange(text); //accepts text in ** 1,2,8-12** format and parses it and returns only integers

                  This is pointless. Parsing your input string to just extract each integer in it will get you nowhere, you have lost the , and - information.

                  Also:

                  for (auto value : valList)
                  {
                  QValidator* validator = new QIntValidator(this);
                  lineEdit->setValidator(validator);
                  

                  A QLineEdit can only have one validator. You are going through a list and setting a validator for each item, it will only respect the last one. This is not useful.

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    Shines
                    wrote on last edited by
                    #9

                    @JonB said in QLineEdit to accept comma seperated integers and validate:

                    This is pointless. Parsing your input string to just extract each integer in it will get you nowhere, you have lost the , and - information.

                    After this point, I dont need , and - info in my code.. I need them only for the user to be readable. After parsing, I would be checking if integers are in permitted range

                    JonBJ 1 Reply Last reply
                    0
                    • S Shines

                      @JonB said in QLineEdit to accept comma seperated integers and validate:

                      This is pointless. Parsing your input string to just extract each integer in it will get you nowhere, you have lost the , and - information.

                      After this point, I dont need , and - info in my code.. I need them only for the user to be readable. After parsing, I would be checking if integers are in permitted range

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

                      @Shines
                      I really don't know what you mean. For example the following two input strings:

                      1,100
                      1-100
                      

                      both (presumably) produce a 2 element integer array containing 1 and 100. But the first one means only accept 1 or 100, the second any number between 1 and 100.

                      In any case you need to address the other points I raised, e.g. a QLineEdit can only have a single validator and you (are trying to) give it multiple ones.

                      S 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Shines
                        I really don't know what you mean. For example the following two input strings:

                        1,100
                        1-100
                        

                        both (presumably) produce a 2 element integer array containing 1 and 100. But the first one means only accept 1 or 100, the second any number between 1 and 100.

                        In any case you need to address the other points I raised, e.g. a QLineEdit can only have a single validator and you (are trying to) give it multiple ones.

                        S Offline
                        S Offline
                        Shines
                        wrote on last edited by
                        #11

                        @JonB
                        I understand that. In case of 1,100 I would be using 1st and 100th unit/element save this in list and drop comma. In case of 1-100, I drop comma and hyphen and save integers between 1 to 100 in my list. So I was just trying to allow user to enter comma and hyphen in user readable format. Now masking/validating the combination of characters and int isnt feasible, I should check for other possibilities

                        JonBJ 1 Reply Last reply
                        0
                        • S Shines

                          @JonB
                          I understand that. In case of 1,100 I would be using 1st and 100th unit/element save this in list and drop comma. In case of 1-100, I drop comma and hyphen and save integers between 1 to 100 in my list. So I was just trying to allow user to enter comma and hyphen in user readable format. Now masking/validating the combination of characters and int isnt feasible, I should check for other possibilities

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

                          @Shines said in QLineEdit to accept comma seperated integers and validate:

                          and save integers between 1 to 100 in my list.

                          Which really does not scale at all well!

                          In any case you cannot attach a QIntValidator to a QLineEdit and have it accept multiple integers in any shape or form. So like I said anything you want to do QValidator-wise will require you to subclass that and write your own logic given your requirement. For any inputMask you might want to add I think it will only be able to specify that input characters allowed are digits, comma & hyphen, but not your requirement about how those can be "put together" legally.

                          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