Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 1445

qtdeclarative5-doc-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" />
<!-- focus.qdoc -->
  <title>Keyboard Focus in Qt Quick | Qt Quick 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="qtquick-index.html">Qt Quick</a></td><td >Keyboard Focus in Qt Quick</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-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="#key-handling-overview">Key Handling Overview</a></li>
<li class="level1"><a href="#querying-the-active-focus-item">Querying the Active Focus Item</a></li>
<li class="level1"><a href="#acquiring-focus-and-focus-scopes">Acquiring Focus and Focus Scopes</a></li>
<li class="level1"><a href="#advanced-uses-of-focus-scopes">Advanced Uses of Focus Scopes</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Keyboard Focus in Qt Quick</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-input-focus.html-description -->
<div class="descr"> <a name="details"></a>
<p>When a key is pressed or released, a key event is generated and delivered to the focused Qt Quick <a href="qml-qtquick-item.html">Item</a>. To facilitate the construction of reusable components and to address some of the cases unique to fluid user interfaces, the Qt Quick items add a scope based extension to Qt's traditional keyboard focus model.</p>
<a name="key-handling-overview"></a>
<h2 id="key-handling-overview">Key Handling Overview</h2>
<p>When the user presses or releases a key, the following occurs:</p>
<ol class="1" type="1"><li>Qt receives the key action and generates a key event.</li>
<li>If a <a href="qquickwindow.html">QQuickWindow</a> is the active window, the key event is delivered to it.</li>
<li>The key event is delivered by the scene to the <a href="qml-qtquick-item.html">Item</a> with <i>active focus</i>. If no item has active focus, the key event is ignored.</li>
<li>If the <a href="qquickitem.html">QQuickItem</a> with active focus accepts the key event, propagation stops. Otherwise the event is sent to the Item's parent until the event is accepted, or the root item is reached.<p>If the <code>Rectangle</code> type in the following example has active focus and the <code>A</code> key is pressed, the event will not be propagated further. Upon pressing the <code>B</code> key, the event will propagate to the root item and thus be ignored.</p>
<pre class="qml">

  Rectangle {
      width: 100; height: 100
      focus: true
      Keys.onPressed: {
          if (event.key == Qt.Key_A) {
              console.log('Key A was pressed');
              event.accepted = true;
          }
      }
  }

</pre>
</li>
<li>If the root <a href="qml-qtquick-item.html">Item</a> is reached, the key event is ignored and regular Qt key handling continues.</li>
</ol>
<p>See also the <a href="qml-qtquick-keys.html">Keys attached property</a> and <a href="qml-qtquick-keynavigation.html">KeyNavigation attached property</a>.</p>
<a name="querying-the-active-focus-item"></a>
<h2 id="querying-the-active-focus-item">Querying the Active Focus Item</h2>
<p>Whether or not an <a href="qml-qtquick-item.html">Item</a> has active focus can be queried through the <code>Item::activeFocus</code> property. For example, here we have a <a href="qml-qtquick-text.html">Text</a> type whose text is determined by whether or not it has active focus.</p>
<pre class="qml">

      Text {
          text: activeFocus ? "I have active focus!" : "I do not have active focus"
      }

</pre>
<a name="acquiring-focus-and-focus-scopes"></a>
<h2 id="acquiring-focus-and-focus-scopes">Acquiring Focus and Focus Scopes</h2>
<p>An <a href="qml-qtquick-item.html">Item</a> requests focus by setting the <code>focus</code> property to <code>true</code>.</p>
<p>For very simple cases simply setting the <code>focus</code> property is sometimes sufficient. If we run the following example with qmlscene, we see that the <code>keyHandler</code> type has active focus and pressing the <code>A</code>, <code>B</code>, or <code>C</code> keys modifies the text appropriately.</p>
<pre class="qml">

  Rectangle {
      color: "lightsteelblue"; width: 240; height: 25
      Text { id: myText }
      Item {
          id: keyHandler
          focus: true
          Keys.onPressed: {
              if (event.key == Qt.Key_A)
                  myText.text = 'Key A was pressed'
              else if (event.key == Qt.Key_B)
                  myText.text = 'Key B was pressed'
              else if (event.key == Qt.Key_C)
                  myText.text = 'Key C was pressed'
          }
      }
  }

