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 |
シグナル
void | bottomChanged(int bottom) |
void | topChanged(int top) |
詳細説明
使用例
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
は中間値を返すことに注目。最大値以下の桁数で構成される値は、中間値とみなされます。これは、数値が範囲内にあることを妨げる桁が、必ずしも入力された最後の桁であるとは限らないためです。これはまた、中間的な数値は先頭にゼロを持つことができることを意味する。
最小値と最大値は、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) |
Notifier シグナル:
void | bottomChanged(int bottom) |
setRange()も参照 。
top : int
このプロパティは、バリデータの最高値を保持する。
デフォルトでは、このプロパティの値は利用可能な符号つき整数値のうち最大の値 (2147483647) となります。
アクセス関数
int | top() const |
void | setTop(int) |
Notifier シグナル:
void | topChanged(int top) |
setRange()も参照 。
メンバ関数ドキュメント
[explicit]
QIntValidator::QIntValidator(QObject *parent = nullptr)
すべての整数を受け付けるparent オブジェクトでバリデータを作成します。
QIntValidator::QIntValidator(int minimum, int maximum, QObject *parent = nullptr)
minimum からmaximum までの整数を受け付けるparent を持つバリデータを作成します。
[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 を返す。
有効範囲が正の整数だけ(例えば32から100)で構成され、input が負の整数である場合、Invalid が返される。(一方、有効範囲が負の整数(例えば、-100から-32)で、input が正の整数で、先頭にプラス記号がない場合は、Intermediateが返されます。これは、ユーザがマイナスを入力しようとしているところかもしれないからです(特に右から左への言語の場合)。
同様に、有効範囲が46から53の場合、41と59はIntermediate として評価されます。
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 パラメータを使用しません。
© 2025 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.