source: trunk/doc/html/checklists-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 13.0 KB
Line 
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/checklists/checklists.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Listviews with Checkable Items</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>Listviews with Checkable Items</h1>
33
34
35<p>
36This example program shows how to use listviews with different types of
37checkable items.
38<p> <hr>
39<p> Header file:
40<p> <pre>/****************************************************************************
41** $Id: checklists-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 CHECKLISTS_H
51#define CHECKLISTS_H
52
53#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
54
55class QListView;
56class QLabel;
57
58class CheckLists : public <a href="qwidget.html">QWidget</a>
59{
60 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
61
62public:
63 CheckLists( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
64
65protected:
66 <a href="qlistview.html">QListView</a> *lv1, *lv2;
67 <a href="qlabel.html">QLabel</a> *label;
68
69protected slots:
70 void copy1to2();
71 void copy2to3();
72
73};
74
75#endif
76</pre>
77
78<p> <hr>
79<p> Implementation:
80<p> <pre>/****************************************************************************
81** $Id: checklists-example.html 2051 2007-02-21 10:04:20Z chehrlic $
82**
83** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
84**
85** This file is part of an example program for Qt. This example
86** program may be used, distributed and modified without limitation.
87**
88*****************************************************************************/
89
90#include "checklists.h"
91
92#include &lt;<a href="qlistview-h.html">qlistview.h</a>&gt;
93#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
94#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
95#include &lt;<a href="qvaluelist-h.html">qvaluelist.h</a>&gt;
96#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
97#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
98#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
99
100/*
101 * Constructor
102 *
103 * Create all child widgets of the CheckList Widget
104 */
105
106<a name="f248"></a>CheckLists::CheckLists( <a href="qwidget.html">QWidget</a> *parent, const char *name )
107 : <a href="qwidget.html">QWidget</a>( parent, name )
108{
109 <a href="qhboxlayout.html">QHBoxLayout</a> *lay = new <a href="qhboxlayout.html">QHBoxLayout</a>( this );
110<a name="x426"></a> lay-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );
111
112 // create a widget which layouts its childs in a column
113 <a href="qvboxlayout.html">QVBoxLayout</a> *vbox1 = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
114 vbox1-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );
115
116 // First child: a Label
117<a name="x423"></a> vbox1-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Check some items!", this ) );
118
119 // Second child: the ListView
120 lv1 = new <a href="qlistview.html">QListView</a>( this );
121 vbox1-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( lv1 );
122<a name="x427"></a> lv1-&gt;<a href="qlistview.html#addColumn">addColumn</a>( "Items" );
123<a name="x429"></a> lv1-&gt;<a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );
124
125 // create a list with 4 ListViewItems which will be parent items of other ListViewItems
126 <a href="qvaluelist.html">QValueList</a>&lt;QListViewItem *&gt; parentList;
127
128<a name="x434"></a> parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">QCheckListItem</a>( lv1, "Parent Item 1", QCheckListItem::CheckBoxController ) );
129 parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">QCheckListItem</a>( lv1, "Parent Item 2", QCheckListItem::CheckBoxController ) );
130 parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">QCheckListItem</a>( lv1, "Parent Item 3", QCheckListItem::CheckBoxController ) );
131 parentList.<a href="qvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">QCheckListItem</a>( lv1, "Parent Item 4", QCheckListItem::CheckBoxController ) );
132
133 <a href="qlistviewitem.html">QListViewItem</a> *item = 0;
134 unsigned int num = 1;
135 // go through the list of parent items...
136<a name="x436"></a><a name="x435"></a> for ( QValueList&lt;QListViewItem*&gt;::Iterator it = parentList.<a href="qvaluelist.html#begin">begin</a>(); it != parentList.<a href="qvaluelist.html#end">end</a>();
137 ( *it )-&gt;setOpen( TRUE ), ++it, num++ ) {
138 item = *it;
139 // ...and create 5 checkable child ListViewItems for each parent item
140 for ( unsigned int i = 1; i &lt;= 5; i++ )
141 (void)new <a href="qchecklistitem.html">QCheckListItem</a>( item, QString( "%1. Child of Parent %2" ).arg( i ).arg( num ), QCheckListItem::CheckBox );
142 }
143
144 // Create another widget for layouting
145 <a href="qvboxlayout.html">QVBoxLayout</a> *tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
146 tmp-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );
147
148 // create a pushbutton
149 <a href="qpushbutton.html">QPushButton</a> *copy1 = new <a href="qpushbutton.html">QPushButton</a>( " -&gt; ", this );
150 tmp-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( copy1 );
151<a name="x437"></a><a name="x433"></a> copy1-&gt;<a href="qwidget.html#setMaximumWidth">setMaximumWidth</a>( copy1-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().width() );
152 // connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2()
153 <a href="qobject.html#connect">connect</a>( copy1, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( copy1to2() ) );
154
155 // another widget for layouting
156 <a href="qvboxlayout.html">QVBoxLayout</a> *vbox2 = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
157 vbox2-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );
158
159 // and another label
160 vbox2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="qlabel.html">QLabel</a>( "Check one item!", this ) );
161
162 // create the second listview
163 lv2 = new <a href="qlistview.html">QListView</a>( this );
164 vbox2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( lv2 );
165 lv2-&gt;<a href="qlistview.html#addColumn">addColumn</a>( "Items" );
166 lv2-&gt;<a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );
167
168 // another widget needed for layouting only
169 tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
170 tmp-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );
171
172 // create another pushbutton...
173 <a href="qpushbutton.html">QPushButton</a> *copy2 = new <a href="qpushbutton.html">QPushButton</a>( " -&gt; ", this );
174 lay-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( copy2 );
175 copy2-&gt;<a href="qwidget.html#setMaximumWidth">setMaximumWidth</a>( copy2-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().width() );
176 // ...and connect its clicked() SIGNAL to the copy2to3() SLOT
177 <a href="qobject.html#connect">connect</a>( copy2, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( copy2to3() ) );
178
179 tmp = new <a href="qvboxlayout.html">QVBoxLayout</a>( lay );
180 tmp-&gt;<a href="qlayout.html#setMargin">setMargin</a>( 5 );
181
182 // and create a label which will be at the right of the window
183 label = new <a href="qlabel.html">QLabel</a>( "No Item yet...", this );
184 tmp-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( label );
185}
186
187/*
188 * SLOT copy1to2()
189 *
190 * Copies all checked ListViewItems from the first ListView to
191 * the second one, and inserts them as Radio-ListViewItem.
192 */
193
194void <a name="f249"></a>CheckLists::copy1to2()
195{
196 // create an iterator which operates on the first ListView
197 <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( lv1 );
198
199<a name="x428"></a> lv2-&gt;<a href="qlistview.html#clear">clear</a>();
200
201 // Insert first a controller Item into the second ListView. Always if Radio-ListViewItems
202 // are inserted into a Listview, the parent item of these MUST be a controller Item!
203 <a href="qchecklistitem.html">QCheckListItem</a> *item = new <a href="qchecklistitem.html">QCheckListItem</a>( lv2, "Controller", QCheckListItem::Controller );
204<a name="x431"></a> item-&gt;<a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE );
205
206 // iterate through the first ListView...
207<a name="x432"></a> for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
208 // ...check state of childs, and...
209 if ( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;parent() )
210 // ...if the item is checked...
211 if ( ( (QCheckListItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )-&gt;isOn() )
212 // ...insert a Radio-ListViewItem with the same text into the second ListView
213 (void)new <a href="qchecklistitem.html">QCheckListItem</a>( item, it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;text( 0 ), QCheckListItem::RadioButton );
214
215<a name="x430"></a> if ( item-&gt;<a href="qlistviewitem.html#firstChild">firstChild</a>() )
216 ( ( <a href="qchecklistitem.html">QCheckListItem</a>* )item-&gt;<a href="qlistviewitem.html#firstChild">firstChild</a>() )-&gt;setOn( TRUE );
217}
218
219/*
220 * SLOT copy2to3()
221 *
222 * Copies the checked item of the second ListView into the
223 * Label at the right.
224 */
225
226void <a name="f250"></a>CheckLists::copy2to3()
227{
228 // create an iterator which operates on the second ListView
229 <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( lv2 );
230
231 label-&gt;<a href="qlabel.html#setText">setText</a>( "No Item checked" );
232
233 // iterate through the second ListView...
234 for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
235 // ...check state of childs, and...
236 if ( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;parent() )
237 // ...if the item is checked...
238 if ( ( (QCheckListItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )-&gt;isOn() )
239 // ...set the text of the item to the label
240 label-&gt;<a href="qlabel.html#setText">setText</a>( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;text( 0 ) );
241}
242
243</pre>
244
245<p> <hr>
246<p> Main:
247<p> <pre>/****************************************************************************
248** $Id: checklists-example.html 2051 2007-02-21 10:04:20Z chehrlic $
249**
250** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
251**
252** This file is part of an example program for Qt. This example
253** program may be used, distributed and modified without limitation.
254**
255*****************************************************************************/
256
257#include "checklists.h"
258#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
259
260int main( int argc, char **argv )
261{
262 <a href="qapplication.html">QApplication</a> a( argc, argv );
263
264 CheckLists checklists;
265 checklists.<a href="qwidget.html#resize">resize</a>( 650, 350 );
266 checklists.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - CheckLists" );
267 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;checklists );
268 checklists.<a href="qwidget.html#show">show</a>();
269
270 return a.<a href="qapplication.html#exec">exec</a>();
271}
272</pre>
273
274<p>See also <a href="examples.html">Examples</a>.
275
276<!-- eof -->
277<p><address><hr><div align=center>
278<table width=100% cellspacing=0 border=0><tr>
279<td>Copyright &copy; 2007
280<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
281<td align=right><div align=right>Qt 3.3.8</div>
282</table></div></address></body>
283</html>
Note: See TracBrowser for help on using the repository browser.