QTcpSocket中的readyReady信号触发不了

上善若水邻 2010-09-24 02:54:49
我用QTcpSocket写了一个程序与QT自带的示例程序连接成功,但是却无法触发readyRead()信号。请问这是什么问题??

#include "myclient2.h"

MyClient2::MyClient2(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
m_TcpSocket = new QTcpSocket(this);
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(CF_Connect()));
//connect(m_TcpSocket,SIGNAL(hostFound()),this,SLOT(CF_HostFound()));
connect(m_TcpSocket,SIGNAL(readyRead()),this,SLOT(CF_ReadyRead()));
connect(m_TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}

MyClient2::~MyClient2()
{

}
void MyClient2::CF_Connect()
{
m_iBlockSize =0;
m_TcpSocket->abort();
//connect(m_TcpSocket,SIGNAL(disconnected()),m_TcpSocket,SLOT(deleteLater()));
m_TcpSocket->connectToHost(ui.Caddress->text(),ui.Cport->text().toInt());

}
void MyClient2::CF_ReadyRead()
{

QMessageBox::information(NULL,tr("Yes Connect"),tr("YES"));
QDataStream in(m_TcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if(m_iBlockSize == 0)
{
if(m_TcpSocket->bytesAvailable() < (int)sizeof(quint16))
{
return ;
}
}
in>>m_iBlockSize;
if(m_TcpSocket->bytesAvailable() < m_iBlockSize)
{
return ;
}
QString s_NextFortune;
in>>s_NextFortune;
if(s_NextFortune == m_sReadyRead)
{
QTimer::singleShot(0,this,SLOT(CF_Connect()));
return ;
}
m_sReadyRead = s_NextFortune;
ui.textEdit->setText(m_sReadyRead);
return ;

}
void MyClient2::CF_HostFound()
{
//QMessageBox::information(this,tr("Yes ChostFound"),tr("hostFound"));
}
void MyClient2::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(m_TcpSocket->errorString()));
}


}
注我几个函数都声明为了slot
...全文
367 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
swift19221 2010-10-10
  • 打赏
  • 举报
回复
qt 传文件,发现 分帧传输,
发现服务端并不是每收到一帧就触发一次 readyread信号,
怎么解决?
kongyanmin 2010-09-25
  • 打赏
  • 举报
回复
那编译时没有有报错吗
上善若水邻 2010-09-24
  • 打赏
  • 举报
回复
解决了原来是我建工程的时候没有勾上网络模块。。。XXXX的。。。
上善若水邻 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 weigang198248832 的回复:]
你的SOCKET连接建立没?对方给你发数据没?CF_ReadyRead()确定放到SLOT的声明中
[/Quote]
现在发现原来是没有连接上,只是触发hostfound()信号而已,我加上connected()信号和error()信号,才发现没连接成功也没有触发error信号,问题出在哪里啊??怎么就这么难啊。
weigang198248832 2010-09-24
  • 打赏
  • 举报
回复
你的SOCKET连接建立没?对方给你发数据没?CF_ReadyRead()确定放到SLOT的声明中

16,235

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