Can't understand how to work with QIntValidator
-
I need to limit input from 1 to 10,therefore I set max and min value in QIntValidator,example QIntValidator(1,10), but i can enter nubmer up to 99, how fix that problem?
@Bol4onok said in Can't understand how to work with QIntValidator:
how fix that problem?
How should we help when you don't show us what you're doing?
-
@Bol4onok said in Can't understand how to work with QIntValidator:
how fix that problem?
How should we help when you don't show us what you're doing?
@Christian-Ehrlicher Yes, my bad, i use thath code:
QValidator *validator = new QIntValidator(1,10);
ui->lineLucky->setValidator(validator);
As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99. -
You can see that 99 is an intermediate state:
QValidator *validator = new QIntValidator(1,10); QLineEdit *le = new QLineEdit; QObject::connect(le, &QLineEdit::textChanged, le, [&]() { QString s = le->text(); int pos = 0; qDebug() << validator->validate(s, pos); }); le->setValidator(validator); le->show();
If you don't want this, you have to derive from QIntValidator and add the appropriate fixups. Or use a QSpinBox.
-
@Christian-Ehrlicher Yes, my bad, i use thath code:
QValidator *validator = new QIntValidator(1,10);
ui->lineLucky->setValidator(validator);
As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.@Bol4onok said in Can't understand how to work with QIntValidator:
As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.
For the record: this is as described in https://doc.qt.io/qt-5/qintvalidator.html#details
QIntValidator v(100, 900, this);
Notice that the value 999 returns Intermediate. Values consisting of a number of digits equal to or less than the max value are considered intermediate. This is intended because the digit that prevents a number from being in range is not necessarily the last digit typed. This also means that an intermediate number can have leading zeros.You should find your
99
returns Intermediate. Follow @Christian-Ehrlicher's advice to resolve. If you just want a number input by user aQSpinBox
is simplest and avoids this issue. -
You can see that 99 is an intermediate state:
QValidator *validator = new QIntValidator(1,10); QLineEdit *le = new QLineEdit; QObject::connect(le, &QLineEdit::textChanged, le, [&]() { QString s = le->text(); int pos = 0; qDebug() << validator->validate(s, pos); }); le->setValidator(validator); le->show();
If you don't want this, you have to derive from QIntValidator and add the appropriate fixups. Or use a QSpinBox.
@Christian-Ehrlicher I would be happy to use SpinBox, but i can't use it in this project, he ban
-
-
To limit 1 to 10 you could try using QRegularExpressionValidator instead of QIntValidator
QRegularExpressionValidator* validator = new QRegularExpressionValidator(QRegularExpression("([1-9]|1[0])"), ui->lineLucky);
ui->lineLucky->setValidator(validator); -
So what is the purpose do the limits on QIntValidator serve? I can't see that they provide any functionality other than limiting the number of digits entered.
-
They dont limit the digits, the input is just Intermediate or Invalid then...
How your Validator reacts on that is up to you -
@Pl45m4
Not quite sure what you mean. FWIW they do limit the number of digits, e.g. as per above:As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.
@JonB said in Can't understand how to work with QIntValidator:
Not quite sure what you mean
When your validator has (1, 42) bounds, 99 is still not an acceptable input even though it's also a 2-digit :))
(99 would also be Intermediate, AFAICS).
So it's not just "limiting the digits" -
@JonB said in Can't understand how to work with QIntValidator:
Not quite sure what you mean
When your validator has (1, 42) bounds, 99 is still not an acceptable input even though it's also a 2-digit :))
(99 would also be Intermediate, AFAICS).
So it's not just "limiting the digits"@Pl45m4
I know. Again I don't see how that tallies with what you wrote:They dont limit the digits, the input is just Intermediate or Invalid then...
But that is not true, they do limit the digits. To 2 in this case. Yes, 99 is not acceptable for a range of 1 to 42, but (for complicated/strange reasons) is does limit your your digits, to 2 here because that includes
42
. It does not allow 3 digits, so it does limit them. That's what I was saying. -
@Pl45m4
I know. Again I don't see how that tallies with what you wrote:They dont limit the digits, the input is just Intermediate or Invalid then...
But that is not true, they do limit the digits. To 2 in this case. Yes, 99 is not acceptable for a range of 1 to 42, but (for complicated/strange reasons) is does limit your your digits, to 2 here because that includes
42
. It does not allow 3 digits, so it does limit them. That's what I was saying.@JonB said in Can't understand how to work with QIntValidator:
It does not allow 3 digits, so it does limit them. That's what I was saying.
Yes, true, but for that purpose I don't need any validator. That can be checked/prevented way easier.
I was replying to@j_lady said in Can't understand how to work with QIntValidator:
I can't see that they provide any functionality other than limiting the number of digits entered
which is kinda wrong, because it does not only do this.
Sure, when your desired input is a number from 1 to 10, it makes no sense to even check inputs like 88888. -
TBH I was pretty "unhappy" when I used Qt's int validator. I know they allow you to type the second
9
in99
when the limit is42
to do with because you could go back and delete a digit, or whatever its rule is. I can't put my finger on it, but I felt pretty sure that other validators I had used in software just wouldn't let you enter99
for a limit of42
in the first place rather than allowing it and then the user's input is illegal, and that seemed more intuitive to me in a UI....