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/listboxcombo/listboxcombo.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Listboxes and Comboboxes</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>Listboxes and Comboboxes</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example program demonstrates how to use listboxes (with single selection
|
---|
37 | and multi selection) and comboboxes (editable and non-editable).
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: listboxcombo-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_COMBO_H
|
---|
51 | #define LISTBOX_COMBO_H
|
---|
52 |
|
---|
53 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
54 |
|
---|
55 | class QListBox;
|
---|
56 | class QLabel;
|
---|
57 |
|
---|
58 | class ListBoxCombo : public <a href="qvbox.html">QVBox</a>
|
---|
59 | {
|
---|
60 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
61 |
|
---|
62 | public:
|
---|
63 | ListBoxCombo( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
|
---|
64 |
|
---|
65 | protected:
|
---|
66 | <a href="qlistbox.html">QListBox</a> *lb1, *lb2;
|
---|
67 | <a href="qlabel.html">QLabel</a> *label1, *label2;
|
---|
68 |
|
---|
69 | protected slots:
|
---|
70 | void slotLeft2Right();
|
---|
71 | void slotCombo1Activated( const <a href="qstring.html">QString</a> &s );
|
---|
72 | void slotCombo2Activated( const <a href="qstring.html">QString</a> &s );
|
---|
73 |
|
---|
74 | };
|
---|
75 |
|
---|
76 | #endif
|
---|
77 | </pre>
|
---|
78 |
|
---|
79 | <p> <hr>
|
---|
80 | <p> Implementation:
|
---|
81 | <p> <pre>/****************************************************************************
|
---|
82 | ** $Id: listboxcombo-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
83 | **
|
---|
84 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
85 | **
|
---|
86 | ** This file is part of an example program for Qt. This example
|
---|
87 | ** program may be used, distributed and modified without limitation.
|
---|
88 | **
|
---|
89 | *****************************************************************************/
|
---|
90 |
|
---|
91 | #include "listboxcombo.h"
|
---|
92 |
|
---|
93 | #include <<a href="qcombobox-h.html">qcombobox.h</a>>
|
---|
94 | #include <<a href="qlistbox-h.html">qlistbox.h</a>>
|
---|
95 | #include <<a href="qhbox-h.html">qhbox.h</a>>
|
---|
96 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
97 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
98 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
99 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
100 | #include <<a href="qimage-h.html">qimage.h</a>>
|
---|
101 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
102 | #include <<a href="qstyle-h.html">qstyle.h</a>>
|
---|
103 |
|
---|
104 |
|
---|
105 | class MyListBoxItem : public <a href="qlistboxitem.html">QListBoxItem</a>
|
---|
106 | {
|
---|
107 | public:
|
---|
108 | MyListBoxItem()
|
---|
109 | : <a href="qlistboxitem.html">QListBoxItem</a>()
|
---|
110 | {
|
---|
111 | setCustomHighlighting( TRUE );
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected:
|
---|
115 | virtual void paint( <a href="qpainter.html">QPainter</a> * );
|
---|
116 | virtual int width( const <a href="qlistbox.html">QListBox</a>* ) const { return 100; }
|
---|
117 | virtual int height( const <a href="qlistbox.html">QListBox</a>* ) const { return 16; }
|
---|
118 |
|
---|
119 | };
|
---|
120 |
|
---|
121 | <a name="x1410"></a>void MyListBoxItem::<a href="qlistboxitem.html#paint">paint</a>( <a href="qpainter.html">QPainter</a> *painter )
|
---|
122 | {
|
---|
123 | // evil trick: find out whether we are painted onto our listbox
|
---|
124 | <a name="x1413"></a> bool in_list_box = <a href="qlistboxitem.html#listBox">listBox</a>() && listBox()->viewport() == painter-><a href="qpainter.html#device">device</a>();
|
---|
125 |
|
---|
126 | <a href="qrect.html">QRect</a> r ( 0, 0, width( <a href="qlistboxitem.html#listBox">listBox</a>() ), height( <a href="qlistboxitem.html#listBox">listBox</a>() ) );
|
---|
127 | if ( in_list_box && isSelected() )
|
---|
128 | <a name="x1414"></a> painter-><a href="qpainter.html#eraseRect">eraseRect</a>( r );
|
---|
129 | <a name="x1415"></a> painter-><a href="qpainter.html#fillRect">fillRect</a>( 5, 5, width( listBox() ) - 10, height( listBox() ) - 10, Qt::red );
|
---|
130 | if ( in_list_box && isCurrent() )
|
---|
131 | listBox()->style().drawPrimitive( QStyle::PE_FocusRect, painter, r, listBox()->colorGroup() );
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Constructor
|
---|
136 | *
|
---|
137 | * Creates child widgets of the ListBoxCombo widget
|
---|
138 | */
|
---|
139 |
|
---|
140 | <a name="f437"></a>ListBoxCombo::ListBoxCombo( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
141 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
142 | {
|
---|
143 | <a href="qframe.html#setMargin">setMargin</a>( 5 );
|
---|
144 | <a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
|
---|
145 |
|
---|
146 | unsigned int i;
|
---|
147 | <a href="qstring.html">QString</a> str;
|
---|
148 |
|
---|
149 | <a href="qhbox.html">QHBox</a> *row1 = new <a href="qhbox.html">QHBox</a>( this );
|
---|
150 | <a name="x1402"></a> row1-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
|
---|
151 |
|
---|
152 | // Create a multi-selection ListBox...
|
---|
153 | lb1 = new <a href="qlistbox.html">QListBox</a>( row1 );
|
---|
154 | <a name="x1408"></a> lb1-><a href="qlistbox.html#setSelectionMode">setSelectionMode</a>( QListBox::Multi );
|
---|
155 |
|
---|
156 | // ...insert a pixmap item...
|
---|
157 | <a name="x1406"></a> lb1-><a href="qlistbox.html#insertItem">insertItem</a>( QPixmap( "qtlogo.png" ) );
|
---|
158 | // ...and 100 text items
|
---|
159 | for ( i = 0; i < 100; i++ ) {
|
---|
160 | str = QString( "Listbox Item %1" ).arg( i );
|
---|
161 | if ( !( i % 4 ) )
|
---|
162 | lb1-><a href="qlistbox.html#insertItem">insertItem</a>( QPixmap( "fileopen.xpm" ), str );
|
---|
163 | else
|
---|
164 | lb1-><a href="qlistbox.html#insertItem">insertItem</a>( str );
|
---|
165 | }
|
---|
166 |
|
---|
167 | // Create a pushbutton...
|
---|
168 | <a href="qpushbutton.html">QPushButton</a> *arrow1 = new <a href="qpushbutton.html">QPushButton</a>( " -> ", row1 );
|
---|
169 | // ...and connect the clicked SIGNAL with the SLOT slotLeft2Right
|
---|
170 | <a href="qobject.html#connect">connect</a>( arrow1, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotLeft2Right() ) );
|
---|
171 |
|
---|
172 | // create an empty single-selection ListBox
|
---|
173 | lb2 = new <a href="qlistbox.html">QListBox</a>( row1 );
|
---|
174 |
|
---|
175 | <a href="qhbox.html">QHBox</a> *row2 = new <a href="qhbox.html">QHBox</a>( this );
|
---|
176 | row2-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
|
---|
177 |
|
---|
178 | <a href="qvbox.html">QVBox</a> *box1 = new <a href="qvbox.html">QVBox</a>( row2 );
|
---|
179 | box1-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
|
---|
180 |
|
---|
181 | // Create a non-editable Combobox and a label below...
|
---|
182 | <a href="qcombobox.html">QComboBox</a> *cb1 = new <a href="qcombobox.html">QComboBox</a>( FALSE, box1 );
|
---|
183 | label1 = new <a href="qlabel.html">QLabel</a>( "Current Item: Combobox Item 0", box1 );
|
---|
184 | <a name="x1416"></a><a name="x1404"></a> label1-><a href="qwidget.html#setMaximumHeight">setMaximumHeight</a>( label1-><a href="qwidget.html#sizeHint">sizeHint</a>().height() * 2 );
|
---|
185 | <a name="x1401"></a> label1-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
|
---|
186 |
|
---|
187 | //...and insert 50 items into the Combobox
|
---|
188 | for ( i = 0; i < 50; i++ ) {
|
---|
189 | str = QString( "Combobox Item %1" ).arg( i );
|
---|
190 | if ( i % 9 )
|
---|
191 | <a name="x1399"></a> cb1-><a href="qcombobox.html#insertItem">insertItem</a>( str );
|
---|
192 | else
|
---|
193 | <a name="x1400"></a> cb1-><a href="qcombobox.html#listBox">listBox</a>()->insertItem( new MyListBoxItem );
|
---|
194 | }
|
---|
195 |
|
---|
196 | <a href="qvbox.html">QVBox</a> *box2 = new <a href="qvbox.html">QVBox</a>( row2 );
|
---|
197 | box2-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
|
---|
198 |
|
---|
199 | // Create an editable Combobox and a label below...
|
---|
200 | <a href="qcombobox.html">QComboBox</a> *cb2 = new <a href="qcombobox.html">QComboBox</a>( TRUE, box2 );
|
---|
201 | label2 = new <a href="qlabel.html">QLabel</a>( "Current Item: Combobox Item 0", box2 );
|
---|
202 | label2-><a href="qwidget.html#setMaximumHeight">setMaximumHeight</a>( label2-><a href="qwidget.html#sizeHint">sizeHint</a>().height() * 2 );
|
---|
203 | label2-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
|
---|
204 |
|
---|
205 | // ... and insert 50 items into the Combobox
|
---|
206 | for ( i = 0; i < 50; i++ ) {
|
---|
207 | str = QString( "Combobox Item %1" ).arg( i );
|
---|
208 | if ( !( i % 4 ) )
|
---|
209 | cb2-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( "fileopen.xpm" ), str );
|
---|
210 | else
|
---|
211 | cb2-><a href="qcombobox.html#insertItem">insertItem</a>( str );
|
---|
212 | }
|
---|
213 |
|
---|
214 | // Connect the activated SIGNALs of the Comboboxes with SLOTs
|
---|
215 | <a name="x1398"></a> <a href="qobject.html#connect">connect</a>( cb1, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> & ) ), this, SLOT( slotCombo1Activated( const <a href="qstring.html">QString</a> & ) ) );
|
---|
216 | <a href="qobject.html#connect">connect</a>( cb2, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> & ) ), this, SLOT( slotCombo2Activated( const <a href="qstring.html">QString</a> & ) ) );
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * SLOT slotLeft2Right
|
---|
221 | *
|
---|
222 | * Copies all selected items of the first ListBox into the
|
---|
223 | * second ListBox
|
---|
224 | */
|
---|
225 |
|
---|
226 | void <a name="f438"></a>ListBoxCombo::slotLeft2Right()
|
---|
227 | {
|
---|
228 | // Go through all items of the first ListBox
|
---|
229 | <a name="x1405"></a> for ( unsigned int i = 0; i < lb1-><a href="qlistbox.html#count">count</a>(); i++ ) {
|
---|
230 | <a name="x1407"></a> <a href="qlistboxitem.html">QListBoxItem</a> *item = lb1-><a href="qlistbox.html#item">item</a>( i );
|
---|
231 | // if the item is selected...
|
---|
232 | <a name="x1409"></a> if ( item-><a href="qlistboxitem.html#isSelected">isSelected</a>() ) {
|
---|
233 | // ...and it is a text item...
|
---|
234 | <a name="x1412"></a><a name="x1411"></a> if ( item-><a href="qlistboxitem.html#pixmap">pixmap</a>() && !item-><a href="qlistboxitem.html#text">text</a>().isEmpty() )
|
---|
235 | lb2-><a href="qlistbox.html#insertItem">insertItem</a>( *item-><a href="qlistboxitem.html#pixmap">pixmap</a>(), item-><a href="qlistboxitem.html#text">text</a>() );
|
---|
236 | else if ( !item-><a href="qlistboxitem.html#pixmap">pixmap</a>() )
|
---|
237 | lb2-><a href="qlistbox.html#insertItem">insertItem</a>( item-><a href="qlistboxitem.html#text">text</a>() );
|
---|
238 | else if ( item-><a href="qlistboxitem.html#text">text</a>().isEmpty() )
|
---|
239 | lb2-><a href="qlistbox.html#insertItem">insertItem</a>( *item-><a href="qlistboxitem.html#pixmap">pixmap</a>() );
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * SLOT slotCombo1Activated( const <a href="qstring.html">QString</a> &s )
|
---|
246 | *
|
---|
247 | * Sets the text of the item which the user just selected
|
---|
248 | * in the first Combobox (and is now the value of s) to
|
---|
249 | * the first Label.
|
---|
250 | */
|
---|
251 |
|
---|
252 | void <a name="f439"></a>ListBoxCombo::slotCombo1Activated( const <a href="qstring.html">QString</a> &s )
|
---|
253 | {
|
---|
254 | <a name="x1403"></a> label1-><a href="qlabel.html#setText">setText</a>( QString( "Current Item: %1" ).arg( s ) );
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * SLOT slotCombo2Activated( const <a href="qstring.html">QString</a> &s )
|
---|
259 | *
|
---|
260 | * Sets the text of the item which the user just selected
|
---|
261 | * in the second Combobox (and is now the value of s) to
|
---|
262 | * the second Label.
|
---|
263 | */
|
---|
264 |
|
---|
265 | void <a name="f440"></a>ListBoxCombo::slotCombo2Activated( const <a href="qstring.html">QString</a> &s )
|
---|
266 | {
|
---|
267 | label2-><a href="qlabel.html#setText">setText</a>( QString( "Current Item: %1" ).arg( s ) );
|
---|
268 | }
|
---|
269 | </pre>
|
---|
270 |
|
---|
271 | <p> <hr>
|
---|
272 | <p> Main:
|
---|
273 | <p> <pre>/****************************************************************************
|
---|
274 | ** $Id: listboxcombo-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
275 | **
|
---|
276 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
277 | **
|
---|
278 | ** This file is part of an example program for Qt. This example
|
---|
279 | ** program may be used, distributed and modified without limitation.
|
---|
280 | **
|
---|
281 | *****************************************************************************/
|
---|
282 |
|
---|
283 | #include "listboxcombo.h"
|
---|
284 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
285 |
|
---|
286 | int main( int argc, char **argv )
|
---|
287 | {
|
---|
288 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
289 |
|
---|
290 | ListBoxCombo listboxcombo;
|
---|
291 | listboxcombo.<a href="qwidget.html#resize">resize</a>( 400, 270 );
|
---|
292 | listboxcombo.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Listboxes and Comboboxes" );
|
---|
293 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &listboxcombo );
|
---|
294 | listboxcombo.<a href="qwidget.html#show">show</a>();
|
---|
295 |
|
---|
296 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
297 | }
|
---|
298 | </pre>
|
---|
299 |
|
---|
300 | <p>See also <a href="examples.html">Examples</a>.
|
---|
301 |
|
---|
302 | <!-- eof -->
|
---|
303 | <p><address><hr><div align=center>
|
---|
304 | <table width=100% cellspacing=0 border=0><tr>
|
---|
305 | <td>Copyright © 2007
|
---|
306 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
307 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
308 | </table></div></address></body>
|
---|
309 | </html>
|
---|