QHostAddress Class

The QHostAddress class provides an IP address. More...

Header: #include <QHostAddress>
qmake: QT += network

Public Types

flags ConversionMode
enum ConversionModeFlag { StrictConversion, ConvertV4MappedToIPv4, ConvertV4CompatToIPv4, ConvertLocalHost, ConvertUnspecifiedAddress, TolerantConversion }
enum SpecialAddress { Null, LocalHost, LocalHostIPv6, Broadcast, AnyIPv4, …, Any }

Public Functions

QHostAddress(QHostAddress::SpecialAddress address)
QHostAddress(const QHostAddress &address)
QHostAddress(const QString &address)
QHostAddress(const sockaddr *sockaddr)
QHostAddress(const Q_IPV6ADDR &ip6Addr)
QHostAddress(const quint8 *ip6Addr)
QHostAddress(quint8 *ip6Addr)
QHostAddress(quint32 ip4Addr)
QHostAddress()
QHostAddress & operator=(const QString &address)
QHostAddress & operator=(const QHostAddress &address)
~QHostAddress()
void clear()
bool isBroadcast() const
bool isEqual(const QHostAddress &other, QHostAddress::ConversionMode mode = TolerantConversion) const
bool isGlobal() const
bool isInSubnet(const QHostAddress &subnet, int netmask) const
bool isInSubnet(const QPair<QHostAddress, int> &subnet) const
bool isLinkLocal() const
bool isLoopback() const
bool isMulticast() const
bool isNull() const
bool isSiteLocal() const
bool isUniqueLocalUnicast() const
QAbstractSocket::NetworkLayerProtocol protocol() const
QString scopeId() const
void setAddress(quint32 ip4Addr)
void setAddress(quint8 *ip6Addr)
void setAddress(const quint8 *ip6Addr)
void setAddress(const Q_IPV6ADDR &ip6Addr)
void setAddress(const sockaddr *sockaddr)
bool setAddress(const QString &address)
void setAddress(QHostAddress::SpecialAddress address)
void setScopeId(const QString &id)
void swap(QHostAddress &other)
quint32 toIPv4Address() const
quint32 toIPv4Address(bool *ok) const
Q_IPV6ADDR toIPv6Address() const
QString toString() const
bool operator!=(const QHostAddress &other) const
bool operator!=(QHostAddress::SpecialAddress other) const
QHostAddress & operator=(QHostAddress::SpecialAddress address)
bool operator==(const QHostAddress &other) const
bool operator==(QHostAddress::SpecialAddress other) const

Static Public Members

QPair<QHostAddress, int> parseSubnet(const QString &subnet)
uint qHash(const QHostAddress &key, uint seed = 0)
bool operator!=(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)
QDataStream & operator<<(QDataStream &out, const QHostAddress &address)
bool operator==(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)
QDataStream & operator>>(QDataStream &in, QHostAddress &address)

Detailed Description

This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner.

QHostAddress is normally used with the QTcpSocket, QTcpServer, and QUdpSocket to connect to a host or to set up a server.

A host address is set with setAddress(), and retrieved with toIPv4Address(), toIPv6Address(), or toString(). You can check the type with protocol().

Note: Please note that QHostAddress does not do DNS lookups. QHostInfo is needed for that.

The class also supports common predefined addresses: Null, LocalHost, LocalHostIPv6, Broadcast, and Any.

See also QHostInfo, QTcpSocket, QTcpServer, and QUdpSocket.

Member Type Documentation

enum QHostAddress::ConversionModeFlagflags QHostAddress::ConversionMode

Constant Value Description
QHostAddress::StrictConversion 0 Don't convert IPv6 addresses to IPv4 when comparing two QHostAddress objects of different protocols, so they will always be considered different.
QHostAddress::ConvertV4MappedToIPv4 1 Convert IPv4-mapped IPv6 addresses (RFC 4291 sect. 2.5.5.2) when comparing. Therefore QHostAddress("::ffff:192.168.1.1") will compare equal to QHostAddress("192.168.1.1").
QHostAddress::ConvertV4CompatToIPv4 2 Convert IPv4-compatible IPv6 addresses (RFC 4291 sect. 2.5.5.1) when comparing. Therefore QHostAddress("::192.168.1.1") will compare equal to QHostAddress("192.168.1.1").
QHostAddress::ConvertLocalHost 8 Convert the IPv6 loopback addresses to its IPv4 equivalent when comparing. Therefore e.g. QHostAddress("::1") will compare equal to QHostAddress("127.0.0.1").
QHostAddress::ConvertUnspecifiedAddress 4 All unspecified addresses will compare equal, namely AnyIPv4, AnyIPv6 and Any.
QHostAddress::TolerantConversion 0xff Sets all three preceding flags.

