1
<?xml version="1.0" encoding="iso-8859-1"?>
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 -->
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>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <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>
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>
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>
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>
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> class Window : public QWidget
47
void createControls(const QString &title);
49
SlidersGroup *horizontalSliders;
50
SlidersGroup *verticalSliders;
51
QStackedWidget *stackedWidget;
53
QGroupBox *controlsGroup;
57
QCheckBox *invertedAppearance;
58
QCheckBox *invertedKeyBindings;
59
QSpinBox *minimumSpinBox;
60
QSpinBox *maximumSpinBox;
61
QSpinBox *valueSpinBox;
62
QComboBox *orientationCombo;
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> Window::Window()
69
horizontalSliders = new SlidersGroup(Qt::Horizontal, tr("Horizontal"));
70
verticalSliders = new SlidersGroup(Qt::Vertical, tr("Vertical"));
72
stackedWidget = new QStackedWidget;
73
stackedWidget->addWidget(horizontalSliders);
74
stackedWidget->addWidget(verticalSliders);
76
createControls(tr("Controls"));</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> 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)));
85
QHBoxLayout *layout = new QHBoxLayout;
86
layout->addWidget(controlsGroup);
87
layout->addWidget(stackedWidget);
90
minimumSpinBox->setValue(0);
91
maximumSpinBox->setValue(20);
92
valueSpinBox->setValue(5);
94
setWindowTitle(tr("Sliders"));
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> void Window::createControls(const QString &title)
100
controlsGroup = new QGroupBox(title);
102
minimumLabel = new QLabel(tr("Minimum value:"));
103
maximumLabel = new QLabel(tr("Maximum value:"));
104
valueLabel = new QLabel(tr("Current value:"));
106
invertedAppearance = new QCheckBox(tr("Inverted appearance"));
107
invertedKeyBindings = new QCheckBox(tr("Inverted key bindings"));</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>
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 "up" 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 "up" 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> minimumSpinBox = new QSpinBox;
120
minimumSpinBox->setRange(-100, 100);
121
minimumSpinBox->setSingleStep(1);
123
maximumSpinBox = new QSpinBox;
124
maximumSpinBox->setRange(-100, 100);
125
maximumSpinBox->setSingleStep(1);
127
valueSpinBox = new QSpinBox;
128
valueSpinBox->setRange(-100, 100);
129
valueSpinBox->setSingleStep(1);
131
orientationCombo = new QComboBox;
132
orientationCombo->addItem(tr("Horizontal slider-like widgets"));
133
orientationCombo->addItem(tr("Vertical slider-like widgets"));</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> 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)));
155
QGridLayout *controlsLayout = new QGridLayout;
156
controlsLayout->addWidget(minimumLabel, 0, 0);
157
controlsLayout->addWidget(maximumLabel, 1, 0);
158
controlsLayout->addWidget(valueLabel, 2, 0);
159
controlsLayout->addWidget(minimumSpinBox, 0, 1);
160
controlsLayout->addWidget(maximumSpinBox, 1, 1);
161
controlsLayout->addWidget(valueSpinBox, 2, 1);
162
controlsLayout->addWidget(invertedAppearance, 0, 2);
163
controlsLayout->addWidget(invertedKeyBindings, 1, 2);
164
controlsLayout->addWidget(orientationCombo, 3, 0, 1, 3);
165
controlsGroup->setLayout(controlsLayout);
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 "page" 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> class SlidersGroup : public QGroupBox
175
SlidersGroup(Qt::Orientation orientation, const QString &title,
176
QWidget *parent = 0);
179
void valueChanged(int value);
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);
190
QScrollBar *scrollBar;
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> SlidersGroup::SlidersGroup(Qt::Orientation orientation, const QString &title,
199
: QGroupBox(title, parent)
201
slider = new QSlider(orientation);
202
slider->setFocusPolicy(Qt::StrongFocus);
203
slider->setTickPosition(QSlider::TicksBothSides);
204
slider->setTickInterval(10);
205
slider->setSingleStep(1);
207
scrollBar = new QScrollBar(orientation);
208
scrollBar->setFocusPolicy(Qt::StrongFocus);
211
dial->setFocusPolicy(Qt::StrongFocus);
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> 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> QBoxLayout::Direction direction;
222
if (orientation == Qt::Horizontal)
223
direction = QBoxLayout::TopToBottom;
225
direction = QBoxLayout::LeftToRight;
227
QBoxLayout *slidersLayout = new QBoxLayout(direction);
228
slidersLayout->addWidget(slider);
229
slidersLayout->addWidget(scrollBar);
230
slidersLayout->addWidget(dial);
231
setLayout(slidersLayout);
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> void SlidersGroup::setValue(int value)
236
slider->setValue(value);
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> void SlidersGroup::setMinimum(int value)
241
slider->setMinimum(value);
242
scrollBar->setMinimum(value);
243
dial->setMinimum(value);
246
void SlidersGroup::setMaximum(int value)
248
slider->setMaximum(value);
249
scrollBar->setMaximum(value);
250
dial->setMaximum(value);
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> void SlidersGroup::invertAppearance(bool invert)
255
slider->setInvertedAppearance(invert);
256
scrollBar->setInvertedAppearance(invert);
257
dial->setInvertedAppearance(invert);
260
void SlidersGroup::invertKeyBindings(bool invert)
262
slider->setInvertedControls(invert);
263
scrollBar->setInvertedControls(invert);
264
dial->setInvertedControls(invert);
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 © 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>