The QSocketNotifier class provides support for monitoring activity on a file descriptor. 更多...
继承 QObject .
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. 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 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 () 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.
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.
最后,可以使用 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.
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 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:
进一步信息: 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.
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.
| 常量 | 值 | 描述 |
|---|---|---|
| QSocketNotifier.Read | 0 | 有数据要读取。 |
| QSocketNotifier.Write | 1 | 数据可以被写入。 |
| QSocketNotifier.Exception | 2 | An exception has occurred. We recommend against using this. |
另请参阅 QSocketNotifier () 和 type ().
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
Constructs a socket notifier with the given parent . It 启用 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.
Windows 用户注意: The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket.
另请参阅 setEnabled () 和 isEnabled ().
重实现自 QObject.event ().
Returns true if the notifier is enabled; otherwise returns false.
另请参阅 setEnabled ().
This method is also a Qt slot with the C++ signature void setEnabled(bool) .
若 enable is true, the notifier is enabled; otherwise the notifier is disabled.
通知默认是启用的,即,它发射 activated () signal whenever a socket event corresponding to its 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 activated () signal has been emitted
另请参阅 isEnabled () 和 activated ().
Returns the socket identifier specified to the constructor.
另请参阅 type ().
Returns the socket event type specified to the constructor.
另请参阅 socket ().
This is the default overload of this signal.
This signal is emitted whenever the socket notifier is enabled and a socket event corresponding to its type occurs.
套接字标识符被传入 socket 参数。