CopperSpice API  1.9.1
QSocketNotifier Class Reference

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

Inheritance diagram for QSocketNotifier:
QObject

Public Types

enum  Type
 

Public Signals

void activated (int socket)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

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

Public Methods

 QSocketNotifier (qintptr socket, Type type, QObject *parent=nullptr)
 
 ~QSocketNotifier ()
 
bool isEnabled () const
 
qintptr socket () const
 
Type type () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

bool event (QEvent *event) override
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QSocketNotifier class provides support for monitoring activity on a file descriptor. This class makes it possible to integrate the event loop with other event loops based on file descriptors. File descriptor action is detected in the CopperSpice main event loop from 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. 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.

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() method. If you need to monitor both reads and writes for the same file descriptor, you must create two socket notifiers. It is not possible to install two socket notifiers of the same type (Read, Write, Exception) on the same socket.

The setEnabled() method 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.

Windows OS

The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket. The activated() signal is sometimes triggered by high general activity on the host, even if there is nothing to read. A subsequent read from the socket can then fail, the error indicating that there is no data available (e.g., WSAEWOULDBLOCK). This is an operating system limitation and not a bug in QSocketNotifier.

To ensure the socket notifier handles read notifications correctly, follow these steps when you receive a notification.

  1. Disable the notifier.
  2. Read data from the socket.
  3. Re-enable the notifier if you are interested in more data (such as after having written a new command to a remote server).

To ensure the socket notifier handles write notifications correctly, follow these steps when you receive a notification.

  1. Disable the notifier.
  2. Write as much data as you can (before EWOULDBLOCK is returned).
  3. Re-enable notifier if you have more data to write.

On Windows, CopperSpice always disables the notifier after getting a notification, and only re-enables it if more data is expected. For example, if data is read from the socket and it can be used to read more, or if reading or writing is not possible because the socket would block, in which case it is necessary to wait before attempting to read or write again.

See also
QFile, QProcess, QTcpSocket, QUdpSocket

Member Enumeration Documentation

This enum describes the various types of events that a socket notifier can recognize. The type must be specified when constructing the socket notifier. If you need to monitor both reads and writes for the same file descriptor, you must create two socket notifiers. It is not possible to install two socket notifiers of the same type (Read, Write, Exception) on the same socket.

ConstantValueDescription
QSocketNotifier::Read0There is data to be read.
QSocketNotifier::Write1Data can be written.
QSocketNotifier::Exception2An exception has occurred. We recommend against using this.
See also
QSocketNotifier(), type()

Constructor & Destructor Documentation

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.

On Windows, the socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket.

See also
setEnabled(), isEnabled()
QSocketNotifier::~QSocketNotifier ( )

Destroys this socket notifier.

Method Documentation

void QSocketNotifier::activated ( int  socket)
signal

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()
bool QSocketNotifier::event ( QEvent event)
overrideprotectedvirtual

Reimplemented from QObject::event()

bool QSocketNotifier::isEnabled ( ) const

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

See also
setEnabled()
void QSocketNotifier::setEnabled ( bool  enable)
slot

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

The notifier is enabled by default and emits the activated() signal whenever a socket event corresponding to its type occurs. If it is disabled, it ignores socket events which is 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()
qintptr QSocketNotifier::socket ( ) const

Returns the socket identifier specified to the constructor.

See also
type()
Type QSocketNotifier::type ( ) const

Returns the socket event type specified to the constructor.

See also
socket()