1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/listbox/listbox.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Listbox Example</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <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>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example shows how to use the different modes (single columns, multiple columns,
|
---|
37 | fixed number of rows, etc.) of <a href="qlistbox.html">QListBox</a>.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: listbox-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
44 | **
|
---|
45 | ** This file is part of an example program for Qt. This example
|
---|
46 | ** program may be used, distributed and modified without limitation.
|
---|
47 | **
|
---|
48 | *****************************************************************************/
|
---|
49 |
|
---|
50 | #ifndef LISTBOX_H
|
---|
51 | #define LISTBOX_H
|
---|
52 |
|
---|
53 |
|
---|
54 | class QSpinBox;
|
---|
55 | class QListBox;
|
---|
56 | class QButtonGroup;
|
---|
57 |
|
---|
58 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
59 |
|
---|
60 |
|
---|
61 | class ListBoxDemo: public <a href="qwidget.html">QWidget</a>
|
---|
62 | {
|
---|
63 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
64 | public:
|
---|
65 | ListBoxDemo();
|
---|
66 | ~ListBoxDemo();
|
---|
67 |
|
---|
68 | private slots:
|
---|
69 | void setNumRows();
|
---|
70 | void setNumCols();
|
---|
71 | void setRowsByHeight();
|
---|
72 | void setColsByWidth();
|
---|
73 | void setVariableWidth( bool );
|
---|
74 | void setVariableHeight( bool );
|
---|
75 | void setMultiSelection( bool );
|
---|
76 | void sortAscending();
|
---|
77 | void sortDescending();
|
---|
78 |
|
---|
79 | private:
|
---|
80 | <a href="qlistbox.html">QListBox</a> * l;
|
---|
81 | <a href="qspinbox.html">QSpinBox</a> * columns;
|
---|
82 | <a href="qspinbox.html">QSpinBox</a> * rows;
|
---|
83 | <a href="qbuttongroup.html">QButtonGroup</a> * bg;
|
---|
84 | };
|
---|
85 |
|
---|
86 |
|
---|
87 | #endif
|
---|
88 | </pre>
|
---|
89 |
|
---|
90 | <p> <hr>
|
---|
91 | <p> Implementation:
|
---|
92 | <p> <pre>/****************************************************************************
|
---|
93 | ** $Id: listbox-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
94 | **
|
---|
95 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
96 | **
|
---|
97 | ** This file is part of an example program for Qt. This example
|
---|
98 | ** program may be used, distributed and modified without limitation.
|
---|
99 | **
|
---|
100 | *****************************************************************************/
|
---|
101 |
|
---|
102 | #include "listbox.h"
|
---|
103 |
|
---|
104 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
105 | #include <<a href="qradiobutton-h.html">qradiobutton.h</a>>
|
---|
106 | #include <<a href="qcheckbox-h.html">qcheckbox.h</a>>
|
---|
107 | #include <<a href="qspinbox-h.html">qspinbox.h</a>>
|
---|
108 | #include <<a href="qlistbox-h.html">qlistbox.h</a>>
|
---|
109 | #include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>
|
---|
110 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
111 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
112 |
|
---|
113 |
|
---|
114 | <a name="f441"></a>ListBoxDemo::ListBoxDemo()
|
---|
115 | : <a href="qwidget.html">QWidget</a>( 0, 0 )
|
---|
116 | {
|
---|
117 | <a href="qgridlayout.html">QGridLayout</a> * g = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 2, 6 );
|
---|
118 |
|
---|
119 | g-><a href="qgridlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "<b>Configuration:</b>", this ), 0, 0 );
|
---|
120 | g-><a href="qgridlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "<b>Result:</b>", this ), 0, 1 );
|
---|
121 |
|
---|
122 | l = new <a href="qlistbox.html">QListBox</a>( this );
|
---|
123 | g-><a href="qgridlayout.html#addWidget">addWidget</a>( l, 1, 1 );
|
---|
124 | <a name="x1447"></a> l-><a href="qwidget.html#setFocusPolicy">setFocusPolicy</a>( QWidget::StrongFocus );
|
---|
125 |
|
---|
126 | <a href="qvboxlayout.html">QVBoxLayout</a> * v = new <a href="qvboxlayout.html">QVBoxLayout</a>;
|
---|
127 | <a name="x1430"></a> g-><a href="qgridlayout.html#addLayout">addLayout</a>( v, 1, 0 );
|
---|
128 |
|
---|
129 | <a href="qradiobutton.html">QRadioButton</a> * b;
|
---|
130 | bg = new <a href="qbuttongroup.html">QButtonGroup</a>( 0 );
|
---|
131 |
|
---|
132 | b = new <a href="qradiobutton.html">QRadioButton</a>( "Fixed number of columns,\n"
|
---|
133 | "as many rows as needed.",
|
---|
134 | this );
|
---|
135 | <a name="x1428"></a> bg-><a href="qbuttongroup.html#insert">insert</a>( b );
|
---|
136 | <a name="x1425"></a> v-><a href="qboxlayout.html#addWidget">addWidget</a>( b );
|
---|
137 | <a name="x1440"></a> b-><a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );
|
---|
138 | <a name="x1426"></a> <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setNumCols()) );
|
---|
139 | <a href="qhboxlayout.html">QHBoxLayout</a> * h = new <a href="qhboxlayout.html">QHBoxLayout</a>;
|
---|
140 | <a name="x1422"></a> v-><a href="qboxlayout.html#addLayout">addLayout</a>( h );
|
---|
141 | <a name="x1423"></a> h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 30 );
|
---|
142 | h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 100 );
|
---|
143 | h-><a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Columns:", this ) );
|
---|
144 | columns = new <a href="qspinbox.html">QSpinBox</a>( this );
|
---|
145 | h-><a href="qboxlayout.html#addWidget">addWidget</a>( columns );
|
---|
146 |
|
---|
147 | v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 );
|
---|
148 |
|
---|
149 | b = new <a href="qradiobutton.html">QRadioButton</a>( "As many columns as fit on-screen,\n"
|
---|
150 | "as many rows as needed.",
|
---|
151 | this );
|
---|
152 | bg-><a href="qbuttongroup.html#insert">insert</a>( b );
|
---|
153 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( b );
|
---|
154 | <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setColsByWidth()) );
|
---|
155 |
|
---|
156 | v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 );
|
---|
157 |
|
---|
158 | b = new <a href="qradiobutton.html">QRadioButton</a>( "Fixed number of rows,\n"
|
---|
159 | "as many columns as needed.",
|
---|
160 | this );
|
---|
161 | bg-><a href="qbuttongroup.html#insert">insert</a>( b );
|
---|
162 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( b );
|
---|
163 | <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setNumRows()) );
|
---|
164 | h = new <a href="qhboxlayout.html">QHBoxLayout</a>;
|
---|
165 | v-><a href="qboxlayout.html#addLayout">addLayout</a>( h );
|
---|
166 | h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 30 );
|
---|
167 | h-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 100 );
|
---|
168 | h-><a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Rows:", this ) );
|
---|
169 | rows = new <a href="qspinbox.html">QSpinBox</a>( this );
|
---|
170 | <a name="x1442"></a> rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
171 | h-><a href="qboxlayout.html#addWidget">addWidget</a>( rows );
|
---|
172 |
|
---|
173 | v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 );
|
---|
174 |
|
---|
175 | b = new <a href="qradiobutton.html">QRadioButton</a>( "As many rows as fit on-screen,\n"
|
---|
176 | "as many columns as needed.",
|
---|
177 | this );
|
---|
178 | bg-><a href="qbuttongroup.html#insert">insert</a>( b );
|
---|
179 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( b );
|
---|
180 | <a href="qobject.html#connect">connect</a>( b, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), this, SLOT(setRowsByHeight()) );
|
---|
181 |
|
---|
182 | v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 12 );
|
---|
183 |
|
---|
184 | <a href="qcheckbox.html">QCheckBox</a> * cb = new <a href="qcheckbox.html">QCheckBox</a>( "Variable-height rows", this );
|
---|
185 | <a name="x1429"></a> cb-><a href="qcheckbox.html#setChecked">setChecked</a>( TRUE );
|
---|
186 | <a name="x1427"></a> <a href="qobject.html#connect">connect</a>( cb, SIGNAL(<a href="qbutton.html#toggled">toggled</a>(bool)), this, SLOT(setVariableHeight(bool)) );
|
---|
187 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( cb );
|
---|
188 | v-><a href="qboxlayout.html#addSpacing">addSpacing</a>( 6 );
|
---|
189 |
|
---|
190 | cb = new <a href="qcheckbox.html">QCheckBox</a>( "Variable-width columns", this );
|
---|
191 | <a href="qobject.html#connect">connect</a>( cb, SIGNAL(<a href="qbutton.html#toggled">toggled</a>(bool)), this, SLOT(setVariableWidth(bool)) );
|
---|
192 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( cb );
|
---|
193 |
|
---|
194 | cb = new <a href="qcheckbox.html">QCheckBox</a>( "Extended-Selection", this );
|
---|
195 | <a href="qobject.html#connect">connect</a>( cb, SIGNAL(<a href="qbutton.html#toggled">toggled</a>(bool)), this, SLOT(setMultiSelection(bool)) );
|
---|
196 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( cb );
|
---|
197 |
|
---|
198 | <a href="qpushbutton.html">QPushButton</a> *pb = new <a href="qpushbutton.html">QPushButton</a>( "Sort ascending", this );
|
---|
199 | <a href="qobject.html#connect">connect</a>( pb, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( sortAscending() ) );
|
---|
200 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( pb );
|
---|
201 |
|
---|
202 | pb = new <a href="qpushbutton.html">QPushButton</a>( "Sort descending", this );
|
---|
203 | <a href="qobject.html#connect">connect</a>( pb, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( sortDescending() ) );
|
---|
204 | v-><a href="qboxlayout.html#addWidget">addWidget</a>( pb );
|
---|
205 |
|
---|
206 | <a name="x1424"></a> v-><a href="qboxlayout.html#addStretch">addStretch</a>( 100 );
|
---|
207 |
|
---|
208 | int i = 0;
|
---|
209 | while( ++i <= 2560 )
|
---|
210 | <a name="x1446"></a><a name="x1433"></a> l-><a href="qlistbox.html#insertItem">insertItem</a>( QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "Item " ) + QString::number( i ),
|
---|
211 | i );
|
---|
212 | <a name="x1441"></a> columns-><a href="qrangecontrol.html#setRange">setRange</a>( 1, 256 );
|
---|
213 | <a name="x1443"></a> columns-><a href="qspinbox.html#setValue">setValue</a>( 1 );
|
---|
214 | rows-><a href="qrangecontrol.html#setRange">setRange</a>( 1, 256 );
|
---|
215 | rows-><a href="qspinbox.html#setValue">setValue</a>( 256 );
|
---|
216 |
|
---|
217 | <a name="x1445"></a> <a href="qobject.html#connect">connect</a>( columns, SIGNAL(<a href="qspinbox.html#valueChanged">valueChanged</a>(int)), this, SLOT(setNumCols()) );
|
---|
218 | <a href="qobject.html#connect">connect</a>( rows, SIGNAL(<a href="qspinbox.html#valueChanged">valueChanged</a>(int)), this, SLOT(setNumRows()) );
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | ListBoxDemo::~ListBoxDemo()
|
---|
223 | {
|
---|
224 | delete bg;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | void <a name="f442"></a>ListBoxDemo::setNumRows()
|
---|
229 | {
|
---|
230 | columns-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
231 | rows-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
232 | <a name="x1444"></a><a name="x1435"></a> l-><a href="qlistbox.html#setRowMode">setRowMode</a>( rows-><a href="qspinbox.html#value">value</a>() );
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | void <a name="f443"></a>ListBoxDemo::setNumCols()
|
---|
237 | {
|
---|
238 | columns-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
239 | rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
240 | <a name="x1434"></a> l-><a href="qlistbox.html#setColumnMode">setColumnMode</a>( columns-><a href="qspinbox.html#value">value</a>() );
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | void <a name="f444"></a>ListBoxDemo::setRowsByHeight()
|
---|
245 | {
|
---|
246 | columns-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
247 | rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
248 | l-><a href="qlistbox.html#setRowMode">setRowMode</a>( QListBox::FitToHeight );
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | void <a name="f445"></a>ListBoxDemo::setColsByWidth()
|
---|
253 | {
|
---|
254 | columns-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
255 | rows-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
256 | l-><a href="qlistbox.html#setColumnMode">setColumnMode</a>( QListBox::FitToWidth );
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | void <a name="f446"></a>ListBoxDemo::setVariableWidth( bool b )
|
---|
261 | {
|
---|
262 | <a name="x1438"></a> l-><a href="qlistbox.html#setVariableWidth">setVariableWidth</a>( b );
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | void <a name="f447"></a>ListBoxDemo::setVariableHeight( bool b )
|
---|
267 | {
|
---|
268 | <a name="x1437"></a> l-><a href="qlistbox.html#setVariableHeight">setVariableHeight</a>( b );
|
---|
269 | }
|
---|
270 |
|
---|
271 | void <a name="f448"></a>ListBoxDemo::setMultiSelection( bool b )
|
---|
272 | {
|
---|
273 | <a name="x1432"></a> l-><a href="qlistbox.html#clearSelection">clearSelection</a>();
|
---|
274 | <a name="x1436"></a> l-><a href="qlistbox.html#setSelectionMode">setSelectionMode</a>( b ? QListBox::Extended : <a href="qlistbox.html">QListBox</a>::Single );
|
---|
275 | }
|
---|
276 |
|
---|
277 | void <a name="f449"></a>ListBoxDemo::sortAscending()
|
---|
278 | {
|
---|
279 | <a name="x1439"></a> l-><a href="qlistbox.html#sort">sort</a>( TRUE );
|
---|
280 | }
|
---|
281 |
|
---|
282 | void <a name="f450"></a>ListBoxDemo::sortDescending()
|
---|
283 | {
|
---|
284 | l-><a href="qlistbox.html#sort">sort</a>( FALSE );
|
---|
285 | }
|
---|
286 | </pre>
|
---|
287 |
|
---|
288 | <p> <hr>
|
---|
289 | <p> Main:
|
---|
290 | <p> <pre>/****************************************************************************
|
---|
291 | ** $Id: listbox-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
292 | **
|
---|
293 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
294 | **
|
---|
295 | ** This file is part of an example program for Qt. This example
|
---|
296 | ** program may be used, distributed and modified without limitation.
|
---|
297 | **
|
---|
298 | *****************************************************************************/
|
---|
299 |
|
---|
300 | #include "listbox.h"
|
---|
301 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
302 |
|
---|
303 | int main( int argc, char **argv )
|
---|
304 | {
|
---|
305 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
306 |
|
---|
307 | ListBoxDemo t;
|
---|
308 | t.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Listbox" );
|
---|
309 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &t );
|
---|
310 | t.<a href="qwidget.html#show">show</a>();
|
---|
311 |
|
---|
312 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
313 | }
|
---|
314 | </pre>
|
---|
315 |
|
---|
316 | <p>See also <a href="examples.html">Examples</a>.
|
---|
317 |
|
---|
318 | <!-- eof -->
|
---|
319 | <p><address><hr><div align=center>
|
---|
320 | <table width=100% cellspacing=0 border=0><tr>
|
---|
321 | <td>Copyright © 2007
|
---|
322 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
323 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
324 | </table></div></address></body>
|
---|
325 | </html>
|
---|