This enum was introduced or modified in Qt 5.8.

The ConversionMode type is a typedef for QFlags<ConversionModeFlag>. It stores an OR combination of ConversionModeFlag values.

See also isEqual().

enum QHostAddress::SpecialAddress

Constant Value Description
QHostAddress::Null 0 The null address object. Equivalent to QHostAddress(). See also QHostAddress::isNull().
QHostAddress::LocalHost 2 The IPv4 localhost address. Equivalent to QHostAddress("127.0.0.1").
QHostAddress::LocalHostIPv6 3 The IPv6 localhost address. Equivalent to QHostAddress("::1").
QHostAddress::Broadcast 1 The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255").
QHostAddress::AnyIPv4 6 The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0"). A socket bound with this address will listen only on IPv4 interfaces.
QHostAddress::AnyIPv6 5 The IPv6 any-address. Equivalent to QHostAddress("::"). A socket bound with this address will listen only on IPv6 interfaces.
QHostAddress::Any 4 The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces.

Member Function Documentation

QHostAddress::QHostAddress(QHostAddress::SpecialAddress address)

Constructs a QHostAddress object for address.

QHostAddress::QHostAddress(const QHostAddress &address)

Constructs a copy of the given address.

QHostAddress::QHostAddress(const QString &address)

Constructs an IPv4 or IPv6 address based on the string address (e.g., "127.0.0.1").

See also setAddress().

QHostAddress::QHostAddress(const sockaddr *sockaddr)

Constructs an IPv4 or IPv6 address using the address specified by the native structure sockaddr.

See also setAddress().

QHostAddress::QHostAddress(const Q_IPV6ADDR &ip6Addr)

Constructs a host address object with the IPv6 address ip6Addr.

QHostAddress::QHostAddress(const quint8 *ip6Addr)

Constructs a host address object with the IPv6 address ip6Addr.

ip6Addr must be a 16-byte array in network byte order (big endian).

This function was introduced in Qt 5.5.

QHostAddress::QHostAddress(quint8 *ip6Addr)

Constructs a host address object with the IPv6 address ip6Addr.

ip6Addr must be a 16-byte array in network byte order (big endian).

QHostAddress::QHostAddress(quint32 ip4Addr)

Constructs a host address object with the IPv4 address ip4Addr.

QHostAddress::QHostAddress()

Constructs a null host address object, i.e. an address which is not valid for any host or interface.

See also clear().

QHostAddress &QHostAddress::operator=(const QString &address)

Assigns the host address address to this object, and returns a reference to this object.

See also setAddress().

QHostAddress &QHostAddress::operator=(const QHostAddress &address)

Assigns another host address to this object, and returns a reference to this object.

QHostAddress::~QHostAddress()

Destroys the host address object.

void QHostAddress::clear()

Sets the host address to null and sets the protocol to QAbstractSocket::UnknownNetworkLayerProtocol.

See also QHostAddress::Null.

bool QHostAddress::isBroadcast() const

Returns true if the address is the IPv4 broadcast address, false otherwise. The IPv4 broadcast address is 255.255.255.255.

Note that this function does not return true for an IPv4 network's local broadcast address. For that, please use QNetworkInterface to obtain the broadcast addresses of the local machine.

This function was introduced in Qt 5.11.

See also isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), and isUniqueLocalUnicast().

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

Returns true if this host address is the same as the other address given; otherwise returns false.

The parameter mode controls which conversions are preformed between addresses of differing protocols. If no mode is given, TolerantConversion is performed by default.

This function was introduced in Qt 5.8.

See also ConversionMode and operator==().

bool QHostAddress::isGlobal() const

Returns true if the address is an IPv4 or IPv6 global address, false otherwise. A global address is an address that is not reserved for special purposes (like loopback or multicast) or future purposes.

Note that IPv6 unique local unicast addresses are considered global addresses (see isUniqueLocalUnicast()), as are IPv4 addresses reserved for local networks by RFC 1918.

Also note that IPv6 site-local addresses are deprecated and should be considered as global in new applications. This function returns true for site-local addresses too.

This function was introduced in Qt 5.11.

See also isLoopback(), isSiteLocal(), and isUniqueLocalUnicast().

bool QHostAddress::isInSubnet(const QHostAddress &subnet, int netmask) const

Returns true if this IP is in the subnet described by the network prefix subnet and netmask netmask.

An IP is considered to belong to a subnet if it is contained between the lowest and the highest address in that subnet. In the case of IP version 4, the lowest address is the network address, while the highest address is the broadcast address.

The subnet argument does not have to be the actual network address (the lowest address in the subnet). It can be any valid IP belonging to that subnet. In particular, if it is equal to the IP address held by this object, this function will always return true (provided the netmask is a valid value).

This function was introduced in Qt 4.5.

