summaryrefslogtreecommitdiffstats
path: root/doc/html/listboxcombo-example.html
blob: f13c05176f33278edb29892e2e7f9f6c322823af (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Listboxes and Comboboxes</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>Listboxes and Comboboxes</h1>

   
<p> 
This example program demonstrates how to use listboxes (with single selection
and multi selection) and comboboxes (editable and non-editable).
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/listboxcombo.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 LISTBOX_COMBO_H
#define LISTBOX_COMBO_H

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

class TQListBox;
class TQLabel;

class ListBoxCombo : public <a href="tqvbox.html">TQVBox</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

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

protected:
    <a href="tqlistbox.html">TQListBox</a> *lb1, *lb2;
    <a href="tqlabel.html">TQLabel</a> *label1, *label2;

protected slots:
    void slotLeft2Right();
    void slotCombo1Activated( const <a href="tqstring.html">TQString</a> &amp;s );
    void slotCombo2Activated( const <a href="tqstring.html">TQString</a> &amp;s );

};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/listboxcombo.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 "listboxcombo.h"

#include &lt;<a href="tqcombobox-h.html">tqcombobox.h</a>&gt;
#include &lt;<a href="tqlistbox-h.html">tqlistbox.h</a>&gt;
#include &lt;<a href="tqhbox-h.html">tqhbox.h</a>&gt;
#include &lt;<a href="tqpushbutton-h.html">tqpushbutton.h</a>&gt;
#include &lt;<a href="tqstring-h.html">tqstring.h</a>&gt;
#include &lt;<a href="tqpixmap-h.html">tqpixmap.h</a>&gt;
#include &lt;<a href="tqlabel-h.html">tqlabel.h</a>&gt;
#include &lt;<a href="tqimage-h.html">tqimage.h</a>&gt;
#include &lt;<a href="tqpainter-h.html">tqpainter.h</a>&gt;
#include &lt;<a href="tqstyle-h.html">tqstyle.h</a>&gt;


class MyListBoxItem : public <a href="tqlistboxitem.html">TQListBoxItem</a>
{
public:
    MyListBoxItem()
        : <a href="tqlistboxitem.html">TQListBoxItem</a>()
    {
        setCustomHighlighting( TRUE );
    }

protected:
    virtual void paint( <a href="tqpainter.html">TQPainter</a> * );
    virtual int width( const <a href="tqlistbox.html">TQListBox</a>* ) const { return 100; }
    virtual int height( const <a href="tqlistbox.html">TQListBox</a>* ) const { return 16; }

};

<a name="x1410"></a>void MyListBoxItem::<a href="tqlistboxitem.html#paint">paint</a>( <a href="tqpainter.html">TQPainter</a> *painter )
{
    // evil trick: find out whether we are painted onto our listbox
<a name="x1413"></a>    bool in_list_box = <a href="tqlistboxitem.html#listBox">listBox</a>() &amp;&amp; listBox()-&gt;viewport() == painter-&gt;<a href="tqpainter.html#device">device</a>();

    <a href="tqrect.html">TQRect</a> r ( 0, 0, width( <a href="tqlistboxitem.html#listBox">listBox</a>() ), height( <a href="tqlistboxitem.html#listBox">listBox</a>() ) );
    if ( in_list_box &amp;&amp; isSelected() )
<a name="x1414"></a>        painter-&gt;<a href="tqpainter.html#eraseRect">eraseRect</a>( r );
<a name="x1415"></a>    painter-&gt;<a href="tqpainter.html#fillRect">fillRect</a>( 5, 5, width( listBox() ) - 10, height( listBox() ) - 10, TQt::red );
    if ( in_list_box &amp;&amp; isCurrent() )
        listBox()-&gt;style().drawPrimitive( TQStyle::PE_FocusRect, painter, r, listBox()-&gt;colorGroup() );
}

/*
 * Constructor
 *
 * Creates child widgets of the ListBoxCombo widget
 */

