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

  1. /****************************************************************************
  2. ** $Id:  qt/listbox.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 "listbox.h"
  12.  
  13. #include <qlabel.h>
  14. #include <qradiobutton.h>
  15. #include <qcheckbox.h>
  16. #include <qspinbox.h>
  17. #include <qlistbox.h>
  18. #include <qbuttongroup.h>
  19. #include <qlayout.h>
  20. #include <qpushbutton.h>
  21.  
  22.  
  23. ListBoxDemo::ListBoxDemo()
  24.     : QWidget( 0, 0 )
  25. {
  26.     QGridLayout * g = new QGridLayout( this, 2, 2, 6 );
  27.  
  28.     g->addWidget( new QLabel( "<b>Configuration:</b>", this ), 0, 0 );
  29.     g->addWidget( new QLabel( "<b>Result:</b>", this ), 0, 1 );
  30.  
  31.     l = new QListBox( this );
  32.     g->addWidget( l, 1, 1 );
  33.     l->setFocusPolicy( QWidget::StrongFocus );
  34.  
  35.     QVBoxLayout * v = new QVBoxLayout;
  36.     g->addLayout( v, 1, 0 );
  37.  
  38.     QRadioButton * b;
  39.     bg = new QButtonGroup( 0 );
  40.  
  41.     b = new QRadioButton( "Fixed number of columns,\n"
  42.                           "as many rows as needed.",
  43.                           this );
  44.     bg->insert( b );
  45.     v->addWidget( b );
  46.     b->setChecked( TRUE );
  47.     connect( b, SIGNAL(clicked()), this, SLOT(setNumCols()) );
  48.     QHBoxLayout * h = new QHBoxLayout;
  49.     v->addLayout( h );
  50.     h->addSpacing( 30 );
  51.     h->addSpacing( 100 );
  52.     h->addWidget( new QLabel( "Columns:", this ) );
  53.     columns = new QSpinBox( this );
  54.     h->addWidget( columns );
  55.  
  56.     v->addSpacing( 12 );
  57.  
  58.     b = new QRadioButton( "As many columns as fit on-screen,\n"
  59.                           "as many rows as needed.",
  60.                           this );
  61.     bg->insert( b );
  62.     v->addWidget( b );
  63.     connect( b, SIGNAL(clicked()), this, SLOT(setColsByWidth()) );
  64.  
  65.     v->addSpacing( 12 );
  66.  
  67.     b = new QRadioButton( "Fixed number of rows,\n"
  68.                           "as many columns as needed.",
  69.                           this );
  70.     bg->insert( b );
  71.     v->addWidget( b );
  72.     connect( b, SIGNAL(clicked()), this, SLOT(setNumRows()) );
  73.     h = new QHBoxLayout;
  74.     v->addLayout( h );
  75.     h->addSpacing( 30 );
  76.     h->addSpacing( 100 );
  77.     h->addWidget( new QLabel( "Rows:", this ) );
  78.     rows = new QSpinBox( this );
  79.     rows->setEnabled( FALSE );
  80.     h->addWidget( rows );
  81.  
  82.     v->addSpacing( 12 );
  83.  
  84.     b = new QRadioButton( "As many rows as fit on-screen,\n"
  85.                           "as many columns as needed.",
  86.                           this );
  87.     bg->insert( b );
  88.     v->addWidget( b );
  89.     connect( b, SIGNAL(clicked()), this, SLOT(setRowsByHeight()) );
  90.  
  91.     v->addSpacing( 12 );
  92.  
  93.     QCheckBox * cb = new QCheckBox( "Variable-height rows", this );
  94.     cb->setChecked( TRUE );
  95.     connect( cb, SIGNAL(toggled(bool)), this, SLOT(setVariableHeight(bool)) );
  96.     v->addWidget( cb );
  97.     v->addSpacing( 6 );
  98.  
  99.     cb = new QCheckBox( "Variable-width columns", this );
  100.     connect( cb, SIGNAL(toggled(bool)), this, SLOT(setVariableWidth(bool)) );
  101.     v->addWidget( cb );
  102.  
  103.     cb = new QCheckBox( "Extended-Selection", this );
  104.     connect( cb, SIGNAL(toggled(bool)), this, SLOT(setMultiSelection(bool)) );
  105.     v->addWidget( cb );
  106.  
  107.     QPushButton *pb = new QPushButton( "Sort ascending", this );
  108.     connect( pb, SIGNAL( clicked() ), this, SLOT( sortAscending() ) );
  109.     v->addWidget( pb );
  110.  
  111.     pb = new QPushButton( "Sort descending", this );
  112.     connect( pb, SIGNAL( clicked() ), this, SLOT( sortDescending() ) );
  113.     v->addWidget( pb );
  114.  
  115.     v->addStretch( 100 );
  116.  
  117.     int i = 0;
  118.     while( ++i <= 2560 )
  119.         l->insertItem( QString::fromLatin1( "Item " ) + QString::number( i ),
  120.                        i );
  121.     columns->setRange( 1, 256 );
  122.     columns->setValue( 1 );
  123.     rows->setRange( 1, 256 );
  124.     rows->setValue( 256 );
  125.  
  126.     connect( columns, SIGNAL(valueChanged(int)), this, SLOT(setNumCols()) );
  127.     connect( rows, SIGNAL(valueChanged(int)), this, SLOT(setNumRows()) );
  128. }
  129.  
  130.  
  131. ListBoxDemo::~ListBoxDemo()
  132. {
  133.     delete bg;
  134. }
  135.  
  136.  
  137. void ListBoxDemo::setNumRows()
  138. {
  139.     columns->setEnabled( FALSE );
  140.     rows->setEnabled( TRUE );
  141.     l->setRowMode( rows->value() );
  142. }
  143.  
  144.  
  145. void ListBoxDemo::setNumCols()
  146. {
  147.     columns->setEnabled( TRUE );
  148.     rows->setEnabled( FALSE );
  149.     l->setColumnMode( columns->value() );
  150. }
  151.  
  152.  
  153. void ListBoxDemo::setRowsByHeight()
  154. {
  155.     columns->setEnabled( FALSE );
  156.     rows->setEnabled( FALSE );
  157.     l->setRowMode( QListBox::FitToHeight );
  158. }
  159.  
  160.  
  161. void ListBoxDemo::setColsByWidth()
  162. {
  163.     columns->setEnabled( FALSE );
  164.     rows->setEnabled( FALSE );
  165.     l->setColumnMode( QListBox::FitToWidth );
  166. }
  167.  
  168.  
  169. void ListBoxDemo::setVariableWidth( bool b )
  170. {
  171.     l->setVariableWidth( b );
  172. }
  173.  
  174.  
  175. void ListBoxDemo::setVariableHeight( bool b )
  176. {
  177.     l->setVariableHeight( b );
  178. }
  179.  
  180. void ListBoxDemo::setMultiSelection( bool b )
  181. {
  182.     l->clearSelection();
  183.     l->setSelectionMode( b ? QListBox::Extended : QListBox::Single );
  184. }
  185.  
  186. void ListBoxDemo::sortAscending()
  187. {
  188.     l->sort( TRUE );
  189. }
  190.  
  191. void ListBoxDemo::sortDescending()
  192. {
  193.     l->sort( FALSE );
  194. }
  195.