QHostAddress Class

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

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

Public Types

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

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::ConversionModeFlag

ConstantValueDescription
QHostAddress::StrictConversion0Don't convert IPv6 addresses to IPv4 when comparing two QHostAddress objects of different protocols, so they will always be considered different.
QHostAddress::ConvertV4MappedToIPv41Convert 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::ConvertV4CompatToIPv42Convert 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::ConvertLocalHost8Convert 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::ConvertUnspecifiedAddress4All unspecified addresses will compare equal, namely AnyIPv4, AnyIPv6 and Any.
QHostAddress::TolerantConversion0xffSets all three preceding flags.

This enum was introduced or modified in Qt 5.8.

See also isEqual().

enum QHostAddress::SpecialAddress

ConstantValueDescription
QHostAddress::Null0The null address object. Equivalent to QHostAddress(). See also QHostAddress::isNull().
QHostAddress::LocalHost2The IPv4 localhost address. Equivalent to QHostAddress("127.0.0.1").
QHostAddress::LocalHostIPv63The IPv6 localhost address. Equivalent to QHostAddress("::1").
QHostAddress::Broadcast1The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255").
QHostAddress::AnyIPv46The IPv4 any-address. Equivalent to QHostAddress("0.0.0.0"). A socket bound with this address will listen only on IPv4 interfaces.
QHostAddress::AnyIPv65The IPv6 any-address. Equivalent to QHostAddress("::"). A socket bound with this address will listen only on IPv6 interfaces.
QHostAddress::Any4The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces.