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.