|  | Home · All Classes · Modules

# QHostAddress Class Reference
[QtNetwork module]

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

### Types

- enum SpecialAddress { Null, Broadcast, LocalHost, LocalHostIPv6, Any, AnyIPv6 }

### Methods

- __init__ (self)
- __init__ (self, SpecialAddress)
- __init__ (self, int)
- __init__ (self, QString)
- __init__ (self, QHostAddress)
- clear (self)
- bool isInSubnet (self, QHostAddress, int)
- bool isInSubnet (self, tuple-of-QHostAddress-int)
- bool isNull (self)
- QAbstractSocket.NetworkLayerProtocol protocol (self)
- QString scopeId (self)
- setAddress (self, int)
- bool setAddress (self, QString)
- setScopeId (self, QString)
- int toIPv4Address (self)
- QString toString (self)

### Static Methods

- tuple-of-QHostAddress-int parseSubnet (QString)

### Special Methods

- bool __eq__ (self, QHostAddress)
- bool __eq__ (self, SpecialAddress)
- int __hash__ (self)
- bool __ne__ (self, QHostAddress)
- bool __ne__ (self, SpecialAddress)

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

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.

## Type Documentation

### QHostAddress.SpecialAddress

Constant | Value | Description
--- | --- | ---
`QHostAddress.Null` | `0` | The null address object. Equivalent to QHostAddress().
`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.Any` | `4` | The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0").
`QHostAddress.AnyIPv6` | `5` | The IPv6 any-address. Equivalent to QHostAddress(".").

## Method Documentation

### QHostAddress.__init__ (self)

Constructs a host address object with the IP address 0.0.0.0.

See also clear().

### QHostAddress.__init__ (self, SpecialAddress)

Constructs a host address object with the IPv4 address ip4Addr.

### QHostAddress.__init__ (self, int)

Constructs a host address object with the IPv6 address ip6Addr.

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

### QHostAddress.__init__ (self, QString)

Constructs a host address object with the IPv6 address ip6Addr.

### QHostAddress.__init__ (self, QHostAddress)

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

See also setAddress().

### QHostAddress.clear (self)

Sets the host address to 0.0.0.0.

### bool QHostAddress.isInSubnet (self, QHostAddress, int)

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 (self, tuple-of-QHostAddress-int)

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.isNull (self)

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.

### tuple-of-QHostAddress-int QHostAddress.parseSubnet (QString)

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

Returns the network layer protocol of the host address.

### QString QHostAddress.scopeId (self)

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

This function was introduced in Qt 4.1.

See also setScopeId().

### QHostAddress.setAddress (self, int)

Set the IPv4 address specified by ip4Addr.

### bool QHostAddress.setAddress (self, QString)

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

### QHostAddress.setScopeId (self, QString)

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

This function was introduced in Qt 4.1.

See also scopeId().

### int QHostAddress.toIPv4Address (self)

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.

See also toString().

### QString QHostAddress.toString (self)

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

See also toIPv4Address().

### bool QHostAddress.__eq__ (self, QHostAddress)

### bool QHostAddress.__eq__ (self, SpecialAddress)

### int QHostAddress.__hash__ (self)

### bool QHostAddress.__ne__ (self, QHostAddress)

### bool QHostAddress.__ne__ (self, SpecialAddress)