</pre>
<p class="centerAlign"><img src="images/declarative-qmlfocus1.png" alt="" /></p><p>However, were the above example to be used as a reusable or imported component, this simple use of the <code>focus</code> property is no longer sufficient.</p>
<p>To demonstrate, we create two instances of our previously defined component and set the first one to have focus. The intention is that when the <code>A</code>, <code>B</code>, or <code>C</code> keys are pressed, the first of the two components receives the event and responds accordingly.</p>
<p>The code that imports and creates two MyWidget instances:</p>
<pre class="qml">

  //Window code that imports MyWidget
  Rectangle {
      id: window
      color: "white"; width: 240; height: 150

      Column {
          anchors.centerIn: parent; spacing: 15

          MyWidget {
              focus: true             //set this MyWidget to receive the focus
              color: "lightblue"
          }
          MyWidget {
              color: "palegreen"
          }
      }
  }

</pre>
<p>The MyWidget code:</p>
<pre class="qml">

  Rectangle {
      id: widget
      color: "lightsteelblue"; width: 175; height: 25; radius: 10; antialiasing: true
      Text { id: label; anchors.centerIn: parent}
      focus: true
      Keys.onPressed: {
          if (event.key == Qt.Key_A)
              label.text = 'Key A was pressed'
          else if (event.key == Qt.Key_B)
              label.text = 'Key B was pressed'
          else if (event.key == Qt.Key_C)
              label.text = 'Key C was pressed'
      }
  }

</pre>
<p>We want the first <code>MyWidget</code> object to have the focus, so we set its <code>focus</code> property to <code>true</code>. However, by running the code, we can confirm that the second widget receives the focus.</p>
<p class="centerAlign"><img src="images/declarative-qmlfocus2.png" alt="" /></p><p>Looking at both <code>MyWidget</code> and <code>window</code> code, the problem is evident - there are three types that set the <code>focus</code> property to <code>true</code>. The two <code>MyWidget</code>s set the <code>focus</code> to <code>true</code> and the <code>window</code> component also sets the focus. Ultimately, only one type can have keyboard focus, and the system has to decide which type receives the focus. When the second <code>MyWidget</code> is created, it receives the focus because it is the last type to set its <code>focus</code> property to <code>true</code>.</p>
<p>This problem is due to visibility. The <code>MyWidget</code> component would like to have the focus, but it cannot control the focus when it is imported or reused. Likewise, the <code>window</code> component does not have the ability to know if its imported components are requesting the focus.</p>
<p>To solve this problem, QML introduces a concept known as a <i>focus scope</i>. For existing Qt users, a focus scope is like an automatic focus proxy. A focus scope is created by declaring the <a href="qml-qtquick-focusscope.html">FocusScope</a> type.</p>
<p>In the next example, a <a href="qml-qtquick-focusscope.html">FocusScope</a> type is added to the component, and the visual result shown.</p>
<pre class="qml">

  FocusScope {

      //FocusScope needs to bind to visual properties of the Rectangle
      property alias color: rectangle.color
      x: rectangle.x; y: rectangle.y
      width: rectangle.width; height: rectangle.height

      Rectangle {
          id: rectangle
          anchors.centerIn: parent
          color: "lightsteelblue"; width: 175; height: 25; radius: 10; antialiasing: true
          Text { id: label; anchors.centerIn: parent }
          focus: true
          Keys.onPressed: {
              if (event.key == Qt.Key_A)
                  label.text = 'Key A was pressed'
              else if (event.key == Qt.Key_B)
                  label.text = 'Key B was pressed'
              else if (event.key == Qt.Key_C)
                  label.text = 'Key C was pressed'
          }
      }
  }

</pre>
<p class="centerAlign"><img src="images/declarative-qmlfocus3.png" alt="" /></p><p>Conceptually <i>focus scopes</i> are quite simple.</p>
<ul>
<li>Within each focus scope one object may have <code>Item::focus</code> set to <code>true</code>. If more than one <a href="qml-qtquick-item.html">Item</a> has the <code>focus</code> property set, the last type to set the <code>focus</code> will have the focus and the others are unset, similar to when there are no focus scopes.</li>
<li>When a focus scope receives active focus, the contained type with <code>focus</code> set (if any) also gets the active focus. If this type is also a <a href="qml-qtquick-focusscope.html">FocusScope</a>, the proxying behavior continues. Both the focus scope and the sub-focused item will have the <code>activeFocus</code> property set.</li>
</ul>
<p>Note that, since the <a href="qml-qtquick-focusscope.html">FocusScope</a> type is not a visual type, the properties of its children need to be exposed to the parent item of the <a href="qml-qtquick-focusscope.html">FocusScope</a>. Layouts and positioning types will use these visual and styling properties to create the layout. In our example, the <code>Column</code> type cannot display the two widgets properly because the <a href="qml-qtquick-focusscope.html">FocusScope</a> lacks visual properties of its own. The MyWidget component directly binds to the <code>rectangle</code> properties to allow the <code>Column</code> type to create the layout containing the children of the <a href="qml-qtquick-focusscope.html">FocusScope</a>.</p>
<p>So far, the example has the second component statically selected. It is trivial now to extend this component to make it clickable, and add it to the original application. We still set one of the widgets as focused by default. Now, clicking either MyClickableWidget gives it focus and the other widget loses the focus.</p>
<p>The code that imports and creates two MyClickableWidget instances:</p>
<pre class="qml">

  Rectangle {
      id: window

      color: "white"; width: 240; height: 150

      Column {
          anchors.centerIn: parent; spacing: 15

          MyClickableWidget {
              focus: true             //set this MyWidget to receive the focus
              color: "lightblue"
          }
          MyClickableWidget {
              color: "palegreen"
          }
      }

  }

