Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > bdbbdfc3f538bf93bb0eb988a7a43005 > files > 435

qtdoc5-5.12.6-1.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" />
<!-- calqlatr.qdoc -->
  <title>Qt Quick Demo - Calqlatr | Qt 5.12</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 ><a href="index.html">Qt 5.12</a></td><td >Qt Quick Demo - Calqlatr</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.12.6 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="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#displaying-custom-components">Displaying Custom Components</a></li>
<li class="level1"><a href="#animating-components">Animating Components</a></li>
<li class="level1"><a href="#performing-calculations">Performing Calculations</a></li>
<li class="level1"><a href="#list-of-files">List of Files</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Demo - Calqlatr</h1>
<span class="subtitle"></span>
<!-- $$$demos/calqlatr-brief -->
<p>A QML app designed for portrait devices that uses custom components, animated with AnimationController, and JavaScript for the application logic.</p>
<!-- @@@demos/calqlatr -->
<!-- $$$demos/calqlatr-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qtquick-demo-calqlatr.png" alt="" /></p><p><i>Calqlatr</i> demonstrates various QML and Qt Quick features, such as displaying custom components and using animation to move the components around in the application view. The application logic is implemented in JavaScript and the appearance is implemented in QML.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit <a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p>
<a name="displaying-custom-components"></a>
<h2 id="displaying-custom-components">Displaying Custom Components</h2>
<p>In the Calqlatr application, we use the following custom types that are each defined in a separate .qml file:</p>
<ul>
<li>Button.qml</li>
<li>Display.qml</li>
<li>NumberPad.qml</li>
</ul>
<p>To use the custom types, we add an import statement to the main QML file, calqlatr.qml that imports the folder called <code>content</code> where the types are located:</p>
<pre class="cpp">

  import <span class="string">&quot;content&quot;</span>

</pre>
<p>We can then display custom components by adding the component types to any QML file. For example, we use the NumberPad type in calqlatr.qml to create the number pad of the calculator. We place the type inside an Item QML type, which is the base type for all visual items in Qt Quick:</p>
<pre class="cpp">

      Item {
          id: pad
          width: 180
          NumberPad { id: numPad; y: 10; anchors.horizontalCenter: parent.horizontalCenter }
      }

</pre>
<p>Further, we use the Button type in the <code>NumberPad</code> type to create the calculator buttons. Button.qml specifies the basic properties for a button that we can modify for each button instance in NumberPad.qml. For the digit and separator buttons, we additionally specify the text property using the property alias <code>text</code> that we define in Button.qml.</p>
<p>For the operator buttons, we also specify another color (green) using the property alias <code>color</code> and set the operator property to <code>true</code>. We use the operator property in functions that perform the calculations.</p>
<p>We place the buttons inside a Grid QML type to position them in a grid:</p>
<pre class="cpp">

  Grid {
      columns: 3
      columnSpacing: 32
      rowSpacing: 16

      signal buttonPressed

      Button { text: "7" }
      Button { text: "8" }
      Button { text: "9" }
      Button { text: "4" }
      Button { text: "5" }
      Button { text: "6" }
      Button { text: "1" }
      Button { text: "2" }
      Button { text: "3" }
      Button { text: "0" }
      Button { text: "."; dimmable: true }
      Button { text: " " }
      Button { text: "±"; color: "#6da43d"; operator: true; dimmable: true }
      Button { text: "−"; color: "#6da43d"; operator: true; dimmable: true }
      Button { text: "+"; color: "#6da43d"; operator: true; dimmable: true }
      Button { text: "√"; color: "#6da43d"; operator: true; dimmable: true }
      Button { text: "÷"; color: "#6da43d"; operator: true; dimmable: true }
      Button { text: "×"; color: "#6da43d"; operator: true; dimmable: true }
      Button { text: "C"; color: "#6da43d"; operator: true }
      Button { text: " "; color: "#6da43d"; operator: true }
      Button { text: "="; color: "#6da43d"; operator: true; dimmable: true }
  }

</pre>
<p>Some of the buttons also have a <code>dimmable</code> property set, meaning that they can be visually disabled (dimmed) whenever the calculator engine does not accept input from that button. As an example, the button for square root operator is dimmed for negative values.</p>
<a name="animating-components"></a>
<h2 id="animating-components">Animating Components</h2>
<p>We use the Display type to display calculations. In Display.qml, we use images to make the display component look like a slip of paper that contains a grip. Users can drag the grip to move the display from left to right.</p>
<p>When users release the grip, the AnimationController QML type that we define in the calqlatr.qml file finishes running the controlled animation in either a forwards or a backwards direction. To run the animation, we call either completeToEnd() or completeToBeginning(), depending on the direction. We do this in the MouseArea's <code>onReleased</code> signal handler, where <code>controller</code> is the id of our AnimationController:</p>
<pre class="cpp">

          MouseArea {
              ...
              onReleased: {
                  if (rewind)
                      controller.completeToBeginning()
                  else
                      controller.completeToEnd()
              }
          }