<a name="f437"></a>ListBoxCombo::ListBoxCombo( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
    : <a href="tqvbox.html">TQVBox</a>( parent, name )
{
    <a href="tqframe.html#setMargin">setMargin</a>( 5 );
    <a href="tqhbox.html#setSpacing">setSpacing</a>( 5 );

    unsigned int i;
    <a href="tqstring.html">TQString</a> str;

    <a href="tqhbox.html">TQHBox</a> *row1 = new <a href="tqhbox.html">TQHBox</a>( this );
<a name="x1402"></a>    row1-&gt;<a href="tqhbox.html#setSpacing">setSpacing</a>( 5 );

    // Create a multi-selection ListBox...
    lb1 = new <a href="tqlistbox.html">TQListBox</a>( row1 );
<a name="x1408"></a>    lb1-&gt;<a href="tqlistbox.html#setSelectionMode">setSelectionMode</a>( TQListBox::Multi );

    // ...insert a pixmap item...
<a name="x1406"></a>    lb1-&gt;<a href="tqlistbox.html#insertItem">insertItem</a>( TQPixmap( "qtlogo.png" ) );
    // ...and 100 text items
    for ( i = 0; i &lt; 100; i++ ) {
        str = TQString( "Listbox Item %1" ).arg( i );
        if ( !( i % 4 ) )
            lb1-&gt;<a href="tqlistbox.html#insertItem">insertItem</a>( TQPixmap( "fileopen.xpm" ), str );
        else
            lb1-&gt;<a href="tqlistbox.html#insertItem">insertItem</a>( str );
    }

    // Create a pushbutton...
    <a href="tqpushbutton.html">TQPushButton</a> *arrow1 = new <a href="tqpushbutton.html">TQPushButton</a>( " -&gt; ", row1 );
    // ...and connect the clicked TQ_SIGNAL with the TQ_SLOT slotLeft2Right
    <a href="tqobject.html#connect">connect</a>( arrow1, TQ_SIGNAL( <a href="tqbutton.html#clicked">clicked</a>() ), this, TQ_SLOT( slotLeft2Right() ) );

    // create an empty single-selection ListBox
    lb2 = new <a href="tqlistbox.html">TQListBox</a>( row1 );

    <a href="tqhbox.html">TQHBox</a> *row2 = new <a href="tqhbox.html">TQHBox</a>( this );
    row2-&gt;<a href="tqhbox.html#setSpacing">setSpacing</a>( 5 );

    <a href="tqvbox.html">TQVBox</a> *box1 = new <a href="tqvbox.html">TQVBox</a>( row2 );
    box1-&gt;<a href="tqhbox.html#setSpacing">setSpacing</a>( 5 );

    // Create a non-editable Combobox and a label below...
    <a href="tqcombobox.html">TQComboBox</a> *cb1 = new <a href="tqcombobox.html">TQComboBox</a>( FALSE, box1 );
    label1 = new <a href="tqlabel.html">TQLabel</a>( "Current Item: Combobox Item 0", box1 );
<a name="x1416"></a><a name="x1404"></a>    label1-&gt;<a href="tqwidget.html#setMaximumHeight">setMaximumHeight</a>( label1-&gt;<a href="tqwidget.html#sizeHint">sizeHint</a>().height() * 2 );
<a name="x1401"></a>    label1-&gt;<a href="tqframe.html#setFrameStyle">setFrameStyle</a>( TQFrame::Panel | TQFrame::Sunken );

    //...and insert 50 items into the Combobox
    for ( i = 0; i &lt; 50; i++ ) {
        str = TQString( "Combobox Item %1" ).arg( i );
        if ( i % 9 )
<a name="x1399"></a>            cb1-&gt;<a href="tqcombobox.html#insertItem">insertItem</a>( str );
        else
<a name="x1400"></a>            cb1-&gt;<a href="tqcombobox.html#listBox">listBox</a>()-&gt;insertItem( new MyListBoxItem );
    }

    <a href="tqvbox.html">TQVBox</a> *box2 = new <a href="tqvbox.html">TQVBox</a>( row2 );
    box2-&gt;<a href="tqhbox.html#setSpacing">setSpacing</a>( 5 );

    // Create an editable Combobox and a label below...
    <a href="tqcombobox.html">TQComboBox</a> *cb2 = new <a href="tqcombobox.html">TQComboBox</a>( TRUE, box2 );
    label2 = new <a href="tqlabel.html">TQLabel</a>( "Current Item: Combobox Item 0", box2 );
    label2-&gt;<a href="tqwidget.html#setMaximumHeight">setMaximumHeight</a>( label2-&gt;<a href="tqwidget.html#sizeHint">sizeHint</a>().height() * 2 );
    label2-&gt;<a href="tqframe.html#setFrameStyle">setFrameStyle</a>( TQFrame::Panel | TQFrame::Sunken );

    // ... and insert 50 items into the Combobox
    for ( i = 0; i &lt; 50; i++ ) {
        str = TQString( "Combobox Item %1" ).arg( i );
        if ( !( i % 4 ) )
            cb2-&gt;<a href="tqcombobox.html#insertItem">insertItem</a>( TQPixmap( "fileopen.xpm" ), str );
        else
            cb2-&gt;<a href="tqcombobox.html#insertItem">insertItem</a>( str );
    }

    // Connect the activated SIGNALs of the Comboboxes with SLOTs
<a name="x1398"></a>    <a href="tqobject.html#connect">connect</a>( cb1, TQ_SIGNAL( <a href="tqcombobox.html#activated">activated</a>( const <a href="tqstring.html">TQString</a> &amp; ) ), this, TQ_SLOT( slotCombo1Activated( const <a href="tqstring.html">TQString</a> &amp; ) ) );
    <a href="tqobject.html#connect">connect</a>( cb2, TQ_SIGNAL( <a href="tqcombobox.html#activated">activated</a>( const <a href="tqstring.html">TQString</a> &amp; ) ), this, TQ_SLOT( slotCombo2Activated( const <a href="tqstring.html">TQString</a> &amp; ) ) );
}

/*
 * TQ_SLOT slotLeft2Right
 *
 * Copies all selected items of the first ListBox into the
 * second ListBox
 */

void <a name="f438"></a>ListBoxCombo::slotLeft2Right()
{
    // Go through all items of the first ListBox
<a name="x1405"></a>    for ( unsigned int i = 0; i &lt; lb1-&gt;<a href="tqlistbox.html#count">count</a>(); i++ ) {
<a name="x1407"></a>        <a href="tqlistboxitem.html">TQListBoxItem</a> *item = lb1-&gt;<a href="tqlistbox.html#item">item</a>( i );
        // if the item is selected...
<a name="x1409"></a>        if ( item-&gt;<a href="tqlistboxitem.html#isSelected">isSelected</a>() ) {
            // ...and it is a text item...
<a name="x1412"></a><a name="x1411"></a>            if ( item-&gt;<a href="tqlistboxitem.html#pixmap">pixmap</a>() &amp;&amp; !item-&gt;<a href="tqlistboxitem.html#text">text</a>().isEmpty() )
                lb2-&gt;<a href="tqlistbox.html#insertItem">insertItem</a>( *item-&gt;<a href="tqlistboxitem.html#pixmap">pixmap</a>(), item-&gt;<a href="tqlistboxitem.html#text">text</a>() );
            else if ( !item-&gt;<a href="tqlistboxitem.html#pixmap">pixmap</a>() )
                lb2-&gt;<a href="tqlistbox.html#insertItem">insertItem</a>( item-&gt;<a href="tqlistboxitem.html#text">text</a>() );
            else if ( item-&gt;<a href="tqlistboxitem.html#text">text</a>().isEmpty() )
                lb2-&gt;<a href="tqlistbox.html#insertItem">insertItem</a>( *item-&gt;<a href="tqlistboxitem.html#pixmap">pixmap</a>() );
        }
    }
}

/*
 * TQ_SLOT slotCombo1Activated( const <a href="tqstring.html">TQString</a> &amp;s )
 *
 * Sets the text of the item which the user just selected
 * in the first Combobox (and is now the value of s) to
 * the first Label.
 */

void <a name="f439"></a>ListBoxCombo::slotCombo1Activated( const <a href="tqstring.html">TQString</a> &amp;s )
{
<a name="x1403"></a>    label1-&gt;<a href="tqlabel.html#setText">setText</a>( TQString( "Current Item: %1" ).arg( s ) );
}

/*
 * TQ_SLOT slotCombo2Activated( const <a href="tqstring.html">TQString</a> &amp;s )
 *
 * Sets the text of the item which the user just selected
 * in the second Combobox (and is now the value of s) to
 * the second Label.
 */

void <a name="f440"></a>ListBoxCombo::slotCombo2Activated( const <a href="tqstring.html">TQString</a> &amp;s )
{
    label2-&gt;<a href="tqlabel.html#setText">setText</a>( TQString( "Current Item: %1" ).arg( s ) );
}
</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 "listboxcombo.h"
#include &lt;<a href="tqapplication-h.html">tqapplication.h</a>&gt;

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

    ListBoxCombo listboxcombo;
    listboxcombo.<a href="tqwidget.html#resize">resize</a>( 400, 270 );
    listboxcombo.<a href="tqwidget.html#setCaption">setCaption</a>( "TQt Example - Listboxes and Comboboxes" );
    a.<a href="tqapplication.html#setMainWidget">setMainWidget</a>( &amp;listboxcombo );
    listboxcombo.<a href="tqwidget.html#show">show</a>();

    return a.<a href="tqapplication.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>