6.0.0-rc4Source

QSocketNotifier Class

The QSocketNotifier class provides a way to watch activity on a file descriptor. More…

Header#include <QSocketNotifier>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQObject

List of all members, including inherited members

Public Types

enumType

Public Functions

QSocketNotifier(qintptr socket, Type type, QObject *parent = nullptr)
~QSocketNotifier()
qintptrsocket() const
Typetype() const
boolisEnabled() const

Public Slots

voidsetEnabled(bool enable)

Signals

voidactivated(QSocketDescriptor socket, QSocketNotifier::Type activationEvent)

Reimplemented Protected Functions

boolevent(QEvent *e)

Detailed Description

A QSocketNotifier emits activated() from the event loop when a file descriptor becomes readable, writable or has an exceptional condition. The notifier does not own the descriptor and does not read or write it; the slot must do so, otherwise activated() fires again on the next pass.

QSocketNotifier notifier(fd, QSocketNotifier::Read);
QObject::connect(&notifier, &QSocketNotifier::activated, [](QSocketDescriptor fd) {
    drain(fd);
});

See also QSocketDescriptor and QEventLoop.

Differences from Qt

The notifier is serviced by the event loop of the thread it lives in, and moves with the object in QObject::moveToThread(). It can be enabled and disabled only from that thread. Descriptors are watched with poll(), so only POSIX descriptors are supported, and the Exception type watches for POLLPRI. There is no constructor taking only a type, no setSocket(), and the type of the activated() signal's first argument is always QSocketDescriptor.

On Zephyr the notifiers of a thread are waited for in one poll() if CONFIG_ZVFS_POLL_MAX allows, and in groups otherwise, which delays a socket by a few milliseconds per group. See Nt on Zephyr.

Member Type Documentation

enum QSocketNotifier::Type

This enum describes the conditions a notifier watches for.

ConstantDescription
QSocketNotifier::ReadThe descriptor has data to read.
QSocketNotifier::WriteThe descriptor can be written without blocking.
QSocketNotifier::ExceptionAn exceptional condition, such as out-of-band data, occurred.

Member Function Documentation

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

Constructs a notifier that watches socket for the condition type. The notifier is enabled and has the given parent.

[override] QSocketNotifier::~QSocketNotifier()

Destroys the notifier and stops watching the descriptor. The descriptor is not closed.

[signal] void QSocketNotifier::activated(QSocketDescriptor socket, QSocketNotifier::Type activationEvent)

This signal is emitted when the watched condition occurs. socket is the descriptor and activationEvent is the notifier type.

See also type().

[override, protected] bool QSocketNotifier::event(QEvent *e)

Reimplements QObject::event().

bool QSocketNotifier::isEnabled() const

Returns true if the notifier is enabled; otherwise returns false. Notifiers are enabled when constructed.

See also setEnabled().

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

Enables the notifier if enable is true; otherwise disables it. A disabled notifier does not emit activated().

See also isEnabled().

qintptr QSocketNotifier::socket() const

Returns the descriptor the notifier watches.

Type QSocketNotifier::type() const

Returns the condition the notifier watches for.