6.0.0-rc4Source

QHostAddress Class

The QHostAddress class provides an IP address. More…

Header#include <QHostAddress>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QHostAddress()
QHostAddress(quint32 ip4Addr)
QHostAddress(const quint8 *ip6Addr)
QHostAddress(const Q_IPV6ADDR &ip6Addr)
QHostAddress(const QString &address)
QHostAddress(SpecialAddress address)
voidsetAddress(quint32 ip4Addr)
voidsetAddress(const quint8 *ip6Addr)
voidsetAddress(const Q_IPV6ADDR &ip6Addr)
boolsetAddress(const QString &address)
voidsetAddress(SpecialAddress address)
voidclear()
QAbstractSocket::NetworkLayerProtocolprotocol() const
quint32toIPv4Address(bool *ok = nullptr) const
toIPv6Address() const
QStringtoString() const
boolisNull() const
boolisLoopback() const
boolisMulticast() const
boolisLinkLocal() const
boolisGlobal() const
boolisEqual(const QHostAddress &other, ConversionMode mode = TolerantConversion) const
booloperator==(const QHostAddress &o) const
booloperator!=(const QHostAddress &o) const
booloperator==(SpecialAddress s) const
booloperator!=(SpecialAddress s) const

Detailed Description

QHostAddress holds an IPv4 or IPv6 address, or one of the special values in SpecialAddress. It does not perform name lookups; use QAbstractSocket::connectToHost() with a host name for that.

QHostAddress a("192.168.0.1");
quint32 ip = a.toIPv4Address();
QHostAddress any(QHostAddress::Any);

See also QAbstractSocket and QNetworkDatagram.

Differences from Qt

Scope IDs are not stored: a %zone suffix is ignored by setAddress(). There is no parseSubnet(), isInSubnet(), scopeId(), isUniqueLocalUnicast(), isSiteLocal() or isBroadcast(). toIPv4Address() and toString() are the only conversions, and there is no QDataStream or QDebug support.

Member Type Documentation

typedef QHostAddress::ConversionMode

Stores an OR combination of ConversionModeFlag values.

enum QHostAddress::ConversionModeFlag

This enum describes the conversions that isEqual() may apply before comparing two addresses.

ConstantDescription
QHostAddress::ConvertV4MappedToIPv4Treat IPv4-mapped IPv6 addresses (::ffff:a.b.c.d) as the equivalent IPv4 address.
QHostAddress::ConvertV4CompatToIPv4Declared for source compatibility; not used.
QHostAddress::ConvertUnspecifiedAddressDeclared for source compatibility; not used.
QHostAddress::ConvertLocalHostTreat any two loopback addresses as equal.
QHostAddress::TolerantConversionEnables every conversion flag.
QHostAddress::StrictConversionNo conversion; the addresses must be identical.

See also ConversionMode and isEqual().

enum QHostAddress::SpecialAddress

This enum describes address types with a special meaning.

ConstantDescription
QHostAddress::NullThe null address object. Equivalent to QHostAddress().
QHostAddress::BroadcastThe IPv4 broadcast address, 255.255.255.255.
QHostAddress::LocalHostThe IPv4 localhost address, 127.0.0.1.
QHostAddress::LocalHostIPv6The IPv6 localhost address, ::1.
QHostAddress::AnyThe dual-stack any-address. A socket bound to it listens on all IPv4 and IPv6 interfaces.
QHostAddress::AnyIPv6The IPv6 any-address, ::. A socket bound to it listens on IPv6 interfaces only.
QHostAddress::AnyIPv4The IPv4 any-address, 0.0.0.0. A socket bound to it listens on IPv4 interfaces only.

Member Function Documentation

QHostAddress::QHostAddress()

Constructs a null address.

See also isNull().

QHostAddress::QHostAddress(quint32 ip4Addr)

Constructs an IPv4 address from ip4Addr, given in host byte order.

