home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / lineedits / lineedits.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  6KB  |  221 lines

  1. /****************************************************************************
  2. ** $Id:  qt/lineedits.cpp   3.0.0   edited Jun 22 13:24 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "lineedits.h"
  12.  
  13. #include <qlineedit.h>
  14. #include <qcombobox.h>
  15. #include <qframe.h>
  16. #include <qvalidator.h>
  17. #include <qlabel.h>
  18. #include <qlayout.h>
  19. #include <qhbox.h>
  20.  
  21. /*
  22.  * Constructor
  23.  *
  24.  * Creates child widgets of the LineEdits widget
  25.  */
  26.  
  27. LineEdits::LineEdits( QWidget *parent, const char *name )
  28.     : QGroupBox( 0, Horizontal, "Line edits", parent, name )
  29. {
  30.     setMargin( 10 );
  31.  
  32.     QVBoxLayout* box = new QVBoxLayout( layout() );
  33.     
  34.     QHBoxLayout *row1 = new QHBoxLayout( box );
  35.     row1->setMargin( 5 );
  36.  
  37.     // Create a Label
  38.     QLabel* label = new QLabel( "Echo Mode: ", this);
  39.     row1->addWidget( label );
  40.  
  41.     // Create a Combobox with three items...
  42.     combo1 = new QComboBox( FALSE, this );
  43.     row1->addWidget( combo1 );
  44.     combo1->insertItem( "Normal", -1 );
  45.     combo1->insertItem( "Password", -1 );
  46.     combo1->insertItem( "No Echo", -1 );
  47.     // ...and connect the activated() SIGNAL with the slotEchoChanged() SLOT to be able
  48.     // to react when an item is selected
  49.     connect( combo1, SIGNAL( activated( int ) ), this, SLOT( slotEchoChanged( int ) ) );
  50.  
  51.     // insert the first LineEdit
  52.     lined1 = new QLineEdit( this );
  53.     box->addWidget( lined1 );
  54.  
  55.     // another widget which is used for layouting
  56.     QHBoxLayout *row2 = new QHBoxLayout( box );
  57.     row2->setMargin( 5 );
  58.  
  59.     // and the second label
  60.     label = new QLabel( "Validator: ", this );
  61.     row2->addWidget( label );
  62.  
  63.     // A second Combobox with again three items...
  64.     combo2 = new QComboBox( FALSE, this );
  65.     row2->addWidget( combo2 );
  66.     combo2->insertItem( "No Validator", -1 );
  67.     combo2->insertItem( "Integer Validator", -1 );
  68.     combo2->insertItem( "Double Validator", -1 );
  69.     // ...and again the activated() SIGNAL gets connected with a SLOT
  70.     connect( combo2, SIGNAL( activated( int ) ), this, SLOT( slotValidatorChanged( int ) ) );
  71.  
  72.     // and the second LineEdit
  73.     lined2 = new QLineEdit( this );
  74.     box->addWidget( lined2 );
  75.  
  76.     // yet another widget which is used for layouting
  77.     QHBoxLayout *row3 = new QHBoxLayout( box );
  78.     row3->setMargin( 5 );
  79.  
  80.     // we need a label for this too
  81.     label = new QLabel( "Alignment: ", this );
  82.     row3->addWidget( label );
  83.  
  84.     // A combo box for setting alignment
  85.     combo3 = new QComboBox( FALSE, this );
  86.     row3->addWidget( combo3 );
  87.     combo3->insertItem( "Left", -1 );
  88.     combo3->insertItem( "Centered", -1 );
  89.     combo3->insertItem( "Right", -1 );
  90.     // ...and again the activated() SIGNAL gets connected with a SLOT
  91.     connect( combo3, SIGNAL( activated( int ) ), this, SLOT( slotAlignmentChanged( int ) ) );
  92.  
  93.     // and the third lineedit
  94.     lined3 = new QLineEdit( this );
  95.     box->addWidget( lined3 );
  96.  
  97.     // last widget used for layouting
  98.     QHBox *row4 = new QHBox( this );
  99.     box->addWidget( row4 );
  100.     row4->setMargin( 5 );
  101.  
  102.     // last label
  103.     (void)new QLabel( "Read-Only: ", row4 );
  104.  
  105.     // A combo box for setting alignment
  106.     combo4 = new QComboBox( FALSE, row4 );
  107.     combo4->insertItem( "False", -1 );
  108.     combo4->insertItem( "True", -1 );
  109.     // ...and again the activated() SIGNAL gets connected with a SLOT
  110.     connect( combo4, SIGNAL( activated( int ) ), this, SLOT( slotReadOnlyChanged( int ) ) );
  111.  
  112.     // and the last lineedit
  113.     lined4 = new QLineEdit( this );
  114.     box->addWidget( lined4 );
  115.  
  116.     // give the first LineEdit the focus at the beginning
  117.     lined1->setFocus();
  118. }
  119.  
  120. /*
  121.  * SLOT slotEchoChanged( int i )
  122.  *
  123.  * i contains the number of the item which the user has been chosen in the
  124.  * first Combobox. According to this value, we set the Echo-Mode for the
  125.  * first LineEdit.
  126.  */
  127.  
  128. void LineEdits::slotEchoChanged( int i )
  129. {
  130.     switch ( i ) {
  131.     case 0:
  132.     lined1->setEchoMode( QLineEdit::Normal );
  133.         break;
  134.     case 1:
  135.     lined1->setEchoMode( QLineEdit::Password );
  136.         break;
  137.     case 2:
  138.     lined1->setEchoMode( QLineEdit::NoEcho );
  139.         break;
  140.     }
  141.  
  142.     lined1->setFocus();
  143. }
  144.  
  145. /*
  146.  * SLOT slotValidatorChanged( int i )
  147.  *
  148.  * i contains the number of the item which the user has been chosen in the
  149.  * second Combobox. According to this value, we set a validator for the
  150.  * second LineEdit. A validator checks in a LineEdit each character which
  151.  * the user enters and accepts it if it is valid, else the character gets
  152.  * ignored and not inserted into the lineedit.
  153.  */
  154.  
  155. void LineEdits::slotValidatorChanged( int i )
  156. {
  157.     switch ( i ) {
  158.     case 0:
  159.     lined2->setValidator( 0 );
  160.         break;
  161.     case 1:
  162.     lined2->setValidator( new QIntValidator( lined2 ) );
  163.         break;
  164.     case 2:
  165.     lined2->setValidator( new QDoubleValidator( -999.0, 999.0, 2,
  166.                             lined2 ) );
  167.         break;
  168.     }
  169.  
  170.     lined2->setText( "" );
  171.     lined2->setFocus();
  172. }
  173.  
  174.  
  175. /*
  176.  * SLOT slotAlignmentChanged( int i )
  177.  *
  178.  * i contains the number of the item which the user has been chosen in
  179.  * the third Combobox.  According to this value, we set an alignment
  180.  * third LineEdit.
  181.  */
  182.  
  183. void LineEdits::slotAlignmentChanged( int i )
  184. {
  185.     switch ( i ) {
  186.     case 0:
  187.     lined3->setAlignment( QLineEdit::AlignLeft );
  188.         break;
  189.     case 1:
  190.     lined3->setAlignment( QLineEdit::AlignCenter );
  191.         break;
  192.     case 2:
  193.     lined3->setAlignment( QLineEdit::AlignRight );
  194.         break;
  195.     }
  196.  
  197.     lined3->setFocus();
  198. }
  199.  
  200.  
  201. /*
  202.  * SLOT slotReadOnlyChanged( int i )
  203.  *
  204.  * i contains the number of the item which the user has been chosen in
  205.  * the fourth Combobox.  According to this value, we toggle read-only.
  206.  */
  207.  
  208. void LineEdits::slotReadOnlyChanged( int i )
  209. {
  210.     switch ( i ) {
  211.     case 0:
  212.     lined4->setReadOnly( FALSE );
  213.         break;
  214.     case 1:
  215.     lined4->setReadOnly( TRUE );
  216.         break;
  217.     }
  218.  
  219.     lined4->setFocus();
  220. }
  221.