QtBase  v6.3.1
Public Types | Public Slots | Signals | Public Member Functions | Protected Member Functions | List of all members
QSocketNotifier Class Reference

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

#include <qsocketnotifier.h>

Inheritance diagram for QSocketNotifier:
Inheritance graph
[legend]
Collaboration diagram for QSocketNotifier:
Collaboration graph
[legend]

Public Types

enum  Type { Read , Write , Exception }
 

Public Slots

void setEnabled (bool)
 
- Public Slots inherited from QObject
void deleteLater ()
 

Signals

void activated (QSocketDescriptor socket, QSocketNotifier::Type activationEvent, QPrivateSignal)
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 

Public Member Functions

 QSocketNotifier (Type, QObject *parent=nullptr)
 
 QSocketNotifier (qintptr socket, Type, QObject *parent=nullptr)
 
 ~QSocketNotifier ()
 
void setSocket (qintptr socket)
 
qintptr socket () const
 
Type type () const
 
bool isValid () const
 
bool isEnabled () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 
virtual ~QObject ()
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const
 
void setObjectName (const QString &name)
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 
bool isWindowType () const
 
bool signalsBlocked () const noexcept
 
bool blockSignals (bool b) noexcept
 
QThreadthread () const
 
void moveToThread (QThread *thread)
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 
template<typename T >
T findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
const QObjectListchildren () const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 
void dumpObjectInfo () const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 
bool inherits (const char *classname) const
 

Protected Member Functions

bool event (QEvent *) override
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object More...
 

Detailed Description

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

\inmodule QtCore

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

\target write notifiers

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 \l 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 (\l Read, \l Write, \l 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, QUdpSocket

Definition at line 49 of file qsocketnotifier.h.

Member Enumeration Documentation

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

\value Read There is data to be read. \value Write Data can be written. \value Exception An exception has occurred. We recommend against using this.

See also
QSocketNotifier(), type()
Enumerator
Read 
Write 
Exception 

Definition at line 55 of file qsocketnotifier.h.

Constructor & Destructor Documentation

◆ QSocketNotifier() [1/2]

QSocketNotifier::QSocketNotifier ( Type  type,
QObject parent = nullptr 
)
explicit
Since
6.1

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.

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

Definition at line 158 of file qsocketnotifier.cpp.

Here is the call graph for this function:

◆ QSocketNotifier() [2/2]

QSocketNotifier::QSocketNotifier ( qintptr  socket,
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(), isEnabled()

Definition at line 182 of file qsocketnotifier.cpp.

Here is the call graph for this function:

◆ ~QSocketNotifier()

QSocketNotifier::~QSocketNotifier ( )

Destroys this socket notifier.

Definition at line 204 of file qsocketnotifier.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ activated

void QSocketNotifier::activated ( QSocketDescriptor  socket,
QSocketNotifier::Type  activationEvent,
QPrivateSignal   
)
signal
Deprecated:
To avoid unintended truncation of the descriptor, use the QSocketDescriptor overload of this function. If you need compatibility with versions older than 5.15 you need to change the slot to accept qintptr if it currently accepts an int, and then connect using Functor-Based Connection.

This signal is emitted whenever the socket notifier is enabled and a socket event corresponding to its \l {Type}{type} occurs.

The socket identifier is passed in the socket parameter.

See also
type(), socket()
Since
5.15

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.

See also
type(), socket()
Here is the caller graph for this function:

◆ event()

bool QSocketNotifier::event ( QEvent e)
overrideprotectedvirtual

\reimp

Reimplemented from QObject.

Reimplemented in QExceptionNotifier, QWriteNotifier, and QReadNotifier.

Definition at line 345 of file qsocketnotifier.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isEnabled()

bool QSocketNotifier::isEnabled ( ) const

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

See also
setEnabled()

Definition at line 297 of file qsocketnotifier.cpp.

◆ isValid()

bool QSocketNotifier::isValid ( ) const
Since
6.1

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

See also
setSocket()

Definition at line 286 of file qsocketnotifier.cpp.

◆ setEnabled

void QSocketNotifier::setEnabled ( bool  enable)
slot

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 \l{type()}{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(), activated()

Definition at line 318 of file qsocketnotifier.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSocket()

void QSocketNotifier::setSocket ( qintptr  socket)
Since
6.1

Assigns the socket to this notifier.

Note
The notifier will be disabled as a side effect and needs to be re-enabled.
See also
setEnabled(), isValid()

Definition at line 248 of file qsocketnotifier.cpp.

Here is the call graph for this function:

◆ socket()

qintptr QSocketNotifier::socket ( ) const

Returns the socket identifier assigned to this object.

See also
isValid(), type()

Definition at line 261 of file qsocketnotifier.cpp.

Here is the caller graph for this function:

◆ type()

QSocketNotifier::Type QSocketNotifier::type ( ) const

Returns the socket event type specified to the constructor.

See also
socket()

Definition at line 272 of file qsocketnotifier.cpp.

Here is the caller graph for this function:

The documentation for this class was generated from the following files: