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);
            }
        }
    }
}
相关推荐
qq_401700414 小时前
Qt Positioning 模块访问设备地理位置信息
开发语言·qt
闫有尽意无琼5 小时前
银河麒麟v11 arm编译Qt creator8.0.2报错
开发语言·qt
lqj_本人5 小时前
鸿蒙Qt触控疑云:事件传递丢失与坐标偏移修复
qt·华为·harmonyos
_OP_CHEN6 小时前
从零开始的Qt开发指南:(五)Qt 常用控件之 QWidget(上):解锁 Qt 界面开发的核心基石
开发语言·c++·qt·前端开发·qwidget·gui开发·qt常用控件
happyjoey21710 小时前
使用Qt自带的Maintenance Tool将Qt6.9升级为QT6.10
开发语言·qt
lqj_本人18 小时前
鸿蒙Qt生命周期:后台被杀后的数据自救
qt·华为·harmonyos
爱码小白1 天前
PyQt5 QTimer总结
开发语言·qt
Jay Chou why did1 天前
13. Qt深入 样式表继承规则
qt
友友马1 天前
『Qt』多元素控件
开发语言·qt
共享家95271 天前
QT-界面优化(中)
开发语言·qt