qt中socket中错误获取的方法

7 篇文章 0 订阅

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<QAbstractSocket::SocketError>::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<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
      [=](QAbstractSocket::SocketError socketError){ /* ... */ });


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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶是江湖中人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值