Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 5114

qtbase5-doc-5.12.6-2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- sliders.qdoc -->
  <title>Sliders Example | Qt Widgets 5.12.6</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-widgets.html">Qt Widgets Examples</a></td><td >Sliders Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#window-class-definition">Window Class Definition</a></li>
<li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li>
<li class="level1"><a href="#slidersgroup-class-definition">SlidersGroup Class Definition</a></li>
<li class="level1"><a href="#slidersgroup-class-implementation">SlidersGroup Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Sliders Example</h1>
<span class="subtitle"></span>
<!-- $$$widgets/sliders-brief -->
<p>The Sliders example shows how to use the different types of sliders available in Qt: <a href="qslider.html">QSlider</a>, <a href="qscrollbar.html">QScrollBar</a> and <a href="qdial.html">QDial</a>.</p>
<!-- @@@widgets/sliders -->
<!-- $$$widgets/sliders-description -->
<div class="descr"> <a name="details"></a>
<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>
<p>The example also demonstrates how signals and slots can be used to synchronize the behavior of two or more widgets.</p>
<div class="border"><p class="centerAlign"><img src="images/sliders-example.png" alt="" /></p></div><p class="figCaption">Screenshot of the Sliders example</p>
<p>The Sliders example consists of two classes:</p>
<ul>
<li><code>SlidersGroup</code> 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>
<li><code>Window</code> 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 <code>SlidersGroup</code> widgets. The <a href="qgroupbox.html">QGroupBox</a> contain several widgets that control the behavior of the slider-like widgets.</li>
</ul>
<p>First we will review the <code>Window</code> class, then we will take a look at the <code>SlidersGroup</code> class.</p>
<a name="window-class-definition"></a>
<h2 id="window-class-definition">Window Class Definition</h2>
<pre class="cpp">

  <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      Window();

  <span class="keyword">private</span>:
      <span class="type">void</span> createControls(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>title);

      SlidersGroup <span class="operator">*</span>horizontalSliders;
      SlidersGroup <span class="operator">*</span>verticalSliders;
      <span class="type"><a href="qstackedwidget.html">QStackedWidget</a></span> <span class="operator">*</span>stackedWidget;

      <span class="type"><a href="qgroupbox.html">QGroupBox</a></span> <span class="operator">*</span>controlsGroup;
      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>minimumLabel;
      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>maximumLabel;
      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>valueLabel;
      <span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>invertedAppearance;
      <span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>invertedKeyBindings;
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>minimumSpinBox;
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>maximumSpinBox;
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>valueSpinBox;
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>orientationCombo;
  };

</pre>
<p>The <code>Window</code> 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 <code>createControls()</code> function to create the widgets that provide these controlling mechanisms and to connect them to the slider widgets.</p>
<a name="window-class-implementation"></a>
<h2 id="window-class-implementation">Window Class Implementation</h2>
<pre class="cpp">

  Window<span class="operator">::</span>Window()
  {
      horizontalSliders <span class="operator">=</span> <span class="keyword">new</span> SlidersGroup(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> tr(<span class="string">&quot;Horizontal&quot;</span>));
      verticalSliders <span class="operator">=</span> <span class="keyword">new</span> SlidersGroup(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Vertical<span class="operator">,</span> tr(<span class="string">&quot;Vertical&quot;</span>));

      stackedWidget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstackedwidget.html">QStackedWidget</a></span>;
      stackedWidget<span class="operator">-</span><span class="operator">&gt;</span>addWidget(horizontalSliders);
      stackedWidget<span class="operator">-</span><span class="operator">&gt;</span>addWidget(verticalSliders);

      createControls(tr(<span class="string">&quot;Controls&quot;</span>));

</pre>
<p>In the constructor we first create the two <code>SlidersGroup</code> 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 <code>createControls()</code> 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>
<pre class="cpp">

      connect(horizontalSliders<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              verticalSliders<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));
      connect(verticalSliders<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              valueSpinBox<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));
      connect(valueSpinBox<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              horizontalSliders<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));

      <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>;
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(controlsGroup);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(stackedWidget);
      setLayout(layout);

      minimumSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="number">0</span>);
      maximumSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="number">20</span>);
      valueSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="number">5</span>);

      setWindowTitle(tr(<span class="string">&quot;Sliders&quot;</span>));
  }

