~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/widgets-sliders.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/sliders.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Sliders Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Sliders Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="widgets-sliders-slidersgroup-cpp.html">widgets/sliders/slidersgroup.cpp</a></li>
 
24
<li><a href="widgets-sliders-slidersgroup-h.html">widgets/sliders/slidersgroup.h</a></li>
 
25
<li><a href="widgets-sliders-window-cpp.html">widgets/sliders/window.cpp</a></li>
 
26
<li><a href="widgets-sliders-window-h.html">widgets/sliders/window.h</a></li>
 
27
<li><a href="widgets-sliders-main-cpp.html">widgets/sliders/main.cpp</a></li>
 
28
</ul>
 
29
<p>Qt provides three types of slider-like widgets: <a href="qslider.html">QSlider</a>, <a href="qscrollbar.html">QScrollBar</a> and <a href="qdial.html">QDial</a>. They all inherit most of their functionality from <a href="qabstractslider.html">QAbstractSlider</a>, and can in theory replace each other in an application since the differences only concern their look and feel. This example shows what they look like, how they work and how their behavior and appearance can be manipulated through their properties.</p>
 
30
<p>The example also demonstrates how signals and slots can be used to synchronize the behavior of two or more widgets.</p>
 
31
<center><img src="images/sliders-example.png" alt="Screenshot of the Sliders example" /></center><p>The Sliders example consists of two classes:</p>
 
32
<ul>
 
33
<li><tt>SlidersGroup</tt> is a custom widget. It combines a <a href="qslider.html">QSlider</a>, a <a href="qscrollbar.html">QScrollBar</a> and a <a href="qdial.html">QDial</a>.</li>
 
34
<li><tt>Window</tt> is the main widget combining a <a href="qgroupbox.html">QGroupBox</a> and a <a href="qstackedwidget.html">QStackedWidget</a>. In this example, the <a href="qstackedwidget.html">QStackedWidget</a> provides a stack of two <tt>SlidersGroup</tt> widgets. The <a href="qgroupbox.html">QGroupBox</a> contain several widgets that control the behavior of the slider-like widgets.</li>
 
35
</ul>
 
36
<p>First we will review the <tt>Window</tt> class, then we will take a look at the <tt>SlidersGroup</tt> class.</p>
 
37
<a name="window-class-definition"></a>
 
38
<h2>Window Class Definition</h2>
 
39
<pre>&nbsp;   class Window : public QWidget
 
40
    {
 
41
        Q_OBJECT
 
42
 
 
43
    public:
 
44
        Window();
 
45
 
 
46
    private:
 
47
        void createControls(const QString &amp;title);
 
48
 
 
49
        SlidersGroup *horizontalSliders;
 
50
        SlidersGroup *verticalSliders;
 
51
        QStackedWidget *stackedWidget;
 
52
 
 
53
        QGroupBox *controlsGroup;
 
54
        QLabel *minimumLabel;
 
55
        QLabel *maximumLabel;
 
56
        QLabel *valueLabel;
 
57
        QCheckBox *invertedAppearance;
 
58
        QCheckBox *invertedKeyBindings;
 
59
        QSpinBox *minimumSpinBox;
 
60
        QSpinBox *maximumSpinBox;
 
61
        QSpinBox *valueSpinBox;
 
62
        QComboBox *orientationCombo;
 
63
    };</pre>
 
64
<p>The <tt>Window</tt> class inherits from <a href="qwidget.html">QWidget</a>. It displays the slider widgets and allows the user to set their minimum, maximum and current values and to customize their appearance, key bindings and orientation. We use a private <tt>createControls()</tt> function to create the widgets that provide these controlling mechanisms and to connect them to the slider widgets.</p>
 
65
<a name="window-class-implementation"></a>
 
66
<h2>Window Class Implementation</h2>
 
67
<pre>&nbsp;   Window::Window()
 
