File: lineedits-main-cpp.html

package info (click to toggle)
qt-x11 3%3A2.3.1-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48,524 kB
  • ctags: 46,337
  • sloc: cpp: 260,077; ansic: 32,457; makefile: 31,131; yacc: 2,444; sh: 1,513; lex: 480; perl: 422; xml: 68; lisp: 15
file content (328 lines) | stat: -rw-r--r-- 11,897 bytes parent folder | download | duplicates (2)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Toolkit - lineedits/main.cpp example file
</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">

<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>

<h1 align=center>Line Edits</h1><br clear="all">

  The Lineedits example shows how to work with single Lineedit widgets, and
  how to use different Echo Modes and Validators.

  <hr>

  Header file: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/lineedits/lineedits.h   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef LINEDITS_H
#define LINEDITS_H

#include &lt;<a href="qgroupbox-h.html">qgroupbox.h</a>&gt;

class QLineEdit;
class QComboBox;

class LineEdits : public QGroupBox
{
    Q_OBJECT

public:
    LineEdits( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );

protected:
    <a href="qlineedit.html">QLineEdit</a> *lined1, *lined2, *lined3, *lined4;
    <a href="qcombobox.html">QComboBox</a> *combo1, *combo2, *combo3, *combo4;

protected slots:
    void slotEchoChanged( int );
    void slotValidatorChanged( int );
    void slotAlignmentChanged( int );
    void slotReadOnlyChanged( int );
};

#endif
</pre>


  <hr>

  Implementation: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/lineedits/lineedits.cpp   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "lineedits.h"

#include &lt;<a href="qlineedit-h.html">qlineedit.h</a>&gt;
#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
#include &lt;<a href="qframe-h.html">qframe.h</a>&gt;
#include &lt;<a href="qvalidator-h.html">qvalidator.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;

/*
 * Constructor
 *
 * Creates child widgets of the LineEdits widget
 */

