Sophie

Sophie

distrib > Mageia > 6 > armv7hl > by-pkgid > 749fcc421771b410525f39bd88a5732d > files > 343

qttools5-doc-5.9.4-1.mga6.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" />
<!-- calculatorform.qdoc -->
  <title>Calculator Form Example | Qt Designer Manual</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.9</td><td ><a href="qtdesigner-manual.html">Qt Designer Manual</a></td><td ><a href="examples-designer.html">Qt Designer Examples</a></td><td >Calculator Form Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</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="#preparation">Preparation</a></li>
<li class="level1"><a href="#calculatorform-class-definition">CalculatorForm Class Definition</a></li>
<li class="level1"><a href="#calculatorform-class-implementation">CalculatorForm Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Calculator Form Example</h1>
<span class="subtitle"></span>
<!-- $$$calculatorform-description -->
<div class="descr"> <a name="details"></a>
<p>The Calculator Form Example shows how to use a form created with <i>Qt Designer</i> in an application by using the user interface information from a QWidget subclass. We use <a href="designer-using-a-ui-file.html">uic's auto-connection</a> feature to automatically connect signals from widgets on the form to slots in our code.</p>
<p class="centerAlign"><img src="images/calculatorform-example.png" alt="Screenshot of the Calculator Form example" /></p><p>The example presents two spin boxes that are used to input integer values and a label that shows their sum. Whenever either of the spin boxes are updated, the signal-slot connections between the widgets and the form ensure that the label is also updated.</p>
<a name="preparation"></a>
<h2 id="preparation">Preparation</h2>
<p>The user interface for this example is designed completely using <i>Qt Designer</i>. The result is a UI file describing the form, the widgets used, any signal-slot connections between them, and other standard user interface properties.</p>
<p>To ensure that the example can use this file, we need to include a <code>FORMS</code> declaration in the example's project file:</p>
<pre class="cpp">

  FORMS       = calculatorform.ui

</pre>
<p>When the project is built, <code>uic</code> will create a header file that lets us construct the form.</p>
<a name="calculatorform-class-definition"></a>
<h2 id="calculatorform-class-definition">CalculatorForm Class Definition</h2>
<p>The <code>CalculatorForm</code> class uses the user interface described in the <code>calculatorform.ui</code> file. To access the form and its contents, we need to include the <code>ui_calculatorform.h</code> header file created by <code>uic</code> during the build process:</p>
<pre class="cpp">

  <span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span>

</pre>
<p>We define the <code>CalculatorForm</code> class by subclassing QWidget because the form itself is based on QWidget:</p>
<pre class="cpp">

  <span class="keyword">class</span> CalculatorForm : <span class="keyword">public</span> <span class="type">QWidget</span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> CalculatorForm(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> on_inputSpinBox1_valueChanged(<span class="type">int</span> value);
      <span class="type">void</span> on_inputSpinBox2_valueChanged(<span class="type">int</span> value);

  <span class="keyword">private</span>:
      Ui<span class="operator">::</span>CalculatorForm ui;
  };

</pre>
<p>Apart from the constructor, the class contains two private slots that are named according to the auto-connection naming convention required by <code>uic</code>. The private <code>ui</code> member variable refers to the form, and is used to access the contents of the user interface.</p>
<a name="calculatorform-class-implementation"></a>
<h2 id="calculatorform-class-implementation">CalculatorForm Class Implementation</h2>
<p>The constructor simply calls the base class's constructor and sets up the form's user interface.</p>
<pre class="cpp">

  CalculatorForm<span class="operator">::</span>CalculatorForm(<span class="type">QWidget</span> <span class="operator">*</span>parent)
      : <span class="type">QWidget</span>(parent)
  {
      ui<span class="operator">.</span>setupUi(<span class="keyword">this</span>);
  }

</pre>
<p>The user interface is set up with the <code>setupUI()</code> function. We pass <code>this</code> as the argument to this function to use the <code>CalculatorForm</code> widget itself as the container for the user interface.</p>
<p>To automatically connect signals from the spin boxes defined in the user interface, we use the naming convention that indicates which widgets and their signals in the user interface should be connected to each slot. The first slot is called whenever the spin box called &quot;inputSpinBox1&quot; in the user interface emits the valueChanged() signal:</p>
<pre class="cpp">

  <span class="type">void</span> CalculatorForm<span class="operator">::</span>on_inputSpinBox1_valueChanged(<span class="type">int</span> value)
  {
      ui<span class="operator">.</span>outputWidget<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type">QString</span><span class="operator">::</span>number(value <span class="operator">+</span> ui<span class="operator">.</span>inputSpinBox2<span class="operator">-</span><span class="operator">&gt;</span>value()));
  }

</pre>
<p>When this occurs, we use the value supplied by the signal to update the output label by setting its new text directly. We access the output label and the other spin box via the class's private <code>ui</code> variable.</p>
<p>The second slot is called whenever the second spin box, called &quot;inputSpinBox2&quot;, emits the valueChanged() signal:</p>
<pre class="cpp">

  <span class="type">void</span> CalculatorForm<span class="operator">::</span>on_inputSpinBox2_valueChanged(<span class="type">int</span> value)
  {
      ui<span class="operator">.</span>outputWidget<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type">QString</span><span class="operator">::</span>number(value <span class="operator">+</span> ui<span class="operator">.</span>inputSpinBox1<span class="operator">-</span><span class="operator">&gt;</span>value()));
  }

</pre>
<p>In this case, the value from the first spin box is read and combined with the value supplied by the signal. Again, the output label is updated directly via the <code>ui</code> variable.</p>
<p>Files:</p>
<ul>
<li><a href="qtdesigner-calculatorform-calculatorform-cpp.html">calculatorform/calculatorform.cpp</a></li>
<li><a href="qtdesigner-calculatorform-calculatorform-h.html">calculatorform/calculatorform.h</a></li>
<li><a href="qtdesigner-calculatorform-calculatorform-ui.html">calculatorform/calculatorform.ui</a></li>
<li><a href="qtdesigner-calculatorform-main-cpp.html">calculatorform/main.cpp</a></li>
<li><a href="qtdesigner-calculatorform-calculatorform-pro.html">calculatorform/calculatorform.pro</a></li>
</ul>
</div>
<!-- @@@calculatorform -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 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>