Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QHostAddress Class Reference

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

#include <qhostaddress.h>

+ Collaboration diagram for QHostAddress:

Public Types

enum  SpecialAddress {
  Null , Broadcast , LocalHost , LocalHostIPv6 ,
  Any , AnyIPv6 , AnyIPv4
}
 \value Null The null address object. More...
 
enum  ConversionModeFlag {
  ConvertV4MappedToIPv4 = 1 , ConvertV4CompatToIPv4 = 2 , ConvertUnspecifiedAddress = 4 , ConvertLocalHost = 8 ,
  TolerantConversion = 0xff , StrictConversion = 0
}
 
enum  NetworkLayerProtocol { IPv4Protocol , IPv6Protocol , AnyIPProtocol , UnknownNetworkLayerProtocol = -1 }
 

Public Member Functions

 QHostAddress ()
 Constructs a null host address object, i.e.
 
 QHostAddress (quint32 ip4Addr)
 Constructs a host address object with the IPv4 address ip4Addr.
 
 QHostAddress (const quint8 *ip6Addr)
 
 QHostAddress (const Q_IPV6ADDR &ip6Addr)
 Constructs a host address object with the IPv6 address ip6Addr.
 
 QHostAddress (const sockaddr *address)
 Constructs an IPv4 or IPv6 address using the address specified by the native structure sockaddr.
 
 QHostAddress (const QString &address)
 Constructs an IPv4 or IPv6 address based on the string address (e.g., "127.0.0.1").
 
 QHostAddress (const QHostAddress &copy)
 Constructs a copy of the given address.
 
 QHostAddress (SpecialAddress address)
 Constructs a QHostAddress object for address.
 
 ~QHostAddress ()
 Destroys the host address object.
 
QHostAddressoperator= (QHostAddress &&other) noexcept
 
QHostAddressoperator= (const QHostAddress &other)
 Assigns another host address to this object, and returns a reference to this object.
 
QHostAddressoperator= (SpecialAddress address)
 
void swap (QHostAddress &other) noexcept
 
void setAddress (quint32 ip4Addr)
 Set the IPv4 address specified by ip4Addr.
 
void setAddress (const quint8 *ip6Addr)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void setAddress (const Q_IPV6ADDR &ip6Addr)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Set the IPv6 address specified by ip6Addr.
 
void setAddress (const sockaddr *address)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the IPv4 or IPv6 address specified by the native structure sockaddr.
 
bool setAddress (const QString &address)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.g.
 
void setAddress (SpecialAddress address)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
NetworkLayerProtocol protocol () const
 Returns the network layer protocol of the host address.
 
quint32 toIPv4Address (bool *ok=nullptr) const
 Returns the IPv4 address as a number.
 
Q_IPV6ADDR toIPv6Address () const
 Returns the IPv6 address as a Q_IPV6ADDR structure.
 
QString toString () const
 Returns the address as a string.
 
QString scopeId () const
 
void setScopeId (const QString &id)
 
bool isEqual (const QHostAddress &address, ConversionMode mode=TolerantConversion) const
 
bool operator== (const QHostAddress &address) const
 Returns true if this host address is the same as the other address given; otherwise returns false.
 
bool operator== (SpecialAddress address) const
 Returns true if this host address is the same as the other address given; otherwise returns false.
 
bool operator!= (const QHostAddress &address) const
 
bool operator!= (SpecialAddress address) const
 Returns true if this host address is not the same as the other address given; otherwise returns false.
 
bool isNull () const
 Returns true if this host address is not valid for any host or interface.
 
void clear ()
 Sets the host address to null and sets the protocol to QAbstractSocket::UnknownNetworkLayerProtocol.
 
bool isInSubnet (const QHostAddress &subnet, int netmask) const
 
bool isInSubnet (const QPair< QHostAddress, int > &subnet) const
 
bool isLoopback () const
 
bool isGlobal () const
 
bool isLinkLocal () const
 
bool isSiteLocal () const
 
bool isUniqueLocalUnicast () const
 
bool isMulticast () const
 
bool isBroadcast () const
 
bool isPrivateUse () const
 

Static Public Member Functions

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

Protected Attributes

QExplicitlySharedDataPointer< QHostAddressPrivated
 

Friends

class QHostAddressPrivate
 
Q_NETWORK_EXPORT size_t qHash (const QHostAddress &key, size_t seed) noexcept
 
bool operator== (QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)
 Returns true if special address lhs is the same as host address rhs; otherwise returns false.
 
bool operator!= (QHostAddress::SpecialAddress lhs, const QHostAddress &rhs)
 

