Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- touchinteraction.qdoc -->
<title>Qt Quick Examples - Touch Interaction | Qt Quick 5.15.0</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">
<ul>
<li><a href="../qtdoc/index.html">Qt 5.15</a></li>
<li><a href="qtquick-index.html">Qt Quick</a></li>
<li>Qt Quick Examples - Touch Interaction</li>
<li id="buildversion"><a href="qtquick-index.html">Qt 5.15.0 Reference Documentation</a></li>
</ul>
</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="#multipoint-flames-example">Multipoint Flames Example</a></li>
<li class="level1"><a href="#bear-whack-example">Bear-Whack Example</a></li>
<li class="level1"><a href="#flick-resize-example">Flick Resize Example</a></li>
<li class="level1"><a href="#flickable-example">Flickable Example</a></li>
<li class="level1"><a href="#corkboards-example">Corkboards Example</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Examples - Touch Interaction</h1>
<span class="subtitle"></span>
<!-- $$$touchinteraction-brief -->
<p>A collection of QML Touch Interaction examples.</p>
<!-- @@@touchinteraction -->
<!-- $$$touchinteraction-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qml-touchinteraction-example.png" alt="" /></p><p><i>Touch Interaction</i> is a collection of small QML examples relating to touch interaction methods. For more information, visit <a href="qtquick-input-topic.html">Important Concepts In Qt Quick - User Input</a>.</p>
<a name="running-the-example"></a>
<h4 id="running-the-example">Running the Example</h4>
<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="multipoint-flames-example"></a>
<h4 id="multipoint-flames-example">Multipoint Flames Example</h4>
<p><i>Multipoint Flames</i> demonstrates distinguishing different fingers in a <a href="qml-qtquick-multipointtoucharea.html">MultiPointTouchArea</a>, by assigning a different colored flame to each touch point.</p>
<p>The MultipointTouchArea sets up multiple touch points:</p>
<pre class="qml">
<span class="type"><a href="qml-qtquick-multipointtoucharea.html">MultiPointTouchArea</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">minimumTouchPoints</span>: <span class="number">1</span>
<span class="name">maximumTouchPoints</span>: <span class="number">5</span>
<span class="name">touchPoints</span>: [
<span class="type"><a href="qml-qtquick-touchpoint.html">TouchPoint</a></span> { <span class="name">id</span>: <span class="name">touch1</span> },
<span class="type"><a href="qml-qtquick-touchpoint.html">TouchPoint</a></span> { <span class="name">id</span>: <span class="name">touch2</span> },
<span class="type"><a href="qml-qtquick-touchpoint.html">TouchPoint</a></span> { <span class="name">id</span>: <span class="name">touch11</span> },
<span class="type"><a href="qml-qtquick-touchpoint.html">TouchPoint</a></span> { <span class="name">id</span>: <span class="name">touch21</span> },
<span class="type"><a href="qml-qtquick-touchpoint.html">TouchPoint</a></span> { <span class="name">id</span>: <span class="name">touch31</span> }
]
}
</pre>
<p>The flames are then simply bound to the coordinates of the touch point, and whether it is currently pressed, as follows:</p>
<pre class="qml">
<span class="type">ParticleFlame</span> {
<span class="name">color</span>: <span class="string">"red"</span>
<span class="name">emitterX</span>: <span class="name">touch1</span>.<span class="name">x</span>
<span class="name">emitterY</span>: <span class="name">touch1</span>.<span class="name">y</span>
<span class="name">emitting</span>: <span class="name">touch1</span>.<span class="name">pressed</span>
}
</pre>
<a name="bear-whack-example"></a>
<h4 id="bear-whack-example">Bear-Whack Example</h4>
<p><i>Bear-Whack</i> demonstrates using <a href="qml-qtquick-multipointtoucharea.html">MultiPointTouchArea</a> to add multiple finger support to a simple game. The interaction with the game is done through a <a href="qml-qtquick-particles-spritegoal.html">SpriteGoal</a> that follows the <a href="qml-qtquick-touchpoint.html">TouchPoint</a>. The TouchPoints added to the <a href="qml-qtquick-multipointtoucharea.html">MultiPointTouchArea</a> are a component with the relevant logic embedded into it:</p>
<pre class="qml">
<span class="type"><a href="qml-qtquick-touchpoint.html">TouchPoint</a></span> {
<span class="name">id</span>: <span class="name">container</span>
property <span class="type"><a href="qml-qtquick-particles-particlesystem.html">ParticleSystem</a></span> <span class="name">system</span>
<span class="name">onPressedChanged</span>: {
<span class="keyword">if</span> (<span class="name">pressed</span>) {
<span class="name">timer</span>.<span class="name">restart</span>();
<span class="name">child</span>.<span class="name">enabled</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">system</span>.<span class="name">explode</span>(<span class="name">x</span>,<span class="name">y</span>);
}
}
property <span class="type"><a href="../qtqml/qml-qtqml-qtobject.html">QtObject</a></span> <span class="name">obj</span>: <span class="name">Timer</span> {
<span class="name">id</span>: <span class="name">timer</span>
<span class="name">interval</span>: <span class="number">100</span>
<span class="name">running</span>: <span class="number">false</span>
<span class="name">repeat</span>: <span class="number">false</span>
<span class="name">onTriggered</span>: <span class="name">container</span>.<span class="name">child</span>.<span class="name">enabled</span> <span class="operator">=</span> <span class="number">false</span>
}
property <span class="type"><a href="qml-qtquick-item.html">Item</a></span> <span class="name">child</span>: <span class="name">SpriteGoal</span> {
<span class="name">enabled</span>: <span class="number">false</span>
<span class="name">x</span>: <span class="name">container</span>.<span class="name">area</span>.<span class="name">x</span> <span class="operator">-</span> <span class="number">16</span>
<span class="name">y</span>: <span class="name">container</span>.<span class="name">area</span>.<span class="name">y</span> <span class="operator">-</span> <span class="number">16</span>
<span class="name">width</span>: <span class="name">container</span>.<span class="name">area</span>.<span class="name">width</span> <span class="operator">+</span> <span class="number">32</span>
<span class="name">height</span>: <span class="name">container</span>.<span class="name">area</span>.<span class="name">height</span> <span class="operator">+</span> <span class="number">32</span> <span class="comment">//+32 so it doesn't have to hit the exact center</span>
<span class="name">system</span>: <span class="name">container</span>.<span class="name">system</span>
<span class="name">parent</span>: <span class="name">container</span>.<span class="name">system</span>
<span class="name">goalState</span>: <span class="string">"falling"</span>
}
}
</pre>
<a name="flick-resize-example"></a>
<h4 id="flick-resize-example">Flick Resize Example</h4>
<p><i>Flick Resize</i> uses a <a href="qml-qtquick-pincharea.html">PinchArea</a> to implement a <i>pinch-to-resize</i> behavior. This is easily achieved by listening to the <a href="qml-qtquick-pincharea.html">PinchArea</a> signals and responding to user input.</p>
<pre class="qml">
<span class="name">onPinchStarted</span>: {
<span class="name">initialWidth</span> <span class="operator">=</span> <span class="name">flick</span>.<span class="name">contentWidth</span>
<span class="name">initialHeight</span> <span class="operator">=</span> <span class="name">flick</span>.<span class="name">contentHeight</span>
}
<span class="name">onPinchUpdated</span>: {
<span class="comment">// adjust content pos due to drag</span>
<span class="name">flick</span>.<span class="name">contentX</span> <span class="operator">+=</span> <span class="name">pinch</span>.<span class="name">previousCenter</span>.<span class="name">x</span> <span class="operator">-</span> <span class="name">pinch</span>.<span class="name">center</span>.<span class="name">x</span>
<span class="name">flick</span>.<span class="name">contentY</span> <span class="operator">+=</span> <span class="name">pinch</span>.<span class="name">previousCenter</span>.<span class="name">y</span> <span class="operator">-</span> <span class="name">pinch</span>.<span class="name">center</span>.<span class="name">y</span>
<span class="comment">// resize content</span>
<span class="name">flick</span>.<span class="name">resizeContent</span>(<span class="name">initialWidth</span> <span class="operator">*</span> <span class="name">pinch</span>.<span class="name">scale</span>, <span class="name">initialHeight</span> <span class="operator">*</span> <span class="name">pinch</span>.<span class="name">scale</span>, <span class="name">pinch</span>.<span class="name">center</span>)
}
<span class="name">onPinchFinished</span>: {
<span class="comment">// Move its content within bounds.</span>
<span class="name">flick</span>.<span class="name">returnToBounds</span>()
}
</pre>
<a name="flickable-example"></a>
<h4 id="flickable-example">Flickable Example</h4>
<p><i>Flickable</i> is a simple example demonstrating the <a href="qml-qtquick-flickable.html">Flickable</a> type.</p>
<pre class="qml">
<span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
<span class="name">width</span>: <span class="number">320</span>
<span class="name">height</span>: <span class="number">480</span>
<span class="type"><a href="qml-qtquick-flickable.html">Flickable</a></span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">contentWidth</span>: <span class="number">1200</span>
<span class="name">contentHeight</span>: <span class="number">1200</span>
<span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
<span class="name">width</span>: <span class="number">1000</span>
<span class="name">height</span>: <span class="number">1000</span>
</pre>
<a name="corkboards-example"></a>
<h4 id="corkboards-example">Corkboards Example</h4>
<p><i>Corkboards</i> shows another use for <a href="qml-qtquick-flickable.html">Flickable</a>, with QML types within the flickable object that respond to mouse and keyboard interaction. This behavior does not require special code as the Qt Quick types already cooperate with the Flickable type for accepting touch events.</p>
<p><a href="https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quick/touchinteraction?h=5.15">Example project @ code.qt.io</a></p>
</div>
<!-- @@@touchinteraction -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2020 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>