qtnfc-ndefeditor-example.html 16.2 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- ndefeditor.qdoc -->
  <title>NDEF Editor Example | Qt NFC 5.11</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 ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtnfc-index.html">Qt NFC</a></td><td >NDEF Editor Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 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="#nfc-tag-detection">NFC Tag detection</a></li>
<li class="level1"><a href="#record-creation">Record creation</a></li>
<li class="level1"><a href="#record-editing-classes">Record editing classes</a></li>
<li class="level2"><a href="#textrecordeditor">TextRecordEditor</a></li>
<li class="level2"><a href="#urirecordeditor">UriRecordEditor</a></li>
<li class="level2"><a href="#mimeimagerecordeditor">MimeImageRecordEditor</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">NDEF Editor Example</h1>
<span class="subtitle"></span>
<!-- $$$ndefeditor-brief -->
<p>An example about reading and writing NFC Data Exchange Format (NDEF) messages to NFC Forum Tags.</p>
<!-- @@@ndefeditor -->
<!-- $$$ndefeditor-description -->
<div class="descr"> <a name="details"></a>
<p>The NDEF Editor example reads and writes NFC Data Exchange Format (NDEF) messages to NFC Forum Tags. NDEF messages can be composed by adding records of supported types. Additionally, NDEF messages can be loaded/saved from/into a file located in the file system of the machine where the application is running.</p>
<p class="centerAlign"><img src="images/ndefeditor.png" alt="" /></p><a name="nfc-tag-detection"></a>
<h2 id="nfc-tag-detection">NFC Tag detection</h2>
<p>The MainWindow class is able to detect if a NFC Tag is in the range for read/write operations. It can also detect if connectivity has been lost. This is achieved by connecting the MainWindow class private handlers to the signals <a href="qnearfieldmanager.html#targetDetected">QNearFieldManager::targetDetected</a> and <a href="qnearfieldmanager.html#targetLost">QNearFieldManager::targetLost</a>.</p>
<pre class="cpp">

  m_manager <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qnearfieldmanager.html">QNearFieldManager</a></span>(<span class="keyword">this</span>);
  connect(m_manager<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qnearfieldmanager.html">QNearFieldManager</a></span><span class="operator">::</span>targetDetected<span class="operator">,</span>
          <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>targetDetected);
  connect(m_manager<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qnearfieldmanager.html">QNearFieldManager</a></span><span class="operator">::</span>targetLost<span class="operator">,</span>
          <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>targetLost);

</pre>
<p>Through the UI a user requests when to start the detection of a NFC Tag by calling the method <a href="qnearfieldmanager.html#startTargetDetection">QNearFieldManager::startTargetDetection</a>.</p>
<pre class="cpp">

  m_manager<span class="operator">-</span><span class="operator">&gt;</span>startTargetDetection();

