QSocketNotifier 类提供监视文件描述符活动的支持。 更多...
头: | #include <QSocketNotifier> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
继承: | QObject |
enum | Type { Read, Write, Exception } |
QSocketNotifier (qintptr socket , QSocketNotifier::Type type , QObject * parent = nullptr) | |
QSocketNotifier (QSocketNotifier::Type type , QObject * parent = nullptr) | |
virtual | ~QSocketNotifier () |
bool | isEnabled () const |
bool | isValid () const |
void | setSocket (qintptr socket ) |
qintptr | socket () const |
QSocketNotifier::Type | type () const |
void | setEnabled (bool enable ) |
void | activated (QSocketDescriptor socket , QSocketNotifier::Type type ) |
virtual bool | event (QEvent * e ) override |
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 ()).
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 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 ( 读取 , 写入 , 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 () 函数以确定通知的当前状态。
最后,可以使用 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.
另请参阅 QFile , QProcess , QTcpSocket ,和 QUdpSocket .
此枚举描述套接字通知程序可以识别的各种类型的事件。必须指定类型当构造套接字通知程序时。
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
|
发生异常。建议不要使用此。 |
另请参阅 QSocketNotifier () 和 type ().
构造套接字通知程序采用给定 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.
Windows 用户注意: The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket.
另请参阅 setEnabled () 和 isEnabled ().
[since 6.1]
QSocketNotifier::
QSocketNotifier
(
QSocketNotifier::Type
type
,
QObject
*
parent
= nullptr)
构造套接字通知程序采用给定 type that has no descriptor assigned. The parent 自变量会被传递给 QObject 的构造函数。
调用 setSocket () 函数设置要监视的描述符。
该函数在 Qt 6.1 引入。
另请参阅 setSocket (), isValid (),和 isEnabled ().
[private signal, since 5.15]
void
QSocketNotifier::
activated
(
QSocketDescriptor
socket
,
QSocketNotifier::Type
type
)
此信号被发射每当套接字通知程序被启用时,且套接字事件对应 type 出现。
套接字标识符被传入 socket 参数。
注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。
注意: 信号 activated 在此类中被重载。通过使用函数指针句法连接到此信号,Qt 提供用于获得如此范例展示的函数指针的方便帮助程序:
connect(socketNotifier, QOverload<QSocketDescriptor, QSocketNotifier::Type>::of(&QSocketNotifier::activated), [=](QSocketDescriptor socket, QSocketNotifier::Type type){ /* ... */ });
该函数在 Qt 5.15 引入。
[slot]
void
QSocketNotifier::
setEnabled
(
bool
enable
)
若 enable 为 true,通知被启用;否则通知被禁用。
When the notifier is enabled, it emits the activated () signal whenever a socket event corresponding to its 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
另请参阅 isEnabled () 和 activated ().
[虚拟]
QSocketNotifier::
~QSocketNotifier
()
销毁此套接字通知程序。
[override virtual protected]
bool
QSocketNotifier::
event
(
QEvent
*
e
)
重实现: QObject::event (QEvent *e).
返回
true
若通知被启用;否则返回
false
.
另请参阅 setEnabled ().
[since 6.1]
bool
QSocketNotifier::
isValid
() const
返回
true
if the notifier is valid (that is, it has a descriptor assigned); otherwise returns
false
.
该函数在 Qt 6.1 引入。
另请参阅 setSocket ().
[since 6.1]
void
QSocketNotifier::
setSocket
(
qintptr
socket
)
赋值 socket to this notifier.
注意: The notifier will be disabled as a side effect and needs to be re-enabled.
该函数在 Qt 6.1 引入。
另请参阅 socket (), setEnabled (),和 isValid ().
Returns the socket identifier assigned to this object.
另请参阅 setSocket (), isValid (),和 type ().
Returns the socket event type specified to the constructor.
另请参阅 socket ().