LineEdits::LineEdits( <a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qgroupbox.html">QGroupBox</a>( 0, Horizontal, "Line edits", parent, name )
{
    <a href="qframe.html#ab991e">setMargin</a>( 10 );

    <a href="qvboxlayout.html">QVBoxLayout</a>* box = new <a href="qvboxlayout.html">QVBoxLayout</a>( <a href="qwidget.html#286df8">layout</a>() );

    <a href="qhboxlayout.html">QHBoxLayout</a> *row1 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
    row1-&gt;<a href="qlayout.html#cd2c99">setMargin</a>( 5 );

    // Create a Label
    <a href="qlabel.html">QLabel</a>* label = new <a href="qlabel.html">QLabel</a>( "Echo Mode: ", this);
    row1-&gt;<a href="qboxlayout.html#219265">addWidget</a>( label );

    // Create a Combobox with three items...
    combo1 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
    row1-&gt;<a href="qboxlayout.html#219265">addWidget</a>( combo1 );
    combo1-&gt;insertItem( "Normal", -1 );
    combo1-&gt;insertItem( "Password", -1 );
    combo1-&gt;insertItem( "No Echo", -1 );
    // ...and connect the activated() SIGNAL with the slotEchoChanged() SLOT to be able
    // to react when an item is selected
    <a href="qobject.html#c2d209">connect</a>( combo1, SIGNAL( activated( int ) ), this, SLOT( <a href=#198>slotEchoChanged</a>( int ) ) );

    // insert the first LineEdit
    lined1 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#219265">addWidget</a>( lined1 );

    // another widget which is used for layouting
    <a href="qhboxlayout.html">QHBoxLayout</a> *row2 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
    row2-&gt;<a href="qlayout.html#cd2c99">setMargin</a>( 5 );

    // and the second label
    label = new <a href="qlabel.html">QLabel</a>( "Validator: ", this );
    row2-&gt;<a href="qboxlayout.html#219265">addWidget</a>( label );

    // A second Combobox with again three items...
    combo2 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
    row2-&gt;<a href="qboxlayout.html#219265">addWidget</a>( combo2 );
    combo2-&gt;insertItem( "No Validator", -1 );
    combo2-&gt;insertItem( "Integer Validator", -1 );
    combo2-&gt;insertItem( "Double Validator", -1 );
    // ...and again the activated() SIGNAL gets connected with a SLOT
    <a href="qobject.html#c2d209">connect</a>( combo2, SIGNAL( activated( int ) ), this, SLOT( <a href=#199>slotValidatorChanged</a>( int ) ) );

    // and the second LineEdit
    lined2 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#219265">addWidget</a>( lined2 );

    // yet another widget which is used for layouting
    <a href="qhboxlayout.html">QHBoxLayout</a> *row3 = new <a href="qhboxlayout.html">QHBoxLayout</a>( box );
    row3-&gt;<a href="qlayout.html#cd2c99">setMargin</a>( 5 );

    // we need a label for this too
    label = new <a href="qlabel.html">QLabel</a>( "Alignment: ", this );
    row3-&gt;<a href="qboxlayout.html#219265">addWidget</a>( label );

    // A combo box for setting alignment
    combo3 = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
    row3-&gt;<a href="qboxlayout.html#219265">addWidget</a>( combo3 );
    combo3-&gt;insertItem( "Left", -1 );
    combo3-&gt;insertItem( "Centered", -1 );
    combo3-&gt;insertItem( "Right", -1 );
    // ...and again the activated() SIGNAL gets connected with a SLOT
    <a href="qobject.html#c2d209">connect</a>( combo3, SIGNAL( activated( int ) ), this, SLOT( <a href=#200>slotAlignmentChanged</a>( int ) ) );

    // and the third lineedit
    lined3 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#219265">addWidget</a>( lined3 );

    // last widget used for layouting
    <a href="qhbox.html">QHBox</a> *row4 = new <a href="qhbox.html">QHBox</a>( this );
    box-&gt;<a href="qboxlayout.html#219265">addWidget</a>( row4 );
    row4-&gt;<a href="qframe.html#ab991e">setMargin</a>( 5 );

    // last label
    (void)new <a href="qlabel.html">QLabel</a>( "Read-Only: ", row4 );

    // A combo box for setting alignment
    combo4 = new <a href="qcombobox.html">QComboBox</a>( FALSE, row4 );
    combo4-&gt;insertItem( "False", -1 );
    combo4-&gt;insertItem( "True", -1 );
    // ...and again the activated() SIGNAL gets connected with a SLOT
    <a href="qobject.html#c2d209">connect</a>( combo4, SIGNAL( activated( int ) ), this, SLOT( <a href=#201>slotReadOnlyChanged</a>( int ) ) );

    // and the last lineedit
    lined4 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#219265">addWidget</a>( lined4 );

    // give the first LineEdit the focus at the beginning
    lined1-&gt;setFocus();
}

/*
 * SLOT slotEchoChanged( int i )
 *
 * i contains the number of the item which the user has been chosen in the
 * first Combobox. According to this value, we set the Echo-Mode for the
 * first LineEdit.
 */

void <a name="198"></a>LineEdits::slotEchoChanged( int i )
{
    switch ( i ) {
    case 0:
        lined1-&gt;setEchoMode( QLineEdit::Normal );
        break;
    case 1:
        lined1-&gt;setEchoMode( QLineEdit::Password );
        break;
    case 2:
        lined1-&gt;setEchoMode( QLineEdit::NoEcho );
        break;
    }

    lined1-&gt;setFocus();
}

/*
 * SLOT slotValidatorChanged( int i )
 *
 * i contains the number of the item which the user has been chosen in the
 * second Combobox. According to this value, we set a validator for the
 * second LineEdit. A validator checks in a LineEdit each character which
 * the user enters and accepts it if it is valid, else the character gets
 * ignored and not inserted into the lineedit.
 */

void <a name="199"></a>LineEdits::slotValidatorChanged( int i )
{
    switch ( i ) {
    case 0:
        lined2-&gt;setValidator( 0 );
        break;
    case 1:
        lined2-&gt;setValidator( new QIntValidator( lined2 ) );
        break;
    case 2:
        lined2-&gt;setValidator( new QDoubleValidator( -999.0, 999.0, 2,
                                                    lined2 ) );
        break;
    }

    lined2-&gt;setText( "" );
    lined2-&gt;setFocus();
}

/*
 * SLOT slotAlignmentChanged( int i )
 *
 * i contains the number of the item which the user has been chosen in
 * the third Combobox.  According to this value, we set an alignment
 * third LineEdit.
 */

void <a name="200"></a>LineEdits::slotAlignmentChanged( int i )
{
    switch ( i ) {
    case 0:
        lined3-&gt;setAlignment( QLineEdit::AlignLeft );
        break;
    case 1:
        lined3-&gt;setAlignment( QLineEdit::AlignCenter );
        break;
    case 2:
        lined3-&gt;setAlignment( QLineEdit::AlignRight );
        break;
    }

    lined3-&gt;setFocus();
}

/*
 * SLOT slotReadOnlyChanged( int i )
 *
 * i contains the number of the item which the user has been chosen in
 * the fourth Combobox.  According to this value, we toggle read-only.
 */

void <a name="201"></a>LineEdits::slotReadOnlyChanged( int i )
{
    switch ( i ) {
    case 0:
        lined4-&gt;setReadOnly( FALSE );
        break;
    case 1:
        lined4-&gt;setReadOnly( TRUE );
        break;
    }

    lined4-&gt;setFocus();
}
</pre>


  <hr>

  Main:
<pre>/****************************************************************************
** &#36;Id&#58; qt/examples/lineedits/main.cpp   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "lineedits.h"
#include &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;

int main( int argc, char **argv )
{
    <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );

    LineEdits lineedits;
    lineedits.<a name="setCaption"></a><a href="qwidget.html#d4f007">setCaption</a>( "LineEdit Examples" );
    a.<a name="setMainWidget"></a><a href="qapplication.html#11c921">setMainWidget</a>( &amp;lineedits );
    lineedits.<a name="show"></a><a href="qwidget.html#ccc8f7">show</a>();

    return a.<a name="exec"></a><a href="qapplication.html#7076af">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright  2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>