QHostAddress::QHostAddress(const quint8 *ip6Addr)

Constructs an IPv6 address from the 16 bytes at ip6Addr, in network byte order.

QHostAddress::QHostAddress(const Q_IPV6ADDR &ip6Addr)

Constructs an IPv6 address from ip6Addr.

QHostAddress::QHostAddress(const QString &address)

Constructs an address by parsing address. If parsing fails, the address is null.

See also setAddress().

QHostAddress::QHostAddress(SpecialAddress address)

Constructs the special address address.

void QHostAddress::clear()

Sets the address to the null address.

See also isNull().

bool QHostAddress::isEqual(const QHostAddress &other, ConversionMode mode = TolerantConversion) const

Returns true if this address is equal to other after applying the conversions enabled in mode; otherwise returns false.

bool QHostAddress::isGlobal() const

Returns true if the address is a globally routable unicast address, that is, not loopback, private, link-local, multicast or unspecified; otherwise returns false.

bool QHostAddress::isLinkLocal() const

Returns true if the address is an IPv4 link-local address (169.254.0.0/16) or an IPv6 link-local address (fe80::/10); otherwise returns false.

bool QHostAddress::isLoopback() const

Returns true if the address is an IPv4 address in 127.0.0.0/8, or the IPv6 loopback address (or its IPv4-mapped form); otherwise returns false.

bool QHostAddress::isMulticast() const

Returns true if the address is an IPv4 multicast address (224.0.0.0/4) or an IPv6 multicast address (ff00::/8); otherwise returns false.

bool QHostAddress::isNull() const

Returns true if the address is null, that is, it holds no address.

See also clear().

bool QHostAddress::operator!=(const QHostAddress &o) const

Returns true if this address differs from o; otherwise returns false.

bool QHostAddress::operator!=(SpecialAddress s) const

This is an overloaded function.

Returns true if this address is not the special address s.

bool QHostAddress::operator==(const QHostAddress &o) const

Returns true if this address is identical to o; otherwise returns false. An IPv4 address does not equal its IPv4-mapped IPv6 form; use isEqual() for that.

bool QHostAddress::operator==(SpecialAddress s) const

This is an overloaded function.

Returns true if this address is the special address s.

QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const

Returns the network layer protocol of the address: IPv4Protocol, IPv6Protocol, AnyIPProtocol for QHostAddress::Any, or UnknownNetworkLayerProtocol for a null address.

void QHostAddress::setAddress(quint32 ip4Addr)

Sets the address to the IPv4 address ip4Addr, given in host byte order.

void QHostAddress::setAddress(const quint8 *ip6Addr)

This is an overloaded function.

Sets the address to the IPv6 address given by the 16 bytes at ip6Addr, in network byte order.

void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr)

This is an overloaded function.

Sets the address to the IPv6 address ip6Addr.

bool QHostAddress::setAddress(const QString &address)

This is an overloaded function.

Parses address as a dotted-decimal IPv4 or textual IPv6 address, ignoring surrounding whitespace and any %scope suffix. Returns true on success. On failure the address is cleared to a null address and the function returns false.

void QHostAddress::setAddress(SpecialAddress address)

This is an overloaded function.

Sets the address to the special address address.

quint32 QHostAddress::toIPv4Address(bool *ok = nullptr) const

Returns the IPv4 address as a host-byte-order number. IPv4-mapped IPv6 addresses are converted. If ok is not null, it is set to true on success and false if the address has no IPv4 form, in which case 0 is returned.

See also toIPv6Address() and toString().

QHostAddress::toIPv6Address() const

Returns the address as an IPv6 address. An IPv4 address is returned in its IPv4-mapped form (::ffff:a.b.c.d); a null address yields all zeros.

See also toIPv4Address() and toString().

QString QHostAddress::toString() const

Returns the address as a string, in dotted-decimal form for IPv4 or in the usual compressed form for IPv6. Returns a null string for a null address.

See also setAddress().