このページでは

QIntValidator Class

QIntValidator クラスは、文字列に指定された範囲内の有効な整数が含まれていることを確認するバリデータを提供します。詳細...

ヘッダー: #include <QIntValidator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
継承元: QValidator

プロパティ

パブリック関数

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

再実装されたパブリック関数

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

詳細な説明

使用例:

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);

以下に、バリデータの例をいくつか示します。実際には、前述の例のように、通常はウィジェットに関連付けられます。

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

999 の戻り値が Intermediate となる点に注意してください。最大値以下の桁数で構成される値は、中間値とみなされます。これは、数値が範囲外となる原因となる桁が、必ずしも最後に入力された桁とは限らないためです。また、これは中間値の数値に先頭のゼロが含まれていてもよいことを意味します。

最小値と最大値は、setRange() を 1 回呼び出すことで設定することも、setBottom() およびsetTop() を個別に呼び出して設定することもできます。

QIntValidator は、独自のlocale() を使用して数値を解釈します。たとえば、アラビア語のロケールでは、QIntValidator はアラビア数字を受け入れます。

注: ` locale()` で設定された ` QLocale::NumberOptions ` も、数値の解釈方法に影響を与えます。たとえば、` QLocale::RejectGroupSeparator ` はデフォルトでは設定されていないため、バリデータはグループ区切り文字を受け入れます。したがって、数値を取得するには `QLocale::toInt()` を使用することをお勧めします。

QDoubleValidator 、QRegularExpressionValidator 、QLocale::toInt()、および「行編集の例」も参照してください 。

プロパティのドキュメント

bottom : int

このプロパティには、バリデータが許容する最小値が格納されます

デフォルトでは、このプロパティの値は、利用可能な最小の符号付き整数(-2147483648)から導出されます。

アクセス関数:

int bottom() const
void setBottom(int)

関連項目: setRange()。

top : int

このプロパティには、バリデータが許容する最大値が格納されます

デフォルトでは、このプロパティの値は、利用可能な最大の符号付き整数(2147483647)から導出されます。

アクセス関数:

int top() const
void setTop(int)

関連項目: setRange()。

メンバ関数のドキュメント

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

すべての整数を受け入れる、parent オブジェクトを使用したバリデータを作成します。

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

parent を持つバリデータを作成します。このバリデータは、minimum からmaximum までの整数(両端を含む)を受け入れます。

[virtual noexcept] QIntValidator::~QIntValidator()

バリデータを取り消します。

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

QValidator::fixup (QString &input)const を再実装します。

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

バリデータが、bottom からtop までの整数(両端を含む)のみを受け入れるように範囲を設定します。

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

QValidator::validate(QString &input, int &pos) const を再実装します。

input が有効範囲内の整数である場合、Acceptable を返します。input の桁数が範囲の上限以下であるか、または有効範囲内の整数の接頭辞である場合は、Intermediate を返します。それ以外の場合は、Invalid を返します。

input 有効範囲が正の整数のみで構成されている場合(例:32 から 100)、かつinput が負の整数である場合は、Invalid が返されます。 (一方、範囲が負の整数(例:-100 ~ -32)で構成されており、 が先頭にプラス記号のない正の整数である場合は、ユーザーがマイナス記号を入力しようとしている可能性(特に右から左へ書く言語の場合)があるため、Intermediateが返されます。

同様に、有効な範囲が 46 から 53 の間である場合、41 および 59 は「Intermediate 」として評価されます。そうしないと、ユーザーが値を 49 から 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

デフォルトでは、このバリデータは `pos ` パラメータを使用しません。

© 2026 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.