QTimer类是一种定时器事件
1.使用时需要先创建一个Timer对象
QTimer *timer = new QTimer(this);
2.然后必须建立信号与槽,当Qtimer对象超时后会发出timeout()信号。
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
3.当需要定时器时使用start函数
timer->start(msec );
主要头文件
QudpSocket
服务端代码
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QPushButton>
#include<QLineEdit>
#include<QVBoxLayout>
#include<QLabel>
#include<QtNetwork/QUdpSocket>
#include<QTimer>
//注意命名规范
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QLabel *TimerLabel;
QPushButton *StartButton;
QLineEdit *TextLineEdit;
QVBoxLayout * mainLayout;
public slots:
void StartButtonClicked();
void timerout();
private:
int port;//端口号
bool isStarted;
QUdpSocket *udpsocket;
QTimer *timer;
};
#endif // DIALOG_H
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
TimerLabel=new QLabel(this);
TimerLabel->setText(QStringLiteral("计时器:"));
TextLineEdit=new QLineEdit(this);
StartButton=new QPushButton(this);
StartButton->setText(QString("开始"));
mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(TimerLabel);
mainLayout->addWidget(TextLineEdit);
mainLayout->addWidget(StartButton);
this->setWindowTitle(QStringLiteral("udp server"));
connect(StartButton,&QPushButton::clicked,this,&Dialog::StartButtonClicked);
port=5555;//设置udp的端口号参数
isStarted=false;
udpsocket=new QUdpSocket(this);
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerout()));//定时发送广播信息
}
Dialog::~Dialog()
{
}
void Dialog::StartButtonClicked()
{
if(!isStarted)
{
StartButton->setText(QStringLiteral("停止"));
timer->start(1000);//开启定时器
isStarted=true;
}
else {
StartButton->setText(QStringLiteral("开始"));
isStarted=false;
timer->stop();
}
}
void Dialog::timerout()
{
QString msg=TextLineEdit->text();//获得我们想发送的文字
int length=0;
if(msg==" ")
{
return;
}
if((length=udpsocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=
msg.length())
return;
//QHostAddress::Broadcast指定向广播地址发送
}
客户端代码
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include<QDialog>
#include<QVBoxLayout>
#include<QPushButton>
#include<QTextEdit>
#include<QtNetwork/QUdpSocket>
#include<QMessageBox>
#include<QHostAddress>
class udpclient:public QDialog
{
public:
udpclient();
private:
QVBoxLayout *mainLayout;
QPushButton *CloseButton;
QTextEdit *textedit;
public slots:
void dataReceived();
private:
int port;
QUdpSocket *udpsocket;
};
#endif // UDPCLIENT_H
#include "udpclient.h"
udpclient::udpclient()
{
CloseButton=new QPushButton(this);
CloseButton->setText(tr("close"));
textedit=new QTextEdit(this);
mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(textedit);
mainLayout->addWidget(CloseButton);
this->setWindowTitle(tr("udp clicent"));
port=5555;//设置udp端口。在此上监听数据
udpsocket=new QUdpSocket(this);
connect(CloseButton,&QPushButton::clicked,this,&udpclient::close);
connect(udpsocket,&QUdpSocket::readyRead,this,&udpclient::dataReceived);
//当有数据被读取时,发送信号
bool result=udpsocket->bind(port);//确定是否绑定到指定的端口上
if(!result)
{
QMessageBox::information(this,tr("error"),tr("udp socket creat error"));
return;
}
}
void udpclient::dataReceived()
{
while(udpsocket->hasPendingDatagrams())//判断udpsocket中是否有数据报可读,在至少有一个数据报可读时返回true,否则返回false
{
QByteArray datagram;
datagram.resize(udpsocket->pendingDatagramSize());
udpsocket->readDatagram(datagram.data(),datagram.size());
QString msg=datagram.data();
textedit->insertPlainText(msg);
}
}