QUuid Class

The QUuid class stores a Universally Unique Identifier (UUID). More...

Header: #include <QUuid>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Note: All functions in this class are reentrant.

Public Types

enum StringFormat { WithBraces, WithoutBraces, Id128 }
enum Variant { VarUnknown, NCS, DCE, Microsoft, Reserved }
enum Version { VerUnknown, Time, EmbeddedPOSIX, Name, Md5, …, Sha1 }

Public Functions

QUuid(const GUID &guid)
QUuid(const QByteArray &text)
QUuid(const QString &text)
QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8)
QUuid()
QUuid & operator=(const GUID &guid)
bool isNull() const
QByteArray toByteArray(QUuid::StringFormat mode = WithBraces) const
CFUUIDRef toCFUUID() const
NSUUID * toNSUUID() const
QByteArray toRfc4122() const
QString toString(QUuid::StringFormat mode = WithBraces) const
QUuid::Variant variant() const
QUuid::Version version() const
GUID operator GUID() const
bool operator!=(const QUuid &other) const
bool operator!=(const GUID &guid) const
bool operator<(const QUuid &other) const
bool operator==(const QUuid &other) const
bool operator==(const GUID &guid) const
bool operator>(const QUuid &other) const

Static Public Members

QUuid createUuid()
QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData)
QUuid createUuidV3(const QUuid &ns, const QString &baseData)
QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData)
QUuid createUuidV5(const QUuid &ns, const QString &baseData)
QUuid fromCFUUID(CFUUIDRef uuid)
QUuid fromNSUUID(const NSUUID *uuid)
QUuid fromRfc4122(const QByteArray &bytes)
QUuid fromString(QStringView text)
QUuid fromString(QLatin1String text)
size_t qHash(const QUuid &uuid, size_t seed = 0)
QDataStream & operator<<(QDataStream &s, const QUuid &id)
QDebug operator<<(QDebug dbg, const QUuid &id)
bool operator<=(const QUuid &lhs, const QUuid &rhs)
bool operator>=(const QUuid &lhs, const QUuid &rhs)
QDataStream & operator>>(QDataStream &s, QUuid &id)

Detailed Description

Using Universally Unique IDentifiers (UUID) is a standard way to uniquely identify entities in a distributed computing environment. A UUID is a 16-byte (128-bit) number generated by some algorithm that is meant to guarantee that the UUID will be unique in the distributed computing environment where it is used. The acronym GUID is often used instead, Globally Unique IDentifiers, but it refers to the same thing.

Actually, the GUID is one variant of UUID. Multiple variants are in use. Each UUID contains a bit field that specifies which type (variant) of UUID it is. Call variant() to discover which type of UUID an instance of QUuid contains. It extracts the three most significant bits of byte 8 of the 16 bytes. In QUuid, byte 8 is QUuid::data4[0]. If you create instances of QUuid using the constructor that accepts all the numeric values as parameters, use the following table to set the three most significant bits of parameter b1, which becomes QUuid::data4[0] and contains the variant field in its three most significant bits. In the table, 'x' means don't care.

msb0 msb1 msb2 Variant
0 x x NCS (Network Computing System)
1 0 x DCE (Distributed Computing Environment)
1 1 0 Microsoft (GUID)
1 1 1 Reserved for future expansion

If variant() returns QUuid::DCE, the UUID also contains a version field in the four most significant bits of QUuid::data3, and you can call version() to discover which version your QUuid contains. If you create instances of QUuid using the constructor that accepts all the numeric values as parameters, use the following table to set the four most significant bits of parameter w2, which becomes QUuid::data3 and contains the version field in its four most significant bits.

msb0 msb1 msb2 msb3 Version
0 0 0 1 Time
0 0 1 0 Embedded POSIX
0 0 1 1 Md5(Name)
0 1 0 0 Random
0 1 0 1 Sha1

The field layouts for the DCE versions listed in the table above are specified in the Network Working Group UUID Specification.

Most platforms provide a tool for generating new UUIDs, e.g. uuidgen and guidgen. You can also use createUuid(). UUIDs generated by createUuid() are of the random type. Their QUuid::Version bits are set to QUuid::Random, and their QUuid::Variant bits are set to QUuid::DCE. The rest of the UUID is composed of random numbers. Theoretically, this means there is a small chance that a UUID generated by createUuid() will not be unique. But it is a very small chance.

UUIDs can be constructed from numeric values or from strings, or using the static createUuid() function. They can be converted to a string with toString(). UUIDs have a variant() and a version(), and null UUIDs return true from isNull().

Member Type Documentation

[since 5.11] enum QUuid::StringFormat

This enum is used by toString(StringFormat) to control the formatting of the string representation. The possible values are:

Constant Value Description
QUuid::WithBraces 0 The default, toString() will return five hex fields, separated by dashes and surrounded by braces. Example: {00000000-0000-0000-0000-000000000000}.
QUuid::WithoutBraces 1 Only the five dash-separated fields, without the braces. Example: 00000000-0000-0000-0000-000000000000.
QUuid::Id128 3 Only the hex digits, without braces or dashes. Note that QUuid cannot parse this back again as input.

