summaryrefslogtreecommitdiffstats
path: root/doc/html/checklists-example.html
blob: 54f945f02e57b9f72e09d553dfdf291939b78666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/checklists/checklists.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Listviews with Checkable Items</title>
<style type="text/css"><!--
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&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;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>Listviews with Checkable Items</h1>

 
<p> 
This example program shows how to use listviews with different types of
checkable items.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/checklists.h   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef CHECKLISTS_H
#define CHECKLISTS_H

#include &lt;<a href="qwidget-h.html">ntqwidget.h</a>&gt;

class TQListView;
class TQLabel;

class CheckLists : public <a href="ntqwidget.html">TQWidget</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

public:
    CheckLists( <a href="ntqwidget.html">TQWidget</a> *parent = 0, const char *name = 0 );

protected:
    <a href="ntqlistview.html">TQListView</a> *lv1, *lv2;
    <a href="ntqlabel.html">TQLabel</a> *label;

protected slots:
    void copy1to2();
    void copy2to3();

};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/checklists.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "checklists.h"

#include &lt;<a href="qlistview-h.html">ntqlistview.h</a>&gt;
#include &lt;<a href="qvbox-h.html">ntqvbox.h</a>&gt;
#include &lt;<a href="qlabel-h.html">ntqlabel.h</a>&gt;
#include &lt;<a href="qvaluelist-h.html">ntqvaluelist.h</a>&gt;
#include &lt;<a href="qstring-h.html">ntqstring.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">ntqpushbutton.h</a>&gt;
#include &lt;<a href="qlayout-h.html">ntqlayout.h</a>&gt;

/*
 * Constructor
 *
 * Create all child widgets of the CheckList Widget
 */

<a name="f248"></a>CheckLists::CheckLists( <a href="ntqwidget.html">TQWidget</a> *parent, const char *name )
    : <a href="ntqwidget.html">TQWidget</a>( parent, name )
{
    <a href="qhboxlayout.html">TQHBoxLayout</a> *lay = new <a href="qhboxlayout.html">TQHBoxLayout</a>( this );
<a name="x426"></a>    lay-&gt;<a href="ntqlayout.html#setMargin">setMargin</a>( 5 );

    // create a widget which layouts its childs in a column
    <a href="qvboxlayout.html">TQVBoxLayout</a> *vbox1 = new <a href="qvboxlayout.html">TQVBoxLayout</a>( lay );
    vbox1-&gt;<a href="ntqlayout.html#setMargin">setMargin</a>( 5 );

    // First child: a Label
<a name="x423"></a>    vbox1-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="ntqlabel.html">TQLabel</a>( "Check some items!", this ) );

    // Second child: the ListView
    lv1 = new <a href="ntqlistview.html">TQListView</a>( this );
    vbox1-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( lv1 );
<a name="x427"></a>    lv1-&gt;<a href="ntqlistview.html#addColumn">addColumn</a>( "Items" );
<a name="x429"></a>    lv1-&gt;<a href="ntqlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );

    // create a list with 4 ListViewItems which will be parent items of other ListViewItems
    <a href="ntqvaluelist.html">TQValueList</a>&lt;TQListViewItem *&gt; parentList;

<a name="x434"></a>    parentList.<a href="ntqvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">TQCheckListItem</a>( lv1, "Parent Item 1", TQCheckListItem::CheckBoxController ) );
    parentList.<a href="ntqvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">TQCheckListItem</a>( lv1, "Parent Item 2", TQCheckListItem::CheckBoxController ) );
    parentList.<a href="ntqvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">TQCheckListItem</a>( lv1, "Parent Item 3", TQCheckListItem::CheckBoxController ) );
    parentList.<a href="ntqvaluelist.html#append">append</a>( new <a href="qchecklistitem.html">TQCheckListItem</a>( lv1, "Parent Item 4", TQCheckListItem::CheckBoxController ) );

    <a href="qlistviewitem.html">TQListViewItem</a> *item = 0;
    unsigned int num = 1;
    // go through the list of parent items...
<a name="x436"></a><a name="x435"></a>    for ( TQValueList&lt;TQListViewItem*&gt;::Iterator it = parentList.<a href="ntqvaluelist.html#begin">begin</a>(); it != parentList.<a href="ntqvaluelist.html#end">end</a>();
          ( *it )-&gt;setOpen( TRUE ), ++it, num++ ) {
        item = *it;
        // ...and create 5 checkable child ListViewItems for each parent item
        for ( unsigned int i = 1; i &lt;= 5; i++ )
            (void)new <a href="qchecklistitem.html">TQCheckListItem</a>( item, TQString( "%1. Child of Parent %2" ).arg( i ).arg( num ), TQCheckListItem::CheckBox );
    }

    // Create another widget for layouting
    <a href="qvboxlayout.html">TQVBoxLayout</a> *tmp = new <a href="qvboxlayout.html">TQVBoxLayout</a>( lay );
    tmp-&gt;<a href="ntqlayout.html#setMargin">setMargin</a>( 5 );

    // create a pushbutton
    <a href="ntqpushbutton.html">TQPushButton</a> *copy1 = new <a href="ntqpushbutton.html">TQPushButton</a>( "  -&gt;  ", this );
    tmp-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( copy1 );
