Can't understand how to work with QIntValidator
-
@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.
-
@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. -
@Christian-Ehrlicher I would be happy to use SpinBox, but i can't use it in this project, he ban
-