Related Symbols

(Note that these are not member symbols.)

size_t qHash (const QHostAddress &key, size_t seed) noexcept
 
QDataStreamoperator<< (QDataStream &out, const QHostAddress &address)
 Writes host address address to the stream out and returns a reference to the stream.
 
QDataStreamoperator>> (QDataStream &in, QHostAddress &address)
 Reads a host address into address from the stream in and returns a reference to the stream.
 

Detailed Description

The QHostAddress class provides an IP address.

\inmodule QtNetwork

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: \l Null, \l LocalHost, \l LocalHostIPv6, \l Broadcast, and \l Any.

See also
QHostInfo, QTcpSocket, QTcpServer, QUdpSocket

Definition at line 37 of file qhostaddress.h.

Member Enumeration Documentation

◆ ConversionModeFlag

Since
5.8

\value StrictConversion Don't convert IPv6 addresses to IPv4 when comparing two QHostAddress objects of different protocols, so they will always be considered different. \value ConvertV4MappedToIPv4 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"). \value ConvertV4CompatToIPv4 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"). \value ConvertLocalHost 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"). \value ConvertUnspecifiedAddress All unspecified addresses will compare equal, namely AnyIPv4, AnyIPv6 and Any. \value TolerantConversion Sets all three preceding flags.

See also
isEqual()
Enumerator
ConvertV4MappedToIPv4 
ConvertV4CompatToIPv4 
ConvertUnspecifiedAddress 
ConvertLocalHost 
TolerantConversion 
StrictConversion 

Definition at line 50 of file qhostaddress.h.

◆ NetworkLayerProtocol

Enumerator
IPv4Protocol 
IPv6Protocol 
AnyIPProtocol 
UnknownNetworkLayerProtocol 

Definition at line 68 of file qhostaddress.h.

◆ SpecialAddress

\value Null The null address object.

Equivalent to QHostAddress(). See also QHostAddress::isNull(). \value LocalHost The IPv4 localhost address. Equivalent to QHostAddress("127.0.0.1"). \value LocalHostIPv6 The IPv6 localhost address. Equivalent to QHostAddress("::1"). \value Broadcast The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255"). \value AnyIPv4 The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0"). A socket bound with this address will listen only on IPv4 interfaces. \value AnyIPv6 The IPv6 any-address. Equivalent to QHostAddress("::"). A socket bound with this address will listen only on IPv6 interfaces. \value Any The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces.

Enumerator
Null 
Broadcast 
LocalHost 
LocalHostIPv6 
Any 
AnyIPv6 
AnyIPv4 

Definition at line 41 of file qhostaddress.h.

Constructor & Destructor Documentation

◆ QHostAddress() [1/8]

QHostAddress::QHostAddress ( )

Constructs a null host address object, i.e.

an address which is not valid for any host or interface.

See also
clear()

Definition at line 379 of file qhostaddress.cpp.

Referenced by parseSubnet().

+ Here is the caller graph for this function:

◆ QHostAddress() [2/8]

QHostAddress::QHostAddress ( quint32 ip4Addr)
explicit

Constructs a host address object with the IPv4 address ip4Addr.

Definition at line 387 of file qhostaddress.cpp.

References setAddress().

+ Here is the call graph for this function:

◆ QHostAddress() [3/8]

QHostAddress::QHostAddress ( const quint8 * ip6Addr)
explicit
Since
5.5 Constructs a host address object with the IPv6 address ip6Addr.

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

Definition at line 400 of file qhostaddress.cpp.

References setAddress().

+ Here is the call graph for this function:

◆ QHostAddress() [4/8]

QHostAddress::QHostAddress ( const Q_IPV6ADDR & ip6Addr)
explicit

Constructs a host address object with the IPv6 address ip6Addr.

Definition at line 409 of file qhostaddress.cpp.

References setAddress().

+ Here is the call graph for this function:

◆ QHostAddress() [5/8]

QHostAddress::QHostAddress ( const sockaddr * sockaddr)
explicit

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

See also
setAddress()

◆ QHostAddress() [6/8]

QHostAddress::QHostAddress ( const QString & address)
explicit

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

See also
setAddress()

Definition at line 421 of file qhostaddress.cpp.

References d, and QHostAddressPrivate::parse().

+ Here is the call graph for this function:

◆ QHostAddress() [7/8]

QHostAddress::QHostAddress ( const QHostAddress & copy)

Constructs a copy of the given address.

Definition at line 447 of file qhostaddress.cpp.

◆ QHostAddress() [8/8]

QHostAddress::QHostAddress ( SpecialAddress address)