See also parseSubnet().

bool QHostAddress::isInSubnet(const QPair<QHostAddress, int> &subnet) const

This is an overloaded function.

Returns true if this IP is in the subnet described by subnet. The QHostAddress member of subnet contains the network prefix and the int (second) member contains the netmask (prefix length).

This function was introduced in Qt 4.5.

bool QHostAddress::isLinkLocal() const

Returns true if the address is an IPv4 or IPv6 link-local address, false otherwise.

An IPv4 link-local address is an address in the network 169.254.0.0/16. An IPv6 link-local address is one in the network fe80::/10. See the IANA IPv6 Address Space registry for more information.

This function was introduced in Qt 5.11.

See also isLoopback(), isGlobal(), isMulticast(), isSiteLocal(), and isUniqueLocalUnicast().

bool QHostAddress::isLoopback() const

returns true if the address is the IPv6 loopback address, or any of the IPv4 loopback addresses.

This function was introduced in Qt 5.0.

bool QHostAddress::isMulticast() const

Returns true if the address is an IPv4 or IPv6 multicast address, false otherwise.

This function was introduced in Qt 5.6.

See also isLoopback(), isGlobal(), isLinkLocal(), isSiteLocal(), and isUniqueLocalUnicast().

bool QHostAddress::isNull() const

Returns true if this host address is not valid for any host or interface.

The default constructor creates a null address.

See also QHostAddress::Null.

bool QHostAddress::isSiteLocal() const

Returns true if the address is an IPv6 site-local address, false otherwise.

An IPv6 site-local address is one in the network fec0::/10. See the IANA IPv6 Address Space registry for more information.

IPv6 site-local addresses are deprecated and should not be depended upon in new applications. New applications should not depend on this function and should consider site-local addresses the same as global (which is why isGlobal() also returns true). Site-local addresses were replaced by Unique Local Addresses (ULA).

This function was introduced in Qt 5.11.

See also isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), and isUniqueLocalUnicast().

bool QHostAddress::isUniqueLocalUnicast() const

Returns true if the address is an IPv6 unique local unicast address, false otherwise.

An IPv6 unique local unicast address is one in the network fc00::/7. See the IANA IPv6 Address Space registry for more information.

Note that Unique local unicast addresses count as global addresses too. RFC 4193 says that, in practice, "applications may treat these addresses like global scoped addresses." Only routers need care about the distinction.

This function was introduced in Qt 5.11.

See also isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), and isUniqueLocalUnicast().

[static] QPair<QHostAddress, int> QHostAddress::parseSubnet(const QString &subnet)

Parses the IP and subnet information contained in subnet and returns the network prefix for that network and its prefix length.

The IP address and the netmask must be separated by a slash (/).

This function supports arguments in the form:

  • 123.123.123.123/n where n is any value between 0 and 32
  • 123.123.123.123/255.255.255.255
  • <ipv6-address>/n where n is any value between 0 and 128

For IP version 4, this function accepts as well missing trailing components (i.e., less than 4 octets, like "192.168.1"), followed or not by a dot. If the netmask is also missing in that case, it is set to the number of octets actually passed (in the example above, it would be 24, for 3 octets).

This function was introduced in Qt 4.5.

See also isInSubnet().

QAbstractSocket::NetworkLayerProtocol QHostAddress::protocol() const

Returns the network layer protocol of the host address.

QString QHostAddress::scopeId() const

Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the address does not contain a scope ID, an empty QString is returned.

The IPv6 scope ID specifies the scope of reachability for non-global IPv6 addresses, limiting the area in which the address can be used. All IPv6 addresses are associated with such a reachability scope. The scope ID is used to disambiguate addresses that are not guaranteed to be globally unique.

IPv6 specifies the following four levels of reachability:

  • Node-local: Addresses that are only used for communicating with services on the same interface (e.g., the loopback interface "::1").
  • Link-local: Addresses that are local to the network interface (link). There is always one link-local address for each IPv6 interface on your host. Link-local addresses ("fe80...") are generated from the MAC address of the local network adaptor, and are not guaranteed to be unique.
  • Global: For globally routable addresses, such as public servers on the Internet.

When using a link-local or site-local address for IPv6 connections, you must specify the scope ID. The scope ID for a link-local address is usually the same as the interface name (e.g., "eth0", "en1") or number (e.g., "1", "2").

This function was introduced in Qt 4.1.

See also setScopeId(), QNetworkInterface, and QNetworkInterface::interfaceFromName.

void QHostAddress::setAddress(quint32 ip4Addr)

Set the IPv4 address specified by ip4Addr.

void QHostAddress::setAddress(quint8 *ip6Addr)

This is an overloaded function.

Set the IPv6 address specified by ip6Addr.

ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).

void QHostAddress::setAddress(const quint8 *ip6Addr)

