闹钟
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer=new QTimer(this);
connect(timer,&QTimer::timeout,this,&Widget::updatetime_slot);
ui->startbtn->setDefault(true);
timer->start(1000);
//updatetime_slot();
}
Widget::~Widget()
{
delete ui;
}
void Widget::updatetime_slot()
{
QString currenttime=QDateTime::currentDateTime().toString("hh:mm:ss");
ui->label->setText(currenttime);
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId()==clocktimerid1)
{
if(ui->lineEdit->text()==ui->label->text())
{
ui->textEdit->setText("闹钟响了!!!");
ui->flaglab->clear();
ui->startbtn->setEnabled(true);
this->killTimer(clocktimerid1);
}
}
}
void Widget::on_startbtn_clicked()
{
clocktimerid1=this->startTimer(1000);
ui->startbtn->setEnabled(false);
ui->flaglab->setText("闹钟已设定!");
}
void Widget::on_stopbtn_released()
{
this->killTimer(clocktimerid1);
ui->startbtn->setEnabled(true);
ui->flaglab->clear();
}
tcp服务器
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
server=new QTcpServer(this); //实例化服务器
connect(server,&QTcpServer::newConnection,this,&Widget::newconnection_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_serverbtn_clicked()
{
if(ui->serverbtn->text()=="启动服务器")
{
quint16 port=ui->portEdit->text().toUInt();//获取输入的端口
if(!server->listen(QHostAddress::Any,port))
{
QMessageBox::information(this,"提示","服务器启动失败!");
return;
}
ui->serverbtn->setText("关闭服务器");
}else
{
server->close();
ui->serverbtn->setText("启动服务器");
}
}
void Widget::newconnection_slot()
{
qDebug()<<"已连接";
QTcpSocket *s=server->nextPendingConnection();//最新套接字连接地址
clientlist.push_back(s);//存入链表
connect(s,&QTcpSocket::readyRead,this,&Widget::readyread_slot);
}
void Widget::readyread_slot()
{
for(auto val:clientlist)//遍历所有客户端
{
if(val->bytesAvailable())//检测套接字中能被读取的字节个数
{
QByteArray msg=val->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
for(auto key:clientlist)//转发给其他客户端
{
key->write(msg);
}
}
}
}
Tcp客户端
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
client = new QTcpSocket(this);
connect(client,&QTcpSocket::connected,this,&Widget::connected_slot);
connect(client,&QTcpSocket::readyRead,this,&Widget::readyread_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connectbtn_released()
{
username=ui->nameEdit->text();
QString ip=ui->IPedit->text();
quint16 port =ui->portEdit->text().toUInt();
client->connectToHost(ip,port);
}
void Widget::connected_slot()
{
QString msg=username+"已连接";
client->write(msg.toLocal8Bit());
}
void Widget::readyread_slot()
{
QByteArray msg=client->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
void Widget::on_sendbtn_released()
{
QString msg=username+":"+ui->msgEdit->text();
client->write(msg.toLocal8Bit());
ui->msgEdit->clear();
}
void Widget::on_disconnectbtn_released()
{
QString msg=username+"已断开";
client->write(msg.toLocal8Bit());
client->disconnectFromHost();
}
void Widget::disconneted_slot()
{
QMessageBox::information(this,"断开","断开成功!");
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 10.10 Qt定时器和Tcp通信
发表评论 取消回复