68
    {
 
69
        horizontalSliders = new SlidersGroup(Qt::Horizontal, tr(&quot;Horizontal&quot;));
 
70
        verticalSliders = new SlidersGroup(Qt::Vertical, tr(&quot;Vertical&quot;));
 
71
 
 
72
        stackedWidget = new QStackedWidget;
 
73
        stackedWidget-&gt;addWidget(horizontalSliders);
 
74
        stackedWidget-&gt;addWidget(verticalSliders);
 
75
 
 
76
        createControls(tr(&quot;Controls&quot;));</pre>
 
77
<p>In the constructor we first create the two <tt>SlidersGroup</tt> widgets that display the slider widgets horizontally and vertically, and add them to the <a href="qstackedwidget.html">QStackedWidget</a>. <a href="qstackedwidget.html">QStackedWidget</a> provides a stack of widgets where only the top widget is visible. With <tt>createControls()</tt> we create a connection from a controlling widget to the <a href="qstackedwidget.html">QStackedWidget</a>, making the user able to choose between horizontal and vertical orientation of the slider widgets. The rest of the controlling mechanisms is implemented by the same function call.</p>
 
78
<pre>&nbsp;       connect(horizontalSliders, SIGNAL(valueChanged(int)),
 
79
                verticalSliders, SLOT(setValue(int)));
 
80
        connect(verticalSliders, SIGNAL(valueChanged(int)),
 
81
                valueSpinBox, SLOT(setValue(int)));
 
82
        connect(valueSpinBox, SIGNAL(valueChanged(int)),
 
83
                horizontalSliders, SLOT(setValue(int)));
 
84
 
 
85
        QHBoxLayout *layout = new QHBoxLayout;
 
86
        layout-&gt;addWidget(controlsGroup);
 
87
        layout-&gt;addWidget(stackedWidget);
 
88
        setLayout(layout);
 
89
 
 
90
        minimumSpinBox-&gt;setValue(0);
 
91
        maximumSpinBox-&gt;setValue(20);
 
92
        valueSpinBox-&gt;setValue(5);
 
93
 
 
94
        setWindowTitle(tr(&quot;Sliders&quot;));
 
95
    }</pre>
 
96
<p>Then we connect the <tt>horizontalSliders</tt>, <tt>verticalSliders</tt> and <tt>valueSpinBox</tt> to each other, so that the slider widgets and the control widget will behave synchronized when the current value of one of them changes. The <tt>valueChanged()</tt> signal is emitted with the new value as argument. The <tt>setValue()</tt> slot sets the current value of the widget to the new value, and emits <tt>valueChanged()</tt> if the new value is different from the old one.</p>
 
97
<p>We put the group of control widgets and the stacked widget in a horizontal layout before we initialize the minimum, maximum and current values. The initialization of the current value will propagate to the slider widgets through the connection we made between <tt>valueSpinBox</tt> and the <tt>SlidersGroup</tt> widgets. The minimum and maximum values propagate through the connections we created with <tt>createControls()</tt>.</p>
 
98
<pre>&nbsp;   void Window::createControls(const QString &amp;title)
 
