CopperSpice API  1.7.2
QHostAddress Class Reference

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

Inherited by QNetmaskAddress.

Public Types

enum  SpecialAddress
 

Public Methods

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

Static Public Methods

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

Friends

uint qHash (const QHostAddress &key, uint seed)
 

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &out, const QHostAddress &address)
 
QDataStreamoperator>> (QDataStream &in, QHostAddress &address)
 

Detailed Description

The QHostAddress class provides an IP address. 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().

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

Note
QHostAddress does not do a DNS lookup, use QHostInfo instead.
See also
QHostInfo, QTcpSocket, QTcpServer, QUdpSocket

Member Enumeration Documentation

ConstantValueDescription
QHostAddress::Null0 Null address object, equivalent to QHostAddress().
QHostAddress::Broadcast1 IPv4 broadcast address, equivalent to QHostAddress("255.255.255.255").
QHostAddress::LocalHost2 IPv4 localhost address, equivalent to QHostAddress("127.0.0.1").
QHostAddress::LocalHostIPv63 IPv6 localhost address, equivalent to QHostAddress("::1").
QHostAddress::Any4 Dual stack any-address. A socket bound with this address will listen only on both IPv4 and IPv6 interfaces.
QHostAddress::AnyIPv45 IPv4 any-address, equivalent to QHostAddress("0.0.0.0"). A socket bound with this address will listen only on IPv4 interfaces.
QHostAddress::AnyIPv66 IPv6 any-address, equivalent to QHostAddress("::"). A socket bound with this address will listen only on IPv6 interfaces.

Constructor & Destructor Documentation

QHostAddress::QHostAddress ( )

Constructs a host address object with the IP address 0.0.0.0.

See also
clear()
QHostAddress::QHostAddress ( quint32  ip4Addr)
explicit

Constructs a host address object with the IPv4 address ip4Addr.

QHostAddress::QHostAddress ( const quint8 ip6Addr)
explicit

Constructs a host address object with the IPv6 address ip6Addr. The address must be a 16-byte array in network byte order (big endian).

QHostAddress::QHostAddress ( const Q_IPV6ADDR &  ip6Addr)
explicit

Constructs a host address object with the IPv6 address ip6Addr.

QHostAddress::QHostAddress ( const sockaddr *  address)
explicit

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

See also
setAddress()
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()
QHostAddress::QHostAddress ( SpecialAddress  address)

Constructs a QHostAddress object for address.

QHostAddress::QHostAddress ( const QHostAddress &  other)

Copy constructs a new QHostAddress from other.

QHostAddress::~QHostAddress ( )

Destroys the host address object.

Method Documentation

void QHostAddress::clear ( )

Sets the host address to 0.0.0.0.

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 IPv4 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 method will always return true (provided the netmask is a valid value).

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

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

bool QHostAddress::isLoopback ( ) const

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

bool QHostAddress::isMulticast ( ) const

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

bool QHostAddress::isNull ( ) const

Returns true if this host address is null (INADDR_ANY or in6addr_any). The default constructor creates a null address, and that address is not valid for any host or interface.

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

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

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

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

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

Copy assigns from other and returns a reference to this object.

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= ( QHostAddress &&  other)
inline

Move assigns from other and returns a reference to this object.

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

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

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

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

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

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 method 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 IPv4 this method 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()
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 adapter, and are not guaranteed to be unique.
  • Site-local: Addresses that are local to the site / private network (e.g., the company intranet). Site-local addresses ("fec0...") are usually distributed by the site router, and are not guaranteed to be unique outside of the local site.
  • 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").

See also
setScopeId()
void QHostAddress::setAddress ( const Q_IPV6ADDR &  ip6Addr)

Set the IPv6 address specified by ip6Addr.

bool QHostAddress::setAddress ( const QString address)

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 ( const quint8 ip6Addr)

Set the IPv6 address specified by ip6Addr. The IP address must be an array of 16 bytes in network byte order high-order byte first).

void QHostAddress::setAddress ( const sockaddr *  sockAddr)

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.

void QHostAddress::setAddress ( quint32  ip4Addr)

Set the IPv4 address specified by ip4Addr.

void QHostAddress::setScopeId ( const QString id)

Sets the IPv6 scope ID of the address to id. If the address protocol is not IPv6 this method does nothing.

See also
scopeId()
void QHostAddress::swap ( QHostAddress &  other)
inline

Swaps the host address with other. This method is very fast and never fails.

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 only valid if the Protocol() is IPv4Protocol or if the protocol is IPv6Protocol and the IPv6 address is an IPv4 mapped address. 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 only valid if the protocol() is IPv6Protocol. If the protocol is IPv4Protocol then the address is returned as an IPv4 mapped IPv6 address.

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.

See also
toIPv4Address()

Friends And Related Function Documentation

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 Data Types for additional information
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 Data Types for additional information
uint qHash ( const QHostAddress &  key,
uint  seed = 0 
)
friend

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