QSocketNotifier

Synopsis

Functions

Slots

Signals

Detailed Description

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

The PySide.QtCore.QSocketNotifier makes it possible to integrate Qt’s event loop with other event loops based on file descriptors. For example, the CORBA Framework uses it to process CORBA events. 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. The socket notifier is enabled by default, i.e. it emits the PySide.QtCore.QSocketNotifier.activated() signal whenever a socket event corresponding to its type occurs. Connect the PySide.QtCore.QSocketNotifier.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 QSocketNotifier.Type enum, and must be specified when constructing the socket notifier. After construction it can be determined using the PySide.QtCore.QSocketNotifier.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 PySide.QtCore.QSocketNotifier.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 PySide.QtCore.QSocketNotifier.isEnabled() function to determine the notifier’s current status.

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

Notes for Windows Users

The socket passed to PySide.QtCore.QSocketNotifier will become non-blocking, even if it was created as a blocking socket. The PySide.QtCore.QSocketNotifier.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 PySide.QtCore.QSocketNotifier .

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

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

Further information: On Windows, Qt 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.

class PySide.QtCore.QSocketNotifier(arg__1, arg__2[, parent=None])
class PySide.QtCore.QSocketNotifier(socket, arg__2[, parent=None])
Parameters:

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 PySide.QtCore.QSocketNotifier will become non-blocking, even if it was created as a blocking socket.

PySide.QtCore.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 Description
QSocketNotifier.Read There is data to be read.
QSocketNotifier.Write Data can be written.
QSocketNotifier.Exception An exception has occurred. We recommend against using this.

See also

PySide.QtCore.QSocketNotifier.QSocketNotifier() PySide.QtCore.QSocketNotifier.type()

PySide.QtCore.QSocketNotifier.activated(socket)
Parameters:socketPySide.QtCore.int
PySide.QtCore.QSocketNotifier.isEnabled()
Return type:PySide.QtCore.bool

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

PySide.QtCore.QSocketNotifier.setEnabled(arg__1)
Parameters:arg__1PySide.QtCore.bool

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

The notifier is enabled by default, i.e. it emits the PySide.QtCore.QSocketNotifier.activated() signal whenever a socket event corresponding to its PySide.QtCore.QSocketNotifier.type() occurs. If 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 PySide.QtCore.QSocketNotifier.activated() signal has been emitted

PySide.QtCore.QSocketNotifier.socket()
Return type:PySide.QtCore.int

Returns the socket identifier specified to the constructor.

PySide.QtCore.QSocketNotifier.type()
Return type:PySide.QtCore.QSocketNotifier.Type

Returns the socket event type specified to the constructor.