listbox-example.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 326 行
HTML
326 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/listbox/listbox.doc:4 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Listbox Example</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 Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped 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>Listbox Example</h1> <p> This example shows how to use the different modes (single columns, multiple columns,fixed number of rows, etc.) of <a href="qlistbox.html">QListBox</a>.<p> <hr><p> Header file:<p> <pre>/****************************************************************************** $Id: qt/listbox.h 3.0.5 edited Oct 12 2001 $**** 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_Hclass QSpinBox;class QListBox;class QButtonGroup;#include <<a href="qwidget-h.html">qwidget.h</a>>class ListBoxDemo: public <a href="qwidget.html">QWidget</a>{ <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>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: <a href="qlistbox.html">QListBox</a> * l; <a href="qspinbox.html">QSpinBox</a> * columns; <a href="qspinbox.html">QSpinBox</a> * rows; <a href="qbuttongroup.html">QButtonGroup</a> * bg;};#endif</pre><p> <hr><p> Implementation:<p> <pre>/****************************************************************************** $Id: qt/listbox.cpp 3.0.5 edited Mar 27 16:05 $**** 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 <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qradiobutton-h.html">qradiobutton.h</a>>#include <<a href="qcheckbox-h.html">qcheckbox.h</a>>#include <<a href="qspinbox-h.html">qspinbox.h</a>>#include <<a href="qlistbox-h.html">qlistbox.h</a>>#include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>><a name="f413"></a>ListBoxDemo::ListBoxDemo() : <a href="qwidget.html">QWidget</a>( 0, 0 ){ <a href="qgridlayout.html">QGridLayout</a> * g = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 2, 6 ); g-><a href="qgridlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "<b>Configuration:</b>", this ), 0, 0 ); g-><a href="qgridlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "<b>Result:</b>", this ), 0, 1 ); l = new <a href="qlistbox.html">QListBox</a>( this ); g-><a href="qgridlayout.html#addWidget">addWidget</a>( l, 1, 1 );<a name="x1330"></a> l-><a href="qwidget.html#setFocusPolicy">setFocusPolicy</a>( QWidget::StrongFocus ); <a href="qvboxlayout.html">QVBoxLayout</a> * v = new <a href="qvboxlayout.html">QVBoxLayout</a>;<a name="x1313"></a> g-><a href="qgridlayout.html#addLayout">addLayout</a>( v, 1, 0 ); <a href="qradiobutton.html">QRadioButton</a> * b; bg = new <a href="qbuttongroup.html">QButtonGroup</a>( 0 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "Fixed number of columns,\n" "as many rows as needed.", this );<a name="x1311"></a> bg-><a href="qbuttongroup.html#insert">insert</a>( b );<a name="x1308"></a> v-><a href="qboxlayout.html#addWidget">addWidget</a>( b );<a name="x1323"></a> b-><a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );<a name="x1309"></a> <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setNumCols()) ); <a href="qhboxlayout.html">QHBoxLayout</a> * h = new <a href="qhboxlayout.html">QHBoxLayout</a>;<a name="x1305"></a> v-><a href="qboxlayout.html#addLayout">addLayout</a>( h );<a name="x1306"></a> h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 30 ); h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 100 ); h-><a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Columns:", this ) ); columns = new <a href="qspinbox.html">QSpinBox</a>( this ); h-><a href="qboxlayout.html#addWidget">addWidget</a>( columns ); v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "As many columns as fit on-screen,\n" "as many rows as needed.", this ); bg-><a href="qbuttongroup.html#insert">insert</a>( b ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( b ); <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setColsByWidth()) ); v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "Fixed number of rows,\n" "as many columns as needed.", this ); bg-><a href="qbuttongroup.html#insert">insert</a>( b ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( b ); <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setNumRows()) ); h = new <a href="qhboxlayout.html">QHBoxLayout</a>; v-><a href="qboxlayout.html#addLayout">addLayout</a>( h ); h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 30 ); h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 100 ); h-><a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Rows:", this ) ); rows = new <a href="qspinbox.html">QSpinBox</a>( this );<a name="x1325"></a> rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE ); h-><a href="qboxlayout.html#addWidget">addWidget</a>( rows ); v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 ); b = new <a href="qradiobutton.html">QRadioButton</a>( "As many rows as fit on-screen,\n" "as many columns as needed.", this ); bg-><a href="qbuttongroup.html#insert">insert</a>( b ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( b ); <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setRowsByHeight()) ); v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 ); <a href="qcheckbox.html">QCheckBox</a> * cb = new <a href="qcheckbox.html">QCheckBox</a>( "Variable-height rows", this );<a name="x1312"></a> cb-><a href="qcheckbox.html#setChecked">setChecked</a>( TRUE );<a name="x1310"></a> <a href="qobject.html#connect">connect</a>( cb, SIGNAL(<a href="qbutton.html#toggled">toggled</a>(bool)), this, SLOT(setVariableHeight(bool)) ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( cb ); v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 6 ); cb = new <a href="qcheckbox.html">QCheckBox</a>( "Variable-width columns", this ); <a href="qobject.html#connect">connect</a>( cb, SIGNAL(<a href="qbutton.html#toggled">toggled</a>(bool)), this, SLOT(setVariableWidth(bool)) ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( cb ); cb = new <a href="qcheckbox.html">QCheckBox</a>( "Extended-Selection", this ); <a href="qobject.html#connect">connect</a>( cb, SIGNAL(<a href="qbutton.html#toggled">toggled</a>(bool)), this, SLOT(setMultiSelection(bool)) ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( cb ); <a href="qpushbutton.html">QPushButton</a> *pb = new <a href="qpushbutton.html">QPushButton</a>( "Sort ascending", this ); <a href="qobject.html#connect">connect</a>( pb, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( sortAscending() ) ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( pb ); pb = new <a href="qpushbutton.html">QPushButton</a>( "Sort descending", this ); <a href="qobject.html#connect">connect</a>( pb, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( sortDescending() ) ); v-><a href="qboxlayout.html#addWidget">addWidget</a>( pb );<a name="x1307"></a> v-><a href="qboxlayout.html#addStretch">addStretch</a>( 100 ); int i = 0; while( ++i <= 2560 )<a name="x1329"></a><a name="x1316"></a> l-><a href="qlistbox.html#insertItem">insertItem</a>( QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "Item " ) + QString::number( i ), i );<a name="x1324"></a> columns-><a href="qrangecontrol.html#setRange">setRange</a>( 1, 256 );<a name="x1326"></a> columns-><a href="qspinbox.html#setValue">setValue</a>( 1 ); rows-><a href="qrangecontrol.html#setRange">setRange</a>( 1, 256 ); rows-><a href="qspinbox.html#setValue">setValue</a>( 256 );<a name="x1328"></a> <a href="qobject.html#connect">connect</a>( columns, SIGNAL(<a href="qspinbox.html#valueChanged">valueChanged</a>(int)), this, SLOT(setNumCols()) ); <a href="qobject.html#connect">connect</a>( rows, SIGNAL(<a href="qspinbox.html#valueChanged">valueChanged</a>(int)), this, SLOT(setNumRows()) );}ListBoxDemo::~ListBoxDemo(){ delete bg;}void <a name="f414"></a>ListBoxDemo::setNumRows(){ columns-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE ); rows-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );<a name="x1327"></a><a name="x1318"></a> l-><a href="qlistbox.html#setRowMode">setRowMode</a>( rows-><a href="qspinbox.html#value">value</a>() );}void <a name="f415"></a>ListBoxDemo::setNumCols(){ columns-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE ); rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );<a name="x1317"></a> l-><a href="qlistbox.html#setColumnMode">setColumnMode</a>( columns-><a href="qspinbox.html#value">value</a>() );}void <a name="f416"></a>ListBoxDemo::setRowsByHeight(){ columns-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE ); rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE ); l-><a href="qlistbox.html#setRowMode">setRowMode</a>( QListBox::FitToHeight );}void <a name="f417"></a>ListBoxDemo::setColsByWidth(){ columns-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE ); rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE ); l-><a href="qlistbox.html#setColumnMode">setColumnMode</a>( QListBox::FitToWidth );}void <a name="f418"></a>ListBoxDemo::setVariableWidth( bool b ){<a name="x1321"></a> l-><a href="qlistbox.html#setVariableWidth">setVariableWidth</a>( b );}void <a name="f419"></a>ListBoxDemo::setVariableHeight( bool b ){<a name="x1320"></a> l-><a href="qlistbox.html#setVariableHeight">setVariableHeight</a>( b );}void <a name="f420"></a>ListBoxDemo::setMultiSelection( bool b ){<a name="x1315"></a> l-><a href="qlistbox.html#clearSelection">clearSelection</a>();<a name="x1319"></a> l-><a href="qlistbox.html#setSelectionMode">setSelectionMode</a>( b ? QListBox::Extended : <a href="qlistbox.html">QListBox</a>::Single );}void <a name="f421"></a>ListBoxDemo::sortAscending(){<a name="x1322"></a> l-><a href="qlistbox.html#sort">sort</a>( TRUE );}void <a name="f422"></a>ListBoxDemo::sortDescending(){ l-><a href="qlistbox.html#sort">sort</a>( FALSE );}</pre><p> <hr><p> Main:<p> <pre>/****************************************************************************** $Id: qt/main.cpp 3.0.5 edited Oct 12 2001 $**** 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 <<a href="qapplication-h.html">qapplication.h</a>>int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); ListBoxDemo t; t.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Listbox" ); a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &t ); t.<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 © 2002 <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.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?