99
    {
 
100
        controlsGroup = new QGroupBox(title);
 
101
 
 
102
        minimumLabel = new QLabel(tr(&quot;Minimum value:&quot;));
 
103
        maximumLabel = new QLabel(tr(&quot;Maximum value:&quot;));
 
104
        valueLabel = new QLabel(tr(&quot;Current value:&quot;));
 
105
 
 
106
        invertedAppearance = new QCheckBox(tr(&quot;Inverted appearance&quot;));
 
107
        invertedKeyBindings = new QCheckBox(tr(&quot;Inverted key bindings&quot;));</pre>
 
108
<p>In the private <tt>createControls()</tt> function, we let a <a href="qgroupbox.html">QGroupBox</a> (<tt>controlsGroup</tt>) display the control widgets. A group box can provide a frame, a title and a keyboard shortcut, and displays various other widgets inside itself. The group of control widgets is composed by two checkboxes, three spin boxes (with labels) and one combobox.</p>
 
109
<p>After creating the labels, we create the two checkboxes. Checkboxes are typically used to represent features in an application that can be enabled or disabled. When <tt>invertedAppearance</tt> is enabled, the slider values are inverted. The table below shows the appearance for the different slider-like widgets:</p>
 
110
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
111
<tr valign="top" bgcolor="#a2c511"><th></th><th colspan="2"><a href="qslider.html">QSlider</a></th><th colspan="2"><a href="qscrollbar.html">QScrollBar</a></th><th colspan="2"><a href="qdial.html">QDial</a></th></tr>
 
112
<tr valign="top" bgcolor="#a2c511"><th></th><th>Normal</th><th>Inverted</th><th>Normal</th><th>Inverted</th><th>Normal</th><th>Inverted</th></tr>
 
113
<tr valign="top" bgcolor="#f0f0f0"><td><a href="qt.html#Orientation-enum">Qt::Horizontal</a></td><td>Left to right</td><td>Right to left</td><td>Left to right</td><td>Right to left</td><td>Clockwise</td><td>Counterclockwise</td></tr>
 
114
<tr valign="top" bgcolor="#e0e0e0"><td><a href="qt.html#Orientation-enum">Qt::Vertical</a></td><td>Bottom to top</td><td>Top to bottom</td><td>Top to bottom</td><td>Bottom to top</td><td>Clockwise</td><td>Counterclockwise</td></tr>
 
115
</table>
 
116
<p>It is common to invert the appearance of a vertical <a href="qslider.html">QSlider</a>. A vertical slider that controls volume, for example, will typically go from bottom to top (the non-inverted appearance), whereas a vertical slider that controls the position of an object on screen might go from top to bottom, because screen coordinates go from top to bottom.</p>
 
117
<p>When the <tt>invertedKeyBindings</tt> option is enabled (corresponding to the <a href="qabstractslider.html#invertedControls-prop">QAbstractSlider::invertedControls</a> property), the slider's wheel and key events are inverted. The normal key bindings mean that scrolling the mouse wheel &quot;up&quot; or using keys like page up will increase the slider's current value towards its maximum. Inverted, the same wheel and key events will move the value toward the slider's minimum. This can be useful if the <i>appearance</i> of a slider is inverted: Some users might expect the keys to still work the same way on the value, whereas others might expect <b>PageUp</b> to mean &quot;up&quot; on the screen.</p>
 
118
<p>Note that for horizontal and vertical scroll bars, the key bindings are inverted by default: <b>PageDown</b> increases the current value, and <b>PageUp</b> decreases it.</p>
 
119
<pre>&nbsp;       minimumSpinBox = new QSpinBox;
 
120
        minimumSpinBox-&gt;setRange(-100, 100);
 
121
        minimumSpinBox-&gt;setSingleStep(1);
 
122
 
 
123
        maximumSpinBox = new QSpinBox;
 
124
        maximumSpinBox-&gt;setRange(-100, 100);
 
125
        maximumSpinBox-&gt;setSingleStep(1);
 
126
 
 
127
        valueSpinBox = new QSpinBox;
 
128
        valueSpinBox-&gt;setRange(-100, 100);
 
129
        valueSpinBox-&gt;setSingleStep(1);
 
130
 
 
131
        orientationCombo = new QComboBox;
 
132
        orientationCombo-&gt;addItem(tr(&quot;Horizontal slider-like widgets&quot;));
 
133
        orientationCombo-&gt;addItem(tr(&quot;Vertical slider-like widgets&quot;));</pre>
 
134
<p>Then we create the spin boxes. <a href="qspinbox.html">QSpinBox</a> allows the user to choose a value by clicking the up and down buttons or pressing the <b>Up</b> and <b>Down</b> keys on the keyboard to modify the value currently displayed. The user can also type in the value manually. The spin boxes control the minimum, maximum and current values for the <a href="qslider.html">QSlider</a>, <a href="qscrollbar.html">QScrollBar</a>, and <a href="qdial.html">QDial</a> widgets.</p>
 
135
<p>We create a <a href="qcombobox.html">QComboBox</a> that allows the user to choose the orientation of the slider widgets. The <a href="qcombobox.html">QComboBox</a> widget is a combined button and popup list. It provides a means of presenting a list of options to the user in a way that takes up the minimum amount of screen space.</p>
 
136
<pre>&nbsp;       connect(orientationCombo, SIGNAL(activated(int)),
 
137
                stackedWidget, SLOT(setCurrentIndex(int)));
 
138
        connect(minimumSpinBox, SIGNAL(valueChanged(int)),
 
139
                horizontalSliders, SLOT(setMinimum(int)));
 
140
        connect(minimumSpinBox, SIGNAL(valueChanged(int)),
 
141
                verticalSliders, SLOT(setMinimum(int)));
 
142
        connect(maximumSpinBox, SIGNAL(valueChanged(int)),
 
143
                horizontalSliders, SLOT(setMaximum(int)));
 
144
        connect(maximumSpinBox, SIGNAL(valueChanged(int)),
 
145
                verticalSliders, SLOT(setMaximum(int)));
 
146
        connect(invertedAppearance, SIGNAL(toggled(bool)),
 
147
                horizontalSliders, SLOT(invertAppearance(bool)));
 
148
        connect(invertedAppearance, SIGNAL(toggled(bool)),
 
149
                verticalSliders, SLOT(invertAppearance(bool)));
 
150
        connect(invertedKeyBindings, SIGNAL(toggled(bool)),
 
151
                horizontalSliders, SLOT(invertKeyBindings(bool)));
 
152
        connect(invertedKeyBindings, SIGNAL(toggled(bool)),
 
153
                verticalSliders, SLOT(invertKeyBindings(bool)));
 
154
 
 
155
        QGridLayout *controlsLayout = new QGridLayout;
 
156
        controlsLayout-&gt;addWidget(minimumLabel, 0, 0);
 
157
        controlsLayout-&gt;addWidget(maximumLabel, 1, 0);
 
158
        controlsLayout-&gt;addWidget(valueLabel, 2, 0);
 
159
        controlsLayout-&gt;addWidget(minimumSpinBox, 0, 1);
 
160
        controlsLayout-&gt;addWidget(maximumSpinBox, 1, 1);
 
161
        controlsLayout-&gt;addWidget(valueSpinBox, 2, 1);
 
162
        controlsLayout-&gt;addWidget(invertedAppearance, 0, 2);
 
163
        controlsLayout-&gt;addWidget(invertedKeyBindings, 1, 2);
 
164
        controlsLayout-&gt;addWidget(orientationCombo, 3, 0, 1, 3);
 
165
        controlsGroup-&gt;setLayout(controlsLayout);
 
166
    }</pre>
 