Constructs a QHostAddress object for address.

Definition at line 455 of file qhostaddress.cpp.

References setAddress().

+ Here is the call graph for this function:

◆ ~QHostAddress()

QHostAddress::~QHostAddress ( )

Destroys the host address object.

Definition at line 464 of file qhostaddress.cpp.

Member Function Documentation

◆ clear()

void QHostAddress::clear ( )

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

See also
QHostAddress::Null

Definition at line 520 of file qhostaddress.cpp.

References QHostAddressPrivate::clear(), d, and QExplicitlySharedDataPointer< T >::detach().

Referenced by QNetworkDatagram::clear(), QIpPacketHeader::clear(), QNativeSocketEnginePrivate::fetchConnectionParameters(), and setAddress().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isBroadcast()

bool QHostAddress::isBroadcast ( ) const
Since
5.11

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 \l QNetworkInterface to obtain the broadcast addresses of the local machine.

See also
isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse()

Definition at line 1214 of file qhostaddress.cpp.

References BroadcastAddress, QHostAddressPrivate::classify(), and d.

+ Here is the call graph for this function:

◆ isEqual()

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

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

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

See also
ConversionMode, operator==()

Definition at line 797 of file qhostaddress.cpp.

References QHostAddressPrivate::a, QHostAddressPrivate::a6, QHostAddressPrivate::a6_64, AnyIPProtocol, convertToIpv4(), ConvertUnspecifiedAddress, d, IPv4Protocol, IPv6Protocol, other(), QHostAddressPrivate::protocol, and UnknownNetworkLayerProtocol.

Referenced by QSslSocketPrivate::isMatchingHostname(), and operator==().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isGlobal()

bool QHostAddress::isGlobal ( ) const
Since
5.11

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 \l {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.

See also
isLoopback(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse()

Definition at line 1122 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, and GlobalAddress.

+ Here is the call graph for this function:

◆ isInSubnet() [1/2]

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

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).

See also
parseSubnet()

Definition at line 915 of file qhostaddress.cpp.

References QHostAddressPrivate::a, QHostAddressPrivate::a6, d, IPv4Protocol, IPv6Protocol, QHostAddressPrivate::protocol, and qToBigEndian().

Referenced by isInSubnet(), and QNetworkConnectionMonitorPrivate::setTargets().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isInSubnet() [2/2]

bool QHostAddress::isInSubnet ( const QPair< QHostAddress, int > & subnet) const
Since
4.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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).

Definition at line 963 of file qhostaddress.cpp.

References isInSubnet().

+ Here is the call graph for this function:

◆ isLinkLocal()

bool QHostAddress::isLinkLocal ( ) const
Since
5.11

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 \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml}{IANA IPv6 Address Space} registry for more information.