</pre>
<p>Then we connect the <code>horizontalSliders</code>, <code>verticalSliders</code> and <code>valueSpinBox</code> 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 <code>valueChanged()</code> signal is emitted with the new value as argument. The <code>setValue()</code> slot sets the current value of the widget to the new value, and emits <code>valueChanged()</code> if the new value is different from the old one.</p>
<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 <code>valueSpinBox</code> and the <code>SlidersGroup</code> widgets. The minimum and maximum values propagate through the connections we created with <code>createControls()</code>.</p>
<pre class="cpp">

  <span class="type">void</span> Window<span class="operator">::</span>createControls(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>title)
  {
      controlsGroup <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(title);

      minimumLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Minimum value:&quot;</span>));
      maximumLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Maximum value:&quot;</span>));
      valueLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Current value:&quot;</span>));

      invertedAppearance <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">&quot;Inverted appearance&quot;</span>));
      invertedKeyBindings <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">&quot;Inverted key bindings&quot;</span>));

</pre>
<p>In the private <code>createControls()</code> function, we let a <a href="qgroupbox.html">QGroupBox</a> (<code>controlsGroup</code>) 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>
<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 <code>invertedAppearance</code> is enabled, the slider values are inverted. The table below shows the appearance for the different slider-like widgets:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><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>
<tr class="qt-style"><th ></th><th >Normal</th><th >Inverted</th><th >Normal</th><th >Inverted</th><th >Normal</th><th >Inverted</th></tr></thead>
<tr valign="top" class="odd"><td ><a href="../qtcore/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>
<tr valign="top" class="even"><td ><a href="../qtcore/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>
</table></div>
<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>
<p>When the <code>invertedKeyBindings</code> 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>
<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>
<pre class="cpp">

      minimumSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>;
      minimumSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setRange(<span class="operator">-</span><span class="number">100</span><span class="operator">,</span> <span class="number">100</span>);
      minimumSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setSingleStep(<span class="number">1</span>);

      maximumSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>;
      maximumSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setRange(<span class="operator">-</span><span class="number">100</span><span class="operator">,</span> <span class="number">100</span>);
      maximumSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setSingleStep(<span class="number">1</span>);

      valueSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>;
      valueSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setRange(<span class="operator">-</span><span class="number">100</span><span class="operator">,</span> <span class="number">100</span>);
      valueSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setSingleStep(<span class="number">1</span>);

      orientationCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>;
      orientationCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Horizontal slider-like widgets&quot;</span>));
      orientationCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Vertical slider-like widgets&quot;</span>));

