Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_gui_util_qvalidator.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QLineEdit>
4#include <QValidator>
5#include <QWidget>
6
8
9struct Wrapper : public QWidget {
10 void wrapper0();
11 void wrapper1();
12 void wrapper2();
13 void wrapper3();
14};
15
17//! [0]
18QValidator *validator = new QIntValidator(100, 999, this);
19QLineEdit *edit = new QLineEdit(this);
20
21// the edit lineedit will only accept integers between 100 and 999
22edit->setValidator(validator);
23//! [0]
24
25
26//! [1]
27QString str;
28int pos = 0;
29QIntValidator v(100, 900, this);
30
31str = "1";
32v.validate(str, pos); // returns Intermediate
33str = "012";
34v.validate(str, pos); // returns Intermediate
35
36str = "123";
37v.validate(str, pos); // returns Acceptable
38str = "678";
39v.validate(str, pos); // returns Acceptable
40
41str = "999";
42v.validate(str, pos); // returns Intermediate
43
44str = "1234";
45v.validate(str, pos); // returns Invalid
46str = "-123";
47v.validate(str, pos); // returns Invalid
48str = "abc";
49v.validate(str, pos); // returns Invalid
50str = "12cm";
51v.validate(str, pos); // returns Invalid
52//! [1]
53} // Wrapper::wrapper0
54
56QString s;
57QIntValidator v(100, 900, this);
58
59//! [2]
60int pos = 0;
61
62s = "abc";
63v.validate(s, pos); // returns Invalid
64
65s = "5";
66v.validate(s, pos); // returns Intermediate
67
68s = "50";
69v.validate(s, pos); // returns Acceptable
70//! [2]
71} // Wrapper::wrapper1
72
73
75
76//! [5]
77// regexp: optional '-' followed by between 1 and 3 digits
78QRegularExpression rx("-?\\d{1,3}");
79QValidator *validator = new QRegularExpressionValidator(rx, this);
80
81QLineEdit *edit = new QLineEdit(this);
82edit->setValidator(validator);
83//! [5]
84
85//! [6]
86// integers 1 to 9999
87QRegularExpression re("[1-9]\\d{0,3}");
88// the validator treats the regexp as "^[1-9]\\d{0,3}$"
89QRegularExpressionValidator v(re, 0);
90QString s;
91int pos = 0;
92
93s = "0"; v.validate(s, pos); // returns Invalid
94s = "12345"; v.validate(s, pos); // returns Invalid
95s = "1"; v.validate(s, pos); // returns Acceptable
96
97re.setPattern("\\S+"); // one or more non-whitespace characters
98v.setRegularExpression(re);
99s = "myfile.txt"; v.validate(s, pos); // Returns Acceptable
100s = "my file.txt"; v.validate(s, pos); // Returns Invalid
101
102// A, B or C followed by exactly five digits followed by W, X, Y or Z
103re.setPattern("[A-C]\\d{5}[W-Z]");
104v.setRegularExpression(re);
105s = "a12345Z"; v.validate(s, pos); // Returns Invalid
106s = "A12345Z"; v.validate(s, pos); // Returns Acceptable
107s = "B12"; v.validate(s, pos); // Returns Intermediate
108
109// match most 'readme' files
110re.setPattern("read\\S?me(\\.(txt|asc|1st))?");
111re.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
112v.setRegularExpression(re);
113s = "readme"; v.validate(s, pos); // Returns Acceptable
114s = "README.1ST"; v.validate(s, pos); // Returns Acceptable
115s = "read me.txt"; v.validate(s, pos); // Returns Invalid
116s = "readm"; v.validate(s, pos); // Returns Intermediate
117//! [6]
118
119} // Wrapper::wrapper2
120
122{
123//! [7]
124QString input = "0.98765e2";
125QDoubleValidator val;
126val.setLocale(QLocale::C);
127val.setNotation(QDoubleValidator::ScientificNotation);
128val.fixup(input); // input == "9.8765e+01"
129//! [7]
130//! [8]
131input = "-1234.6789";
132val.setDecimals(2);
133val.setLocale(QLocale::C);
134val.setNotation(QDoubleValidator::StandardNotation);
135val.fixup(input); // input == "-1234.68"
136//! [8]
137} // Wrapper::wrapper3
138
139} // src_gui_util_qvalidator