This enum was introduced or modified in Qt 5.11.

enum QUuid::Variant

This enum defines the values used in the variant field of the UUID. The value in the variant field determines the layout of the 128-bit value.

Constant Value Description
QUuid::VarUnknown -1 Variant is unknown
QUuid::NCS 0 Reserved for NCS (Network Computing System) backward compatibility
QUuid::DCE 2 Distributed Computing Environment, the scheme used by QUuid
QUuid::Microsoft 6 Reserved for Microsoft backward compatibility (GUID)
QUuid::Reserved 7 Reserved for future definition

enum QUuid::Version

This enum defines the values used in the version field of the UUID. The version field is meaningful only if the value in the variant field is QUuid::DCE.

Constant Value Description
QUuid::VerUnknown -1 Version is unknown
QUuid::Time 1 Time-based, by using timestamp, clock sequence, and MAC network card address (if available) for the node sections
QUuid::EmbeddedPOSIX 2 DCE Security version, with embedded POSIX UUIDs
QUuid::Name Md5 Name-based, by using values from a name for all sections
QUuid::Md5 3 Alias for Name
QUuid::Random 4 Random-based, by using random numbers for all sections
QUuid::Sha1 5

Member Function Documentation

QUuid::QUuid(const GUID &guid)

Casts a Windows guid to a Qt QUuid.

Warning: This function is only for Windows platforms.

QUuid::QUuid(const QByteArray &text)

Creates a QUuid object from the QByteArray text, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex digit. The curly braces shown here are optional, but it is normal to include them. If the conversion fails, a null UUID is created. See toByteArray() for an explanation of how the five hex fields map to the public data members in QUuid.

See also toByteArray() and QUuid().

QUuid::QUuid(const QString &text)

Creates a QUuid object from the string text, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex digit. The curly braces shown here are optional, but it is normal to include them. If the conversion fails, a null UUID is created. See toString() for an explanation of how the five hex fields map to the public data members in QUuid.

See also toString() and QUuid().

QUuid::QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8)

Creates a UUID with the value specified by the parameters, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8.

Example:

// {67C8770B-44F1-410A-AB9A-F9B5446F13EE}
QUuid IID_MyInterface(0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee);

QUuid::QUuid()

Creates the null UUID. toString() will output the null UUID as "{00000000-0000-0000-0000-000000000000}".

QUuid &QUuid::operator=(const GUID &guid)

Assigns a Windows guid to a Qt QUuid.

Warning: This function is only for Windows platforms.

[static] QUuid QUuid::createUuid()

On any platform other than Windows, this function returns a new UUID with variant QUuid::DCE and version QUuid::Random. On Windows, a GUID is generated using the Windows API and will be of the type that the API decides to create.

See also variant() and version().

[static, since 5.0] QUuid QUuid::createUuidV3(const QUuid &ns, const QByteArray &baseData)

This function returns a new UUID with variant QUuid::DCE and version QUuid::Md5. ns is the namespace and baseData is the basic data as described by RFC 4122.

This function was introduced in Qt 5.0.

See also variant(), version(), and createUuidV5().

[static, since 5.0] QUuid QUuid::createUuidV3(const QUuid &ns, const QString &baseData)

This function returns a new UUID with variant QUuid::DCE and version QUuid::Md5. ns is the namespace and baseData is the basic data as described by RFC 4122.

This function was introduced in Qt 5.0.

See also variant(), version(), and createUuidV5().

[static, since 5.0] QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData)

This function returns a new UUID with variant QUuid::DCE and version QUuid::Sha1. ns is the namespace and baseData is the basic data as described by RFC 4122.

This function was introduced in Qt 5.0.

See also variant(), version(), and createUuidV3().

[static, since 5.0] QUuid QUuid::createUuidV5(const QUuid &ns, const QString &baseData)

This function returns a new UUID with variant QUuid::DCE and version QUuid::Sha1. ns is the namespace and baseData is the basic data as described by RFC 4122.

This function was introduced in Qt 5.0.

See also variant(), version(), and createUuidV3().

[static, since 5.7] QUuid QUuid::fromCFUUID(CFUUIDRef uuid)

Constructs a new QUuid containing a copy of the uuid CFUUID.

Note: this function is only available on Apple platforms.

This function was introduced in Qt 5.7.

[static, since 5.7] QUuid QUuid::fromNSUUID(const NSUUID *uuid)

Constructs a new QUuid containing a copy of the uuid NSUUID.

Note: this function is only available on Apple platforms.

This function was introduced in Qt 5.7.

[static] QUuid QUuid::fromRfc4122(const QByteArray &bytes)

Creates a QUuid object from the binary representation of the UUID, as specified by RFC 4122 section 4.1.2. See toRfc4122() for a further explanation of the order of bytes required.

The byte array accepted is NOT a human readable format.

If the conversion fails, a null UUID is created.

See also toRfc4122() and QUuid().

[static, since 5.10] QUuid QUuid::fromString(QStringView text)

