<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- picturetransfer.qdoc --> <title>QML Bluetooth Picture Push Example | Qt Bluetooth 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="qtbluetooth-index.html">Qt Bluetooth</a></td><td >QML Bluetooth Picture Push Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtbluetooth-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="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#device-discovery">Device Discovery</a></li> <li class="level1"><a href="#file-selection">File Selection</a></li> <li class="level1"><a href="#root-element">Root Element</a></li> <li class="level1"><a href="#file-transfer">File Transfer</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QML Bluetooth Picture Push Example</h1> <span class="subtitle"></span> <!-- $$$picturetransfer-brief --> <p>An example showing the use Bluetooth Object Push Profile (OPP).</p> <!-- @@@picturetransfer --> <!-- $$$picturetransfer-description --> <div class="descr"> <a name="details"></a> <p>The Bluetooth Picture Push example shows how to use the <a href="qbluetoothtransfermanager.html">QBluetoothTransferManager</a> API. The example transfers a local image to a remote device. Unfortunately this example cannot be used on Android as Qt does not support the Object Push Profile (OPP) on this platform.</p> <p>On the first user interface page the application scans for remote Bluetooth devices. The user must select the appropriate device to continue:</p> <p class="centerAlign"><img src="images/opp-example-1.png" alt="" /></p><p>The next page presents a list of image files on the device. The files must be located under QStandardPaths::PicturesLocation}:</p> <p class="centerAlign"><img src="images/opp-example-2.png" alt="" /></p><p>Once the picture was selected the UI shows the progress of the file transfer:</p> <p class="centerAlign"><img src="images/opp-example-3.png" alt="" /></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="device-discovery"></a> <h2 id="device-discovery">Device Discovery</h2> <p>The device discovery uses the <a href="qml-qtbluetooth-bluetoothdiscoverymodel.html">BluetoothDiscoveryModel</a> to detect the remote devices. Each discovery is displayed as an entry in a list. Once a device was selected the device address is stored in the <code>root</code> element. More details about the <code>root</code> element will follow further below.</p> <pre class="qml"> ListView { model: BluetoothDiscoveryModel { discoveryMode: BluetoothDiscoveryModel.DeviceDiscovery onErrorChanged: { if (error == BluetoothDiscoveryModel.NoError) return; if (error == BluetoothDiscoveryModel.PoweredOffError) titleLabel.text = "Bluetooth turned off"; else titleLabel.text = "Cannot find devices"; } } delegate: Button { width: listView.width + 2 text: model.name onClicked: root.remoteDevice = model.remoteAddress } } </pre> <a name="file-selection"></a> <h2 id="file-selection">File Selection</h2> <p>The file is selected with the help of FolderListModel. Once again the selected file is stored in the <code>root</code> element:</p> <pre class="qml"> model: FolderListModel { folder: "file://"+SystemPictureFolder showDirs: false } delegate: Rectangle { Text { text: model.fileName } MouseArea { id: mArea anchors.fill: parent onClicked: { print ("path: " + model.filePath + " " + model.fileName) root.fileName = model.filePath } } } </pre> <a name="root-element"></a> <h2 id="root-element">Root Element</h2> <p>The already mentioned <code>root</code> element collects the necessary pieces of data for the picture transfer. Once the file name has been set it triggers the file transfer:</p> <pre class="qml"> Image { id: root property string remoteDevice; property string fileName; onFileNameChanged: { fileTransfer.initTransfer(remoteDevice, fileName); loader.source = "FileSending.qml" } onFileNameChanged: { fileTransfer.initTransfer(remoteDevice, fileName); loader.source = "FileSending.qml" } </pre> <a name="file-transfer"></a> <h2 id="file-transfer">File Transfer</h2> <p>The file transfer is implemented in C++:</p> <pre class="cpp"> <span class="type">void</span> FileTransfer<span class="operator">::</span>initTransfer(<span class="type">QString</span> address<span class="operator">,</span> <span class="type">QString</span> fileName) { qDebug() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Begin sharing file: "</span> <span class="operator"><</span><span class="operator"><</span> address <span class="operator"><</span><span class="operator"><</span> fileName; <span class="type"><a href="qbluetoothaddress.html">QBluetoothAddress</a></span> btAddress <span class="operator">=</span> <span class="type"><a href="qbluetoothaddress.html">QBluetoothAddress</a></span>(address); <span class="type"><a href="qbluetoothtransferrequest.html">QBluetoothTransferRequest</a></span> request(btAddress); <span class="type">QFile</span> <span class="operator">*</span>file <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QFile</span>(fileName); reply <span class="operator">=</span> manager<span class="operator">.</span>put(request<span class="operator">,</span> file); connect(reply<span class="operator">,</span> SIGNAL(transferProgress(<span class="type">qint64</span><span class="operator">,</span><span class="type">qint64</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(updateProgress(<span class="type">qint64</span><span class="operator">,</span><span class="type">qint64</span>))); } </pre> <p>and exposed to QML via a context property:</p> <pre class="cpp"> <span class="type">QQuickView</span> view; FileTransfer fileTransfer; view<span class="operator">.</span>rootContext()<span class="operator">-</span><span class="operator">></span>setContextProperty(<span class="string">"fileTransfer"</span><span class="operator">,</span> <span class="type">QVariant</span><span class="operator">::</span>fromValue(<span class="operator">&</span>fileTransfer)); </pre> <p>Files:</p> <ul> <li><a href="qtbluetooth-picturetransfer-button-qml.html">picturetransfer/Button.qml</a></li> <li><a href="qtbluetooth-picturetransfer-devicediscovery-qml.html">picturetransfer/DeviceDiscovery.qml</a></li> <li><a href="qtbluetooth-picturetransfer-filesending-qml.html">picturetransfer/FileSending.qml</a></li> <li><a href="qtbluetooth-picturetransfer-pictureselector-qml.html">picturetransfer/PictureSelector.qml</a></li> <li><a href="qtbluetooth-picturetransfer-bttransfer-qml.html">picturetransfer/bttransfer.qml</a></li> <li><a href="qtbluetooth-picturetransfer-filetransfer-cpp.html">picturetransfer/filetransfer.cpp</a></li> <li><a href="qtbluetooth-picturetransfer-filetransfer-h.html">picturetransfer/filetransfer.h</a></li> <li><a href="qtbluetooth-picturetransfer-main-cpp.html">picturetransfer/main.cpp</a></li> <li><a href="qtbluetooth-picturetransfer-picturetransfer-pro.html">picturetransfer/picturetransfer.pro</a></li> <li><a href="qtbluetooth-picturetransfer-qmltransfer-qrc.html">picturetransfer/qmltransfer.qrc</a></li> </ul> <p>Images:</p> <ul> <li><a href="images/used-in-examples/picturetransfer/background.png">picturetransfer/background.png</a></li> <li><a href="images/used-in-examples/picturetransfer/icon.png">picturetransfer/icon.png</a></li> </ul> </div> <p><b>See also </b><a href="qtbluetooth-index.html">Qt Bluetooth</a>.</p> <!-- @@@picturetransfer --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</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>