day5Qt作业

news2025/2/24 18:32:12

 服务器端

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //准备组件,初始化组件状态
    this->setFixedSize(800,600);
    chatwidget = new QListWidget(this);
    chatwidget->setFixedSize(800,430);
    chatwidget->setEnabled(false);
    portedit = new QLineEdit(this);
    portedit->resize(400,50);
    portedit->move(80,480);
    startbtn = new QPushButton("启动",this);
    startbtn->setStyleSheet("background-color:red");
    startbtn->resize(150,80);
    startbtn->move(520,465);

    //startbtn按钮的点击信号与槽函数连接
    connect(startbtn,&QPushButton::clicked,this,&Widget::startbtn_slot);

    ser = new QTcpServer(this);//实意化服务器类对象

    connect(ser,&QTcpServer::newConnection,this,&Widget::serconnect_slot); //连接 每当新的客户端连接,服务器发出的newconection信号 与对应的槽函数

}
Widget::~Widget()
{
    delete ui;
}

//startbtn按钮的点击信号对应的槽函数
void Widget::startbtn_slot()
{
    if(startbtn->text() == "启动")
    {

        //启动按钮后调用监听
        int port = portedit->text().toUInt();//获取行编辑器输入的端口号
        if(port<1024 || port>49151)
        {
            QMessageBox::information(this,"提示","端口号不可用");
            return;
        }

        if(ser->listen(QHostAddress::Any,port)==true)//设置服务器的IP地址端口号,监听状态
        {
            //启用聊天窗口,禁用端口行编辑器,设置按钮背景色
            chatwidget->setEnabled(true);
            portedit->setEnabled(false);
            startbtn->setStyleSheet("background-color:blue");
            QMessageBox::information(this,"成功","服务器启动成功");
            startbtn->setText("关闭");//启动后将按钮文本设置为关闭

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

        }
    }
    else if(startbtn->text() == "关闭")
    {

        chatwidget->setEnabled(false);
        portedit->setEnabled(true);
        startbtn->setStyleSheet("background-color:red");
        QMessageBox::information(this,"成功","服务器关闭");

        startbtn->setText("开启");//启动后将按钮文本设置为开启

    }
}

//服务器发出的newconection信号对应的槽函数处理
void Widget::serconnect_slot()
{
    QTcpSocket *socket = ser->nextPendingConnection();//返回连接到的客户端信息

    clilist.append(socket);//将信息放到容器中保存

    //每当有客户端向服务器发送数据时,socket会向服务器发送一个readyread信号
    connect(socket,&QTcpSocket::readyRead,this,&Widget::socket_readyread);//将所连接客户端的服务器都连接到槽函数
}

//处理socket发送的readyread信号的槽函数
void Widget::socket_readyread()
{
    //在这个函数内就可以实现数据收发
    //先遍历链表中的客户端,如果是无效链接则删除掉
    for(int i=0;i<clilist.length();i++)
    {
        //判断连接状态
        if(clilist[i]->state() == QTcpSocket::UnconnectedState) //判断这个链接状态是否是无效链接
        {
            //是则删除
            clilist.removeAt(i);
        }
        //不是无效链接判断是否有数据待读
        else if(clilist[i]->bytesAvailable() != 0)
        {
            QByteArray msg = clilist[i]->readAll();//将客户端发来的数据督导msg里面
            chatwidget->addItem(QString::fromLocal8Bit(msg));//将信息展示在聊天框上
            //遍历转发除了发送信息客户端
            for(int j=0;j<clilist.length();j++)
            {
                if(i!=j)
                {
                    clilist[j]->write(msg);
                }
            }
        }

    }
}













客户端 

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(800,600);
    chatwidget = new QListWidget(this);
    chatwidget->resize(800,400);
    chatwidget->setEnabled(false);

    usrlab = new QLabel("用户名",this);
    usrlab->move(30,470);
    usredit = new QLineEdit(this);
    usredit->move(90,460);
    usredit->resize(300,30);

    iplab = new QLabel("ip",this);
    iplab->move(30,510);
    ipedit = new QLineEdit(this);
    ipedit->move(90,500);
    ipedit->resize(300,30);


    portlab = new QLabel("port",this);
    portlab->move(30,545);
    portedit = new QLineEdit(this);
    portedit->move(90,540);
    portedit->resize(300,30);

    msgedit = new QLineEdit(this);
    msgedit->move(90,410);
    msgedit->resize(400,40);
    msgedit->setEnabled(false);

    sendbtn = new QPushButton("发送",this);
    sendbtn->move(510,408);
    sendbtn->resize(150,47);
