//定时器的设置
UpdatepainterTimer=new QTimer;//创建一个定时器的对象
//UpdatepainterTimer->setInterval(2000);//2s单触发定时器
//timer 超时后会发出timeout()信号,所以在创建好定时器对象后给其建立信号与槽
connect(UpdatepainterTimer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
//SIGNAL(timeout())表示:每当计时结束,计时器归零并重新计时,并发送一个信号激活slot函数。
UpdatepainterTimer->start(2000);
定时发送心跳包
#include<QTimer>
QTimer *timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(heartbeatPacketsSend()));
//客户端与服务器通讯后发送心跳命令,此处可能有点疑问,感觉应该加个定时器啥的
timer->start(1000);
heartbeatPacketsSend();
void TcpClient::heartbeatPacketsSend()
{
QByteArray vehicleInforLogin;
vehicleInforLogin[0]=0x23;
vehicleInforLogin[1]=0x23;
vehicleInforLogin[2]=0x07;
vehicleInforLogin[3]=0xFE;
tcpSocket->write(vehicleInforLogin.data());
}