<a name="x437"></a><a name="x433"></a>    copy1-&gt;<a href="ntqwidget.html#setMaximumWidth">setMaximumWidth</a>( copy1-&gt;<a href="ntqwidget.html#sizeHint">sizeHint</a>().width() );
    // connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2()
    <a href="ntqobject.html#connect">connect</a>( copy1, SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), this, SLOT( copy1to2() ) );

    // another widget for layouting
    <a href="qvboxlayout.html">TQVBoxLayout</a> *vbox2 = new <a href="qvboxlayout.html">TQVBoxLayout</a>( lay );
    vbox2-&gt;<a href="ntqlayout.html#setMargin">setMargin</a>( 5 );

    // and another label
    vbox2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( new <a href="ntqlabel.html">TQLabel</a>( "Check one item!", this ) );

    // create the second listview
    lv2 = new <a href="ntqlistview.html">TQListView</a>( this );
    vbox2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( lv2 );
    lv2-&gt;<a href="ntqlistview.html#addColumn">addColumn</a>( "Items" );
    lv2-&gt;<a href="ntqlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );

    // another widget needed for layouting only
    tmp = new <a href="qvboxlayout.html">TQVBoxLayout</a>( lay );
    tmp-&gt;<a href="ntqlayout.html#setMargin">setMargin</a>( 5 );

    // create another pushbutton...
    <a href="ntqpushbutton.html">TQPushButton</a> *copy2 = new <a href="ntqpushbutton.html">TQPushButton</a>( "  -&gt;  ", this );
    lay-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( copy2 );
    copy2-&gt;<a href="ntqwidget.html#setMaximumWidth">setMaximumWidth</a>( copy2-&gt;<a href="ntqwidget.html#sizeHint">sizeHint</a>().width() );
    // ...and connect its clicked() SIGNAL to the copy2to3() SLOT
    <a href="ntqobject.html#connect">connect</a>( copy2, SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), this, SLOT( copy2to3() ) );

    tmp = new <a href="qvboxlayout.html">TQVBoxLayout</a>( lay );
    tmp-&gt;<a href="ntqlayout.html#setMargin">setMargin</a>( 5 );

    // and create a label which will be at the right of the window
    label = new <a href="ntqlabel.html">TQLabel</a>( "No Item yet...", this );
    tmp-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( label );
}

/*
 * SLOT copy1to2()
 *
 * Copies all checked ListViewItems from the first ListView to
 * the second one, and inserts them as Radio-ListViewItem.
 */

void <a name="f249"></a>CheckLists::copy1to2()
{
    // create an iterator which operates on the first ListView
    <a href="qlistviewitemiterator.html">TQListViewItemIterator</a> it( lv1 );

<a name="x428"></a>    lv2-&gt;<a href="ntqlistview.html#clear">clear</a>();

    // Insert first a controller Item into the second ListView. Always if Radio-ListViewItems
    // are inserted into a Listview, the parent item of these MUST be a controller Item!
    <a href="qchecklistitem.html">TQCheckListItem</a> *item = new <a href="qchecklistitem.html">TQCheckListItem</a>( lv2, "Controller", TQCheckListItem::Controller );
<a name="x431"></a>    item-&gt;<a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE );

    // iterate through the first ListView...
<a name="x432"></a>    for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
        // ...check state of childs, and...
        if ( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;parent() )
            // ...if the item is checked...
            if ( ( (TQCheckListItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )-&gt;isOn() )
                // ...insert a Radio-ListViewItem with the same text into the second ListView
                (void)new <a href="qchecklistitem.html">TQCheckListItem</a>( item, it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;text( 0 ), TQCheckListItem::RadioButton );

<a name="x430"></a>    if ( item-&gt;<a href="qlistviewitem.html#firstChild">firstChild</a>() )
        ( ( <a href="qchecklistitem.html">TQCheckListItem</a>* )item-&gt;<a href="qlistviewitem.html#firstChild">firstChild</a>() )-&gt;setOn( TRUE );
}

/*
 * SLOT copy2to3()
 *
 * Copies the checked item of the second ListView into the
 * Label at the right.
 */

void <a name="f250"></a>CheckLists::copy2to3()
{
    // create an iterator which operates on the second ListView
    <a href="qlistviewitemiterator.html">TQListViewItemIterator</a> it( lv2 );

    label-&gt;<a href="ntqlabel.html#setText">setText</a>( "No Item checked" );

    // iterate through the second ListView...
    for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
        // ...check state of childs, and...
        if ( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;parent() )
            // ...if the item is checked...
            if ( ( (TQCheckListItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )-&gt;isOn() )
                // ...set the text of the item to the label
                label-&gt;<a href="ntqlabel.html#setText">setText</a>( it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;text( 0 ) );
}

</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "checklists.h"
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;

int main( int argc, char **argv )
{
    <a href="ntqapplication.html">TQApplication</a> a( argc, argv );

    CheckLists checklists;
    checklists.<a href="ntqwidget.html#resize">resize</a>( 650, 350 );
    checklists.<a href="ntqwidget.html#setCaption">setCaption</a>( "TQt Example - CheckLists" );
    a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &amp;checklists );
    checklists.<a href="ntqwidget.html#show">show</a>();

    return a.<a href="ntqapplication.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 &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>