QIntValidator Class

The QIntValidator class provides a validator that ensures a string contains a valid integer within a specified range. More...

Header: #include <QIntValidator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
Inherits: QValidator

Properties

Public Functions

QIntValidator(QObject *parent = nullptr)
QIntValidator(int minimum, int maximum, QObject *parent = nullptr)
virtual ~QIntValidator()
int bottom() const
void setBottom(int)
void setRange(int bottom, int top)
void setTop(int)
int top() const

Reimplemented Public Functions

virtual void fixup(QString &input) const override
virtual QValidator::State validate(QString &input, int &pos) const override

Signals

void bottomChanged(int bottom)
void topChanged(int top)

Detailed Description

Example of use:

QValidator *validator = new QIntValidator(100, 999, this);
QLineEdit *edit = new QLineEdit(this);

// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);

Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above.

QString str;
int pos = 0;
QIntValidator v(100, 900, this);

str = "1";
v.validate(str, pos);     // returns Intermediate
str = "012";
v.validate(str, pos);     // returns Intermediate

str = "123";
v.validate(str, pos);     // returns Acceptable
str = "678";
v.validate(str, pos);     // returns Acceptable

str = "999";
v.validate(str, pos);    // returns Intermediate

str = "1234";
v.validate(str, pos);     // returns Invalid
str = "-123";
v.validate(str, pos);     // returns Invalid
str = "abc";
v.validate(str, pos);     // returns Invalid
str = "12cm";
v.validate(str, pos);     // returns Invalid

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.

The minimum and maximum values are set in one call with setRange(), or individually with setBottom() and setTop().

QIntValidator uses its locale() to interpret the number. For example, in Arabic locales, QIntValidator will accept Arabic digits.

Note: The QLocale::NumberOptions set on the locale() also affect the way the number is interpreted. For example, since QLocale::RejectGroupSeparator is not set by default, the validator will accept group separators. It is thus recommended to use QLocale::toInt() to obtain the numeric value.

See also QDoubleValidator, QRegularExpressionValidator, QLocale::toInt(), and Line Edits Example.

Property Documentation

bottom : int

This property holds the validator's lowest acceptable value

By default, this property's value is derived from the lowest signed integer available (-2147483648).

Access functions:

int bottom() const
void setBottom(int)

Notifier signal:

void bottomChanged(int bottom)

See also setRange().

top : int

This property holds the validator's highest acceptable value

By default, this property's value is derived from the highest signed integer available (2147483647).

Access functions:

int top() const
void setTop(int)

Notifier signal:

void topChanged(int top)

See also setRange().

Member Function Documentation

[explicit] QIntValidator::QIntValidator(QObject *parent = nullptr)

Constructs a validator with a parent object that accepts all integers.

QIntValidator::QIntValidator(int minimum, int maximum, QObject *parent = nullptr)

Constructs a validator with a parent, that accepts integers from minimum to maximum inclusive.

[virtual] QIntValidator::~QIntValidator()

Destroys the validator.

[override virtual] void QIntValidator::fixup(QString &input) const

Reimplements: QValidator::fixup(QString &input) const.

void QIntValidator::setRange(int bottom, int top)

Sets the range of the validator to only accept integers between bottom and top inclusive.

[override virtual] QValidator::State QIntValidator::validate(QString &input, int &pos) const

Reimplements: QValidator::validate(QString &input, int &pos) const.

Returns Acceptable if the input is an integer within the valid range. If input has at most as many digits as the top of the range, or is a prefix of an integer in the valid range, returns Intermediate. Otherwise, returns Invalid.

If the valid range consists of just positive integers (e.g., 32 to 100) and input is a negative integer, then Invalid is returned. (On the other hand, if the range consists of negative integers (e.g., -100 to -32) and input is a positive integer without leading plus sign, then Intermediate is returned, because the user might be just about to type the minus (especially for right-to-left languages).

Similarly, if the valid range is between 46 and 53, then 41 and 59 will be evaluated as Intermediate, as otherwise the user wouldn't be able to change a value from 49 to 51.

int pos = 0;

s = "abc";
v.validate(s, pos);    // returns Invalid

s = "5";
v.validate(s, pos);    // returns Intermediate

s = "50";
v.validate(s, pos);    // returns Acceptable

By default, the pos parameter is not used by this validator.

© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.