Listboxe ExampleThis example shows how to use the different modes (single columns, multipe columns, fixed number of rows, etc.) of QListBox. Header file: /**************************************************************************** ** $Id: //depot/qt/main/examples/listbox/listbox.h#2 $ ** ** 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 LISTBOX_H #define LISTBOX_H class QSpinBox; class QListBox; class QButtonGroup; #include <qwidget.h> class ListBoxDemo: public QWidget { Q_OBJECT public: ListBoxDemo(); ~ListBoxDemo(); private slots: void setNumRows(); void setNumCols(); void setRowsByHeight(); void setColsByWidth(); void setVariableWidth( bool ); void setVariableHeight( bool ); void setMultiSelection( bool ); void sortAscending(); void sortDescending(); private: QListBox * l; QSpinBox * columns; QSpinBox * rows; QButtonGroup * bg; }; #endif Implementation: /**************************************************************************** ** $Id: //depot/qt/main/examples/listbox/listbox.cpp#3 $ ** ** 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 "listbox.h" #include <qlabel.h> #include <qradiobutton.h> #include <qcheckbox.h> #include <qspinbox.h> #include <qlistbox.h> #include <qbuttongroup.h> #include <qlayout.h> #include <qpushbutton.h> ListBoxDemo::ListBoxDemo() : QWidget( 0, 0 ) { QGridLayout * g = new QGridLayout( this, 2, 2, 6 ); g->addWidget( new QLabel( "<b>Configuration:</b>", this ), 0, 0 ); g->addWidget( new QLabel( "<b>Result:</b>", this ), 0, 1 ); l = new QListBox( this ); g->addWidget( l, 1, 1 ); l->setFocusPolicy( QWidget::StrongFocus ); QVBoxLayout * v = new QVBoxLayout; g->addLayout( v, 1, 0 ); QRadioButton * b; bg = new QButtonGroup( 0 ); b = new QRadioButton( "Fixed number of columns,\n" "as many rows as needed.", this ); bg->insert( b ); v->addWidget( b ); b->setChecked( TRUE ); connect( b, SIGNAL(clicked()), this, SLOT(setNumCols()) ); QHBoxLayout * h = new QHBoxLayout; v->addLayout( h ); h->addSpacing( 30 ); h->addSpacing( 100 ); h->addWidget( new QLabel( "Columns:", this ) ); columns = new QSpinBox( this ); h->addWidget( columns ); v->addSpacing( 12 ); b = new QRadioButton( "As many columns as fit on-screen,\n" "as many rows as needed.", this ); bg->insert( b ); v->addWidget( b ); connect( b, SIGNAL(clicked()), this, SLOT(setColsByWidth()) ); v->addSpacing( 12 ); b = new QRadioButton( "Fixed number of rows,\n" "as many columns as needed.", this ); bg->insert( b ); v->addWidget( b ); connect( b, SIGNAL(clicked()), this, SLOT(setNumRows()) ); h = new QHBoxLayout; v->addLayout( h ); h->addSpacing( 30 ); h->addSpacing( 100 ); h->addWidget( new QLabel( "Rows:", this ) ); rows = new QSpinBox( this ); rows->setEnabled( FALSE ); h->addWidget( rows ); v->addSpacing( 12 ); b = new QRadioButton( "As many rows as fit on-screen,\n" "as many columns as needed.", this ); bg->insert( b ); v->addWidget( b ); connect( b, SIGNAL(clicked()), this, SLOT(setRowsByHeight()) ); v->addSpacing( 12 ); QCheckBox * cb = new QCheckBox( "Variable-height rows", this ); cb->setChecked( TRUE ); connect( cb, SIGNAL(toggled(bool)), this, SLOT(setVariableHeight(bool)) ); v->addWidget( cb ); v->addSpacing( 6 ); cb = new QCheckBox( "Variable-width columns", this ); connect( cb, SIGNAL(toggled(bool)), this, SLOT(setVariableWidth(bool)) ); v->addWidget( cb ); cb = new QCheckBox( "Extended-Selection", this ); connect( cb, SIGNAL(toggled(bool)), this, SLOT(setMultiSelection(bool)) ); v->addWidget( cb ); QPushButton *pb = new QPushButton( "Sort ascending", this ); connect( pb, SIGNAL( clicked() ), this, SLOT( sortAscending() ) ); v->addWidget( pb ); pb = new QPushButton( "Sort descending", this ); connect( pb, SIGNAL( clicked() ), this, SLOT( sortDescending() ) ); v->addWidget( pb ); v->addStretch( 100 ); int i = 0; while( ++i <= 2560 ) l->insertItem( QString::fromLatin1( "Item " ) + QString::number( i ), i ); columns->setRange( 1, 256 ); columns->setValue( 1 ); rows->setRange( 1, 256 ); rows->setValue( 256 ); connect( columns, SIGNAL(valueChanged(int)), this, SLOT(setNumCols()) ); connect( rows, SIGNAL(valueChanged(int)), this, SLOT(setNumRows()) ); } ListBoxDemo::~ListBoxDemo() { delete bg; } void Main: /**************************************************************************** ** $Id: //depot/qt/main/examples/listbox/main.cpp#1 $ ** ** 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 "listbox.h" #include < |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 2.3 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com