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);
            }
        }
    }
}
相关推荐
机器视觉知识推荐、就业指导3 小时前
开源QML控件:进度条滑动控件(含源码下载链接)
前端·qt·开源·qml
꧁坚持很酷꧂6 小时前
Linux Ubuntu18.04下安装Qt Craeator 5.12.9(图文详解)
linux·运维·qt
ChoSeitaku7 小时前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
OpenC++10 小时前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft
我真的不会C10 小时前
QT窗口相关控件及其属性
开发语言·qt
云小逸11 小时前
【QQMusic项目界面开发复习笔记】第二章
c++·qt
꧁坚持很酷꧂12 小时前
配置Ubuntu18.04中的Qt Creator为中文(图文详解)
开发语言·qt·ubuntu
快乐飒男13 小时前
Qt基础009(HTTP编程和QJSON)
qt
此刻我在家里喂猪呢15 小时前
Qt指ModbusTcp协议的使用
qt
爱上解放晚晚15 小时前
QT 的.pro 转 vsproject 工程
开发语言·qt