Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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);
24
25
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
53} // Wrapper::wrapper0
54
57QIntValidator v(100, 900, this);
58
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
71} // Wrapper::wrapper1
72
73
75
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);
84
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);
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))?");
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
118
119} // Wrapper::wrapper2
120
122{
124QString input = "0.98765e2";
128val.fixup(input); // input == "9.8765e+01"
131input = "-1234.6789";
132val.setDecimals(2);
133val.setLocale(QLocale::C);
135val.fixup(input); // input == "-1234.68"
137} // Wrapper::wrapper3
138
139} // src_gui_util_qvalidator
The QDoubleValidator class provides range checking of floating-point numbers.
Definition qvalidator.h:89
The QIntValidator class provides a validator that ensures a string contains a valid integer within a ...
Definition qvalidator.h:56
The QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
void setValidator(const QValidator *)
Sets the validator for values of line edit to v.
\inmodule QtCore \reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QValidator class provides validation of input text.
Definition qvalidator.h:24
void setLocale(const QLocale &locale)
Sets the locale that will be used for the validator.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QPoint pos
the position of the widget within its parent widget
Definition qwidget.h:111
QString str
[2]
GLsizei const GLfloat * v
[13]
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLfloat * val
GLenum GLenum GLenum input
p rx()++