//    sendbtn->setStyleSheet("background-color:red");
    sendbtn->setEnabled(false);

    connectbtn = new QPushButton("连接服务器",this);
    connectbtn->move(480,490);
    connectbtn->resize(200,80);
    connectbtn->setStyleSheet("background-color:red");

    cli = new QTcpSocket(this);//实意化客户端类对象

    //点击连接服务器按钮的信号与槽函数连接

    connect(connectbtn,&QPushButton::clicked,&Widget::connectbtn_slot);
    connect(sendbtn,&QPushButton::clicked,&Widget::sendbtn_slot);

    //客户端连接信号与槽函数连接
    connect(cli,&QTcpSocket::connected,this,&Widget::connected_slot);
    connect(msgedit,&QLineEdit::textChanged,this,&Widget::sentbtn_available_slot);


}

void Widget::connected_slot()
{
     QMessageBox::information(this,"连接","连接服务器成功!!");
}

void Widget::connectbtn_slot()
{
    //一按钮两用
    if(connectbtn->text()=="连接服务器")
    {
        //点击连接服务器之后获取行编辑器的文本信息
        QString usrname=usredit->text();
        quint16 port=portedit->text().toUInt();
        QString ip=ipedit->text();

        //连接整个后禁用服务器端信息编辑,启用聊天窗口和信息编辑器
        chatwidget->setEnabled(true);
        msgedit->setEnabled(true);
        usredit->setEnabled(false);
        ipedit->setEnabled(false);
        portedit->setEnabled(false);



        //向给定的IP地址端口号发送链接请求
        cli->connectToHost(ip,port);

        connectbtn->setText("断开服务器");

        //连接成功后会发送cli会发送一个connected信号,对这个信号处理即可
    }
    else
    {

        //断开连接整个后启用服务器端信息编辑,禁用聊天窗口和信息编辑器

        usredit->setEnabled(true);
        ipedit->setEnabled(true);
        portedit->setEnabled(true);
        chatwidget->setEnabled(false);
        sendbtn->setEnabled(false);
        msgedit->setEnabled(false);
        QString msg = usrname + "离开聊天室" ;
        cli->write(msg.toLocal8Bit());
        cli->disconnectFromHost();
        connectbtn->setText("连接服务器");
    }
}

void Widget::sendbtn_slot()
{
    QString msg= usrname + ":" + msgedit->text();
    cli->write(msg.toLocal8Bit());
    QString msg1=msgedit->text();
    QListWidgetItem *Item=new QListWidgetItem(msg1);
    Item->setTextAlignment(Qt::AlignRight);   //自己发送的文本右边显示
    chatwidget->addItem(Item);
    msgedit->clear();
}



//处理readyread对应槽函数
void Widget::readyRead_slot()
{
    QByteArray msg =cli->readAll();
    chatwidget->addItem(QString::fromLocal8Bit(msg));

}


void Widget::disconnect_slot()
{
    QMessageBox::information(this,"断开","断开服务器连接成功");
}

void Widget::sentbtn_available_slot()
{
    if(msgedit->text().length()>0)
    {
        sendbtn->setEnabled(true);
    }
}

