summaryrefslogtreecommitdiffstats
path: root/doc/html/progressbar-example.html
blob: ba45b7f16f19311b98af54c224cad06e217802a0 (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
<!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>Progress Bar</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>Progress Bar</h1>

   
<p> 
This example shows how to use a progress bar.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/progressbar.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 PROGRESSBAR_H
#define PROGRESSBAR_H

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

class TQRadioButton;
class TQPushButton;
class TQProgressBar;

class ProgressBar : public <a href="tqbuttongroup.html">TQButtonGroup</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

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

protected:
    <a href="tqradiobutton.html">TQRadioButton</a> *slow, *normal, *fast;
    <a href="tqpushbutton.html">TQPushButton</a> *start, *pause, *reset;
    <a href="tqprogressbar.html">TQProgressBar</a> *progress;
    <a href="tqtimer.html">TQTimer</a> timer;

protected slots:
    void slotStart();
    void slotReset();
    void slotTimeout();

};

#endif
</pre>

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

#include &lt;<a href="tqradiobutton-h.html">tqradiobutton.h</a>&gt;
#include &lt;<a href="tqpushbutton-h.html">tqpushbutton.h</a>&gt;
#include &lt;<a href="tqprogressbar-h.html">tqprogressbar.h</a>&gt;
#include &lt;<a href="tqlayout-h.html">tqlayout.h</a>&gt;

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

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

