<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- photosurface.qdoc --> <title>Qt Quick Demo - Photo Surface | Qt Quick 5.9</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="qtquick-index.html">Qt Quick</a></td><td >Qt Quick Demo - Photo Surface</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="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#creating-the-main-window">Creating the Main Window</a></li> <li class="level1"><a href="#accessing-folder-contents">Accessing Folder Contents</a></li> <li class="level1"><a href="#displaying-images-on-the-photo-surface">Displaying Images on the Photo Surface</a></li> <li class="level1"><a href="#handling-pinch-gestures">Handling Pinch Gestures</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt Quick Demo - Photo Surface</h1> <span class="subtitle"></span> <!-- $$$demos/photosurface-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/qtquick-demo-photosurface-small.png" alt="" /></p><p><i>Photo Surface</i> demonstrates how to use a <a href="qml-qtquick-repeater.html">Repeater</a> with a <a href="qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> and a FileDialog to access images from a folder selected by a user and how to handle dragging, rotation and pinch zooming within the same item using a <a href="qml-qtquick-pincharea.html">PinchArea</a> that contains a <a href="qml-qtquick-mousearea.html">MouseArea</a>.</p> <p>All the app code is contained in one QML file, photosurface.qml. Inline JavaScript code is used to place, rotate, and scale images on the photo surface.</p> <a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p> <a name="creating-the-main-window"></a> <h2 id="creating-the-main-window">Creating the Main Window</h2> <p>To create the main window for the Photo Surface app, we use the <a href="qml-qtquick-window-window.html">Window</a> QML type as the root item. It automatically sets up the window for use with <a href="qtquick-index.html">Qt Quick</a> graphical types:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-window-window.html">Window</a></span> { <span class="name">id</span>: <span class="name">root</span> <span class="name">visible</span>: <span class="number">true</span> <span class="name">width</span>: <span class="number">1024</span>; <span class="name">height</span>: <span class="number">600</span> <span class="name">color</span>: <span class="string">"black"</span> property <span class="type">int</span> <span class="name">highestZ</span>: <span class="number">0</span> property <span class="type">real</span> <span class="name">defaultSize</span>: <span class="number">200</span> property <span class="type">var</span> <span class="name">currentFrame</span>: <span class="name">undefined</span> </pre> <p>To use the <a href="qml-qtquick-window-window.html">Window</a> type, we must import it:</p> <pre class="cpp"> import <span class="type"><a href="qtquick-module.html">QtQuick</a></span><span class="operator">.</span>Window <span class="number">2.1</span> </pre> <a name="accessing-folder-contents"></a> <h2 id="accessing-folder-contents">Accessing Folder Contents</h2> <p>We use a <a href="qml-qtquick-repeater.html">Repeater</a> QML type together with the <a href="qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> to display GIF, JPG, and PNG images located in a folder:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-repeater.html">Repeater</a></span> { <span class="name">model</span>: <span class="name">FolderListModel</span> { <span class="name">id</span>: <span class="name">folderModel</span> <span class="name">objectName</span>: <span class="string">"folderModel"</span> <span class="name">showDirs</span>: <span class="number">false</span> <span class="name">nameFilters</span>: <span class="name">imageNameFilters</span> } </pre> <p>To use the <a href="qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> type, we must import it:</p> <pre class="cpp"> import <span class="type">Qt</span><span class="operator">.</span>labs<span class="operator">.</span>folderlistmodel <span class="number">1.0</span> </pre> <p>We use a FileDialog to enable users to select the folder that contains the images:</p> <pre class="qml"> <span class="type">FileDialog</span> { <span class="name">id</span>: <span class="name">fileDialog</span> <span class="name">title</span>: <span class="string">"Choose a folder with some images"</span> <span class="name">selectFolder</span>: <span class="number">true</span> <span class="name">folder</span>: <span class="name">picturesLocation</span> <span class="name">onAccepted</span>: <span class="name">folderModel</span>.<span class="name">folder</span> <span class="operator">=</span> <span class="name">fileUrl</span> <span class="operator">+</span> <span class="string">"/"</span> } </pre> <p>To use the FileDialog type, we must import Qt Quick Dialogs:</p> <pre class="cpp"> import <span class="type"><a href="qtquick-module.html">QtQuick</a></span><span class="operator">.</span>Dialogs <span class="number">1.0</span> </pre> <p>We use the <code>fileDialog.open()</code> function to open the file dialog when the app starts:</p> <pre class="cpp"> Component<span class="operator">.</span>onCompleted: fileDialog<span class="operator">.</span>open() </pre> <p>Users can also click the file dialog icon to open the file dialog. We use an <a href="qtquick-imageelements-example.html#image">Image</a> QML type to display the icon. Inside the <a href="qtquick-imageelements-example.html#image">Image</a> type, we use a <a href="qml-qtquick-mousearea.html">MouseArea</a> with the <code>onClicked</code> signal handler to call the <code>fileDialog.open()</code> function:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">anchors</span>.top: <span class="name">parent</span>.<span class="name">top</span> <span class="name">anchors</span>.left: <span class="name">parent</span>.<span class="name">left</span> <span class="name">anchors</span>.margins: <span class="number">10</span> <span class="name">source</span>: <span class="string">"resources/folder.png"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">anchors</span>.margins: -<span class="number">10</span> <span class="name">onClicked</span>: <span class="name">fileDialog</span>.<span class="name">open</span>() <span class="name">hoverEnabled</span>: <span class="number">true</span> <span class="name">onPositionChanged</span>: { <span class="name">tooltip</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">false</span> <span class="name">hoverTimer</span>.<span class="name">start</span>() } <span class="name">onExited</span>: { <span class="name">tooltip</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">false</span> <span class="name">hoverTimer</span>.<span class="name">stop</span>() } </pre> <a name="displaying-images-on-the-photo-surface"></a> <h2 id="displaying-images-on-the-photo-surface">Displaying Images on the Photo Surface</h2> <p>We use a <a href="qml-qtquick-rectangle.html">Rectangle</a> as a delegate for a <a href="qml-qtquick-repeater.html">Repeater</a> to provide a frame for each image that the <a href="qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> finds in the selected folder. We use JavaScript <code>Math()</code> methods to place the frames randomly on the photo surface and to rotate them at random angles, as well as to scale the images:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">photoFrame</span> <span class="name">width</span>: <span class="name">image</span>.<span class="name">width</span> <span class="operator">*</span> (<span class="number">1</span> <span class="operator">+</span> <span class="number">0.10</span> <span class="operator">*</span> <span class="name">image</span>.<span class="name">height</span> <span class="operator">/</span> <span class="name">image</span>.<span class="name">width</span>) <span class="name">height</span>: <span class="name">image</span>.<span class="name">height</span> <span class="operator">*</span> <span class="number">1.10</span> <span class="name">scale</span>: <span class="name">defaultSize</span> <span class="operator">/</span> <span class="name">Math</span>.<span class="name">max</span>(<span class="name">image</span>.<span class="name">sourceSize</span>.<span class="name">width</span>, <span class="name">image</span>.<span class="name">sourceSize</span>.<span class="name">height</span>) Behavior on <span class="name">scale</span> { <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">200</span> } } Behavior on <span class="name">x</span> { <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">200</span> } } Behavior on <span class="name">y</span> { <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">200</span> } } <span class="name">border</span>.color: <span class="string">"black"</span> <span class="name">border</span>.width: <span class="number">2</span> <span class="name">smooth</span>: <span class="number">true</span> <span class="name">antialiasing</span>: <span class="number">true</span> <span class="name">Component</span>.onCompleted: { <span class="name">x</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">root</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">width</span> <span class="operator">/</span> <span class="number">2</span> <span class="name">y</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">root</span>.<span class="name">height</span> <span class="operator">-</span> <span class="name">height</span> <span class="operator">/</span> <span class="number">2</span> <span class="name">rotation</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">13</span> <span class="operator">-</span> <span class="number">6</span> } </pre> <a name="handling-pinch-gestures"></a> <h2 id="handling-pinch-gestures">Handling Pinch Gestures</h2> <p>We use a <a href="qml-qtquick-pincharea.html">PinchArea</a> that contains a <a href="qml-qtquick-mousearea.html">MouseArea</a> in the photo frames to handle dragging, rotation and pinch zooming of the frame:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-pincharea.html">PinchArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">pinch</span>.target: <span class="name">photoFrame</span> <span class="name">pinch</span>.minimumRotation: -<span class="number">360</span> <span class="name">pinch</span>.maximumRotation: <span class="number">360</span> <span class="name">pinch</span>.minimumScale: <span class="number">0.1</span> <span class="name">pinch</span>.maximumScale: <span class="number">10</span> <span class="name">pinch</span>.dragAxis: <span class="name">Pinch</span>.<span class="name">XAndYAxis</span> <span class="name">onPinchStarted</span>: <span class="name">setFrameColor</span>(); </pre> <p>We use the <code>pinch</code> group property to control how the photo frames react to pinch gestures. The <code>pinch.target</code> sets <code>photoFrame</code> as the item to manipulate. The rotation properties specify that the frames can be rotated at all angles and the scale properties specify that they can be scaled between <code>0.1</code> and <code>10</code>.</p> <p>In the <a href="qml-qtquick-mousearea.html">MouseArea</a>'s <code>onPressed</code> signal handler, we raise the selected photo frame to the top by increasing the value of its <code>z</code> property. The root item stores the z value of the top-most frame. The border color of the photo frame is controlled in the <code>onEntered</code> signal handler to highlight the selected image:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">id</span>: <span class="name">dragArea</span> <span class="name">hoverEnabled</span>: <span class="number">true</span> <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">drag</span>.target: <span class="name">photoFrame</span> <span class="name">scrollGestureEnabled</span>: <span class="number">false</span> <span class="comment">// 2-finger-flick gesture should pass through to the Flickable</span> <span class="name">onPressed</span>: { <span class="name">photoFrame</span>.<span class="name">z</span> <span class="operator">=</span> ++<span class="name">root</span>.<span class="name">highestZ</span>; <span class="name">parent</span>.<span class="name">setFrameColor</span>(); } <span class="name">onEntered</span>: <span class="name">parent</span>.<span class="name">setFrameColor</span>(); </pre> <p>To enable you to test the example on the desktop, we use the <a href="qml-qtquick-mousearea.html">MouseArea</a>'s <code>onWheel</code> signal handler to simulate pinch gestures by using a mouse:</p> <pre class="qml"> <span class="name">onWheel</span>: { <span class="keyword">if</span> (<span class="name">wheel</span>.<span class="name">modifiers</span> <span class="operator">&</span> <span class="name">Qt</span>.<span class="name">ControlModifier</span>) { <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">+=</span> <span class="name">wheel</span>.<span class="name">angleDelta</span>.<span class="name">y</span> <span class="operator">/</span> <span class="number">120</span> <span class="operator">*</span> <span class="number">5</span>; <span class="keyword">if</span> (<span class="name">Math</span>.<span class="name">abs</span>(<span class="name">photoFrame</span>.<span class="name">rotation</span>) <span class="operator"><</span> <span class="number">4</span>) <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="number">0</span>; } <span class="keyword">else</span> { <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">+=</span> <span class="name">wheel</span>.<span class="name">angleDelta</span>.<span class="name">x</span> <span class="operator">/</span> <span class="number">120</span>; <span class="keyword">if</span> (<span class="name">Math</span>.<span class="name">abs</span>(<span class="name">photoFrame</span>.<span class="name">rotation</span>) <span class="operator"><</span> <span class="number">0.6</span>) <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="number">0</span>; var <span class="name">scaleBefore</span> = <span class="name">photoFrame</span>.<span class="name">scale</span>; <span class="name">photoFrame</span>.<span class="name">scale</span> <span class="operator">+=</span> <span class="name">photoFrame</span>.<span class="name">scale</span> <span class="operator">*</span> <span class="name">wheel</span>.<span class="name">angleDelta</span>.<span class="name">y</span> <span class="operator">/</span> <span class="number">120</span> <span class="operator">/</span> <span class="number">10</span>; } } </pre> <p>The <code>onWheel</code> signal handler is called in response to mouse wheel gestures. Use the vertical wheel to zoom and Ctrl and the vertical wheel to rotate frames. If the mouse has a horizontal wheel, use it to rotate frames.</p> <p>Files:</p> <ul> <li><a href="qtquick-demos-photosurface-photosurface-qml.html">demos/photosurface/photosurface.qml</a></li> <li><a href="qtquick-demos-photosurface-main-cpp.html">demos/photosurface/main.cpp</a></li> <li><a href="qtquick-demos-photosurface-photosurface-pro.html">demos/photosurface/photosurface.pro</a></li> <li><a href="qtquick-demos-photosurface-photosurface-qmlproject.html">demos/photosurface/photosurface.qmlproject</a></li> <li><a href="qtquick-demos-photosurface-photosurface-qrc.html">demos/photosurface/photosurface.qrc</a></li> </ul> </div> <p><b>See also </b>QML Applications.</p> <!-- @@@demos/photosurface --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</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>