QSocketNotifier 類提供監視文件描述符活動的支持。 更多...
| 頭: |
#include <QSocketNotifier>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| 繼承: | QObject |
| enum | Type { Read, Write, Exception } |
(從 6.1 起)
|
QSocketNotifier (QSocketNotifier::Type type , QObject * parent = nullptr) |
| QSocketNotifier (qintptr socket , QSocketNotifier::Type type , QObject * parent = nullptr) | |
| virtual | ~QSocketNotifier () |
| bool | isEnabled () const |
(從 6.1 起)
bool
|
isValid () const |
(從 6.1 起)
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.
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 () 函數以確定通知的當前狀態。
最後,可以使用 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 ().
[explicit, 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 ().
構造套接字通知程序采用給定 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 ().
[virtual noexcept]
QSocketNotifier::
~QSocketNotifier
()
銷毀此套接字通知程序。
[private signal]
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){ /* ... */ });
[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 ().
[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 ().
[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 ().