day5Qt作业

 服务器端

#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;}
}
//lasrErrorvoid 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.mzph.cn/web/9877.shtml

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

相关文章

代码随想录算法训练营第四十九天| 123.买卖股票的最佳时机III,188.买卖股票的最佳时机IV

目录 题目链接&#xff1a;123.买卖股票的最佳时机III 思路 代码 题目链接&#xff1a;188.买卖股票的最佳时机IV 思路 代码 总结 题目链接&#xff1a;123.买卖股票的最佳时机III 思路 与之前买卖股票不同的是本题要求最多买卖两次&#xff0c;那么dp数组以及递推公式都…

攻击者正在利用AI,对保险公司发起大规模欺诈

保险欺诈一直是保险行业面临的重要挑战之一&#xff0c;尤其随着技术的进步&#xff0c;欺诈者也在不断更新其手段&#xff0c;利用AI技术&#xff0c;包括生成式模型、机器学习和数据分析工具等欺骗保险公司&#xff0c;而AI技术的应用正成为他们的新工具&#xff0c;使其犯罪…

如何打造个人IP?

打造个人IP&#xff08;Intellectual Property&#xff09;是当今社会中越来越受到关注的话题。个人IP指的是个人在某个领域内所拥有的独特的、具有商业价值的知识、技能、品牌和影响力。为什么要打造个人IP&#xff1f;如何打造个人IP&#xff1f;下面我将为您详细解答。 首先…

Navicat连接远程数据库时,隔一段时间不操作出现的卡顿问题

使用 Navicat 连接服务器上的数据库时&#xff0c;如果隔一段时间没有使用&#xff0c;再次点击就会出现卡顿的问题。 如&#xff1a;隔一段时间再查询完数据会出现&#xff1a; 2013 - Lost connection to MySQL server at waiting for initial communication packet, syste…

LinkedList链表

LinkedList 的全面说明 LinkList底层实现了双向链表和双端队列特点可以添加任意元素&#xff08;元素可以重复&#xff09;&#xff0c;包括null线程不安全&#xff0c;没有实现同步 LinkedList 的底层操作机制 LinkedList底层维护了一个双向链表LinkList中维护了两个属性fi…

【算法入门赛】A.坐标变换(推荐学习)C++题解与代码

比赛链接&#xff1a;https://www.starrycoding.com/contest/8 题目描述 武汉市可以看做一个二维地图。 牢 e e e掌握了一项特异功能&#xff0c;他可以“瞬移”&#xff0c;每次瞬移需要分别设定 x x x和 y y y的偏移量 d x dx dx和 d y dy dy&#xff0c;瞬移完成后位置会…

【Fastadmin】表格列改input框输入编辑,以排序权重为例

目录 1.自定义权重排序,以字段sort为例 js列代码 在// 初始化表格table.bootstrapTable({ });的后面添加事件 api里面增加formatter方法,如果存在角色权限问题,控制器添

谷歌外链怎么发?

既要数量也要质量&#xff0c;要保证你的链接广泛分布&#xff0c;在数量上&#xff0c;确实需要你的链接在各种平台上有所展现&#xff0c;这样能提升你网站的知名度和曝光率&#xff0c;但是&#xff0c;光有数量是不够的&#xff0c;如果这些链接的内容不行&#xff0c;那对…

ARIMA模型在河流水质预测中的应用_含代码

#水质模型 #时间序列 #python应用 ARIMA 时间序列模型简介 时间序列是研究数据随时间变化而变化的一种算法&#xff0c;是一种预测性分析算法。它的基本出发点就是事物发展都有连续性&#xff0c;按照它本身固有的规律进行。ARIMA(p,d,q)模型全称为差分自回归移动平均模型 (A…

SSH文件传输

一、设置SSH密钥对&#xff0c;实现记住密码 要避免每次使用scp或ssh时都输入密码&#xff0c;你可以设置SSH密钥对&#xff08;一对公钥和私钥&#xff09;&#xff0c;并将公钥添加到远程服务器上。这样&#xff0c;你的系统可以通过密钥自动验证身份&#xff0c;而无需手动…

Blazor入门-基础知识+vs2022自带例程的理解

参考&#xff1a; Blazor 教程 - 生成首个应用 https://dotnet.microsoft.com/zh-cn/learn/aspnet/blazor-tutorial/intro Blazor基础知识&#xff1a;Visual Studio 2022 中的Blazor开发入门_vs2022 blazor webassembly-CSDN博客 https://blog.csdn.net/mzl87/article/detail…

NSSCTF | [SWPUCTF 2021 新生赛]jicao

打开题目&#xff0c;发现高亮显示了一个 php 脚本 这是脚本的内容 <?php highlight_file(index.php); include("flag.php"); $id$_POST[id]; $jsonjson_decode($_GET[json],true); if ($id"wllmNB"&&$json[x]"wllm") {echo $flag;…

idea中数据库的连接(保姆级)

点击idea中的database 然后再点击加号 创建 然后选择第一栏data source 再选择mysql 然后选择数据库的连接方式 再输入密码 这里我们本来就是localhost所有就不用改 选择端口号 然后点击Test Connection 测试连接 第一次连接会下载连接的文件 我们只需要 等待它下载完成就好了 …

文本批量操作指南:文本合并技巧,批量处理大量文本的方法

在数字化时代&#xff0c;文本处理成为我们日常生活和工作中不可或缺的一部分。无论是整理文档、数据分析还是内容创作&#xff0c;我们都需要处理大量的文本数据。为了提升工作效率&#xff0c;掌握文本批量操作和合并的技巧变得尤为重要。本文将为您提供一份详细的文本批量操…

机器学习算法应用——CART决策树

CART决策树&#xff08;4-2&#xff09; CART&#xff08;Classification and Regression Trees&#xff09;决策树是一种常用的机器学习算法&#xff0c;它既可以用于分类问题&#xff0c;也可以用于回归问题。CART决策树的主要原理是通过递归地将数据集划分为两个子集来构建决…

力扣 256. 粉刷房子 LCR 091. 粉刷房子 python AC

动态规划 class Solution:def minCost(self, costs):row, col len(costs), 3dp [[0] * col for _ in range(row 1)]for i in range(1, row 1):for j in range(col):dp[i][j] costs[i - 1][j - 1]if j 0:dp[i][j] min(dp[i - 1][1], dp[i - 1][2])elif j 1:dp[i][j] m…

【QT教程】QT6硬件高级编程实战案例 QT硬件高级编程

QT6硬件高级编程实战案例 使用AI技术辅助生成 QT界面美化视频课程 QT性能优化视频课程 QT原理与源码分析视频课程 QT QML C扩展开发视频课程 免费QT视频课程 您可以看免费1000个QT技术视频 免费QT视频课程 QT统计图和QT数据可视化视频免费看 免费QT视频课程 QT性能优化视频免…

【GoLang基础】通道(channel)是什么?

问题引出&#xff1a; Go语言中的通道&#xff08;channel&#xff09;是什么&#xff1f; 解答&#xff1a; 通道&#xff08;channel&#xff09;是 Go 语言中用于协程&#xff08;goroutine&#xff09;之间通信和同步的机制。通道提供了一种安全、简单且高效的方式&#x…

idea运行SpringBoot项目爆红提示出现:Java HotSpot(TM) 64-Bit Server VM warning...让我来看看~

在运行SpringBoot项目的时候&#xff0c;发现总有这个警告提示出现&#xff0c;有点强迫症真的每次运行项目都很难受啊&#xff01;那么今天便来解决这个问题&#xff01; 先来看一下提示内容&#xff1a;Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none an…

FreeRTOS标准库例程代码

1.设备STM32F103C8T6 2.工程模板 单片机: 部分单片机的程序例程 - Gitee.comhttps://gitee.com/lovefoolnotme/singlechip/tree/master/STM32_FREERTOS/1.%E5%B7%A5%E7%A8%8B%E6%A8%A1%E6%9D%BF 3.代码 1-FreeRTOS移植模板 #include "system.h" #include "…