1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/lineedits/lineedits.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Line Edits</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Line Edits</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example shows how to work with single lineedit widgets, and
|
---|
37 | how to use different echo modes and validators.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: lineedits-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
44 | **
|
---|
45 | ** This file is part of an example program for Qt. This example
|
---|
46 | ** program may be used, distributed and modified without limitation.
|
---|
47 | **
|
---|
48 | *****************************************************************************/
|
---|
49 |
|
---|
50 | #ifndef LINEDITS_H
|
---|
51 | #define LINEDITS_H
|
---|
52 |
|
---|
53 | #include <<a href="qgroupbox-h.html">qgroupbox.h</a>>
|
---|
54 |
|
---|
55 | class QLineEdit;
|
---|
56 | class QComboBox;
|
---|
57 |
|
---|
58 | class LineEdits : public <a href="qgroupbox.html">QGroupBox</a>
|
---|
59 | {
|
---|
60 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
61 |
|
---|
62 | public:
|
---|
63 | LineEdits( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
|
---|
64 |
|
---|
65 | protected:
|
---|
66 | <a href="qlineedit.html">QLineEdit</a> *lined1, *lined2, *lined3, *lined4, *lined5;
|
---|
67 | <a href="qcombobox.html">QComboBox</a> *combo1, *combo2, *combo3, *combo4, *combo5;
|
---|
68 |
|
---|
69 | protected slots:
|
---|
70 | void slotEchoChanged( int );
|
---|
71 | void slotValidatorChanged( int );
|
---|
72 | void slotAlignmentChanged( int );
|
---|
73 | void slotInputMaskChanged( int );
|
---|
74 | void slotReadOnlyChanged( int );
|
---|
75 | };
|
---|
76 |
|
---|
77 | #endif
|
---|
78 | </pre>
|
---|
79 |
|
---|
80 | <p> <hr>
|
---|
81 | <p> Implementation:
|
---|
82 | <p> <pre>/****************************************************************************
|
---|
83 | ** $Id: lineedits-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
84 | **
|
---|
85 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
86 | **
|
---|
87 | ** This file is part of an example program for Qt. This example
|
---|
88 | ** program may be used, distributed and modified without limitation.
|
---|
89 | **
|
---|
90 | *****************************************************************************/
|
---|
91 |
|
---|
92 | #include "lineedits.h"
|
---|
93 |
|
---|
94 | #include <<a href="qlineedit-h.html">qlineedit.h</a>>
|
---|
95 | #include <<a href="qcombobox-h.html">qcombobox.h</a>>
|
---|
96 | #include <<a href="qframe-h.html">qframe.h</a>>
|
---|
97 | #include <<a href="qvalidator-h.html">qvalidator.h</a>>
|
---|
98 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
99 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
100 | #include <<a href="qhbox-h.html">qhbox.h</a>>
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Constructor
|
---|
104 | *
|
---|
105 | * Creates child widgets of the LineEdits widget
|
---|
106 | */
|
---|
107 |
|
---|
108 | <a name="f219"></a>LineEdits::LineEdits( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
109 | : <a href="qgroupbox.html">QGroupBox</a>( 0, Horizontal, "Line edits", parent, name )
|
---|
110 | {
|
---|
111 | <a href="qframe.html#setMargin">setMargin</a>( 10 );
|
---|
112 |
|
---|
113 | <a href="qvboxlayout.html">QVBoxLayout</a>* box = new <a href="qvboxlayout.html">QVBoxLayout</a>( <a href="qwidget.html#layout">layout</a>() );
|
---|
114 |
|
---|
115 | <a href="qhboxlayout.html">QHBoxLayout</a> *row1 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
|
---|
116 | <a name="x173"></a> row1-><a href="qlayout.html#setMargin">setMargin</a>( 5 );
|
---|
117 |
|
---|
118 | // Create a Label
|
---|
119 | <a href="qlabel.html">QLabel</a>* label = new <a href="qlabel.html">QLabel</a>( "Echo Mode: ", this);
|
---|
120 | <a name="x169"></a> row1-><a href="qboxlayout.html#addWidget">addWidget</a>( label );
|
---|
121 |
|
---|
122 | // Create a Combobox with three items...
|
---|
123 | combo1 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
|
---|
124 | row1-><a href="qboxlayout.html#addWidget">addWidget</a>( combo1 );
|
---|
125 | <a name="x171"></a> combo1-><a href="qcombobox.html#insertItem">insertItem</a>( "Normal" );
|
---|
126 | combo1-><a href="qcombobox.html#insertItem">insertItem</a>( "Password" );
|
---|
127 | combo1-><a href="qcombobox.html#insertItem">insertItem</a>( "No Echo" );
|
---|
128 | // ...and connect the activated() SIGNAL with the slotEchoChanged() SLOT to be able
|
---|
129 | // to react when an item is selected
|
---|
130 | <a name="x170"></a> <a href="qobject.html#connect">connect</a>( combo1, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotEchoChanged( int ) ) );
|
---|
131 |
|
---|
132 | // insert the first LineEdit
|
---|
133 | lined1 = new <a href="qlineedit.html">QLineEdit</a>( this );
|
---|
134 | box-><a href="qboxlayout.html#addWidget">addWidget</a>( lined1 );
|
---|
135 |
|
---|
136 | // another widget which is used for layouting
|
---|
137 | <a href="qhboxlayout.html">QHBoxLayout</a> *row2 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
|
---|
138 | row2-><a href="qlayout.html#setMargin">setMargin</a>( 5 );
|
---|
139 |
|
---|
140 | // and the second label
|
---|
141 | label = new <a href="qlabel.html">QLabel</a>( "Validator: ", this );
|
---|
142 | row2-><a href="qboxlayout.html#addWidget">addWidget</a>( label );
|
---|
143 |
|
---|
144 | // A second Combobox with again three items...
|
---|
145 | combo2 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
|
---|
146 | row2-><a href="qboxlayout.html#addWidget">addWidget</a>( combo2 );
|
---|
147 | combo2-><a href="qcombobox.html#insertItem">insertItem</a>( "No Validator" );
|
---|
148 | combo2-><a href="qcombobox.html#insertItem">insertItem</a>( "Integer Validator" );
|
---|
149 | combo2-><a href="qcombobox.html#insertItem">insertItem</a>( "Double Validator" );
|
---|
150 | // ...and again the activated() SIGNAL gets connected with a SLOT
|
---|
151 | <a href="qobject.html#connect">connect</a>( combo2, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotValidatorChanged( int ) ) );
|
---|
152 |
|
---|
153 | // and the second LineEdit
|
---|
154 | lined2 = new <a href="qlineedit.html">QLineEdit</a>( this );
|
---|
155 | box-><a href="qboxlayout.html#addWidget">addWidget</a>( lined2 );
|
---|
156 |
|
---|
157 | // yet another widget which is used for layouting
|
---|
158 | <a href="qhboxlayout.html">QHBoxLayout</a> *row3 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
|
---|
159 | row3-><a href="qlayout.html#setMargin">setMargin</a>( 5 );
|
---|
160 |
|
---|
161 | // we need a label for this too
|
---|
162 | label = new <a href="qlabel.html">QLabel</a>( "Alignment: ", this );
|
---|
163 | row3-><a href="qboxlayout.html#addWidget">addWidget</a>( label );
|
---|
164 |
|
---|
165 | // A combo box for setting alignment
|
---|
166 | combo3 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
|
---|
167 | row3-><a href="qboxlayout.html#addWidget">addWidget</a>( combo3 );
|
---|
168 | combo3-><a href="qcombobox.html#insertItem">insertItem</a>( "Left" );
|
---|
169 | combo3-><a href="qcombobox.html#insertItem">insertItem</a>( "Centered" );
|
---|
170 | combo3-><a href="qcombobox.html#insertItem">insertItem</a>( "Right" );
|
---|
171 | // ...and again the activated() SIGNAL gets connected with a SLOT
|
---|
172 | <a href="qobject.html#connect">connect</a>( combo3, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotAlignmentChanged( int ) ) );
|
---|
173 |
|
---|
174 | // and the third lineedit
|
---|
175 | lined3 = new <a href="qlineedit.html">QLineEdit</a>( this );
|
---|
176 | box-><a href="qboxlayout.html#addWidget">addWidget</a>( lined3 );
|
---|
177 |
|
---|
178 | // exactly the same for the fourth
|
---|
179 | <a href="qhboxlayout.html">QHBoxLayout</a> *row4 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
|
---|
180 | row4-><a href="qlayout.html#setMargin">setMargin</a>( 5 );
|
---|
181 |
|
---|
182 | // we need a label for this too
|
---|
183 | label = new <a href="qlabel.html">QLabel</a>( "Input mask: ", this );
|
---|
184 | row4-><a href="qboxlayout.html#addWidget">addWidget</a>( label );
|
---|
185 |
|
---|
186 | // A combo box for choosing an input mask
|
---|
187 | combo4 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
|
---|
188 | row4-><a href="qboxlayout.html#addWidget">addWidget</a>( combo4 );
|
---|
189 | combo4-><a href="qcombobox.html#insertItem">insertItem</a>( "No mask" );
|
---|
190 | combo4-><a href="qcombobox.html#insertItem">insertItem</a>( "Phone number" );
|
---|
191 | combo4-><a href="qcombobox.html#insertItem">insertItem</a>( "ISO date" );
|
---|
192 | combo4-><a href="qcombobox.html#insertItem">insertItem</a>( "License key" );
|
---|
193 |
|
---|
194 | // ...this time we use the activated( const <a href="qstring.html">QString</a> & ) signal
|
---|
195 | <a href="qobject.html#connect">connect</a>( combo4, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ),
|
---|
196 | this, SLOT( slotInputMaskChanged( int ) ) );
|
---|
197 |
|
---|
198 | // and the fourth lineedit
|
---|
199 | lined4 = new <a href="qlineedit.html">QLineEdit</a>( this );
|
---|
200 | box-><a href="qboxlayout.html#addWidget">addWidget</a>( lined4 );
|
---|
201 |
|
---|
202 | // last widget used for layouting
|
---|
203 | <a href="qhbox.html">QHBox</a> *row5 = new <a href="qhbox.html">QHBox</a>( this );
|
---|
204 | box-><a href="qboxlayout.html#addWidget">addWidget</a>( row5 );
|
---|
205 | row5-><a href="qframe.html#setMargin">setMargin</a>( 5 );
|
---|
206 |
|
---|
207 | // last label
|
---|
208 | (void)new <a href="qlabel.html">QLabel</a>( "Read-Only: ", row5 );
|
---|
209 |
|
---|
210 | // A combo box for setting alignment
|
---|
211 | combo5 = new <a href="qcombobox.html">QComboBox</a>( FALSE, row5 );
|
---|
212 | combo5-><a href="qcombobox.html#insertItem">insertItem</a>( "False" );
|
---|
213 | combo5-><a href="qcombobox.html#insertItem">insertItem</a>( "True" );
|
---|
214 | // ...and again the activated() SIGNAL gets connected with a SLOT
|
---|
215 | <a href="qobject.html#connect">connect</a>( combo5, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotReadOnlyChanged( int ) ) );
|
---|
216 |
|
---|
217 | // and the last lineedit
|
---|
218 | lined5 = new <a href="qlineedit.html">QLineEdit</a>( this );
|
---|
219 | box-><a href="qboxlayout.html#addWidget">addWidget</a>( lined5 );
|
---|
220 |
|
---|
221 | // give the first LineEdit the focus at the beginning
|
---|
222 | <a name="x181"></a> lined1-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
223 | }
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * SLOT slotEchoChanged( int i )
|
---|
227 | *
|
---|
228 | * i contains the number of the item which the user has been chosen in the
|
---|
229 | * first Combobox. According to this value, we set the Echo-Mode for the
|
---|
230 | * first LineEdit.
|
---|
231 | */
|
---|
232 |
|
---|
233 | void <a name="f220"></a>LineEdits::slotEchoChanged( int i )
|
---|
234 | {
|
---|
235 | switch ( i ) {
|
---|
236 | case 0:
|
---|
237 | <a name="x176"></a> lined1-><a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::Normal );
|
---|
238 | break;
|
---|
239 | case 1:
|
---|
240 | lined1-><a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::Password );
|
---|
241 | break;
|
---|
242 | case 2:
|
---|
243 | lined1-><a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::NoEcho );
|
---|
244 | break;
|
---|
245 | }
|
---|
246 |
|
---|
247 | lined1-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * SLOT slotValidatorChanged( int i )
|
---|
252 | *
|
---|
253 | * i contains the number of the item which the user has been chosen in the
|
---|
254 | * second Combobox. According to this value, we set a validator for the
|
---|
255 | * second LineEdit. A validator checks in a LineEdit each character which
|
---|
256 | * the user enters and accepts it if it is valid, else the character gets
|
---|
257 | * ignored and not inserted into the lineedit.
|
---|
258 | */
|
---|
259 |
|
---|
260 | void <a name="f221"></a>LineEdits::slotValidatorChanged( int i )
|
---|
261 | {
|
---|
262 | switch ( i ) {
|
---|
263 | case 0:
|
---|
264 | <a name="x180"></a> lined2-><a href="qlineedit.html#setValidator">setValidator</a>( 0 );
|
---|
265 | break;
|
---|
266 | case 1:
|
---|
267 | lined2-><a href="qlineedit.html#setValidator">setValidator</a>( new <a href="qintvalidator.html">QIntValidator</a>( lined2 ) );
|
---|
268 | break;
|
---|
269 | case 2:
|
---|
270 | lined2-><a href="qlineedit.html#setValidator">setValidator</a>( new <a href="qdoublevalidator.html">QDoubleValidator</a>( -999.0, 999.0, 2,
|
---|
271 | lined2 ) );
|
---|
272 | break;
|
---|
273 | }
|
---|
274 |
|
---|
275 | <a name="x179"></a> lined2-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
276 | lined2-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * SLOT slotAlignmentChanged( int i )
|
---|
282 | *
|
---|
283 | * i contains the number of the item which the user has been chosen in
|
---|
284 | * the third Combobox. According to this value, we set an alignment
|
---|
285 | * third LineEdit.
|
---|
286 | */
|
---|
287 |
|
---|
288 | void <a name="f222"></a>LineEdits::slotAlignmentChanged( int i )
|
---|
289 | {
|
---|
290 | switch ( i ) {
|
---|
291 | case 0:
|
---|
292 | <a name="x174"></a> lined3-><a href="qlineedit.html#setAlignment">setAlignment</a>( QLineEdit::AlignLeft );
|
---|
293 | break;
|
---|
294 | case 1:
|
---|
295 | lined3-><a href="qlineedit.html#setAlignment">setAlignment</a>( QLineEdit::AlignCenter );
|
---|
296 | break;
|
---|
297 | case 2:
|
---|
298 | lined3-><a href="qlineedit.html#setAlignment">setAlignment</a>( QLineEdit::AlignRight );
|
---|
299 | break;
|
---|
300 | }
|
---|
301 |
|
---|
302 | lined3-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
303 | }
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * SLOT slotInputMaskChanged( const <a href="qstring.html">QString</a> &mask )
|
---|
307 | *
|
---|
308 | * i contains the number of the item which the user has been chosen in
|
---|
309 | * the third Combobox. According to this value, we set an input mask on
|
---|
310 | * third LineEdit.
|
---|
311 | */
|
---|
312 |
|
---|
313 | void <a name="f223"></a>LineEdits::slotInputMaskChanged( int i )
|
---|
314 | {
|
---|
315 | switch( i ) {
|
---|
316 | case 0:
|
---|
317 | <a name="x177"></a> lined4-><a href="qlineedit.html#setInputMask">setInputMask</a>( <a href="qstring.html#QString-null">QString::null</a> );
|
---|
318 | break;
|
---|
319 | case 1:
|
---|
320 | lined4-><a href="qlineedit.html#setInputMask">setInputMask</a>( "+99 99 99 99 99;_" );
|
---|
321 | break;
|
---|
322 | case 2:
|
---|
323 | lined4-><a href="qlineedit.html#setInputMask">setInputMask</a>( "0000-00-00" );
|
---|
324 | lined4-><a href="qlineedit.html#setText">setText</a>( "00000000" );
|
---|
325 | <a name="x175"></a> lined4-><a href="qlineedit.html#setCursorPosition">setCursorPosition</a>( 0 );
|
---|
326 | break;
|
---|
327 | case 3:
|
---|
328 | lined4-><a href="qlineedit.html#setInputMask">setInputMask</a>( ">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#" );
|
---|
329 | break;
|
---|
330 | }
|
---|
331 | lined4-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
332 | }
|
---|
333 |
|
---|
334 | /*
|
---|
335 | * SLOT slotReadOnlyChanged( int i )
|
---|
336 | *
|
---|
337 | * i contains the number of the item which the user has been chosen in
|
---|
338 | * the fourth Combobox. According to this value, we toggle read-only.
|
---|
339 | */
|
---|
340 |
|
---|
341 | void <a name="f224"></a>LineEdits::slotReadOnlyChanged( int i )
|
---|
342 | {
|
---|
343 | switch ( i ) {
|
---|
344 | case 0:
|
---|
345 | <a name="x178"></a> lined5-><a href="qlineedit.html#setReadOnly">setReadOnly</a>( FALSE );
|
---|
346 | break;
|
---|
347 | case 1:
|
---|
348 | lined5-><a href="qlineedit.html#setReadOnly">setReadOnly</a>( TRUE );
|
---|
349 | break;
|
---|
350 | }
|
---|
351 |
|
---|
352 | lined5-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
353 | }
|
---|
354 |
|
---|
355 | </pre>
|
---|
356 |
|
---|
357 | <p> <hr>
|
---|
358 | <p> Main:
|
---|
359 | <p> <pre>/****************************************************************************
|
---|
360 | ** $Id: lineedits-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
361 | **
|
---|
362 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
363 | **
|
---|
364 | ** This file is part of an example program for Qt. This example
|
---|
365 | ** program may be used, distributed and modified without limitation.
|
---|
366 | **
|
---|
367 | *****************************************************************************/
|
---|
368 |
|
---|
369 | #include "lineedits.h"
|
---|
370 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
371 |
|
---|
372 | int main( int argc, char **argv )
|
---|
373 | {
|
---|
374 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
375 |
|
---|
376 | LineEdits lineedits;
|
---|
377 | lineedits.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Lineedits" );
|
---|
378 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &lineedits );
|
---|
379 | lineedits.<a href="qwidget.html#show">show</a>();
|
---|
380 |
|
---|
381 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
382 | }
|
---|
383 | </pre>
|
---|
384 |
|
---|
385 | <p>See also <a href="examples.html">Examples</a>.
|
---|
386 |
|
---|
387 | <!-- eof -->
|
---|
388 | <p><address><hr><div align=center>
|
---|
389 | <table width=100% cellspacing=0 border=0><tr>
|
---|
390 | <td>Copyright © 2007
|
---|
391 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
392 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
393 | </table></div></address></body>
|
---|
394 | </html>
|
---|