This is an overloaded function.

Set the IPv6 address specified by ip6Addr.

ip6Addr must be an array of 16 bytes in network byte order (high-order byte first).

This function was introduced in Qt 5.5.

void QHostAddress::setAddress(const Q_IPV6ADDR &ip6Addr)

This is an overloaded function.

Set the IPv6 address specified by ip6Addr.

void QHostAddress::setAddress(const sockaddr *sockaddr)

This is an overloaded function.

Sets the IPv4 or IPv6 address specified by the native structure sockaddr. Returns true and sets the address if the address was successfully parsed; otherwise returns false.

bool QHostAddress::setAddress(const QString &address)

This is an overloaded function.

Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.g. "127.0.0.1"). Returns true and sets the address if the address was successfully parsed; otherwise returns false.

void QHostAddress::setAddress(QHostAddress::SpecialAddress address)

This is an overloaded function.

Sets the special address specified by address.

This function was introduced in Qt 5.8.

void QHostAddress::setScopeId(const QString &id)

Sets the IPv6 scope ID of the address to id. If the address protocol is not IPv6, this function does nothing. The scope ID may be set as an interface name (such as "eth0" or "en1") or as an integer representing the interface index. If id is an interface name, QtNetwork will convert to an interface index using QNetworkInterface::interfaceIndexFromName() before calling the operating system networking functions.

This function was introduced in Qt 4.1.

See also scopeId(), QNetworkInterface, and QNetworkInterface::interfaceFromName.

void QHostAddress::swap(QHostAddress &other)

Swaps this host address with other. This operation is very fast and never fails.

This function was introduced in Qt 5.6.

quint32 QHostAddress::toIPv4Address() const

Returns the IPv4 address as a number.

For example, if the address is 127.0.0.1, the returned value is 2130706433 (i.e. 0x7f000001).

This value is valid if the protocol() is IPv4Protocol, or if the protocol is IPv6Protocol, and the IPv6 address is an IPv4 mapped address. (RFC4291)

See also toString().

quint32 QHostAddress::toIPv4Address(bool *ok) const

Returns the IPv4 address as a number.

For example, if the address is 127.0.0.1, the returned value is 2130706433 (i.e. 0x7f000001).

This value is valid if the protocol() is IPv4Protocol, or if the protocol is IPv6Protocol, and the IPv6 address is an IPv4 mapped address. (RFC4291). In those cases, ok will be set to true. Otherwise, it will be set to false.

See also toString().

Q_IPV6ADDR QHostAddress::toIPv6Address() const

Returns the IPv6 address as a Q_IPV6ADDR structure. The structure consists of 16 unsigned characters.

Q_IPV6ADDR addr = hostAddr.toIPv6Address();
// addr contains 16 unsigned characters

for (int i = 0; i < 16; ++i) {
    // process addr[i]
}

This value is valid if the protocol() is IPv6Protocol. If the protocol is IPv4Protocol, then the address is returned an an IPv4 mapped IPv6 address. (RFC4291)

See also toString().

QString QHostAddress::toString() const

Returns the address as a string.

For example, if the address is the IPv4 address 127.0.0.1, the returned string is "127.0.0.1". For IPv6 the string format will follow the RFC5952 recommendation. For QHostAddress::Any, its IPv4 address will be returned ("0.0.0.0")

See also toIPv4Address().

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

Returns true if this host address is not the same as the other address given; otherwise returns false.

This function was introduced in Qt 4.2.

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

Returns true if this host address is not the same as the other address given; otherwise returns false.

QHostAddress &QHostAddress::operator=(QHostAddress::SpecialAddress address)

Assigns the special address address to this object, and returns a reference to this object.

This function was introduced in Qt 5.8.

See also setAddress().

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

Returns true if this host address is the same as the other address given; otherwise returns false. This operator just calls isEqual(other, StrictConversion).

See also isEqual().

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

Returns true if this host address is the same as the other address given; otherwise returns false.

Related Non-Members

uint qHash(const QHostAddress &key, uint seed = 0)

Returns a hash of the host address key, using seed to seed the calculation.

This function was introduced in Qt 5.0.

bool operator!=(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)

Returns false if special address lhs is the same as host address rhs; otherwise returns true.

This function was introduced in Qt 5.9.

See also isEqual().

QDataStream &operator<<(QDataStream &out, const QHostAddress &address)

Writes host address address to the stream out and returns a reference to the stream.

See also Serializing Qt Data Types.

bool operator==(QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)

Returns true if special address lhs is the same as host address rhs; otherwise returns false.

See also isEqual().

QDataStream &operator>>(QDataStream &in, QHostAddress &address)

Reads a host address into address from the stream in and returns a reference to the stream.

See also Serializing Qt Data Types.

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