QLineEdit to accept comma seperated integers and validate
-
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.@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. -
@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
-
@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
-
@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. -
@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@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 fromQValidator
.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...!
-
@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
-
@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
@Shines
Of course, sinceQIntValidator
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. -
@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
-
@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
@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
and100
. But the first one means only accept1
or100
, the second any number between1
and100
.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. -
@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
and100
. But the first one means only accept1
or100
, the second any number between1
and100
.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.@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 -
@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@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 aQLineEdit
and have it accept multiple integers in any shape or form. So like I said anything you want to doQValidator
-wise will require you to subclass that and write your own logic given your requirement. For anyinputMask
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.