Widget::~Widget()
{
    delete ui;
}







 学生管理系统

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    if(!db.contains("mydb.db"))
    {
        db = QSqlDatabase::addDatabase("QSQLITE");

        db.setDatabaseName("mydb.db");
    }
    if(!db.open())
    {
        QMessageBox::information(this,"失败","数据库打开失败");
        return;
    }
    QString sql = "create table if not exists Stu(numb int ,name char,sex char,score float);";
    QSqlQuery query;
    if(query.exec(sql)==false)
    {
        QMessageBox::information(this,"提示","创建数据表失败");
        return;
    }

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_addbtn_clicked()
{
    ui->tableWidget->clear();
    //QString sql = QString(%1)
    int ui_numb=ui->numbedit->text().toUInt();
    QString ui_name= ui->nameedit->text();
    QString ui_sex = ui->sexedit->text();
    float ui_score = ui->scoreedit->text().toFloat();
    if(ui_sex==NULL || ui_numb==0 || ui_score==0 || ui_name==NULL)
    {
        QMessageBox::information(this,"提示","请将信息填写完整");
        return;
    }
    else if(!(ui_sex == "男" || ui_sex == "女"))
    {

        QMessageBox::information(this,"提示","性别信息错误");
        return;
    }
    QString sql = QString("insert into Stu values(%1,'%2','%3',%4);").arg(ui_numb).arg(ui_name).arg(ui_sex).arg(ui_score);

    QSqlQuery query;
    if(query.exec(sql))
    {
        QMessageBox::information(this,"提示","添加数据成功");
        ui->numbedit->clear();
        ui->nameedit->clear();
        ui->sexedit->clear();
        ui->scoreedit->clear();
        return;
    }
    else
    {
        QMessageBox::information(this,"提示","添加数据失败");
        return;
    }
}

void Widget::on_searchbtn_clicked()
{
    ui->tableWidget->clear();
    QString sql;
    if(ui->numbedit->text()==NULL)
    {
        sql = "select * from Stu";
    }
    else
    {
        sql = QString("select * from stu where numb=%1;").arg(ui->numbedit->text());
    }
    QSqlQuery query;
    if(!query.exec(sql))
    {
        QMessageBox::information(this,"提示","查询失败");
        return;
    }
    int i=0;
    while(query.next())
    {
        //任意一个查询的结果
        //qDebug() << query.record().value(1).toString();
        for(int j=0;j<query.record().count();j++)
        {
            qDebug() << query.record().value(j).toString();
            QTableWidgetItem *Item=new QTableWidgetItem(query.record().value(j).toString());
            Item->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(i,j,Item);

        }
        i++;
    }
}

void Widget::on_updatabtn_clicked()
{

    ui->tableWidget->clear();
    int ui_numb=ui->numbedit->text().toUInt();
    QString ui_name= ui->nameedit->text();
    QString ui_sex = ui->sexedit->text();
    float ui_score = ui->scoreedit->text().toFloat();
    if(ui_sex==NULL || ui_numb==0 || ui_score==0 || ui_name==NULL)
    {
        QMessageBox::information(this,"提示","请将信息填写完整");
         return;
    }
    else if(!(ui_sex == "男" || ui_sex == "女"))
    {

        QMessageBox::information(this,"提示","性别信息错误");
        return;
    }
    //QString sql = QString("update Stu set numb=%1 where name='%2'").arg(ui_numb).arg(ui_name);
    //qDebug() << sql;
    QString sql = QString("update Stu set name='%1',sex='%2',score=%3 where numb=%4;").arg(ui_name).arg(ui_sex).arg(ui_score).arg(ui_numb);
    QSqlQuery query;
    if(query.exec(sql))//
    {
        QMessageBox::information(this,"提示","修改数据成功");
        ui->numbedit->clear();
        ui->nameedit->clear();
        ui->sexedit->clear();
        ui->scoreedit->clear();
        return;
    }
    else
    {
        QMessageBox::information(this,"提示","修该数据失败");
        query.lastError();
        return;
    }
}
//lasrError

void Widget::on_delbtn_clicked()
{
    ui->tableWidget->clear();
    int ui_numb=ui->numbedit->text().toUInt();
    if( ui_numb==0 )
    {
        QMessageBox::information(this,"提示","请填写正确学号");
        return;
    }
    QString sql = QString("delete from Stu where numb=%1;").arg(ui_numb);
    QSqlQuery query;
    if(query.exec(sql))//
    {
        QMessageBox::information(this,"提示","删除数据成功");
        ui->numbedit->clear();
        ui->nameedit->clear();
        ui->sexedit->clear();
        ui->scoreedit->clear();
        return;
    }
    else
    {
        QMessageBox::information(this,"提示","删除数据失败");
        query.lastError();
        return;
    }
}

