Clarify Q{Abstract,Local}Socket::waitForDisconnected() documentation

Make it clear that the functions will return false if the socket was already
disconnected.

Fix the QLocalSocket example snippet to handle that case correctly by checking
state() before attempting to wait.

Fixes: QTBUG-50711
Change-Id: I4ab4062446a0041a35a3a1d65a19202ffa103298
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Kari Oikarinen 2019-10-07 12:15:09 +03:00
parent 26f9cb7ce5
commit 2d680b27f3
4 changed files with 18 additions and 13 deletions

View File

@ -57,9 +57,10 @@ if (socket->waitForConnected(1000))
//! [1] //! [1]
socket->disconnectFromHost(); socket->disconnectFromHost();
if (socket->state() == QAbstractSocket::UnconnectedState || if (socket->state() == QAbstractSocket::UnconnectedState
socket->waitForDisconnected(1000)) || socket->waitForDisconnected(1000)) {
qDebug("Disconnected!"); qDebug("Disconnected!");
}
//! [1] //! [1]

View File

@ -57,6 +57,8 @@ if (socket->waitForConnected(1000))
//! [1] //! [1]
socket->disconnectFromServer(); socket->disconnectFromServer();
if (socket->waitForDisconnected(1000)) if (socket->state() == QLocalSocket::UnconnectedState
|| socket->waitForDisconnected(1000)) {
qDebug("Disconnected!"); qDebug("Disconnected!");
}
//! [1] //! [1]

View File

@ -2359,11 +2359,12 @@ bool QAbstractSocket::waitForBytesWritten(int msecs)
} }
/*! /*!
Waits until the socket has disconnected, up to \a msecs Waits until the socket has disconnected, up to \a msecs milliseconds. If the
milliseconds. If the connection has been disconnected, this connection was successfully disconnected, this function returns \c true;
function returns \c true; otherwise it returns \c false. In the case otherwise it returns \c false (if the operation timed out, if an error
where it returns \c false, you can call error() to determine occurred, or if this QAbstractSocket is already disconnected). In the case
the cause of the error. where it returns \c false, you can call error() to determine the cause of
the error.
The following example waits up to one second for a connection The following example waits up to one second for a connection
to be closed: to be closed:

View File

@ -287,11 +287,12 @@ QT_BEGIN_NAMESPACE
/*! /*!
\fn bool QLocalSocket::waitForDisconnected(int msecs) \fn bool QLocalSocket::waitForDisconnected(int msecs)
Waits until the socket has disconnected, up to \a msecs Waits until the socket has disconnected, up to \a msecs milliseconds. If the
milliseconds. If the connection has been disconnected, this connection was successfully disconnected, this function returns \c true;
function returns \c true; otherwise it returns \c false. In the case otherwise it returns \c false (if the operation timed out, if an error
where it returns \c false, you can call error() to determine occurred, or if this QLocalSocket is already disconnected). In the case
the cause of the error. where it returns \c false, you can call error() to determine the cause of
the error.
The following example waits up to one second for a connection The following example waits up to one second for a connection
to be closed: to be closed: