QSocketNotifier Class | Qt Core | Qt 6.10.3
Description: The QSocketNotifier class provides support for monitoring activity on a file descriptor.

- Qt 6.10
- Qt Core
- C++ Classes
- QSocketNotifier
- Qt 6.10.3 Reference Documentation

# QSocketNotifier Class

The QSocketNotifier class provides support for monitoring activity on a file descriptor. More...

Header: | `#include <QSocketNotifier>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`
Inherits: | QObject

- List of all members, including inherited members
- Deprecated members
- QSocketNotifier is part of Network Programming API and Input/Output and Networking.

## Public Types

enum | Type { Read, Write, Exception }

## Public Functions

`(since 6.1)` | QSocketNotifier(QSocketNotifier::Type type, QObject *parent = nullptr)
| QSocketNotifier(qintptr socket, QSocketNotifier::Type type, QObject *parent = nullptr)
virtual | ~QSocketNotifier()
bool | isEnabled() const
`(since 6.1)` bool | isValid() const
`(since 6.1)` void | setSocket(qintptr socket)
qintptr | socket() const
QSocketNotifier::Type | type() const

## Public Slots

void | setEnabled(bool enable)

## Signals

void | activated(QSocketDescriptor socket, QSocketNotifier::Type type)

## Reimplemented Protected Functions

virtual bool | event(QEvent *e) override

## Detailed Description

The QSocketNotifier makes it possible to integrate Qt's event loop with other event loops based on file descriptors. File descriptor action is detected in Qt's main event loop (QCoreApplication::exec()).

Once you have opened a device using a low-level (usually platform-specific) API, you can create a socket notifier to monitor the file descriptor. If the descriptor is passed to the notifier's constructor, the socket notifier is enabled by default, i.e. it emits the activated() signal whenever a socket event corresponding to its type occurs. Connect the activated() signal to the slot you want to be called when an event corresponding to your socket notifier's type occurs.

You can create a socket notifier with no descriptor assigned. In this case, you should call the setSocket() function after the descriptor has been obtained.

There are three types of socket notifiers: read, write, and exception. The type is described by the Type enum, and must be specified when constructing the socket notifier. After construction it can be determined using the type() function. Note that if you need to monitor both reads and writes for the same file descriptor, you must create two socket notifiers. Note also that it is not possible to install two socket notifiers of the same type (Read, Write, Exception) on the same socket.

The setEnabled() function allows you to disable as well as enable the socket notifier. It is generally advisable to explicitly enable or disable the socket notifier, especially for write notifiers. A disabled notifier ignores socket events (the same effect as not creating the socket notifier). Use the isEnabled() function to determine the notifier's current status.

Finally, you can use the socket() function to retrieve the socket identifier. Although the class is called QSocketNotifier, it is normally used for other types of devices than sockets. QTcpSocket and QUdpSocket provide notification through signals, so there is normally no need to use a QSocketNotifier on them.

See also QFile, QProcess, QTcpSocket, and QUdpSocket.

## Member Type Documentation

### enum QSocketNotifier::Type

This enum describes the various types of events that a socket notifier can recognize. The type must be specified when constructing the socket notifier.

Note that if you need to monitor both reads and writes for the same file descriptor, you must create two socket notifiers. Note also that it is not possible to install two socket notifiers of the same type (Read, Write, Exception) on the same socket.

Constant | Value | Description
--- | --- | ---
`QSocketNotifier::Read` | `0` | There is data to be read.
`QSocketNotifier::Write` | `1` | Data can be written.
`QSocketNotifier::Exception` | `2` | An exception has occurred. We recommend against using this.

See also QSocketNotifier() and type().

## Member Function Documentation

### `[explicit, since 6.1]` QSocketNotifier::QSocketNotifier(QSocketNotifier::Type type, QObject *parent = nullptr)

Constructs a socket notifier with the given type that has no descriptor assigned. The parent argument is passed to QObject's constructor.

Call the setSocket() function to set the descriptor for monitoring.

This function was introduced in Qt 6.1.

See also setSocket(), isValid(), and isEnabled().

### QSocketNotifier::QSocketNotifier(qintptr socket, QSocketNotifier::Type type, QObject *parent = nullptr)

Constructs a socket notifier with the given parent. It enables the socket, and watches for events of the given type.

It is generally advisable to explicitly enable or disable the socket notifier, especially for write notifiers.

Note for Windows users: The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket.

See also setEnabled() and isEnabled().

### `[virtual noexcept]` QSocketNotifier::~QSocketNotifier()

Destroys this socket notifier.

### `[private signal]` void QSocketNotifier::activated(QSocketDescriptor socket, QSocketNotifier::Type type)

This signal is emitted whenever the socket notifier is enabled and a socket event corresponding to its type occurs.

The socket identifier is passed in the socket parameter.

Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.

Note: This signal is overloaded. To connect to this signal:

     // Connect using qOverload:
     connect(socketNotifier, qOverload(&QSocketNotifier::activated),
             receiver, &ReceiverClass::slot);

     // Or using a lambda:
     connect(socketNotifier, qOverload(&QSocketNotifier::activated),
             this, [](QSocketDescriptor socket, QSocketNotifier::Type type) { /* handle activated */ });

For more examples and approaches, see connecting to overloaded signals.

See also type() and socket().

### `[override virtual protected]` bool QSocketNotifier::event(QEvent *e)

Reimplements: QObject::event(QEvent *e).

### bool QSocketNotifier::isEnabled() const

Returns `true` if the notifier is enabled; otherwise returns `false`.

See also setEnabled().

### `[since 6.1]` bool QSocketNotifier::isValid() const

Returns `true` if the notifier is valid (that is, it has a descriptor assigned); otherwise returns `false`.

This function was introduced in Qt 6.1.

See also setSocket().

### `[slot]` void QSocketNotifier::setEnabled(bool enable)

If enable is true, the notifier is enabled; otherwise the notifier is disabled.

When the notifier is enabled, it emits the activated() signal whenever a socket event corresponding to its type occurs. When it is disabled, it ignores socket events (the same effect as not creating the socket notifier).

Write notifiers should normally be disabled immediately after the activated() signal has been emitted

See also isEnabled() and activated().

### `[since 6.1]` void QSocketNotifier::setSocket(qintptr socket)

Assigns the socket to this notifier.

Note: The notifier will be disabled as a side effect and needs to be re-enabled.

This function was introduced in Qt 6.1.

See also socket(), setEnabled(), and isValid().

### qintptr QSocketNotifier::socket() const

Returns the socket identifier assigned to this object.

See also setSocket(), isValid(), and type().

### QSocketNotifier::Type QSocketNotifier::type() const

Returns the socket event type specified to the constructor.

See also socket().