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