</pre>
<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>
<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>
<pre class="cpp">

      connect(orientationCombo<span class="operator">,</span> SIGNAL(activated(<span class="type">int</span>))<span class="operator">,</span>
              stackedWidget<span class="operator">,</span> SLOT(setCurrentIndex(<span class="type">int</span>)));
      connect(minimumSpinBox<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              horizontalSliders<span class="operator">,</span> SLOT(setMinimum(<span class="type">int</span>)));
      connect(minimumSpinBox<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              verticalSliders<span class="operator">,</span> SLOT(setMinimum(<span class="type">int</span>)));
      connect(maximumSpinBox<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              horizontalSliders<span class="operator">,</span> SLOT(setMaximum(<span class="type">int</span>)));
      connect(maximumSpinBox<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              verticalSliders<span class="operator">,</span> SLOT(setMaximum(<span class="type">int</span>)));
      connect(invertedAppearance<span class="operator">,</span> SIGNAL(toggled(bool))<span class="operator">,</span>
              horizontalSliders<span class="operator">,</span> SLOT(invertAppearance(bool)));
      connect(invertedAppearance<span class="operator">,</span> SIGNAL(toggled(bool))<span class="operator">,</span>
              verticalSliders<span class="operator">,</span> SLOT(invertAppearance(bool)));
      connect(invertedKeyBindings<span class="operator">,</span> SIGNAL(toggled(bool))<span class="operator">,</span>
              horizontalSliders<span class="operator">,</span> SLOT(invertKeyBindings(bool)));
      connect(invertedKeyBindings<span class="operator">,</span> SIGNAL(toggled(bool))<span class="operator">,</span>
              verticalSliders<span class="operator">,</span> SLOT(invertKeyBindings(bool)));

      <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>controlsLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>;
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(minimumLabel<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(maximumLabel<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(valueLabel<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(minimumSpinBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(maximumSpinBox<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(valueSpinBox<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(invertedAppearance<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">2</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(invertedKeyBindings<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(orientationCombo<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">3</span>);
      controlsGroup<span class="operator">-</span><span class="operator">&gt;</span>setLayout(controlsLayout);
  }

</pre>
<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 <code>orientationCombo</code> 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 <code>controlsGroup</code> group box.</p>
<a name="slidersgroup-class-definition"></a>
<h2 id="slidersgroup-class-definition">SlidersGroup Class Definition</h2>
<pre class="cpp">

  <span class="keyword">class</span> SlidersGroup : <span class="keyword">public</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      SlidersGroup(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientation orientation<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>title<span class="operator">,</span>
                   <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

  <span class="keyword">signals</span>:
      <span class="type">void</span> valueChanged(<span class="type">int</span> value);

  <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="type">void</span> setValue(<span class="type">int</span> value);
      <span class="type">void</span> setMinimum(<span class="type">int</span> value);
      <span class="type">void</span> setMaximum(<span class="type">int</span> value);
      <span class="type">void</span> invertAppearance(bool invert);
      <span class="type">void</span> invertKeyBindings(bool invert);

  <span class="keyword">private</span>:
      <span class="type"><a href="qslider.html">QSlider</a></span> <span class="operator">*</span>slider;
      <span class="type"><a href="qscrollbar.html">QScrollBar</a></span> <span class="operator">*</span>scrollBar;
      <span class="type"><a href="qdial.html">QDial</a></span> <span class="operator">*</span>dial;
  };

</pre>
<p>The <code>SlidersGroup</code> 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>
<p>We provide a <code>valueChanged()</code> signal and a public <code>setValue()</code> 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>
<a name="slidersgroup-class-implementation"></a>
<h2 id="slidersgroup-class-implementation">SlidersGroup Class Implementation</h2>
<pre class="cpp">

  SlidersGroup<span class="operator">::</span>SlidersGroup(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientation orientation<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>title<span class="operator">,</span>
                             <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(title<span class="operator">,</span> parent)
  {
      slider <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qslider.html">QSlider</a></span>(orientation);
      slider<span class="operator">-</span><span class="operator">&gt;</span>setFocusPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>StrongFocus);
      slider<span class="operator">-</span><span class="operator">&gt;</span>setTickPosition(<span class="type"><a href="qslider.html">QSlider</a></span><span class="operator">::</span>TicksBothSides);
      slider<span class="operator">-</span><span class="operator">&gt;</span>setTickInterval(<span class="number">10</span>);
      slider<span class="operator">-</span><span class="operator">&gt;</span>setSingleStep(<span class="number">1</span>);

      scrollBar <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qscrollbar.html">QScrollBar</a></span>(orientation);
      scrollBar<span class="operator">-</span><span class="operator">&gt;</span>setFocusPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>StrongFocus);

      dial <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdial.html">QDial</a></span>;
      dial<span class="operator">-</span><span class="operator">&gt;</span>setFocusPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>StrongFocus);

      connect(slider<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span> scrollBar<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));
      connect(scrollBar<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span> dial<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));
      connect(dial<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span> slider<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));

</pre>
<p>First we create the slider-like widgets with the appropriate properties. In particular we set the focus policy for each widget. <a href="../qtcore/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="../qtcore/qt.html#FocusPolicy-enum">Qt::StrongFocus</a> policy means that the widget accepts focus by both tabbing and clicking.</p>
<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>
<pre class="cpp">

      connect(dial<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>)));