167
<p>We synchronize the behavior of the control widgets and the slider widgets through their signals and slots. We connect each control widget to both the horizontal and vertical group of slider widgets. We also connect <tt>orientationCombo</tt> to the <a href="qstackedwidget.html">QStackedWidget</a>, so that the correct &quot;page&quot; is shown. Finally, we lay out the control widgets in a <a href="qgridlayout.html">QGridLayout</a> within the <tt>controlsGroup</tt> group box.</p>
 
168
<a name="slidersgroup-class-definition"></a>
 
169
<h2>SlidersGroup Class Definition</h2>
 
170
<pre>&nbsp;   class SlidersGroup : public QGroupBox
 
171
    {
 
172
        Q_OBJECT
 
173
 
 
174
    public:
 
175
        SlidersGroup(Qt::Orientation orientation, const QString &amp;title,
 
176
                     QWidget *parent = 0);
 
177
 
 
178
    signals:
 
179
        void valueChanged(int value);
 
180
 
 
181
    public slots:
 
182
        void setValue(int value);
 
183
        void setMinimum(int value);
 
184
        void setMaximum(int value);
 
185
        void invertAppearance(bool invert);
 
186
        void invertKeyBindings(bool invert);
 
187
 
 
188
    private:
 
189
        QSlider *slider;
 
190
        QScrollBar *scrollBar;
 
191
        QDial *dial;
 
192
    };</pre>
 
193
<p>The <tt>SlidersGroup</tt> class inherits from <a href="qgroupbox.html">QGroupBox</a>. It provides a frame and a title, and contains a <a href="qslider.html">QSlider</a>, a <a href="qscrollbar.html">QScrollBar</a> and a <a href="qdial.html">QDial</a>.</p>
 
194
<p>We provide a <tt>valueChanged()</tt> signal and a public <tt>setValue()</tt> slot with equivalent functionality to the ones in <a href="qabstractslider.html">QAbstractSlider</a> and <a href="qspinbox.html">QSpinBox</a>. In addition, we implement several other public slots to set the minimum and maximum value, and invert the slider widgets' appearance as well as key bindings.</p>
 
195
<a name="slidersgroup-class-implementation"></a>
 
196
<h2>SlidersGroup Class Implementation</h2>
 
197
<pre>&nbsp;   SlidersGroup::SlidersGroup(Qt::Orientation orientation, const QString &amp;title,
 
198
                               QWidget *parent)
 
199
        : QGroupBox(title, parent)
 
200
    {
 
201
        slider = new QSlider(orientation);
 
202
        slider-&gt;setFocusPolicy(Qt::StrongFocus);
 
203
        slider-&gt;setTickPosition(QSlider::TicksBothSides);
 
204
        slider-&gt;setTickInterval(10);
 
205
        slider-&gt;setSingleStep(1);
 
206
 
 
207
        scrollBar = new QScrollBar(orientation);
 
208
        scrollBar-&gt;setFocusPolicy(Qt::StrongFocus);
 
209
 
 
210
        dial = new QDial;
 
211
        dial-&gt;setFocusPolicy(Qt::StrongFocus);
 
212
 
 
213
        connect(slider, SIGNAL(valueChanged(int)), scrollBar, SLOT(setValue(int)));
 
214
        connect(scrollBar, SIGNAL(valueChanged(int)), dial, SLOT(setValue(int)));
 
215
        connect(dial, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));</pre>
 
