<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- annotatedurl.qdoc --> <title>Annotated URL Example | Qt NFC 5.12.2</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="qtnfc-index.html">Qt NFC</a></td><td >Annotated URL Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtnfc-index.html">Qt 5.12.2 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="#annotatedurl-class-definition">AnnotatedUrl Class Definition</a></li> <li class="level1"><a href="#annotatedurl-handler-implementation">AnnotatedUrl Handler Implementation</a></li> <li class="level1"><a href="#running-the-example">Running the Example</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Annotated URL Example</h1> <span class="subtitle"></span> <!-- $$$annotatedurl-brief --> <p>An example showing reading from formatted NFC Data Exchange Format (NDEF) messages.</p> <!-- @@@annotatedurl --> <!-- $$$annotatedurl-description --> <div class="descr"> <a name="details"></a> <p>The Annotated URL example displays the contents of specifically formatted NFC Data Exchange Format (NDEF) messages read from an NFC Tag. The NDEF message should contain a URI record, an optional <code>image/*</code> MIME record, and one or more localized Text records.</p> <p>This is the initial state of the example:</p> <p class="centerAlign"><img src="images/annotatedurl.png" alt="" /></p><p>In this example the NFC Tag used contains a text record. The UI gets updated accordingly to:</p> <p class="centerAlign"><img src="images/annotatedurl2.png" alt="" /></p><a name="annotatedurl-class-definition"></a> <h2 id="annotatedurl-class-definition">AnnotatedUrl Class Definition</h2> <p>The <code>AnnotatedUrl</code> class wraps the <code>QNearFieldManager</code>, the class providing the NFC Tag detection functionality. NDEF messages are read by the <a href="qnearfieldmanager.html">QNearFieldManager</a> and forwarded to a handler contained in the <code>AnnotatedUrl</code> class. After parsing the NDEF message the class emits the signal AnnotatedUrl::annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap). The UI reacts to the signal displaying the contents of the NDEF message.</p> <pre class="cpp"> <span class="keyword">class</span> AnnotatedUrl : <span class="keyword">public</span> <span class="type">QObject</span> { Q_OBJECT <span class="keyword">public</span>: <span class="keyword">explicit</span> AnnotatedUrl(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="operator">~</span>AnnotatedUrl(); <span class="keyword">signals</span>: <span class="type">void</span> annotatedUrl(<span class="keyword">const</span> <span class="type">QUrl</span> <span class="operator">&</span>url<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>title<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QPixmap</span> <span class="operator">&</span>pixmap); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> targetDetected(<span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span> <span class="operator">*</span>target); <span class="type">void</span> targetLost(<span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span> <span class="operator">*</span>target); <span class="type">void</span> handleMessage(<span class="keyword">const</span> <span class="type"><a href="qndefmessage.html">QNdefMessage</a></span> <span class="operator">&</span>message<span class="operator">,</span> <span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span> <span class="operator">*</span>target); <span class="type">void</span> handlePolledNdefMessage(<span class="type"><a href="qndefmessage.html">QNdefMessage</a></span> message); <span class="keyword">private</span>: <span class="type"><a href="qnearfieldmanager.html">QNearFieldManager</a></span> <span class="operator">*</span>manager; }; </pre> <a name="annotatedurl-handler-implementation"></a> <h2 id="annotatedurl-handler-implementation">AnnotatedUrl Handler Implementation</h2> <p>NFC messages read by the <code>QNearFieldManager</code> are forwarded to AnnotatedUrl::handleMessage. The callback signature details can be read in <a href="qnearfieldmanager.html#registerNdefMessageHandler">QNearFieldManager::registerNdefMessageHandler</a>.</p> <pre class="cpp"> <span class="type">void</span> AnnotatedUrl<span class="operator">::</span>handleMessage(<span class="keyword">const</span> <span class="type"><a href="qndefmessage.html">QNdefMessage</a></span> <span class="operator">&</span>message<span class="operator">,</span> <span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span> <span class="operator">*</span>target) { </pre> <p>Because NFC messages are composed of several NDEF records, looping through all of the records allows the extraction of the 3 parameters to be displayed in the UI: the Uri, the Title and the Pixmap:</p> <pre class="cpp"> <span class="keyword">for</span> (<span class="keyword">const</span> <span class="type"><a href="qndefrecord.html">QNdefRecord</a></span> <span class="operator">&</span>record : message) { <span class="keyword">if</span> (record<span class="operator">.</span>isRecordType<span class="operator"><</span><span class="type"><a href="qndefnfctextrecord.html">QNdefNfcTextRecord</a></span><span class="operator">></span>()) { <span class="type"><a href="qndefnfctextrecord.html">QNdefNfcTextRecord</a></span> textRecord(record); title <span class="operator">=</span> textRecord<span class="operator">.</span>text(); <span class="type">QLocale</span> locale(textRecord<span class="operator">.</span>locale()); } <span class="keyword">else</span> <span class="keyword">if</span> (record<span class="operator">.</span>isRecordType<span class="operator"><</span><span class="type"><a href="qndefnfcurirecord.html">QNdefNfcUriRecord</a></span><span class="operator">></span>()) { <span class="type"><a href="qndefnfcurirecord.html">QNdefNfcUriRecord</a></span> uriRecord(record); url <span class="operator">=</span> uriRecord<span class="operator">.</span>uri(); } <span class="keyword">else</span> <span class="keyword">if</span> (record<span class="operator">.</span>typeNameFormat() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qndefrecord.html">QNdefRecord</a></span><span class="operator">::</span>Mime <span class="operator">&</span><span class="operator">&</span> record<span class="operator">.</span>type()<span class="operator">.</span>startsWith(<span class="string">"image/"</span>)) { pixmap <span class="operator">=</span> <span class="type">QPixmap</span><span class="operator">::</span>fromImage(<span class="type">QImage</span><span class="operator">::</span>fromData(record<span class="operator">.</span>payload())); } </pre> <p>Finally after having extracted the parameters of the NFC message the corresponding signal is emitted so that the UI can handle it.</p> <pre class="cpp"> } <span class="keyword">emit</span> annotatedUrl(url<span class="operator">,</span> title<span class="operator">,</span> pixmap); } </pre> <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> <p>Files:</p> <ul> <li><a href="qtnfc-annotatedurl-annotatedurl-cpp.html">annotatedurl/annotatedurl.cpp</a></li> <li><a href="qtnfc-annotatedurl-annotatedurl-h.html">annotatedurl/annotatedurl.h</a></li> <li><a href="qtnfc-annotatedurl-annotatedurl-pro.html">annotatedurl/annotatedurl.pro</a></li> <li><a href="qtnfc-annotatedurl-main-cpp.html">annotatedurl/main.cpp</a></li> <li><a href="qtnfc-annotatedurl-mainwindow-cpp.html">annotatedurl/mainwindow.cpp</a></li> <li><a href="qtnfc-annotatedurl-mainwindow-h.html">annotatedurl/mainwindow.h</a></li> <li><a href="qtnfc-annotatedurl-mainwindow-ui.html">annotatedurl/mainwindow.ui</a></li> </ul> </div> <p><b>See also </b><a href="qtnfc-index.html">Qt NFC</a>.</p> <!-- @@@annotatedurl --> </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>