Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 765

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/lineedits/lineedits.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Line Edits</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: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

   
<p> 
This example shows how to work with single lineedit widgets, and
how to use different echo modes and validators.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id:  qt/lineedits.h   3.0.2   edited Oct 12 12:18 $
**
** 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 <a href="qgroupbox.html">QGroupBox</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

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>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id:  qt/lineedits.cpp   3.0.2   edited Oct 12 12:18 $
**
** 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
 */

<a name="f395"></a>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#setMargin">setMargin</a>( 10 );

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

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

    // Create a Label
    <a href="qlabel.html">QLabel</a>* label = new <a href="qlabel.html">QLabel</a>( "Echo Mode: ", this);
<a name="x1293"></a>    row1-&gt;<a href="qboxlayout.html#addWidget">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#addWidget">addWidget</a>( combo1 );
<a name="x1295"></a>    combo1-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Normal", -1 );
    combo1-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Password", -1 );
    combo1-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "No Echo", -1 );
    // ...and connect the activated() SIGNAL with the slotEchoChanged() SLOT to be able
    // to react when an item is selected
<a name="x1294"></a>    <a href="qobject.html#connect">connect</a>( combo1, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotEchoChanged( int ) ) );

    // insert the first LineEdit
    lined1 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#addWidget">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#setMargin">setMargin</a>( 5 );

    // and the second label
    label = new <a href="qlabel.html">QLabel</a>( "Validator: ", this );
    row2-&gt;<a href="qboxlayout.html#addWidget">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#addWidget">addWidget</a>( combo2 );
    combo2-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "No Validator", -1 );
    combo2-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Integer Validator", -1 );
    combo2-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Double Validator", -1 );
    // ...and again the activated() SIGNAL gets connected with a SLOT
    <a href="qobject.html#connect">connect</a>( combo2, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotValidatorChanged( int ) ) );

    // and the second LineEdit
    lined2 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#addWidget">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#setMargin">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#addWidget">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#addWidget">addWidget</a>( combo3 );
    combo3-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Left", -1 );
    combo3-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Centered", -1 );
    combo3-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Right", -1 );
    // ...and again the activated() SIGNAL gets connected with a SLOT
    <a href="qobject.html#connect">connect</a>( combo3, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotAlignmentChanged( int ) ) );

    // and the third lineedit
    lined3 = new <a href="qlineedit.html">QLineEdit</a>( this );
    box-&gt;<a href="qboxlayout.html#addWidget">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#addWidget">addWidget</a>( row4 );
    row4-&gt;<a href="qframe.html#setMargin">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;<a href="qcombobox.html#insertItem">insertItem</a>( "False", -1 );
    combo4-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "True", -1 );
    // ...and again the activated() SIGNAL gets connected with a SLOT
    <a href="qobject.html#connect">connect</a>( combo4, SIGNAL( <a href="qcombobox.html#activated">activated</a>( int ) ), this, SLOT( slotReadOnlyChanged( int ) ) );

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

    // give the first LineEdit the focus at the beginning
<a name="x1303"></a>    lined1-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
}

/*
 * 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="f396"></a>LineEdits::slotEchoChanged( int i )
{
    switch ( i ) {
    case 0:
<a name="x1299"></a>        lined1-&gt;<a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::Normal );
        break;
    case 1:
        lined1-&gt;<a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::Password );
        break;
    case 2:
        lined1-&gt;<a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::NoEcho );
        break;
    }

    lined1-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
}

/*
 * 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="f397"></a>LineEdits::slotValidatorChanged( int i )
{
    switch ( i ) {
    case 0:
<a name="x1302"></a>        lined2-&gt;<a href="qlineedit.html#setValidator">setValidator</a>( 0 );
        break;
    case 1:
        lined2-&gt;<a href="qlineedit.html#setValidator">setValidator</a>( new <a href="qintvalidator.html">QIntValidator</a>( lined2 ) );
        break;
    case 2:
        lined2-&gt;<a href="qlineedit.html#setValidator">setValidator</a>( new <a href="qdoublevalidator.html">QDoubleValidator</a>( -999.0, 999.0, 2,
                                                    lined2 ) );
        break;
    }

<a name="x1301"></a>    lined2-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
    lined2-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
}


/*
 * 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="f398"></a>LineEdits::slotAlignmentChanged( int i )
{
    switch ( i ) {
    case 0:
<a name="x1298"></a>        lined3-&gt;<a href="qlineedit.html#setAlignment">setAlignment</a>( QLineEdit::AlignLeft );
        break;
    case 1:
        lined3-&gt;<a href="qlineedit.html#setAlignment">setAlignment</a>( QLineEdit::AlignCenter );
        break;
    case 2:
        lined3-&gt;<a href="qlineedit.html#setAlignment">setAlignment</a>( QLineEdit::AlignRight );
        break;
    }

    lined3-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
}


/*
 * 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="f399"></a>LineEdits::slotReadOnlyChanged( int i )
{
    switch ( i ) {
    case 0:
<a name="x1300"></a>        lined4-&gt;<a href="qlineedit.html#setReadOnly">setReadOnly</a>( FALSE );
        break;
    case 1:
        lined4-&gt;<a href="qlineedit.html#setReadOnly">setReadOnly</a>( TRUE );
        break;
    }

    lined4-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
}
</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id:  qt/main.cpp   3.0.2   edited Oct 12 12:18 $
**
** 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="qapplication-h.html">qapplication.h</a>&gt;

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

    LineEdits lineedits;
    lineedits.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Lineedits" );
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;lineedits );
    lineedits.<a href="qwidget.html#show">show</a>();

    return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>