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.pswp.cn/web/9877.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/9877.shtml
英文地址,請注明出處:http://en.pswp.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 "…