</pre>
<p>Once the target is detected the MainWindow connects the following signals to its internal private slots: <a href="qnearfieldtarget.html#ndefMessageRead">QNearFieldTarget::ndefMessageRead</a>, <a href="qnearfieldtarget.html#Error-enum">QNearFieldTarget::NdefReadError</a>, <a href="qnearfieldtarget.html#ndefMessagesWritten">QNearFieldTarget::ndefMessagesWritten</a>, <a href="qnearfieldtarget.html#Error-enum">QNearFieldTarget::NdefWriteError</a> and <a href="qnearfieldtarget.html#error">QNearFieldTarget::error</a></p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>targetDetected(<span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span> <span class="operator">*</span>target)
  {
      <span class="keyword">switch</span> (m_touchAction) {
      <span class="keyword">case</span> NoAction:
          <span class="keyword">break</span>;
      <span class="keyword">case</span> ReadNdef:
          connect(target<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span><span class="operator">::</span>ndefMessageRead<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>ndefMessageRead);
          connect(target<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span><span class="operator">::</span>error<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>targetError);

          m_request <span class="operator">=</span> target<span class="operator">-</span><span class="operator">&gt;</span>readNdefMessages();
          <span class="keyword">if</span> (<span class="operator">!</span>m_request<span class="operator">.</span>isValid()) <span class="comment">// cannot read messages</span>
              targetError(<span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span><span class="operator">::</span>NdefReadError<span class="operator">,</span> m_request);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> WriteNdef:
          connect(target<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span><span class="operator">::</span>ndefMessagesWritten<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>ndefMessageWritten);
          connect(target<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span><span class="operator">::</span>error<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>targetError);

          m_request <span class="operator">=</span> target<span class="operator">-</span><span class="operator">&gt;</span>writeNdefMessages(<span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qndefmessage.html">QNdefMessage</a></span><span class="operator">&gt;</span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> ndefMessage());
          <span class="keyword">if</span> (<span class="operator">!</span>m_request<span class="operator">.</span>isValid()) <span class="comment">// cannot write messages</span>
              targetError(<span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span><span class="operator">::</span>NdefWriteError<span class="operator">,</span> m_request);
          <span class="keyword">break</span>;
      }
  }

</pre>
<p>If during the process of reading or writing to a NFC Tag the connection is lost, the MainWindow reacts to this event by scheduling the target deletion (<a href="../qtcore/qobject.html#deleteLater">QObject::deleteLater</a>).</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>targetLost(<span class="type"><a href="qnearfieldtarget.html">QNearFieldTarget</a></span> <span class="operator">*</span>target)
  {
      target<span class="operator">-</span><span class="operator">&gt;</span>deleteLater();
  }

</pre>
<a name="record-creation"></a>
<h2 id="record-creation">Record creation</h2>
<p>The main window of the ndefeditor example manages the composition and creation of NFC records. The UI contains a QScrollArea where RecordEditors are added dynamically on a user requests basis. The following methods of the MainWindow class provide an interface towards each of the record editing classes managing the different types of records.</p>
<pre class="cpp">

  <span class="type">void</span> addNfcTextRecord();
  <span class="type">void</span> addNfcUriRecord();
  <span class="type">void</span> addMimeImageRecord();
  <span class="type">void</span> addEmptyRecord();

</pre>
<p>The following sections explain each of the record editing classes.</p>
<a name="record-editing-classes"></a>
<h2 id="record-editing-classes">Record editing classes</h2>
<a name="textrecordeditor"></a>
<h3 >TextRecordEditor</h3>
<p>The <a href="qtnfc-ndefeditor-example.html#textrecordeditor">TextRecordEditor</a> is a QWidget that can handle editing the values of text record that has been requested by the user. For each text record, there is a new instance of this class.</p>
<pre class="cpp">

  <span class="keyword">class</span> TextRecordEditor : <span class="keyword">public</span> <span class="type">QWidget</span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> TextRecordEditor(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
      <span class="operator">~</span>TextRecordEditor();

      <span class="type">void</span> setRecord(<span class="keyword">const</span> <span class="type"><a href="qndefnfctextrecord.html">QNdefNfcTextRecord</a></span> <span class="operator">&amp;</span>textRecord);
      <span class="type"><a href="qndefnfctextrecord.html">QNdefNfcTextRecord</a></span> record() <span class="keyword">const</span>;

  <span class="keyword">private</span>:
      Ui<span class="operator">::</span>TextRecordEditor <span class="operator">*</span>ui;
  };

</pre>
<a name="urirecordeditor"></a>
<h3 >UriRecordEditor</h3>
<p>The <a href="qtnfc-ndefeditor-example.html#urirecordeditor">UriRecordEditor</a> is a QWidget that can handle editing the values of Uri record that has been requested by the user. For each new Uri record there is a new instance of this class.</p>
<pre class="cpp">

  <span class="keyword">class</span> UriRecordEditor : <span class="keyword">public</span> <span class="type">QWidget</span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> UriRecordEditor(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
      <span class="operator">~</span>UriRecordEditor();

      <span class="type">void</span> setRecord(<span class="keyword">const</span> <span class="type"><a href="qndefnfcurirecord.html">QNdefNfcUriRecord</a></span> <span class="operator">&amp;</span>uriRecord);
      <span class="type"><a href="qndefnfcurirecord.html">QNdefNfcUriRecord</a></span> record() <span class="keyword">const</span>;

  <span class="keyword">private</span>:
      Ui<span class="operator">::</span>UriRecordEditor <span class="operator">*</span>ui;
  };

</pre>
<a name="mimeimagerecordeditor"></a>
<h3 >MimeImageRecordEditor</h3>
<p>The <a href="qtnfc-ndefeditor-example.html#urirecordeditor">UriRecordEditor</a> is a QWidget that can handle editing the values of a Mime Image record that has been requested by the user. For each Mime Image record there is a new instance of this class.</p>
<pre class="cpp">

  <span class="keyword">class</span> MimeImageRecordEditor : <span class="keyword">public</span> <span class="type">QWidget</span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> MimeImageRecordEditor(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
      <span class="operator">~</span>MimeImageRecordEditor();

      <span class="type">void</span> setRecord(<span class="keyword">const</span> <span class="type"><a href="qndefrecord.html">QNdefRecord</a></span> <span class="operator">&amp;</span>record);
      <span class="type"><a href="qndefrecord.html">QNdefRecord</a></span> record() <span class="keyword">const</span>;

  <span class="keyword">private</span>:
      Ui<span class="operator">::</span>MimeImageRecordEditor <span class="operator">*</span>ui;
      <span class="type"><a href="qndefrecord.html">QNdefRecord</a></span> m_record;

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> on_mimeImageOpen_clicked();
  };

</pre>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<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>
<p>Files:</p>
<ul>
<li><a href="qtnfc-ndefeditor-main-cpp.html">ndefeditor/main.cpp</a></li>
<li><a href="qtnfc-ndefeditor-mainwindow-cpp.html">ndefeditor/mainwindow.cpp</a></li>
<li><a href="qtnfc-ndefeditor-mainwindow-h.html">ndefeditor/mainwindow.h</a></li>
<li><a href="qtnfc-ndefeditor-mainwindow-ui.html">ndefeditor/mainwindow.ui</a></li>
<li><a href="qtnfc-ndefeditor-mimeimagerecordeditor-cpp.html">ndefeditor/mimeimagerecordeditor.cpp</a></li>
<li><a href="qtnfc-ndefeditor-mimeimagerecordeditor-h.html">ndefeditor/mimeimagerecordeditor.h</a></li>
<li><a href="qtnfc-ndefeditor-mimeimagerecordeditor-ui.html">ndefeditor/mimeimagerecordeditor.ui</a></li>
<li><a href="qtnfc-ndefeditor-ndefeditor-pro.html">ndefeditor/ndefeditor.pro</a></li>
<li><a href="qtnfc-ndefeditor-textrecordeditor-cpp.html">ndefeditor/textrecordeditor.cpp</a></li>
<li><a href="qtnfc-ndefeditor-textrecordeditor-h.html">ndefeditor/textrecordeditor.h</a></li>
<li><a href="qtnfc-ndefeditor-textrecordeditor-ui.html">ndefeditor/textrecordeditor.ui</a></li>
<li><a href="qtnfc-ndefeditor-urirecordeditor-cpp.html">ndefeditor/urirecordeditor.cpp</a></li>
<li><a href="qtnfc-ndefeditor-urirecordeditor-h.html">ndefeditor/urirecordeditor.h</a></li>
<li><a href="qtnfc-ndefeditor-urirecordeditor-ui.html">ndefeditor/urirecordeditor.ui</a></li>
</ul>
</div>
<p><b>See also </b><a href="qtnfc-index.html">Qt NFC</a>.</p>
<!-- @@@ndefeditor -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 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>