Qt 5.10 Book Examples
chapter10
Validator
main.cpp
Go to the documentation of this file.
1
// ======================================================================
2
// main.cpp
3
// ======================================================================
4
// This file is a part of the book
5
// "Qt 5.10 Professional programming with C++"
6
// http://qt-book.com
7
// ======================================================================
8
// Copyright (c) 2017 by Max Schlee
9
// ======================================================================
10
11
#include <QtWidgets>
12
13
// ======================================================================
14
class
NameValidator
:
public
QValidator {
15
public
:
16
NameValidator
(QObject* parent) : QValidator(parent)
17
{
18
}
19
20
virtual
State
validate
(QString& str,
int
&)
const
21
{
22
QRegExp rxp = QRegExp(
"[0-9]"
);
23
if
(str.contains(rxp)) {
24
return
Invalid;
25
}
26
return
Acceptable;
27
}
28
};
29
30
31
// ----------------------------------------------------------------------
32
int
main
(
int
argc,
char
** argv)
33
{
34
QApplication app(argc, argv);
35
QWidget wgt;
36
37
QLabel* plblText =
38
new
QLabel(
"&Name (The digits will not be accepted!):"
);
39
40
QLineEdit* ptxt =
new
QLineEdit;
41
42
NameValidator
* pnameValidator =
new
NameValidator
(ptxt);
43
ptxt->setValidator(pnameValidator);
44
plblText->setBuddy(ptxt);
45
46
//Layout setup
47
QVBoxLayout* pvbxLayout =
new
QVBoxLayout;
48
pvbxLayout->addWidget(plblText);
49
pvbxLayout->addWidget(ptxt);
50
wgt.setLayout(pvbxLayout);
51
wgt.show();
52
53
return
app.exec();
54
}
NameValidator
Definition:
main.cpp:14
NameValidator::validate
virtual State validate(QString &str, int &) const
Definition:
main.cpp:20
main
int main(int argc, char **argv)
Definition:
main.cpp:15
NameValidator::NameValidator
NameValidator(QObject *parent)
Definition:
main.cpp:16
Generated by
1.8.14