Creates a QUuid object from the string text, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex digit. The curly braces shown here are optional, but it is normal to include them. If the conversion fails, a null UUID is returned. See toString() for an explanation of how the five hex fields map to the public data members in QUuid.

This function was introduced in Qt 5.10.

See also toString() and QUuid().

[static, since 5.10] QUuid QUuid::fromString(QLatin1String text)

This is an overloaded function.

Creates a QUuid object from the string text, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where each 'x' is a hex digit. The curly braces shown here are optional, but it is normal to include them. If the conversion fails, a null UUID is returned. See toString() for an explanation of how the five hex fields map to the public data members in QUuid.

This function was introduced in Qt 5.10.

See also toString() and QUuid().

bool QUuid::isNull() const

Returns true if this is the null UUID {00000000-0000-0000-0000-000000000000}; otherwise returns false.

[since 5.11] QByteArray QUuid::toByteArray(QUuid::StringFormat mode = WithBraces) const

Returns the string representation of this QUuid, with the formattiong controlled by the mode parameter. From left to right, the five hex fields are obtained from the four public data members in QUuid as follows:

Field # Source
1 data1
2 data2
3 data3
4 data4[0] .. data4[1]
5 data4[2] .. data4[7]

This function was introduced in Qt 5.11.

[since 5.7] CFUUIDRef QUuid::toCFUUID() const

Creates a CFUUID from a QUuid.

The caller owns the CFUUID and is responsible for releasing it.

Note: this function is only available on Apple platforms.

This function was introduced in Qt 5.7.

[since 5.7] NSUUID *QUuid::toNSUUID() const

Creates a NSUUID from a QUuid.

The NSUUID is autoreleased.

Note: this function is only available on Apple platforms.

This function was introduced in Qt 5.7.

QByteArray QUuid::toRfc4122() const

Returns the binary representation of this QUuid. The byte array is in big endian format, and formatted according to RFC 4122, section 4.1.2 - "Layout and byte order".

The order is as follows:

Field # Source
1 data1
2 data2
3 data3
4 data4[0] .. data4[7]

[since 5.11] QString QUuid::toString(QUuid::StringFormat mode = WithBraces) const

Returns the string representation of this QUuid, with the formattiong controlled by the mode parameter. From left to right, the five hex fields are obtained from the four public data members in QUuid as follows:

Field # Source
1 data1
2 data2
3 data3
4 data4[0] .. data4[1]
5 data4[2] .. data4[7]

This function was introduced in Qt 5.11.

QUuid::Variant QUuid::variant() const

Returns the value in the variant field of the UUID. If the return value is QUuid::DCE, call version() to see which layout it uses. The null UUID is considered to be of an unknown variant.

See also version().

QUuid::Version QUuid::version() const

Returns the version field of the UUID, if the UUID's variant field is QUuid::DCE. Otherwise it returns QUuid::VerUnknown.

See also variant().

GUID QUuid::operator GUID() const

Returns a Windows GUID from a QUuid.

Warning: This function is only for Windows platforms.

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

Returns true if this QUuid and the other QUuid are different; otherwise returns false.

bool QUuid::operator!=(const GUID &guid) const

Returns true if this UUID is not equal to the Windows GUID guid; otherwise returns false.

bool QUuid::operator<(const QUuid &other) const

Returns true if this QUuid has the same variant field as the other QUuid and is lexicographically before the other QUuid. If the other QUuid has a different variant field, the return value is determined by comparing the two variants.

See also variant().

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

Returns true if this QUuid and the other QUuid are identical; otherwise returns false.

bool QUuid::operator==(const GUID &guid) const

Returns true if this UUID is equal to the Windows GUID guid; otherwise returns false.

bool QUuid::operator>(const QUuid &other) const

Returns true if this QUuid has the same variant field as the other QUuid and is lexicographically after the other QUuid. If the other QUuid has a different variant field, the return value is determined by comparing the two variants.

See also variant().

Related Non-Members

[since 5.0] size_t qHash(const QUuid &uuid, size_t seed = 0)

Returns a hash of the UUID uuid, using seed to seed the calculation.

This function was introduced in Qt 5.0.

QDataStream &operator<<(QDataStream &s, const QUuid &id)

Writes the UUID id to the data stream s.

QDebug operator<<(QDebug dbg, const QUuid &id)

Writes the UUID id to the output stream for debugging information dbg.

[since 5.5] bool operator<=(const QUuid &lhs, const QUuid &rhs)

Returns true if lhs has the same variant field as rhs and is lexicographically not after rhs. If rhs has a different variant field, the return value is determined by comparing the two variants.

This function was introduced in Qt 5.5.

See also variant().

[since 5.5] bool operator>=(const QUuid &lhs, const QUuid &rhs)

Returns true if lhs has the same variant field as rhs and is lexicographically not before rhs. If rhs has a different variant field, the return value is determined by comparing the two variants.

This function was introduced in Qt 5.5.

See also variant().

QDataStream &operator>>(QDataStream &s, QUuid &id)

Reads a UUID from the stream s into id.

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.0/quuid.html