QNdefRecord Class

The QNdefRecord class provides an NFC NDEF record. 更多...

头: #include <QNdefRecord>
CMake: find_package(Qt6 REQUIRED COMPONENTS Nfc)
target_link_libraries(mytarget PRIVATE Qt6::Nfc)
qmake: QT += nfc
继承者: QNdefNfcIconRecord , QNdefNfcSmartPosterRecord , QNdefNfcTextRecord ,和 QNdefNfcUriRecord

公共类型

enum TypeNameFormat { Empty, NfcRtd, Mime, Uri, ExternalRtd, Unknown }

公共函数

QNdefRecord ()
QNdefRecord (const QNdefRecord & other )
~QNdefRecord ()
void clear ()
QByteArray id () const
bool isEmpty () const
bool isRecordType () const
QByteArray payload () const
void setId (const QByteArray & id )
void setPayload (const QByteArray & payload )
void setType (const QByteArray & type )
void setTypeNameFormat (QNdefRecord::TypeNameFormat typeNameFormat )
QByteArray type () const
QNdefRecord::TypeNameFormat typeNameFormat () const
bool operator!= (const QNdefRecord & other ) const
QNdefRecord & operator= (const QNdefRecord & other )
bool operator== (const QNdefRecord & other ) const

Q_DECLARE_ISRECORDTYPE_FOR_NDEF_RECORD ( className , typeNameFormat , type )
Q_DECLARE_NDEF_RECORD ( className , typeNameFormat , type , initialPayload )

详细描述

QNdefRecord and derived classes are used to parse the contents of NDEF messages and create new NDEF messages.

使用 typeNameFormat () 和 setTypeNameFormat () to get and set the type name format of the NDEF record.

使用 type () 和 setType () to get and set the type of the NDEF record.

使用 id () 和 setId () to get and set the id of the NDEF record.

使用 payload () 和 setPayload () to get and set the NDEF record payload. isEmpty () can be used to test if the payload is empty.

QNdefRecord is an implicitly shared class. This means you can efficiently convert between QNdefRecord and specialized record classes. The isRecordType () template function can be used to test if a conversion is possible. The following example shows how to test if a QNdefRecord is an NFC RTD Text record and extract the text information from it.

if (record.isRecordType<QNdefNfcTextRecord>()) {
    QNdefNfcTextRecord textRecord(record);
    qDebug() << textRecord.text();
}
					

Creating Specialized NDEF Record Classes

Specialized NDEF record classes can be easily created with the Q_DECLARE_NDEF_RECORD () 和 Q_DECLARE_ISRECORDTYPE_FOR_NDEF_RECORD () macros. The following example shows the class declaration of the hypothetical example.com:f record type that encapsulates a single int property foo.

class ExampleComF : public QNdefRecord
{
public:
    Q_DECLARE_NDEF_RECORD(ExampleComF, QNdefRecord::ExternalRtd, "example.com:f",
                          QByteArray(sizeof(int), char(0)))
    int foo() const;
    void setFoo(int v);
};
Q_DECLARE_ISRECORDTYPE_FOR_NDEF_RECORD(ExampleComF, QNdefRecord::ExternalRtd, "example.com:f")
					

The developer only needs to provide implementations for the foo() and setFoo() functions that parse and set the contents of the NDEF record's payload.

成员类型文档编制

enum QNdefRecord:: TypeNameFormat

This enum describes the type name format of an NDEF record.

常量 描述
QNdefRecord::Empty 0x00 An empty NDEF record, the record does not contain a payload
QNdefRecord::NfcRtd 0x01 The NDEF record type is defined by an NFC RTD Specification
QNdefRecord::Mime 0x02 The NDEF record type follows the construct described in RFC 2046
QNdefRecord::Uri 0x03 The NDEF record type follows the construct described in RFC 3986
QNdefRecord::ExternalRtd 0x04 The NDEF record type follows the construct for external type names described the NFC RTD Specification
QNdefRecord::Unknown 0x05 The type of the record is unknown and should be treated similar to content with MIME type 'application/octet-stream' without further context

成员函数文档编制

QNdefRecord:: QNdefRecord ()

Constructs a new empty NDEF record.

QNdefRecord:: QNdefRecord (const QNdefRecord & other )

Constructs a new NDEF record that is a copy of other .

QNdefRecord:: ~QNdefRecord ()

Destroys the NDEF record.

[since 6.2] void QNdefRecord:: clear ()

Clear the NDEF record.

An isEmpty () call returns true for a cleared record. The record type () 被设为 Empty .

该函数在 Qt 6.2 引入。

QByteArray QNdefRecord:: id () const

Returns the id of the NDEF record.

另请参阅 setId ().

bool QNdefRecord:: isEmpty () const

返回 true if the NDEF record contains an empty payload; otherwise returns false .

这相当于调用 payload().isEmpty() .

template <typename T> bool QNdefRecord:: isRecordType () const

返回 true if the NDEF record is of the specified record type; otherwise returns false .

QByteArray QNdefRecord:: payload () const

Returns the payload of the NDEF record.

另请参阅 setPayload ().

void QNdefRecord:: setId (const QByteArray & id )

Sets the id of the NDEF record to id .

另请参阅 id ().

void QNdefRecord:: setPayload (const QByteArray & payload )

Sets the payload of the NDEF record to payload .

另请参阅 payload ().

void QNdefRecord:: setType (const QByteArray & type )

Sets the type of the NDEF record to type .

另请参阅 type ().

void QNdefRecord:: setTypeNameFormat ( QNdefRecord::TypeNameFormat typeNameFormat )

Sets the type name format of the NDEF record to typeNameFormat .

另请参阅 typeNameFormat ().

QByteArray QNdefRecord:: type () const

Returns the type of the NDEF record.

另请参阅 setType ().

QNdefRecord::TypeNameFormat QNdefRecord:: typeNameFormat () const

Returns the type name format of the NDEF record.

另请参阅 setTypeNameFormat ().

bool QNdefRecord:: operator!= (const QNdefRecord & other ) const

返回 true if this NDEF record does not equal other ; otherwise return false .

QNdefRecord &QNdefRecord:: operator= (const QNdefRecord & other )

Assigns this NDEF record to other .

bool QNdefRecord:: operator== (const QNdefRecord & other ) const

返回 true if other and this NDEF record are the same. Otherwise returns false .

宏文档编制

Q_DECLARE_ISRECORDTYPE_FOR_NDEF_RECORD ( className , typeNameFormat , type )

This macro declares a template specialization for the QNdefRecord::isRecordType () 函数。

This macro should be used in the header file directly after the definition of a specialized NDEF record class.

className is the name of the specialized class, typeNameFormat is the appropriate QNdefRecord::TypeNameFormat for the custom type and type is the type without the NID or NSS prefixes. That is example.com:f not urn:nfc:ext:example.com:f .

See the section on Creating specialized NDEF record classes 了解细节。

另请参阅 Q_DECLARE_NDEF_RECORD ().

Q_DECLARE_NDEF_RECORD ( className , typeNameFormat , type , initialPayload )

This macro declares default and copy constructors for specialized NDEF record classes.

className is the name of the specialized class, typeNameFormat is the appropriate QNdefRecord::TypeNameFormat for the custom type and type is the type without the NID or NSS prefixes. That is example.com:f not urn:nfc:ext:example.com:f . initialPayload is the initial payload of an empty record, it must be a QByteArray or a type that can be implicitly converted into a QByteArray .

See the section on Creating specialized NDEF record classes 了解细节。

另请参阅 Q_DECLARE_ISRECORDTYPE_FOR_NDEF_RECORD ().