-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidatordemo.cpp
142 lines (116 loc) · 4.46 KB
/
validatordemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "validatordemo.h"
ValidatorDemo::ValidatorDemo(QWidget *parent)
: QWidget{parent}
{
setupWidgets();
setupValidators();
connect(radIntValid, &QRadioButton::toggled, this, &ValidatorDemo::onIntValidSel);
connect(radDoubleValidUs, &QRadioButton::toggled, this, &ValidatorDemo::onDoubleUsValidSel);
connect(radDoubleValidDe, &QRadioButton::toggled, this, &ValidatorDemo::onDoubleDeValidSel);
connect(radRegExValid, &QRadioButton::toggled, this, &ValidatorDemo::onRegExValidSel);
connect(radNoValid, &QRadioButton::toggled, this, &ValidatorDemo::onNoValidSel);
connect(txtMaxVal, &QLineEdit::editingFinished, this, &ValidatorDemo::onMaxLenChange);
connect(lineEdit, &QLineEdit::editingFinished, sigindEditingFinished, &SignalIndicator::signalTriggered);
connect(lineEdit, &QLineEdit::returnPressed, sigindReturnPressed, &SignalIndicator::signalTriggered);
radIntValid->toggle();
setupLayout();
}
void ValidatorDemo::setupWidgets()
{
lineEdit = new QLineEdit();
radIntValid = new QRadioButton("QIntValidator 100-200");
radDoubleValidUs = new QRadioButton("QDoubleValidator en_US 100.0-200.0, 1dez");
radDoubleValidDe = new QRadioButton("QDoubleValidator de_DE 100,0-200,0, 1dez");
// QLabel is picky about text input. \173 = octal code for left bracket {
radRegExValid = new QRadioButton("QRegExpValidator \\w\1733, 16\175");
radNoValid = new QRadioButton("No Validator");
// the max length line edit reads the default value on startup
lblMaxVal = new QLabel("Maximum Length: ");
txtMaxVal = new QLineEdit();
sigindEditingFinished = new SignalIndicator();
sigindReturnPressed = new SignalIndicator();
sigindEditingFinished->setText("editingFinished()");
sigindReturnPressed->setText("returnPressed()");
sigindEditingFinished->setChecked(true);
sigindReturnPressed->setChecked(true);
}
void ValidatorDemo::setupValidators()
{
validInt = new QIntValidator(100, 200, this);
validDoubleUs = new QDoubleValidator(100.0, 200.0, 1);
validDoubleUs->setLocale(QLocale("en_US"));
validDoubleDe = new QDoubleValidator(100.0, 200.0, 1);
validDoubleUs->setLocale(QLocale("de_DE"));
validRegEx = new QRegularExpressionValidator(QRegularExpression("\\w{3,16}"), this);
}
void ValidatorDemo::setupLayout()
{
QGroupBox *groupValidators = new QGroupBox("Validators");
QVBoxLayout *layoutValidators = new QVBoxLayout(groupValidators);
layoutValidators->addWidget(radIntValid);
layoutValidators->addWidget(radDoubleValidUs);
layoutValidators->addWidget(radDoubleValidDe);
layoutValidators->addWidget(radRegExValid);
layoutValidators->addWidget(radNoValid);
QGroupBox *groupMaxLen = new QGroupBox("Maximum Length");
QHBoxLayout *layoutMaxLen = new QHBoxLayout(groupMaxLen);
layoutMaxLen->addWidget(lblMaxVal);
layoutMaxLen->addWidget(txtMaxVal);
QGroupBox *groupIndicators = new QGroupBox("Signal Monitor");
QVBoxLayout *layoutIndicators = new QVBoxLayout(groupIndicators);
layoutIndicators->addWidget(sigindEditingFinished);
layoutIndicators->addWidget(sigindReturnPressed);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(lineEdit);
mainLayout->addWidget(groupValidators);
mainLayout->addWidget(groupMaxLen);
mainLayout->addWidget(groupIndicators);
}
void ValidatorDemo::onIntValidSel(bool checked)
{
if(checked)
{
lineEdit->setValidator(validInt);
txtMaxVal->setText(QString().setNum(lineEdit->maxLength()));
}
}
void ValidatorDemo::onDoubleUsValidSel(bool checked)
{
if(checked)
{
lineEdit->setValidator(validDoubleUs);
txtMaxVal->setText(QString().setNum(lineEdit->maxLength()));
}
}
void ValidatorDemo::onDoubleDeValidSel(bool checked)
{
if(checked)
{
lineEdit->setValidator(validDoubleDe);
txtMaxVal->setText(QString().setNum(lineEdit->maxLength()));
}
}
void ValidatorDemo::onRegExValidSel(bool checked)
{
if(checked)
{
lineEdit->setValidator(validRegEx);
txtMaxVal->setText(QString().setNum(lineEdit->maxLength()));
}
}
void ValidatorDemo::onNoValidSel(bool checked)
{
if(checked)
{
lineEdit->setValidator(0);
txtMaxVal->setText(QString().setNum(lineEdit->maxLength()));
}
}
void ValidatorDemo::onMaxLenChange()
{
QString maxValStr = txtMaxVal->text();
bool ok;
int maxVal = maxValStr.toInt(&ok);
if (ok)
lineEdit->setMaxLength(maxVal);
}