栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

qt中socket中错误获取的方法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

qt中socket中错误获取的方法

qt5.15之前是使用QAbstractSocket::error

5.15版本以后就可以使用 errorOccurred 了,这时就可以使用 connect愉快的绑定了

connect(this,&QIODevice::readyRead,this,&ClientSocket::slotReadData);
connect(this,&QAbstractSocket::disconnected,this,&ClientSocket::slotDisconnect);
connect(this,&QAbstractSocket::connected,this,&ClientSocket::slotConnect);
#if (QT_VERSION <= QT_VERSION_CHECK(5,15,0))
    connect(this,QOverload::of(&QAbstractSocket::error), this, &ClientSocket::slotErr);
#else
    connect(this,&QAbstractSocket::errorOccurred,this,&ClientSocket::slotErr);
#endif

附官方文档介绍

 void QAbstractSocket::errorOccurred(QAbstractSocket::SocketError socketError)
This signal is emitted after an error occurred. The socketError parameter describes the type of error that occurred.
When this signal is emitted, the socket may not be ready for a reconnect attempt. In that case, attempts to reconnect should be done from the event loop. For example, use a QTimer::singleShot() with 0 as the timeout.
QAbstractSocket::SocketError is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_metaTYPE() and qRegistermetaType().
This function was introduced in Qt 5.15.

void QAbstractSocket::error(QAbstractSocket::SocketError socketError)
This signal is emitted after an error occurred. The socketError parameter describes the type of error that occurred.
When this signal is emitted, the socket may not be ready for a reconnect attempt. In that case, attempts to reconnect should be done from the event loop. For example, use a QTimer::singleShot() with 0 as the timeout.
QAbstractSocket::SocketError is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_metaTYPE() and qRegistermetaType().
Note: Signal error is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:
  connect(abstractSocket, QOverload::of(&QAbstractSocket::error),
      [=](QAbstractSocket::SocketError socketError){ });


See also error(), errorString(), and Creating Custom Qt Types.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716962.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号