QRegularExpressionValidator Class

QRegularExpressionValidator クラスは、文字列を正規表現と照合するために使用します。詳細...

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

プロパティ

パブリック関数

QRegularExpressionValidator(QObject *parent = nullptr)
QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr)
virtual ~QRegularExpressionValidator()
QRegularExpression regularExpression() const

再実装パブリック関数

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

パブリック・スロット

void setRegularExpression(const QRegularExpression &re)

シグナル

void regularExpressionChanged(const QRegularExpression &re)

詳細説明

QRegularExpressionValidator は、正規表現 (regexp) を使用して、入力文字列がAcceptableIntermediateInvalid のいずれであるかを判定します。 正規表現は、QRegularExpressionValidator が構築されるときに提供されるか、または後で提供されます。

正規表現が文字列と部分的にマッチする場合、その結果はIntermediate と見なされます。例えば、""と "A "は正規表現[A-Z][0-9]に対してIntermediate (一方、"_"はInvalid )。

\\A \\z つまり、常に完全一致を試みます。

使用例

// regexp: optional '-' followed by between 1 and 3 digits
QRegularExpression rx("-?\\d{1,3}");
QValidator *validator = new QRegularExpressionValidator(rx, this);

QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);

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

// integers 1 to 9999
QRegularExpression re("[1-9]\\d{0,3}");
// the validator treats the regexp as "^[1-9]\\d{0,3}$"
QRegularExpressionValidator v(re, 0);
QString s;
int pos = 0;

s = "0";     v.validate(s, pos);    // returns Invalid
s = "12345"; v.validate(s, pos);    // returns Invalid
s = "1";     v.validate(s, pos);    // returns Acceptable

re.setPattern("\\S+");            // one or more non-whitespace characters
v.setRegularExpression(re);
s = "myfile.txt";  v.validate(s, pos); // Returns Acceptable
s = "my file.txt"; v.validate(s, pos); // Returns Invalid

// A, B or C followed by exactly five digits followed by W, X, Y or Z
re.setPattern("[A-C]\\d{5}[W-Z]");
v.setRegularExpression(re);
s = "a12345Z"; v.validate(s, pos);        // Returns Invalid
s = "A12345Z"; v.validate(s, pos);        // Returns Acceptable
s = "B12";     v.validate(s, pos);        // Returns Intermediate

// match most 'readme' files
re.setPattern("read\\S?me(\\.(txt|asc|1st))?");
re.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
v.setRegularExpression(re);
s = "readme";      v.validate(s, pos); // Returns Acceptable
s = "README.1ST";  v.validate(s, pos); // Returns Acceptable
s = "read me.txt"; v.validate(s, pos); // Returns Invalid
s = "readm";       v.validate(s, pos); // Returns Intermediate

QRegularExpressionQIntValidatorQDoubleValidatorも参照してください

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

regularExpression : QRegularExpression

このプロパティは、検証に使用される正規表現を保持します。

デフォルトでは、このプロパティには空のパターンを持つ正規表現が含まれます(したがって、任意の文字列にマッチします)。

アクセス関数:

QRegularExpression regularExpression() const
void setRegularExpression(const QRegularExpression &re)

Notifier シグナル:

void regularExpressionChanged(const QRegularExpression &re)

メンバ関数ドキュメント

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

任意の文字列 (空文字列も含む) を有効なものとして受け付けるparent オブジェクトでバリデータを作成します。

[explicit] QRegularExpressionValidator::QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr)

正規表現re にマッチするすべての文字列を受け付けるparent オブジェクトを持つバリデータを作成します。

[virtual noexcept] QRegularExpressionValidator::~QRegularExpressionValidator()

バリデータを破棄する。

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

再実装:QValidator::validate(QString &input, int &pos) const.

input がこのバリデータの正規表現にマッチした場合はAcceptable を、 部分的にマッチした場合はIntermediate を (つまり、有効な文字を追加すればマッチする可能性がある)、input にマッチしなかった場合はInvalid を返します。

input にマッチしなかった場合、pos パラメータはinput パラメータの長さに設定される。

例えば、正規表現が"A57 "はAcceptable 、"E5 "はIntermediate 、"+9 "はInvalid である。

QRegularExpression::match()も参照のこと

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