Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 814a2b4c48f3ef6444b2ff5bf854d05a > files > 339

qtconnectivity5-doc-5.12.6-1.mga7.noarch.rpm

<?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.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="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"><a href="qtnfc-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="#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">QList</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 (QObject::deleteLater).</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 id="textrecordeditor">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 id="urirecordeditor">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 id="mimeimagerecordeditor">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 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-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> 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>