笔记
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QDebug>
#include <QTcpServer>//服务器类
#include <QTcpSocket>//客户端类
#include <QMessageBox>
#include <QList>//链表容器QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_startBtn_clicked();void newConnection_slot();void readyRead();private:Ui::Widget *ui;QTcpServer *server;//定义服务器指针QList<QTcpSocket *> socketList;//定义客户端容器
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给服务器指针 实例化空间server = new QTcpServer(this);
}Widget::~Widget()
{delete ui;
}//启动服务器按钮对应的槽函数
void Widget::on_startBtn_clicked()
{//获取ui界面上输入的端口号quint16 port = ui->portEdit->text().toUInt();//将服务器设置成监听状态if(server->listen(QHostAddress::Any,port)){
// QMessageBox::information(this,"","服务器启动成功");qDebug()<<"服务器启动成功";}else{QMessageBox::information(this,"","服务器启动失败");}//服务器进入了监听状态, 如果有客户端发来连接请求,那么该服务器就会自动发射一个newConnection信号//我们可以将该信号连接到自定义的槽函数connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}//处理newConnection信号的槽函数的实现
void Widget::newConnection_slot()
{qDebug()<<"有新用户连接";//获取最新连接的客户端套接字QTcpSocket* s = server->nextPendingConnection();//将该套接字放入到客户端容器中socketList.push_back(s);//此时,客户端与服务器已经建立起来连接//如果有客户端向服务器发来数据,那么该客户端会自动发射一个readyRead信号//我们可以在connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead);
}//readyRead信号对于的槽函数的实现
void Widget::readyRead()
{//移除无效客户端for(int i=0;i<socketList.count();i++){if(socketList.at(i)->state() == 0){socketList.removeAt(i);}}for(int i=0;i<socketList.count();i++){if(socketList.at(i)->bytesAvailable() != 0){//说明该套接字中有数据//读取该套接字中的所有数据QByteArray msgArray = socketList.at(i)->readAll();//将数据展示到ui->listWidgetui->msgWidget->addItem(QString::fromLocal8Bit(msgArray));qDebug()<<QString::fromLocal8Bit(msgArray);//将数据发送给所有客户端for(int j=0;j<socketList.count();i++){socketList.at(j)->write(msgArray);}}}
}
时钟绘制
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::paintEvent(QPaintEvent *event){drawDialPlate();drawDialLine();
}void Widget::drawDialPlate()
{float dialWidth = 5.0;float radius = 250.0;float startPointX = this->width()*0.5;float startPointY = this->height()*0.5-radius;int hourRotate = 360/12;int quarterHourRotate = 360/60;//实例化一个画家QPainter painter(this);//实例化一个画笔QPen pen(QColor("red"));pen.setWidth(5);//添加画笔到画家painter.setPen(pen);//绘制表盘外圆QRectF rectangle(this->width()*0.5-radius, this->height()*0.5-radius, 2*radius, 2*radius);painter.drawEllipse(rectangle);}void Widget::drawDialLine()
{int Div_Max = 12;//大刻度值int Div_Min = 5;//小刻度值float BaseAngle = 270;//基础仰角int R_Inside = 250;//内部圆半径float startPointX = this->width()*0.5;float radius = 250.0;float startPointY = this->height()*0.5-radius;QPoint Center_pos = QPoint(startPointX,this->height()*0.5);//时钟圆心坐标//实例化一个画家QPainter painter(this);//实例化一个画笔QPen pen(QColor("black"));
// pen.setWidth(10);//添加画笔到画家painter.setPen(pen);for(int Loop = 0; Loop <= Div_Max*Div_Min; Loop++){float Angle = BaseAngle + (360 / (Div_Max * Div_Min))*Loop;//从基础仰角开始绘制的每条刻度线对应的角度int R = R_Inside-1;int x_start = Center_pos.x() + R * cos((Angle / 180) * PI);int y_start = Center_pos.y() + R * sin((Angle / 180) * PI);if(Loop % Div_Min == 0)//判断是否为大刻度{QPen pen(Qt::black);pen.setWidth(4);painter.setPen(pen);R = R_Inside-20;}else //判断是否为小刻度{QPen pen(Qt::black);pen.setWidth(2);painter.setPen(pen);R = R_Inside-15;}int x_end = Center_pos.x() + R * cos((Angle / 180) * PI);int y_end = Center_pos.y() + R * sin((Angle / 180) * PI);painter.drawLine(QPoint(x_start,y_start),QPoint(x_end,y_end));//绘制刻度线}}/*
1程序启动,绘制表盘
2开启一个定时器, 槽函数里执行update
3绘制事件函数, 绘制最新的时分秒针
3.1获取当前的时间,获取时,分,秒数值float 转换成圆形的弧度
3.1画家的原点设置为widget的中心点/每次都旋转画家的正方向 指定角度
3.2绘制时针,分针,秒针
*/