Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QSocketNotifier Class Reference

\inmodule QtCore More...

#include <qsocketnotifier.h>

+ Inheritance diagram for QSocketNotifier:
+ Collaboration diagram for QSocketNotifier:

Public Types

enum  Type { Read , Write , Exception }
 This enum describes the various types of events that a socket notifier can recognize. More...
 

Public Slots

void setEnabled (bool)
 If enable is true, the notifier is enabled; otherwise the notifier is disabled.
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Signals

void activated (QSocketDescriptor socket, QSocketNotifier::Type activationEvent, QPrivateSignal)
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QSocketNotifier (Type, QObject *parent=nullptr)
 
 QSocketNotifier (qintptr socket, Type, QObject *parent=nullptr)
 Constructs a socket notifier with the given parent.
 
 ~QSocketNotifier ()
 Destroys this socket notifier.
 
void setSocket (qintptr socket)
 
qintptr socket () const
 Returns the socket identifier assigned to this object.
 
Type type () const
 Returns the socket event type specified to the constructor.
 
bool isValid () const
 
bool isEnabled () const
 Returns true if the notifier is enabled; otherwise returns false.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
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
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Protected Member Functions

bool event (QEvent *) override
 \reimp
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
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)
 \threadsafe
 
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::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a 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
 

Detailed Description

\inmodule QtCore

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

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 13 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 19 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 123 of file qsocketnotifier.cpp.

◆ 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 147 of file qsocketnotifier.cpp.

◆ ~QSocketNotifier()

QSocketNotifier::~QSocketNotifier ( )

Destroys this socket notifier.

Definition at line 169 of file qsocketnotifier.cpp.

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

◆ event()

bool QSocketNotifier::event ( QEvent * e)
overrideprotectedvirtual

\reimp

Reimplemented from QObject.

Reimplemented in QWriteNotifier, and QWriteNotifier.

Definition at line 310 of file qsocketnotifier.cpp.

◆ isEnabled()

bool QSocketNotifier::isEnabled ( ) const

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

See also
setEnabled()

Definition at line 262 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 251 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 283 of file qsocketnotifier.cpp.

◆ 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 213 of file qsocketnotifier.cpp.

◆ socket()

qintptr QSocketNotifier::socket ( ) const

Returns the socket identifier assigned to this object.

See also
isValid(), type()

Definition at line 226 of file qsocketnotifier.cpp.

◆ type()

QSocketNotifier::Type QSocketNotifier::type ( ) const

Returns the socket event type specified to the constructor.

See also
socket()

Definition at line 237 of file qsocketnotifier.cpp.


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