void Widget::on_sortbtn_clicked()
{

   //  int ui_numb=ui->numbedit->text().toUInt();
//     QMessageBox box(QMessageBox::Question,"选择","请选择学号或者分数排序",QMessageBox::Ok|QMessageBox::Save);
//     box.setDefaultButton(QMessageBox::Ok);
//     box.setButtonText(QMessageBox::Ok,"学号");
//     box.setButtonText(QMessageBox::Save,"分数");
//     int res = box.exec();
//     QString sql;
//     if(res==QMessageBox::Ok)
//     {
//        sql = QString("select * from Stu order by numb asc");
//     }
//     else if(res==QMessageBox::Save)
//     {
//         sql = QString("select * from Stu order by score asc");
//     }

     QString sql="select * from Stu ORDER BY score;";
     QSqlQuery query;
     ui->tableWidget->clear();
     if(query.exec(sql))//
     {
         QMessageBox::information(this,"提示","更改排序成功");
         ui->numbedit->clear();
         ui->nameedit->clear();
         ui->sexedit->clear();
         ui->scoreedit->clear();
         return;
     }
     else
     {
         QMessageBox::information(this,"提示","更改排序失败");
         query.lastError();
         return;
     }

}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1661172.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Box86源码解读记录

1. 背景说明 Github地址&#xff1a;https://github.com/ptitSeb/box86 官方推荐的视频教程&#xff1a;Box86/Box64视频教程网盘 2. 程序执行主体图 Box86版本: Box86 with Dynarec v0.3.4 主函数会执行一大堆的初始化工作&#xff0c;包括但不限于&#xff1a;BOX上下文 …

三层交换机静态路由连通实验

静态路由是一种手动配置路由表的方式&#xff0c;网络管理员需要手动指定网络中的每一个路由器下一跳路由器的地址&#xff0c;以及到达目的网络的最短路径。静态路由的路由表不会自动更新&#xff0c;如果网络拓扑发生了变化&#xff0c;管理员需要手动更改路由表。 实验拓扑图…

信息系统架构模型_1.单机应用模式和客户机/服务器模式

1.单机应用模式&#xff08;Standalone&#xff09; 单机应用系统是最简单的软件结构&#xff0c;是指运行在一台物理机器上的独立应用程序。这些软件系统&#xff0c;从今天的软件架构上来讲&#xff0c;是很简单&#xff0c;是标准的单机系统。当然至今&#xff0c;这种复杂的…

面向对象 03:类与对象的创建、初始化和使用,通过 new 关键字调用构造方法,以及创建对象过程的内存分析

一、前言 记录时间 [2024-05-10] 系列文章简摘&#xff1a; Java 笔记 01&#xff1a;Java 概述&#xff0c;MarkDown 常用语法整理 Java 笔记 11&#xff1a;Java 方法相关内容&#xff0c;方法的设计原则&#xff0c;以及方法的定义和调用 面向对象 01&#xff1a;Java 面向对…

数学:人工智能领域的基石与灵魂

在科技日新月异的今天&#xff0c;人工智能&#xff08;AI&#xff09;已经渗透到了我们生活的方方面面&#xff0c;从智能家居、智能医疗到自动驾驶、智能客服&#xff0c;AI无处不在。然而&#xff0c;当我们赞叹于AI的神奇时&#xff0c;却往往忽视了其背后的推动力——数学…

Leetcode—2105. 给植物浇水 II【中等】

2024每日刷题&#xff08;131&#xff09; Leetcode—2105. 给植物浇水 II 实现代码 class Solution { public:int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {int size plants.size();int i 0;int j size - 1;int capA capacityA;in…

结合创新!通道注意力+UNet,实现高精度分割

在U-Net网络中加入通道注意力机制能显著提升模型的性能&#xff01; 具体点说是在U-Net的卷积层之后添加一个通道注意力模块&#xff0c;这样这个模块可以学习不同通道之间的权重&#xff0c;并根据这些权重对通道进行加权&#xff0c;从而增强重要通道的特征表示。 这种结合…

【WebGIS实例】(14)MapboxGL 加载地形高程数据

前言 官网示例&#xff1a;Add 3D terrain to a map | Mapbox GL JS | Mapbox 大佬博客&#xff1a;Mapbox GL基础&#xff08;七&#xff09;&#xff1a;地形数据的处理与加载 (jl1mall.com) 加载Mapbox地形数据 map.once(style.load, () > {map.addSource(mapbox-dem,…

常见扩频系统的基础概念和模型

扩频系统是一种通信技术&#xff0c;它通过将信号的频谱扩展到一定程度来实现传输&#xff0c;这种系统的设计和实现涉及到多种不同的方法和技术。 扩频系统的主要特点和好处包括&#xff1a; 抗干扰能力强&#xff1a;由于信号被扩展到较宽的频带上&#xff0c;单位带宽内的功…

