1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "window.h"
52
53#include <QComboBox>
54#include <QGridLayout>
55#include <QGroupBox>
56#include <QLabel>
57#include <QLineEdit>
58
59//! [0]
60Window::Window(QWidget *parent)
61 : QWidget(parent)
62{
63 QGroupBox *echoGroup = new QGroupBox(tr(s: "Echo"));
64
65 QLabel *echoLabel = new QLabel(tr(s: "Mode:"));
66 QComboBox *echoComboBox = new QComboBox;
67 echoComboBox->addItem(atext: tr(s: "Normal"));
68 echoComboBox->addItem(atext: tr(s: "Password"));
69 echoComboBox->addItem(atext: tr(s: "PasswordEchoOnEdit"));
70 echoComboBox->addItem(atext: tr(s: "No Echo"));
71
72 echoLineEdit = new QLineEdit;
73 echoLineEdit->setPlaceholderText("Placeholder Text");
74 echoLineEdit->setFocus();
75//! [0]
76
77//! [1]
78 QGroupBox *validatorGroup = new QGroupBox(tr(s: "Validator"));
79
80 QLabel *validatorLabel = new QLabel(tr(s: "Type:"));
81 QComboBox *validatorComboBox = new QComboBox;
82 validatorComboBox->addItem(atext: tr(s: "No validator"));
83 validatorComboBox->addItem(atext: tr(s: "Integer validator"));
84 validatorComboBox->addItem(atext: tr(s: "Double validator"));
85
86 validatorLineEdit = new QLineEdit;
87 validatorLineEdit->setPlaceholderText("Placeholder Text");
88//! [1]
89
90//! [2]
91 QGroupBox *alignmentGroup = new QGroupBox(tr(s: "Alignment"));
92
93 QLabel *alignmentLabel = new QLabel(tr(s: "Type:"));
94 QComboBox *alignmentComboBox = new QComboBox;
95 alignmentComboBox->addItem(atext: tr(s: "Left"));
96 alignmentComboBox->addItem(atext: tr(s: "Centered"));
97 alignmentComboBox->addItem(atext: tr(s: "Right"));
98
99 alignmentLineEdit = new QLineEdit;
100 alignmentLineEdit->setPlaceholderText("Placeholder Text");
101//! [2]
102
103//! [3]
104 QGroupBox *inputMaskGroup = new QGroupBox(tr(s: "Input mask"));
105
106 QLabel *inputMaskLabel = new QLabel(tr(s: "Type:"));
107 QComboBox *inputMaskComboBox = new QComboBox;
108 inputMaskComboBox->addItem(atext: tr(s: "No mask"));
109 inputMaskComboBox->addItem(atext: tr(s: "Phone number"));
110 inputMaskComboBox->addItem(atext: tr(s: "ISO date"));
111 inputMaskComboBox->addItem(atext: tr(s: "License key"));
112
113 inputMaskLineEdit = new QLineEdit;
114 inputMaskLineEdit->setPlaceholderText("Placeholder Text");
115//! [3]
116
117//! [4]
118 QGroupBox *accessGroup = new QGroupBox(tr(s: "Access"));
119
120 QLabel *accessLabel = new QLabel(tr(s: "Read-only:"));
121 QComboBox *accessComboBox = new QComboBox;
122 accessComboBox->addItem(atext: tr(s: "False"));
123 accessComboBox->addItem(atext: tr(s: "True"));
124
125 accessLineEdit = new QLineEdit;
126 accessLineEdit->setPlaceholderText("Placeholder Text");
127//! [4]
128
129//! [5]
130 connect(sender: echoComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated),
131 receiver: this, slot: &Window::echoChanged);
132 connect(sender: validatorComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated),
133 receiver: this, slot: &Window::validatorChanged);
134 connect(sender: alignmentComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated),
135 receiver: this, slot: &Window::alignmentChanged);
136 connect(sender: inputMaskComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated),
137 receiver: this, slot: &Window::inputMaskChanged);
138 connect(sender: accessComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated),
139 receiver: this, slot: &Window::accessChanged);
140//! [5]
141
142//! [6]
143 QGridLayout *echoLayout = new QGridLayout;
144 echoLayout->addWidget(echoLabel, row: 0, column: 0);
145 echoLayout->addWidget(echoComboBox, row: 0, column: 1);
146 echoLayout->addWidget(echoLineEdit, row: 1, column: 0, rowSpan: 1, columnSpan: 2);
147 echoGroup->setLayout(echoLayout);
148//! [6]
149
150//! [7]
151 QGridLayout *validatorLayout = new QGridLayout;
152 validatorLayout->addWidget(validatorLabel, row: 0, column: 0);
153 validatorLayout->addWidget(validatorComboBox, row: 0, column: 1);
154 validatorLayout->addWidget(validatorLineEdit, row: 1, column: 0, rowSpan: 1, columnSpan: 2);
155 validatorGroup->setLayout(validatorLayout);
156
157 QGridLayout *alignmentLayout = new QGridLayout;
158 alignmentLayout->addWidget(alignmentLabel, row: 0, column: 0);
159 alignmentLayout->addWidget(alignmentComboBox, row: 0, column: 1);
160 alignmentLayout->addWidget(alignmentLineEdit, row: 1, column: 0, rowSpan: 1, columnSpan: 2);
161 alignmentGroup-> setLayout(alignmentLayout);
162
163 QGridLayout *inputMaskLayout = new QGridLayout;
164 inputMaskLayout->addWidget(inputMaskLabel, row: 0, column: 0);
165 inputMaskLayout->addWidget(inputMaskComboBox, row: 0, column: 1);
166 inputMaskLayout->addWidget(inputMaskLineEdit, row: 1, column: 0, rowSpan: 1, columnSpan: 2);
167 inputMaskGroup->setLayout(inputMaskLayout);
168
169 QGridLayout *accessLayout = new QGridLayout;
170 accessLayout->addWidget(accessLabel, row: 0, column: 0);
171 accessLayout->addWidget(accessComboBox, row: 0, column: 1);
172 accessLayout->addWidget(accessLineEdit, row: 1, column: 0, rowSpan: 1, columnSpan: 2);
173 accessGroup->setLayout(accessLayout);
174//! [7]
175
176//! [8]
177 QGridLayout *layout = new QGridLayout;
178 layout->addWidget(echoGroup, row: 0, column: 0);
179 layout->addWidget(validatorGroup, row: 1, column: 0);
180 layout->addWidget(alignmentGroup, row: 2, column: 0);
181 layout->addWidget(inputMaskGroup, row: 0, column: 1);
182 layout->addWidget(accessGroup, row: 1, column: 1);
183 setLayout(layout);
184
185 setWindowTitle(tr(s: "Line Edits"));
186}
187//! [8]
188
189//! [9]
190void Window::echoChanged(int index)
191{
192 switch (index) {
193 case 0:
194 echoLineEdit->setEchoMode(QLineEdit::Normal);
195 break;
196 case 1:
197 echoLineEdit->setEchoMode(QLineEdit::Password);
198 break;
199 case 2:
200 echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
201 break;
202 case 3:
203 echoLineEdit->setEchoMode(QLineEdit::NoEcho);
204 break;
205 }
206}
207//! [9]
208
209//! [10]
210void Window::validatorChanged(int index)
211{
212 switch (index) {
213 case 0:
214 validatorLineEdit->setValidator(nullptr);
215 break;
216 case 1:
217 validatorLineEdit->setValidator(new QIntValidator(
218 validatorLineEdit));
219 break;
220 case 2:
221 validatorLineEdit->setValidator(new QDoubleValidator(-999.0,
222 999.0, 2, validatorLineEdit));
223 break;
224 }
225
226 validatorLineEdit->clear();
227}
228//! [10]
229
230//! [11]
231void Window::alignmentChanged(int index)
232{
233 switch (index) {
234 case 0:
235 alignmentLineEdit->setAlignment(Qt::AlignLeft);
236 break;
237 case 1:
238 alignmentLineEdit->setAlignment(Qt::AlignCenter);
239 break;
240 case 2:
241 alignmentLineEdit->setAlignment(Qt::AlignRight);
242 break;
243 }
244}
245//! [11]
246
247//! [12]
248void Window::inputMaskChanged(int index)
249{
250 switch (index) {
251 case 0:
252 inputMaskLineEdit->setInputMask("");
253 break;
254 case 1:
255 inputMaskLineEdit->setInputMask("+99 99 99 99 99;_");
256 break;
257 case 2:
258 inputMaskLineEdit->setInputMask("0000-00-00");
259 inputMaskLineEdit->setText("00000000");
260 inputMaskLineEdit->setCursorPosition(0);
261 break;
262 case 3:
263 inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
264 break;
265 }
266}
267//! [12]
268
269//! [13]
270void Window::accessChanged(int index)
271{
272 switch (index) {
273 case 0:
274 accessLineEdit->setReadOnly(false);
275 break;
276 case 1:
277 accessLineEdit->setReadOnly(true);
278 break;
279 }
280}
281//! [13]
282

source code of qtbase/examples/widgets/widgets/lineedits/window.cpp