QSocketNotifier Class

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

头文件: #include <QSocketNotifier>
qmake: QT += core
基类: QObject

公有类型

enum Type { Read, Write, Exception }

公有函数

QSocketNotifier(qintptr socket, Type type, QObject *parent = Q_NULLPTR)
~QSocketNotifier()
bool isEnabled() const
qintptr socket() const
Type type() const
  • 32 个公有函数继承自 QObject

公有槽函数

void setEnabled(bool enable)
  • 1 个公有槽函数继承自 QObject

信号

void activated(int socket)

重新实现的受保护函数

virtual bool event(QEvent *e)
  • 9 个受保护的函数继承自 QObject

其他继承的成员

  • 1 个属性继承自 QObject
  • 11 个静态公有成员继承自 QObject
  • 9 个受保护的函数继承自 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. 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.

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() 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.

参见 QFile, QProcess, QTcpSocket, and QUdpSocket.

成员类型

enum 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.

ConstantValueDescription
QSocketNotifier::Read0There is data to be read.
QSocketNotifier::Write1Data can be written.
QSocketNotifier::Exception2An exception has occurred. We recommend against using this.

参见 QSocketNotifier() and type().

成员函数

QSocketNotifier::QSocketNotifier(qintptr socket, Type type, QObject *parent = Q_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.

Note for Windows users: The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket.

参见 setEnabled() and isEnabled().

QSocketNotifier::~QSocketNotifier()

Destroys this socket notifier.

[signal] void QSocketNotifier::activated(int socket)

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.

Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.

参见 type() and socket().

[virtual protected] bool QSocketNotifier::event(QEvent *e)

Reimplemented from QObject::event().

bool QSocketNotifier::isEnabled() const

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

参见 setEnabled().

[slot] void QSocketNotifier::setEnabled(bool enable)

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

The notifier is enabled by default, i.e. it emits the 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() and activated().

qintptr QSocketNotifier::socket() const

Returns the socket identifier specified to the constructor.

参见 type().

Type QSocketNotifier::type() const

Returns the socket event type specified to the constructor.

参见 socket().