216
<p>First we create the slider-like widgets with the appropiate properties. In particular we set the focus policy for each widget. <a href="qt.html#FocusPolicy-enum">Qt::FocusPolicy</a> is an enum type that defines the various policies a widget can have with respect to acquiring keyboard focus. The <a href="qt.html#FocusPolicy-enum">Qt::StrongFocus</a> policy means that the widget accepts focus by both tabbing and clicking.</p>
 
217
<p>Then we connect the widgets with each other, so that they will stay synchronized when the current value of one of them changes.</p>
 
218
<pre>&nbsp;       connect(dial, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));</pre>
 
219
<p>We connect <tt>dial</tt>'s <tt>valueChanged()</tt> signal to the <tt>SlidersGroup</tt>'s <tt>valueChanged()</tt> signal, to notify the other widgets in the application (i.e., the control widgets) of the changed value.</p>
 
220
<pre>&nbsp;       QBoxLayout::Direction direction;
 
221
 
 
222
        if (orientation == Qt::Horizontal)
 
223
            direction = QBoxLayout::TopToBottom;
 
224
        else
 
225
            direction = QBoxLayout::LeftToRight;
 
226
 
 
227
        QBoxLayout *slidersLayout = new QBoxLayout(direction);
 
228
        slidersLayout-&gt;addWidget(slider);
 
229
        slidersLayout-&gt;addWidget(scrollBar);
 
230
        slidersLayout-&gt;addWidget(dial);
 
231
        setLayout(slidersLayout);
 
232
    }</pre>
 
233
<p>Finally, depending on the <a href="qt.html#Orientation-enum">orientation</a> given at the time of construction, we choose and create the layout for the slider widgets within the group box.</p>
 
234
<pre>&nbsp;   void SlidersGroup::setValue(int value)
 
235
    {
 
236
        slider-&gt;setValue(value);
 
237
    }</pre>
 
238
<p>The <tt>setValue()</tt> slot sets the value of the <a href="qslider.html">QSlider</a>. We don't need to explicitly call <a href="qabstractslider.html#value-prop">setValue()</a> on the <a href="qscrollbar.html">QScrollBar</a> and <a href="qdial.html">QDial</a> widgets, since <a href="qslider.html">QSlider</a> will emit the <a href="qabstractslider.html#valueChanged">valueChanged()</a> signal when its value changes, triggering a domino effect.</p>
 
239
<pre>&nbsp;   void SlidersGroup::setMinimum(int value)
 
240
    {
 
241
        slider-&gt;setMinimum(value);
 
242
        scrollBar-&gt;setMinimum(value);
 
243
        dial-&gt;setMinimum(value);
 
244
    }
 
245
 
 
246
    void SlidersGroup::setMaximum(int value)
 
247
    {
 
248
        slider-&gt;setMaximum(value);
 
249
        scrollBar-&gt;setMaximum(value);
 
250
        dial-&gt;setMaximum(value);
 
251
    }</pre>
 
252
<p>The <tt>setMinimum()</tt> and <tt>setMaximum()</tt> slots are used by the <tt>Window</tt> class to set the range of the <a href="qslider.html">QSlider</a>, <a href="qscrollbar.html">QScrollBar</a>, and <a href="qdial.html">QDial</a> widgets.</p>
 
253
<pre>&nbsp;   void SlidersGroup::invertAppearance(bool invert)
 
254
    {
 
255
        slider-&gt;setInvertedAppearance(invert);
 
256
        scrollBar-&gt;setInvertedAppearance(invert);
 
257
        dial-&gt;setInvertedAppearance(invert);
 
258
    }
 
259
 
 
260
    void SlidersGroup::invertKeyBindings(bool invert)
 
261
    {
 
262
        slider-&gt;setInvertedControls(invert);
 
263
        scrollBar-&gt;setInvertedControls(invert);
 
264
        dial-&gt;setInvertedControls(invert);
 
265
    }</pre>
 
266
<p>The <tt>invertAppearance()</tt> and <tt>invertKeyBindings()</tt> slots control the child widgets' <a href="qabstractslider.html#invertedAppearance-prop">invertedAppearance</a> and <a href="qabstractslider.html#invertedControls-prop">invertedControls</a> properties.</p>
 
267
<p /><address><hr /><div align="center">
 
268
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
269
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
270
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
271
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
272
</tr></table></div></address></body>
 
273
</html>