QT DAY4

1.思维导图

2.手动完成服务器的实现,并具体程序要注释清楚

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QList>
#include <QDebug>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    QPushButton *btn1 = new QPushButton;
    QLabel *lab = new QLabel;
    QLabel *lab2 = new QLabel;
    QLineEdit *edit = new QLineEdit;
    QListWidget *list=new QListWidget;

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void btn1_clicked();

    void newConnection_slot();  //自定义处理newConnection信号的槽函数

    void readyRead_slot();  //自定义处理readyRead信号的槽函数


private:
    Ui::Widget *ui;

    //定义客户端容器
    QList<QTcpSocket*> socketList;

    //定义服务器指针
    QTcpServer *server;




};
#endif // WIDGET_H

源文件

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //给服务器指针实例化空间
    server = new QTcpServer(this);

    qDebug()<<this->size();
    qDebug()<<this->rect().size();
    qDebug()<<this->geometry().size();
    qDebug()<<this->frameGeometry().size();
    qDebug()<<"width:"<<this->width()<<"  height:"<<this->height();
    qDebug()<<"width:"<<this->size().width()<<"    height:"<<this->size().height();

    //设定窗口指定大小
    this->setFixedSize(500,500);
    //窗口标题
    qDebug()<<this->windowTitle();  //获取窗口标题
    this->setWindowTitle("服务器");

    //设置标签
    lab=new QLabel(this);
    lab->resize(500,400);
    lab->setAlignment(Qt::AlignCenter);  //垂直和水平全部居中

    //设置EditList窗口,显示运行结果
    list=new QListWidget(this);
    list->resize(500,300);

    //端口号标签
    lab2=new QLabel(this);
    lab2->resize(100,50);
    lab2->setText("端口号:");
    lab2->move(90,350);

    //设定端口号
    edit=new QLineEdit(this);
    edit->resize(200,50);
    edit->move(150,350);

    //设定按钮
    btn1=new QPushButton(this);
    btn1->setText("启动服务端");
    btn1->resize(90,50);
    btn1->move(85,420);
}

Widget::~Widget()
{
    delete ui;
}
//启动服务器按钮对应的槽函数
void Widget::btn1_clicked()
{
    //获取端口号
    quint16 port=edit->text().toUInt();

    if(server->listen(QHostAddress::Any,port))
    {
        QMessageBox::information(this,"","服务器启动成功");

    }else
    {
        QMessageBox::information(this,"","服务器启动失败");
    }

    connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}

//处理newConnection信号的槽函数的实现
void Widget::newConnection_slot()
{
    qDebug()<<"有新客户连接";

    QTcpSocket* s=server->nextPendingConnection();


    //将套接字放入到客户端容器中
    socketList.push_back(s);


    connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}

//关于readyRead信号对应槽函数的实现
void Widget::readyRead_slot()
{
    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 msg = socketList.at(i)->readAll();

            //将数据展示到list窗口中
            list->addItem(QString::fromLocal8Bit(msg));

            //将数据发送给所有客户端
            for(int j=0;j<socketList.count();j++)
            {
                socketList.at(j)->write(msg);
            }
        }
    }
}
相关推荐
winfield82134 分钟前
MCP 协议详解
开发语言·网络·qt
秦jh_2 小时前
【Qt】常用控件(上)
服务器·数据库·qt
刃神太酷啦2 小时前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
水煎包V:YEDIYYDS8883 小时前
QT modbus 通信教程,把modbus封装到线程单例中,在线程内完成数据收发,解析。把重要数据以信号方式通知到qml层展示,解决UI卡顿
qt·modbus·线程服务
东哥很忙XH3 小时前
python使用PyQt5开发桌面端串口通信
开发语言·驱动开发·python·qt
汪宁宇5 小时前
如何在QT5+MinGW环境中编译使用QGIS开发地图应用
c++·qt·qgis·mingw·地图库
刺客xs5 小时前
Qt-----QSS样式表
开发语言·javascript·qt
qq_401700415 小时前
QProgressBar+QSS 进度条
qt
小灰灰搞电子7 小时前
Qt PDF模块详解
数据库·qt·pdf
ibuki_fuko7 小时前
QT/C++ 程序启动时检查程序是否已经启动
开发语言·c++·qt