<a name="f346"></a>ProgressBar::ProgressBar( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
    : <a href="tqbuttongroup.html">TQButtonGroup</a>( 0, Horizontal, "Progress Bar", parent, name ), timer()
{
    <a href="tqframe.html#setMargin">setMargin</a>( 10 );

    <a href="tqgridlayout.html">TQGridLayout</a>* toplayout = new <a href="tqgridlayout.html">TQGridLayout</a>( <a href="tqwidget.html#layout">layout</a>(), 2, 2, 5);

    <a href="tqbuttongroup.html#setRadioButtonExclusive">setRadioButtonExclusive</a>( TRUE );

    // insert three radiobuttons which the user can use
    // to set the speed of the progress and two pushbuttons
    // to start/pause/continue and reset the progress
    slow = new <a href="tqradiobutton.html">TQRadioButton</a>( "S&amp;low", this );
    normal = new <a href="tqradiobutton.html">TQRadioButton</a>( "&amp;Normal", this );
    fast = new <a href="tqradiobutton.html">TQRadioButton</a>( "&amp;Fast", this );
    <a href="tqvboxlayout.html">TQVBoxLayout</a>* vb1 = new <a href="tqvboxlayout.html">TQVBoxLayout</a>;
<a name="x964"></a>    toplayout-&gt;<a href="tqgridlayout.html#addLayout">addLayout</a>( vb1, 0, 0 );
    vb1-&gt;<a href="tqboxlayout.html#addWidget">addWidget</a>( slow );
    vb1-&gt;<a href="tqboxlayout.html#addWidget">addWidget</a>( normal );
    vb1-&gt;<a href="tqboxlayout.html#addWidget">addWidget</a>( fast );

    // two push buttons, one for start, for for reset.
    start = new <a href="tqpushbutton.html">TQPushButton</a>( "&amp;Start", this );
    reset = new <a href="tqpushbutton.html">TQPushButton</a>( "&amp;Reset", this );
    <a href="tqvboxlayout.html">TQVBoxLayout</a>* vb2 = new <a href="tqvboxlayout.html">TQVBoxLayout</a>;
    toplayout-&gt;<a href="tqgridlayout.html#addLayout">addLayout</a>( vb2, 0, 1 );
    vb2-&gt;<a href="tqboxlayout.html#addWidget">addWidget</a>( start );
    vb2-&gt;<a href="tqboxlayout.html#addWidget">addWidget</a>( reset );

    // Create the progressbar
    progress = new <a href="tqprogressbar.html">TQProgressBar</a>( 100, this );
<a name="x975"></a>    //    progress-&gt;<a href="tqwidget.html#setStyle">setStyle</a>( new <a href="tqmotifstyle.html">TQMotifStyle</a>() );
<a name="x965"></a>    toplayout-&gt;<a href="tqgridlayout.html#addMultiCellWidget">addMultiCellWidget</a>( progress, 1, 1, 0, 1 );

    // connect the clicked() SIGNALs of the pushbuttons to SLOTs
<a name="x962"></a>    <a href="tqobject.html#connect">connect</a>( start, TQ_SIGNAL( <a href="tqbutton.html#clicked">clicked</a>() ), this, TQ_SLOT( slotStart() ) );
    <a href="tqobject.html#connect">connect</a>( reset, TQ_SIGNAL( <a href="tqbutton.html#clicked">clicked</a>() ), this, TQ_SLOT( slotReset() ) );

    // connect the timeout() TQ_SIGNAL of the progress-timer to a TQ_SLOT
    <a href="tqobject.html#connect">connect</a>( &amp;timer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( slotTimeout() ) );

    // Let's start with normal speed...
<a name="x972"></a>    normal-&gt;<a href="tqradiobutton.html#setChecked">setChecked</a>( TRUE );


    // some contraints
<a name="x974"></a>    start-&gt;<a href="tqwidget.html#setFixedWidth">setFixedWidth</a>( 80 );
    <a href="tqwidget.html#setMinimumWidth">setMinimumWidth</a>( 300 );
}

/*
 * TQ_SLOT slotStart
 *
 * This TQ_SLOT is called if the user clicks start/pause/continue
 * button
 */

void <a name="f347"></a>ProgressBar::slotStart()
{
    // If the progress bar is at the beginning...
<a name="x966"></a>    if ( progress-&gt;<a href="tqprogressbar.html#progress">progress</a>() == -1 ) {
        // ...set according to the checked speed-radiobutton
        // the number of steps which are needed to complete the process
<a name="x971"></a>        if ( slow-&gt;<a href="tqradiobutton.html#isChecked">isChecked</a>() )
<a name="x969"></a>            progress-&gt;<a href="tqprogressbar.html#setTotalSteps">setTotalSteps</a>( 10000 );
        else if ( normal-&gt;<a href="tqradiobutton.html#isChecked">isChecked</a>() )
            progress-&gt;<a href="tqprogressbar.html#setTotalSteps">setTotalSteps</a>( 1000 );
        else
            progress-&gt;<a href="tqprogressbar.html#setTotalSteps">setTotalSteps</a>( 50 );

        // disable the speed-radiobuttons
<a name="x973"></a>        slow-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( FALSE );
        normal-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( FALSE );
        fast-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( FALSE );
    }

    // If the progress is not running...
    if ( !timer.isActive() ) {
        // ...start the timer (and so the progress) with a interval of 1 ms...
        timer.start( 1 );
        // ...and rename the start/pause/continue button to Pause
<a name="x963"></a>        start-&gt;<a href="tqbutton.html#setText">setText</a>( "&amp;Pause" );
    } else { // if the prgress is running...
        // ...stop the timer (and so the prgress)...
        timer.stop();
        // ...and rename the start/pause/continue button to Continue
        start-&gt;<a href="tqbutton.html#setText">setText</a>( "&amp;Continue" );
    }
}

/*
 * TQ_SLOT slotReset
 *
 * This TQ_SLOT is called when the user clicks the reset button
 */

void <a name="f348"></a>ProgressBar::slotReset()
{
    // stop the timer and progress
    timer.stop();

    // rename the start/pause/continue button to Start...
    start-&gt;<a href="tqbutton.html#setText">setText</a>( "&amp;Start" );
    // ...and enable this button
    start-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( TRUE );

    // enable the speed-radiobuttons
    slow-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( TRUE );
    normal-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( TRUE );
    fast-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( TRUE );

    // reset the progressbar
<a name="x967"></a>    progress-&gt;<a href="tqprogressbar.html#reset">reset</a>();
}

/*
 * TQ_SLOT slotTimeout
 *
 * This TQ_SLOT is called each ms when the timer is
 * active (== progress is running)
 */

void <a name="f349"></a>ProgressBar::slotTimeout()
{
    int p = progress-&gt;<a href="tqprogressbar.html#progress">progress</a>();

#if 1
    // If the progress is complete...
<a name="x970"></a>    if ( p == progress-&gt;<a href="tqprogressbar.html#totalSteps">totalSteps</a>() )  {
        // ...rename the start/pause/continue button to Start...
        start-&gt;<a href="tqbutton.html#setText">setText</a>( "&amp;Start" );
        // ...and disable it...
        start-&gt;<a href="tqwidget.html#setEnabled">setEnabled</a>( FALSE );
        // ...and return
        return;
    }
#endif

    // If the process is not complete increase it
<a name="x968"></a>    progress-&gt;<a href="tqprogressbar.html#setProgress">setProgress</a>( ++p );
}
</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 "progressbar.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);

    ProgressBar progressbar;
    progressbar.<a href="tqwidget.html#setCaption">setCaption</a>("TQt Example - ProgressBar");
    a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>(&amp;progressbar);
    progressbar.<a href="tqwidget.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>