</pre>
<p>We connect <code>dial</code>'s <code>valueChanged()</code> signal to the <code>SlidersGroup</code>'s <code>valueChanged()</code> signal, to notify the other widgets in the application (i.e&#x2e;, the control widgets) of the changed value.</p>
<pre class="cpp">

      <span class="type"><a href="qboxlayout.html">QBoxLayout</a></span><span class="operator">::</span>Direction direction;

      <span class="keyword">if</span> (orientation <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal)
          direction <span class="operator">=</span> <span class="type"><a href="qboxlayout.html">QBoxLayout</a></span><span class="operator">::</span>TopToBottom;
      <span class="keyword">else</span>
          direction <span class="operator">=</span> <span class="type"><a href="qboxlayout.html">QBoxLayout</a></span><span class="operator">::</span>LeftToRight;

      <span class="type"><a href="qboxlayout.html">QBoxLayout</a></span> <span class="operator">*</span>slidersLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qboxlayout.html">QBoxLayout</a></span>(direction);
      slidersLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(slider);
      slidersLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(scrollBar);
      slidersLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(dial);
      setLayout(slidersLayout);
  }

</pre>
<p>Finally, depending on the <a href="../qtcore/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>
<pre class="cpp">

  <span class="type">void</span> SlidersGroup<span class="operator">::</span>setValue(<span class="type">int</span> value)
  {
      slider<span class="operator">-</span><span class="operator">&gt;</span>setValue(value);
  }

</pre>
<p>The <code>setValue()</code> 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>
<pre class="cpp">

  <span class="type">void</span> SlidersGroup<span class="operator">::</span>setMinimum(<span class="type">int</span> value)
  {
      slider<span class="operator">-</span><span class="operator">&gt;</span>setMinimum(value);
      scrollBar<span class="operator">-</span><span class="operator">&gt;</span>setMinimum(value);
      dial<span class="operator">-</span><span class="operator">&gt;</span>setMinimum(value);
  }

  <span class="type">void</span> SlidersGroup<span class="operator">::</span>setMaximum(<span class="type">int</span> value)
  {
      slider<span class="operator">-</span><span class="operator">&gt;</span>setMaximum(value);
      scrollBar<span class="operator">-</span><span class="operator">&gt;</span>setMaximum(value);
      dial<span class="operator">-</span><span class="operator">&gt;</span>setMaximum(value);
  }

</pre>
<p>The <code>setMinimum()</code> and <code>setMaximum()</code> slots are used by the <code>Window</code> 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>
<pre class="cpp">

  <span class="type">void</span> SlidersGroup<span class="operator">::</span>invertAppearance(bool invert)
  {
      slider<span class="operator">-</span><span class="operator">&gt;</span>setInvertedAppearance(invert);
      scrollBar<span class="operator">-</span><span class="operator">&gt;</span>setInvertedAppearance(invert);
      dial<span class="operator">-</span><span class="operator">&gt;</span>setInvertedAppearance(invert);
  }

  <span class="type">void</span> SlidersGroup<span class="operator">::</span>invertKeyBindings(bool invert)
  {
      slider<span class="operator">-</span><span class="operator">&gt;</span>setInvertedControls(invert);
      scrollBar<span class="operator">-</span><span class="operator">&gt;</span>setInvertedControls(invert);
      dial<span class="operator">-</span><span class="operator">&gt;</span>setInvertedControls(invert);
  }

</pre>
<p>The <code>invertAppearance()</code> and <code>invertKeyBindings()</code> slots control the child widgets' <a href="qabstractslider.html#invertedAppearance-prop">invertedAppearance</a> and <a href="qabstractslider.html#invertedControls-prop">invertedControls</a> properties.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-widgets-sliders-main-cpp.html">widgets/sliders/main.cpp</a></li>
<li><a href="qtwidgets-widgets-sliders-sliders-pro.html">widgets/sliders/sliders.pro</a></li>
<li><a href="qtwidgets-widgets-sliders-slidersgroup-cpp.html">widgets/sliders/slidersgroup.cpp</a></li>
<li><a href="qtwidgets-widgets-sliders-slidersgroup-h.html">widgets/sliders/slidersgroup.h</a></li>
<li><a href="qtwidgets-widgets-sliders-window-cpp.html">widgets/sliders/window.cpp</a></li>
<li><a href="qtwidgets-widgets-sliders-window-h.html">widgets/sliders/window.h</a></li>
</ul>
</div>
<!-- @@@widgets/sliders -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>