NDEF Editor Example#

An example of reading and writing NFC Data Exchange Format (NDEF) messages to NFC Forum Tags.

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 device where the application is running.


NFC Tag detection#

The MainWindow class is able to detect if an NFC Tag is in the range for read/write operations. It can also detect if the connection has been lost. This is achieved by connecting the MainWindow class private handlers to the targetDetected and targetLost signals.

m_manager = QNearFieldManager(self)

When Read or Write button is pressed, the detection of NFC tags is started by calling the startTargetDetection method.


Once the target is detected, the MainWindow connects the following signals to its internal private slots: ndefMessageRead , NdefReadError , requestCompleted , NdefWriteError and error .

def targetDetected(self, target):

    switch (m_touchAction) {
    NoAction: = case()
    ReadNdef: = case()
        m_request = target.readNdefMessages()
        if not m_request.isValid(): # cannot read messages
            targetError(QNearFieldTarget.NdefReadError, m_request)
    WriteNdef: = case()
        m_request = target.writeNdefMessages(QList<QNdefMessage>() << ndefMessage())
        if not m_request.isValid(): # cannot write messages
            targetError(QNearFieldTarget.NdefWriteError, m_request)

If during the process of reading or writing to an NFC Tag the connection is lost, the MainWindow reacts to this event by scheduling the target deletion (using deleteLater ).

def targetLost(self, target):


Record creation#

The main window of the NDEF Editor example manages the composition and creation of NFC records. The UI contains a QScrollArea , which is used to dynamically add the record editors. The following methods of the MainWindow class provide an interface towards each of the record editing classes managing the different types of records.

def addNfcTextRecord():
def addNfcUriRecord():
def addMimeImageRecord():
def addEmptyRecord():

The following sections explain each of the record editing classes.

Record editing classes#


The TextRecordEditor is a QWidget that allows to edit the contents of the NDEF Text record. A new instance of this class is created for each text record.

class TextRecordEditor(QWidget):

# public
    TextRecordEditor = explicit(QWidget parent = 0)
    def setRecord(textRecord):
    record = QNdefNfcTextRecord()
# private
    Ui.TextRecordEditor ui


The UriRecordEditor is a QWidget that allows to edit the contents of the NDEF Uri record. A new instance of this class is created for each uri record.

class UriRecordEditor(QWidget):

# public
    UriRecordEditor = explicit(QWidget parent = 0)
    def setRecord(uriRecord):
    record = QNdefNfcUriRecord()
# private
    Ui.UriRecordEditor ui


The MimeImageRecordEditor is a QWidget that allows to edit the contents of the NDEF MIME record. In this example MIME record can be used to store an icon. A new instance of this class is created for each MIME record.

class MimeImageRecordEditor(QWidget):

# public
    MimeImageRecordEditor = explicit(QWidget parent = 0)
    def setRecord(record):
    record = QNdefRecord()
# public slots
    def handleScreenOrientationChange(orientation):
# protected
    def resizeEvent(arg__0):
# private
    def updatePixmap():
    Ui.MimeImageRecordEditor ui
    m_record = QNdefRecord()
    m_pixmap = QPixmap()
    m_imageSelected = False()
    m_screenRotated = False()
# private slots
    def on_mimeImageOpen_clicked():

Running the Example#

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

See also


Example project @ code.qt.io