简介
QSocketNotifier用于处理网络事件的,即事件处理器
结构
qintptr socket():表示对应的网络套接字
Type type():表示关注的事件类型,使用枚举表示
enum Type { Read, Write, Exception }
bool isEnabled():表示对应的是否注册到事件分发顺器中
void setEnabled(bool):是槽函数,表示是否将套接字注册到事件分发器中
void QSocketNotifier::setEnabled(bool enable)
{Q_D(QSocketNotifier);if (d->sockfd < 0)return;if (d->snenabled == enable) return;d->snenabled = enable;if (!d->threadData->hasEventDispatcher())return;if (Q_UNLIKELY(thread() != QThread::currentThread())) {return;}if (d->snenabled)d->threadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);elsed->threadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
}
void activated(int socket, QPrivateSignal):信号,在event事件处理函数中,如果事件类型为QEvent::SockAct
或者QEvent::SockClose
,会触发该信号
bool event(QEvent *):事件处理函数
bool QSocketNotifier::event(QEvent *e)
{Q_D(QSocketNotifier);// Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is// received.if (e->type() == QEvent::ThreadChange) {if (d->snenabled) {QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection,Q_ARG(bool, d->snenabled));setEnabled(false);}}QObject::event(e); // will activate filtersif ((e->type() == QEvent::SockAct) || (e->type() == QEvent::SockClose)) {emit activated(d->sockfd, QPrivateSignal());return true;}return false;
}