</pre>
<p>Unlike other QML animation types, AnimationController is not driven by internal timers but by explicitly setting its progress property to a value between <code>0.0</code> and <code>1.0</code>.</p>
<p>Inside the AnimationController, we run two NumberAnimation instances in parallel to move the number pad and the display components simultaneously to the opposite sides of the view. In addition, we run a SequentialAnimation instance to scale the number pad during the transition, giving the animation some depth.</p>
<pre class="cpp">

      AnimationController {
          id: controller
          animation: ParallelAnimation {
              id: anim
              NumberAnimation { target: display; property: "x"; duration: 400; from: -16; to: window.width - display.width; easing.type: Easing.InOutQuad }
              NumberAnimation { target: pad; property: "x"; duration: 400; from: window.width - pad.width; to: 0; easing.type: Easing.InOutQuad }
              SequentialAnimation {
                  NumberAnimation { target: pad; property: "scale"; duration: 200; from: 1; to: 0.97; easing.type: Easing.InOutQuad }
                  NumberAnimation { target: pad; property: "scale"; duration: 200; from: 0.97; to: 1; easing.type: Easing.InOutQuad }
              }
          }
      }

</pre>
<p>We use the easing curve of the type <code>Easing.InOutQuad</code> to accelerate the motion until halfway and then decelerate it.</p>
<p>In Button.qml, the text colors of the number pad buttons are also animated.</p>
<pre class="cpp">

      Text {
          id: textItem
          ...
          color: (dimmable && dimmed) ? Qt.darker(button.color) : button.color
          Behavior on color { ColorAnimation { duration: 120; easing.type: Easing.OutElastic} }
          states: [
              State {
                  name: "pressed"
                  when: mouse.pressed && !dimmed
                  PropertyChanges {
                      target: textItem
                      color: Qt.lighter(button.color)
                  }
              }
          ]
      }

</pre>
<p>We use Qt.darker() to darken the color when the button is dimmed, and Qt.lighter() to <i>light up</i> the button when pressed. The latter is done in a separate state called <i>&quot;pressed&quot;</i>, which activates when the <code>pressed</code> property of the button's MouseArea is set.</p>
<p>The color changes are animated by defining a Behavior on the <code>color</code> property.</p>
<p>In order to dynamically change the <code>dimmed</code> property of all the buttons of the <code>NumberPad</code>, we connect its <code>buttonPressed</code> signal to the <code>Button</code>'s <code>updateDimmed()</code> function in Button.qml:</p>
<pre class="cpp">

      function updateDimmed() {
          dimmed = window.isButtonDisabled(button.text)
      }

      Component.onCompleted: {
          numPad.buttonPressed.connect(updateDimmed)
          updateDimmed()
      }

</pre>
<p>This way, when a button is pressed, all buttons on the <code>NumPad</code> receive a <code>buttonPressed</code> signal and are activated or deactivated according to the state of the calculator engine.</p>
<a name="performing-calculations"></a>
<h2 id="performing-calculations">Performing Calculations</h2>
<p>The calculator.js file defines our calculator engine. It contains variables to store the calculator state, and functions that are called when the user presses the digit and operator buttons. To use the engine, we import calculator.js in the calqlatr.qml file as <code>CalcEngine</code>:</p>
<pre class="cpp">

  import <span class="string">&quot;content/calculator.js&quot;</span> as CalcEngine

</pre>
<p>Importing the engine creates a new instance of it. Therefore, we only do it in the main QML file, <code>calqlatr.qml</code>. The root item defined in this file contains helper functions that allow other types to access the calculator engine:</p>
<pre class="cpp">

      function operatorPressed(operator) {
          CalcEngine.operatorPressed(operator)
          numPad.buttonPressed()
      }
      function digitPressed(digit) {
          CalcEngine.digitPressed(digit)
          numPad.buttonPressed()
      }
      function isButtonDisabled(op) {
          return CalcEngine.disabled(op)
      }

</pre>
<p>When users press a digit, the text from the digit appears on the display. When they press an operator, the appropriate calculation is performed, and the result can be displayed using the equals (=) operator. The clear (C) operator resets the calculator engine.</p>
<a name="list-of-files"></a>
<h2 id="list-of-files">List of Files</h2>
<p>Files:</p>
<ul>
<li><a href="qtdoc-demos-calqlatr-calqlatr-pro.html">demos/calqlatr/calqlatr.pro</a></li>
<li><a href="qtdoc-demos-calqlatr-calqlatr-qml.html">demos/calqlatr/calqlatr.qml</a></li>
<li><a href="qtdoc-demos-calqlatr-calqlatr-qmlproject.html">demos/calqlatr/calqlatr.qmlproject</a></li>
<li><a href="qtdoc-demos-calqlatr-calqlatr-qrc.html">demos/calqlatr/calqlatr.qrc</a></li>
<li><a href="qtdoc-demos-calqlatr-content-button-qml.html">demos/calqlatr/content/Button.qml</a></li>
<li><a href="qtdoc-demos-calqlatr-content-display-qml.html">demos/calqlatr/content/Display.qml</a></li>
<li><a href="qtdoc-demos-calqlatr-content-numberpad-qml.html">demos/calqlatr/content/NumberPad.qml</a></li>
<li><a href="qtdoc-demos-calqlatr-content-calculator-js.html">demos/calqlatr/content/calculator.js</a></li>
<li><a href="qtdoc-demos-calqlatr-main-cpp.html">demos/calqlatr/main.cpp</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/demos/calqlatr/content/images/paper-edge-left.png">demos/calqlatr/content/images/paper-edge-left.png</a></li>
<li><a href="images/used-in-examples/demos/calqlatr/content/images/paper-edge-right.png">demos/calqlatr/content/images/paper-edge-right.png</a></li>
<li><a href="images/used-in-examples/demos/calqlatr/content/images/paper-grip.png">demos/calqlatr/content/images/paper-grip.png</a></li>
</ul>
</div>
<p><b>See also </b><a href="qmlapplications.html">QML Applications</a>.</p>
<!-- @@@demos/calqlatr -->
        </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>