代码随想录算法训练营第六十二天|503.下一个更大元素II、42.接雨水

代码随想录算法训练营第六十二天|503.下一个更大元素II、42.接雨水 503.下一个更大元素II 给定一个循环数组 nums &#xff08; nums[nums.length - 1] 的下一个元素是 nums[0] &#xff09;&#xff0c;返回 nums 中每个元素的 下一个更大元素 。 数字 x 的 下一个更大的元…

jenkins使用gitLab(极狐)认证登陆

jenkins安装 GitLab Authentication插件 我因为java版本和最新GitLab Authentication 1.19版本不兼容&#xff0c;选择了本地安装 找个历史版本1.13版本&#xff0c;然后下载到电脑上 - 本地上传插件并安装 在极狐上创建一个应用 - 配置应用信息 应用名&#xff1a;jenkinsLo…

GAMMA Lab——知识图谱和LLM大模型

图机器学习的发展与分类 图基础模型 LLM基础模型 GNN LLM 前沿工作

【Web后端】servlet基本概念

1.ServletAPI架构 HttpServlet继承GenericServletGenericServlet实现了Servlet接口&#xff0c;ServletConfig接口,Serializable接口自定义Servlet继承HttpServlet 2.Servlet生命周期 第一步&#xff1a;容器加载Servlet第二步&#xff1a;调用Servlet的无参构造方法&#xf…

Oracle -在线回缩表

conn scott/tiger DROP TABLE EMP1 PURGE; CREATE TABLE EMP1 AS SELECT * FROM EMP; alter table emp1 enable row movement; -- 启动回缩特性 insert into emp1 select * from emp1; / / commit; -- 增加到14000行 -- 分析表的结构 analyze table emp1 comput…

mysql从库SHOW SLAVE STATUS字段详解

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 mysql从库SHOW SLAVE STATUS字段详解 前言输出字段展示字段说明 前言 在数据库的舞台上&#xff0c;主从同步就像是一场华丽的舞蹈&#xff0c;而SHOW SLAVE STATUS命令则是这场舞蹈的灯光&#xff0…

Spark Streaming笔记总结(保姆级)

万字长文警告&#xff01;&#xff01;&#xff01; 目录 一、离线计算与流式计算 1.1 离线计算 1.1.1 离线计算的特点 1.1.2 离线计算的应用场景 1.1.3 离线计算代表技术 1.2 流式计算 1.2.1 流式计算的特点 1.2.2 流式计算的应用场景 1.2.3 流式计算的代表技术 二…

10个超实用的Excel技巧!207套Excel模板(电商、数据分析、排班表、工作计划表)

职场如战场&#xff0c;掌握Excel技能&#xff0c;让你的工作效率飙升&#xff01; 今天&#xff0c;我给大家分享10个Excel的超实用技巧&#xff0c;让你从Excel小白秒变大神&#xff01; 1️⃣ 快速求和&#xff1a;使用 Alt 一键求和&#xff0c;告别手动输入SUM函数。 …

【HMWeb】HTML使用Leaflet实现本地离线地图Gis应用

下载Leaflet 官网下载&#xff1a;https://leafletjs.com/reference.html CSDN&#xff1a;https://download.csdn.net/download/hmxm6/89291989 选择版本号 添加html文件 加入代码 <!DOCTYPE html> <html lang"en"> <head><meta charset&qu…

智慧公厕解决什么问题?实现了什么样的价值?

公共厕所一直是城市管理的难题&#xff0c;常常面临着卫生条件不佳、管理不善以及使用体验差等问题。为了解决这些困扰城市的难题&#xff0c;智慧公厕应运而生。智慧公厕不仅应用了信息化和数字化技术&#xff0c;还通过全方位的智能化应用&#xff0c;彻底改变了传统公厕的面…

(十)JSP教程——config对象

config对象是脚本程序配置对象&#xff0c;表示当前JSP页面的配置信息。由于JSP页面通常无需配置&#xff0c;因此该对象在JSP页面中比较少见。 config对象可以读取一些初始化参数的值&#xff0c;而这些参数一般在web.xml配置文件中可以看到&#xff0c;并通过config对象的相应…