</pre>
<p>The MyClickableWidget code:</p>
<pre class="qml">

  FocusScope {

      id: scope

      //FocusScope needs to bind to visual properties of the children
      property alias color: rectangle.color
      x: rectangle.x; y: rectangle.y
      width: rectangle.width; height: rectangle.height

      Rectangle {
          id: rectangle
          anchors.centerIn: parent
          color: "lightsteelblue"; width: 175; height: 25; radius: 10; antialiasing: true
          Text { id: label; anchors.centerIn: parent }
          focus: true
          Keys.onPressed: {
              if (event.key == Qt.Key_A)
                  label.text = 'Key A was pressed'
              else if (event.key == Qt.Key_B)
                  label.text = 'Key B was pressed'
              else if (event.key == Qt.Key_C)
                  label.text = 'Key C was pressed'
          }
      }
      MouseArea { anchors.fill: parent; onClicked: { scope.focus = true } }
  }

</pre>
<p class="centerAlign"><img src="images/declarative-qmlfocus4.png" alt="" /></p><p>When a QML <a href="qml-qtquick-item.html">Item</a> explicitly relinquishes focus (by setting its <code>focus</code> property to <code>false</code> while it has active focus), the system does not automatically select another type to receive focus. That is, it is possible for there to be no currently active focus.</p>
<p>See <a href="qtquick-keyinteraction-example.html">Qt Quick Examples - Key Interaction</a> for a demonstration of moving keyboard focus between multiple areas using <a href="qml-qtquick-focusscope.html">FocusScope</a> types.</p>
<a name="advanced-uses-of-focus-scopes"></a>
<h2 id="advanced-uses-of-focus-scopes">Advanced Uses of Focus Scopes</h2>
<p>Focus scopes allow focus to allocation to be easily partitioned. Several QML items use it to this effect.</p>
<p><a href="qml-qtquick-listview.html">ListView</a>, for example, is itself a focus scope. Generally this isn't noticeable as <a href="qml-qtquick-listview.html">ListView</a> doesn't usually have manually added visual children. By being a focus scope, <a href="qml-qtquick-listview.html">ListView</a> can focus the current list item without worrying about how that will effect the rest of the application. This allows the current item delegate to react to key presses.</p>
<p>This contrived example shows how this works. Pressing the <code>Return</code> key will print the name of the current list item.</p>
<pre class="qml">

  Rectangle {
      color: "lightsteelblue"; width: 100; height: 50

      ListView {
          anchors.fill: parent
          focus: true

          model: ListModel {
              ListElement { name: "Bob" }
              ListElement { name: "John" }
              ListElement { name: "Michael" }
          }

          delegate: FocusScope {
                  width: childrenRect.width; height: childrenRect.height
                  x:childrenRect.x; y: childrenRect.y
                  TextInput {
                      focus: true
                      text: name
                      Keys.onReturnPressed: console.log(name)
                  }
          }
      }
  }

</pre>
<p class="centerAlign"><img src="images/declarative-qmlfocus5.png" alt="" /></p><p>While the example is simple, there is a lot going on behind the scenes. Whenever the current item changes, the <a href="qml-qtquick-listview.html">ListView</a> sets the delegate's <code>Item::focus</code> property. As the <a href="qml-qtquick-listview.html">ListView</a> is a focus scope, this doesn't affect the rest of the application. However, if the <a href="qml-qtquick-listview.html">ListView</a> itself has active focus this causes the delegate itself to receive active focus. In this example, the root type of the delegate is also a focus scope, which in turn gives active focus to the <code>Text</code> type that actually performs the work of handling the <code>Return</code> key.</p>
<p>All of the QML view classes, such as <a href="qml-qtquick-pathview.html">PathView</a> and <a href="qml-qtquick-gridview.html">GridView</a>, behave in a similar manner to allow key handling in their respective delegates.</p>
</div>
<!-- @@@qtquick-input-focus.html -->
        </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>