Networking and Connectivity | Qt 5.14

Qt 5.14 | Networking and Connectivity

Qt 5.14.2 Reference Documentation

# Networking and Connectivity

Qt provides classes for both high-level and low-level network communication, classes for web integration, and classes for interprocess communication.

For high-level network traffic, Qt Network provides an abstraction layer over the operations used, showing only high-level classes and functions. Qt Network can also handle lower level protocols like TCP and UDP. Classes like QTcpSocket and QUdpSocket enable the developer to send and receive messages using the TCP or UDP protocol.Name resolution is done with QHostInfo. QHostInfo is called before creating a network connection with QTcpSocket or QUdpSocket. Filtering and redistributing network traffic via proxies can handled by the QNetWorkProxy class.

Qt Network also provides the Bearer Management API. It has functions that can start or stop network interfaces and roam between access points. It does however not take care of the network configurations itself. The platform needs to take care of that.

Qt WebEngine offers a Chromium-based web engine that enables using web content in your application. Qt WebEngine can render HTML5, XHTML, and SVG documents, and execute Javascript.

Qt also offers functionalities for inter-process communication (IPC). The class QProcess is used to start external programs. Qt D-Bus provides support for D-Bus, an interprocess communication and remoteprocedure calling mechanism. It mostly communicates via a central server application, called a bus. However, it is also possible to let applications communicate directly with each other. QSharedMemory provides access to a shared memory segment by multiple threads and processes. It is however also possible for a single process to exclusively lock that shared memory.

## Network Communication

Qt supports a wide range of network communication, with a focus on HTTP, TCP, and UDP.

At the HTTP level, the Qt Network module offers the network access API, which consists mainly of QNetworkRequest, QNetworkAccessManager, and QNetworkReply. Put simply, the QNetworkRequest resembles an HTTP request, which gets passed to the QNetworkAccessManager to send the request on the wire; this class returns a QNetworkReply, which enables parsing the HTTP reply. The network access API uses the socket classes mentioned below (for TCP and SSL) internally.

For communication at the socket level, QTcpSocket, QUdpSocket and QSslSocket should be used. These classes offer a synchronous API through the waitFor* methods as well as an asynchronous API; if possible (i.e. if the event loop is running), the asynchronous API should always be preferred. Qt also offers QTcpServer to enable the server-side part of a TCP communication. Please note that there is no HTTP server class in Qt.

For secure communication via SSL, Qt Network offers a wide range of classes alongside the central QSslSocket, e.g. QSslCertificate, QSslConfiguration and QSslError. The only supported backend for SSL in Qt is OpenSSL, which needs to be installed separately.

For mobile devices, Qt Network offers the bearer management API to track the status of a connection (e.g. getting notified about online/offline status or whether Wifi or 3G is used).

Qt also offers HTTP support in QML by either using HTTP explicitly via XmlHttpRequest, or by transparently using HTTP URLs in e.g. Image sources.

Related Topics:

- Qt Network - module documentation
  - Network Programming API
  - Network Programming with Qt
  - Qt Network C++ Classes
  - Secure Sockets Layer (SSL) Classes
  - Bearer Management
- Resource Loading and Network Transparency - network and URL transparency in QML

## WebSockets

An alternative to direct TCP or HTTP communication is to use the WebSocket protocol (RFC 6455). It is a two-way communication protocol on top of the TCP protocol to utilize existing web infrastructure without having to create additional client-server based communication. The Qt WebSockets module provides both a QML and C++ API, in addition to several examples to demonstrate its use.

## World Wide Web Support

Qt makes it easy to embed web content into your Qt application using features of the Qt WebEngine layout engine. The Qt WebEngine module equips Qt with support for a broad range of standard web technologies that make it possible to embed HTML content styled with CSS and scripted with JavaScript into your Qt application. Qt WebEngine facilitates both integration with traditional QWidget based desktop applications as well as embedding into Qt Quick QML applications.

For more information see Integrating Web Content.

## Inter-Process Communication

Qt provides several ways to implement Inter-Process Communication (IPC) in Qt applications.

The cross-platform Qt Network module provides classes that make network programming portable and easy. It offers high-level classes (e.g., QNetworkAccessManager, QFtp) that communicate using specific application-level protocols, and lower-level classes (e.g., QTcpSocket, QTcpServer, QSslSocket) for implementing protocols.

The cross-platform shared memory class, QSharedMemory, provides access to the operating system's shared memory implementation. It allows safe access to shared memory segments by multiple threads and processes. Additionally, QSystemSemaphore can be used to control access to resources shared by the system, as well as to communicate between processes.

The Qt D-Bus module is a cross-platform library you can use to implement IPC using the D-Bus protocol. It extends Qt's signals and slots mechanism to the IPC level, allowing a signal emitted by one process to be connected to a slot in another process.

The cross-platform class QProcess can be used to start external programs as child processes, and to communicate with them. It provides an API for monitoring and controlling the state of the child process. QProcess gives access to the input/output channels of child process by inheriting from QIODevice.

The QLocalSocket class provides a local socket. On Windows this is a named pipe and on Unix this is a local domain socket. The QLocalServer class provides a local socket based server. This class makes it possible to accept incoming local socket connections.

Related Topics:

- Qt D-Bus - module documentation
  - Using Qt D-Bus Adaptors
  - Declaring Signals in D-Bus Adaptors
  - Declaring Slots in D-Bus Adaptors
  - The Qt D-Bus Type System
  - Qt D-Bus XML compiler (qdbusxml2cpp)
- QLocalSocket
- QSharedMemory

## Serial Port Communication

The Qt Serial Port module provides a C++ API for communicating through serial ports, using the RS-232 standard. It works with physical ports and also with drivers that emulate these ports. Examples of serial port emulators include virtual COM ports, com0com emulators, and the Bluetooth SPP.

This module is designed to make serial port programming easier and portable. As of Qt 5.2, it is supported on Windows, macOS, and Linux.

Related topics:

- Qt Serial Port - module documentation
- Qt Serial Port C++ Classes

## Bluetooth Communication

The Qt Bluetooth module provides both C++ and QML APIs for the short-range (less than 100 meters) wireless protocol developed by the Bluetooth Special Interest Group. It provides classic Bluetooth and Bluetooth Low Energy features.

Related topics:

- Qt Bluetooth - module documentation
  - Bluetooth Classic
  - Bluetooth Low Energy
  - Qt Bluetooth C++ Classes
  - Qt Bluetooth QML Types

## Near Field Communication

The Qt NFC module provides both C++ and QML APIs for the extremely short-range (less than 20 centimeters) wireless protocol developed by the NFC Forum.

Related topics:

- Qt NFC - module documentation
  - Qt NFC Overview
  - Qt NFC C++ Classes
  - Qt NFC QML Types