See also
isLoopback(), isGlobal(), isMulticast(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse()

Definition at line 1140 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, and LinkLocalAddress.

Referenced by QNetworkConnectionMonitorPrivate::setTargets().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isLoopback()

bool QHostAddress::isLoopback ( ) const
Since
5.0

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

Definition at line 1100 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, and LoopbackAddress.

Referenced by QGlobalNetworkProxy::proxyForQuery().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isMulticast()

bool QHostAddress::isMulticast ( ) const
Since
5.6

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

See also
isLoopback(), isGlobal(), isLinkLocal(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse()

Definition at line 1197 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, and MulticastAddress.

+ Here is the call graph for this function:

◆ isNull()

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

Definition at line 891 of file qhostaddress.cpp.

References d, QHostAddressPrivate::protocol, and UnknownNetworkLayerProtocol.

Referenced by applyNameServer(), and QNetworkConnectionMonitor::setTargets().

+ Here is the caller graph for this function:

◆ isPrivateUse()

bool QHostAddress::isPrivateUse ( ) const
Since
6.6

Returns true if the address is an IPv6 unique local unicast address or IPv4 address reserved for local networks by \l {RFC 1918}, false otherwise.

See also
isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isBroadcast()

Definition at line 1227 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, PrivateNetworkAddress, and UniqueLocalAddress.

+ Here is the call graph for this function:

◆ isSiteLocal()

bool QHostAddress::isSiteLocal ( ) const
Since
5.11

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 \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml}{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).

See also
isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse()

Definition at line 1163 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, and SiteLocalAddress.

+ Here is the call graph for this function:

◆ isUniqueLocalUnicast()

bool QHostAddress::isUniqueLocalUnicast ( ) const
Since
5.11

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 \l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml} {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.

See also
isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse()

Definition at line 1184 of file qhostaddress.cpp.

References QHostAddressPrivate::classify(), d, and UniqueLocalAddress.

+ Here is the call graph for this function:

◆ operator!=() [1/2]

bool QHostAddress::operator!= ( const QHostAddress & other) const
inline
Since
4.2

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

Definition at line 113 of file qhostaddress.h.

References operator==().

+ Here is the call graph for this function:

◆ operator!=() [2/2]

bool QHostAddress::operator!= ( SpecialAddress address) const
inline

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

Definition at line 115 of file qhostaddress.h.

References operator==().

+ Here is the call graph for this function:

◆ operator=() [1/3]

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

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

Definition at line 472 of file qhostaddress.cpp.

References d.

◆ operator=() [2/3]

QHostAddress & QHostAddress::operator= ( QHostAddress && other)
inlinenoexcept

Definition at line 87 of file qhostaddress.h.

References other(), and swap().

+ Here is the call graph for this function:

◆ operator=() [3/3]

QHostAddress & QHostAddress::operator= ( SpecialAddress address)
Since
5.8 Assigns the special address address to this object, and returns a reference to this object.
See also
setAddress()

Definition at line 485 of file qhostaddress.cpp.

References setAddress().

+ Here is the call graph for this function:

◆ operator==() [1/2]

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()

Definition at line 780 of file qhostaddress.cpp.

References d, isEqual(), other(), and StrictConversion.

+ Here is the call graph for this function:

◆ operator==() [2/2]

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

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

Definition at line 850 of file qhostaddress.cpp.

References QHostAddressPrivate::a, QHostAddressPrivate::a6_64, Any, AnyIPProtocol, AnyIPv4, AnyIPv6, Broadcast, d, IPv4Protocol, IPv6Protocol, LocalHost, LocalHostIPv6, Null, other(), QHostAddressPrivate::protocol, qToBigEndian(), and UnknownNetworkLayerProtocol.

+ Here is the call graph for this function:

◆ parseSubnet()

QPair< QHostAddress, int > QHostAddress::parseSubnet ( const QString & subnet)
static
Since
4.5

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: \list

  • 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 \endlist

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).

See also
isInSubnet()

Definition at line 993 of file qhostaddress.cpp.

References QHostAddress(), clearBits(), i, QStringView::mid(), ok, QNetmask::prefixLength(), qMakePair(), and QNetmask::setAddress().

Referenced by isBypassed(), and isHostExcluded().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ protocol()

◆ scopeId()

QString QHostAddress::scopeId ( ) const
Since
4.1

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:

\list

  • 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.

\endlist

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").

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

Definition at line 750 of file qhostaddress.cpp.

References d, IPv6Protocol, QHostAddressPrivate::protocol, and QHostAddressPrivate::scopeId.

◆ setAddress() [1/6]

void QHostAddress::setAddress ( const Q_IPV6ADDR & ip6Addr)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Set the IPv6 address specified by ip6Addr.

Definition at line 555 of file qhostaddress.cpp.

References d, QExplicitlySharedDataPointer< T >::detach(), and QHostAddressPrivate::setAddress().

+ Here is the call graph for this function:

◆ setAddress() [2/6]

bool QHostAddress::setAddress ( const QString & address)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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.

Definition at line 569 of file qhostaddress.cpp.

References d, QExplicitlySharedDataPointer< T >::detach(), and QHostAddressPrivate::parse().

+ Here is the call graph for this function:

◆ setAddress() [3/6]

void QHostAddress::setAddress ( const quint8 * ip6Addr)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.5

Set the IPv6 address specified by ip6Addr.

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

Definition at line 544 of file qhostaddress.cpp.

References d, QExplicitlySharedDataPointer< T >::detach(), and QHostAddressPrivate::setAddress().

+ Here is the call graph for this function:

◆ setAddress() [4/6]

void QHostAddress::setAddress ( const sockaddr * sockaddr)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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.

◆ setAddress() [5/6]

void QHostAddress::setAddress ( quint32 ip4Addr)

Set the IPv4 address specified by ip4Addr.

Definition at line 529 of file qhostaddress.cpp.

References d, QExplicitlySharedDataPointer< T >::detach(), and QHostAddressPrivate::setAddress().

Referenced by QHostAddress(), QHostAddress(), QHostAddress(), QHostAddress(), addressFromSockaddr(), addressFromSockaddr(), QAbstractSocket::connectToHost(), isBypassed(), isHostExcluded(), QHostInfoAgent::lookup(), operator=(), QGlobalNetworkProxy::proxyForQuery(), qt_socket_getPortAndAddress(), qt_socket_getPortAndAddress(), QHttpNetworkConnectionPrivate::startHostInfoLookup(), and QAbstractSocket::waitForConnected().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAddress() [6/6]

void QHostAddress::setAddress ( SpecialAddress address)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.8

Sets the special address specified by address.

Definition at line 599 of file qhostaddress.cpp.

References Any, AnyIPProtocol, AnyIPv4, AnyIPv6, Broadcast, clear(), d, LocalHost, LocalHostIPv6, Null, QHostAddressPrivate::protocol, Q_FALLTHROUGH, and QHostAddressPrivate::setAddress().

+ Here is the call graph for this function:

◆ setScopeId()

void QHostAddress::setScopeId ( const QString & id)
Since
4.1

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.

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

Definition at line 767 of file qhostaddress.cpp.

References d, QExplicitlySharedDataPointer< T >::detach(), IPv6Protocol, QHostAddressPrivate::protocol, and QHostAddressPrivate::scopeId.

+ Here is the call graph for this function:

◆ swap()

void QHostAddress::swap ( QHostAddress & other)
inlinenoexcept
Since
5.6

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

Definition at line 92 of file qhostaddress.h.

References d, and other().

+ Here is the call graph for this function:

◆ toIPv4Address()

quint32 QHostAddress::toIPv4Address ( bool * ok = nullptr) 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 \l{QAbstractSocket::}{IPv4Protocol}, or if the protocol is \l{QAbstractSocket::}{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()

Definition at line 651 of file qhostaddress.cpp.

References QHostAddressPrivate::a, QHostAddressPrivate::a6, AnyIPProtocol, convertToIpv4(), ConvertUnspecifiedAddress, ConvertV4MappedToIPv4, d, IPv4Protocol, IPv6Protocol, ok, and QHostAddressPrivate::protocol.

Referenced by dtlsutil::cookie_for_peer(), multicastMembershipHelper(), multicastMembershipHelper(), QNativeSocketEnginePrivate::nativeSetMulticastInterface(), and toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toIPv6Address()

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]
}
QIPv6Address Q_IPV6ADDR
GLenum const void * addr

This value is valid if the protocol() is \l{QAbstractSocket::}{IPv6Protocol}. If the protocol is \l{QAbstractSocket::}{IPv4Protocol}, then the address is returned as an IPv4 mapped IPv6 address. (RFC4291)

See also
toString()

Definition at line 684 of file qhostaddress.cpp.

References QHostAddressPrivate::a6, and d.

Referenced by dtlsutil::cookie_for_peer().

+ Here is the caller graph for this function:

◆ 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()

Definition at line 699 of file qhostaddress.cpp.

References QHostAddressPrivate::a6, AnyIPProtocol, d, i, IPv4Protocol, IPv6Protocol, QString::isEmpty(), QHostAddressPrivate::protocol, QHostAddressPrivate::scopeId, toIPv4Address(), and QIPAddressUtils::toString().

Referenced by QAbstractSocketPrivate::_q_connectToNextAddress(), QAbstractSocketPrivate::_q_startConnecting(), QAbstractSocketPrivate::fetchConnectionParameters(), QNativeSocketEnginePrivate::fetchConnectionParameters(), and QHttpSocketEngine::slotSocketConnected().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

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

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

See also
isEqual()

Definition at line 138 of file qhostaddress.h.

◆ operator<<()

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

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

See also
{Serializing Qt Data Types}

Definition at line 1284 of file qhostaddress.cpp.

References AnyIPProtocol, i, IPv4Protocol, IPv6Protocol, out, qint8, and UnknownNetworkLayerProtocol.

◆ operator==

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

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

See also
isEqual()

Definition at line 136 of file qhostaddress.h.

◆ operator>>()

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

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

See also
{Serializing Qt Data Types}

Definition at line 1315 of file qhostaddress.cpp.

References Any, AnyIPProtocol, QString::clear(), i, IPv4Protocol, IPv6Protocol, qint8, and UnknownNetworkLayerProtocol.

+ Here is the call graph for this function:

◆ qHash() [1/2]

size_t qHash ( const QHostAddress & key,
size_t seed )
related
Since
5.0

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

Definition at line 1251 of file qhostaddress.cpp.

◆ qHash [2/2]

size_t qHash ( const QHostAddress & key,
size_t seed = 0 )
friend
Since
5.0

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

Definition at line 1251 of file qhostaddress.cpp.

◆ QHostAddressPrivate

friend class QHostAddressPrivate
friend

Definition at line 142 of file qhostaddress.h.

Member Data Documentation

◆ d


The documentation for this class was generated from the following files: