QTcpSocket和QSocketNotifier配合使用出現問題,表現為能接收到QSocketNotifier::activated(int)信號,但是從QTcpSocket中讀資料時始侄訓傳0。
代碼如下:
// class TcpServer : public QTcpServer
void TcpServer::incomingConnection(qintptr socketDescriptor)
{
QString connName = ...
TcpConnectionPtr conn(new TcpConnection(connName, socketDescriptor));
......
}
TcpConnection::TcpConnection(const QString &name, qintptr sockfd)
: _name(name),
_socket(new QTcpSocket(this)),
_readNotifier(new QSocketNotifier(sockfd, QSocketNotifier::Read, this)),
_writeNotifier(new QSocketNotifier(sockfd, QSocketNotifier::Write, this)),
...
{
_socket->setSocketDescriptor(sockfd);
_socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
_readNotifier->setEnabled(true);
_writeNotifier->setEnabled(false);
connect(_readNotifier, &QSocketNotifier::activated,
this, &TcpConnection::handleRead,
Qt::DirectConnection);
connect(_writeNotifier, &QSocketNotifier::activated,
this, &TcpConnection::handleWrite,
Qt::DirectConnection);
...
}
毫無頭緒,請大家幫忙!
uj5u.com熱心網友回復:
去掉_readNotifier,連接TCPSocket::readReady信號就可以了uj5u.com熱心網友回復:
@ljq1000 關于海圖有問題請教,能留個聯系方式嗎?我的Q-Q 122-635-224uj5u.com熱心網友回復:
看下Qt幫助檔案(QSocketNotifier ):Finally, you can use the socket() function to retrieve the socket identifier. Although the class is called QSocketNotifier, it is normally used for other types of devices than sockets. QTcpSocket and QUdpSocket provide notification through signals, so there is normally no need to use a QSocketNotifier on them.
QTcpSocket 和 QUdpSocket 自身就有readyRead()和bytesWritten(qint64 bytes)信號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/130720.html
標籤:Qt
上一篇:oc byte函式轉換
下一篇:終端運行qt 會報段錯誤
