数据库
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QSqlDatabase>//数据库管理类
#include <QSqlRecord>//记录类
#include <QSqlQuery>//执行sql语句对应的类
#include <QMessageBox>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_inputbtn_clicked();void on_showbtn_clicked();void on_searchbtn_clicked();void on_deletebtn_clicked();private:Ui::Widget *ui;//定义一个数据库对象QSqlDatabase db;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//判断 数据库对象是否包含了自己使用的数据库student.dbif(!db.contains("student.db")){//添加一个数据库//函数原型:static QsqlDatabase addDatabase(const Qstring& type);//参数:数据库的版本db=QSqlDatabase::addDatabase("QSQLITE");//表明使用的是sqlite3版本的数据库//给数据库命名db.setDatabaseName("Student.db");}//给数据库命名if(!db.open()){QMessageBox::information(this,"提示","数据库打开失败");return;}//此时说明数据库已经创建出来并且打开了,就可以创建数据表了//创建数据表需要使用sql语句,需要使用QSQLQuerry类对象来完成//准备sql语句QString sql = "create table if not exists myTable(" //创建表的sql语句"id integer primary key autoincrement," //id主键,允许自增"numb integer," //学号,是整形"name varchar(10)," //姓名 字符串"score integer," //成绩 整型"sex varchar(4))"; //性别 字符串//定义语句执行者QSqlQuery querry;//使用querry执行sql语句if(!querry.exec(sql)){QMessageBox::information(this,"失败","创建表失败");}else{QMessageBox::information(this,"成功","创建表成功");}
}Widget::~Widget()
{delete ui;
}//录入按钮对应的槽函数
void Widget::on_inputbtn_clicked()
{//获取ui界面中要存入数据库中的数据int numb_ui=ui->numEdit->text().toUInt(); //获取ui界面的学号QString name_ui=ui->nameEdit->text(); //获取ui界面上的新名int score_ui = ui->scoreEdit->text().toInt(); //获取ui界面上的成绩QString sex_ui= ui->sexEdit->text(); //获取ui界面上的性别if(numb_ui==0||name_ui.isEmpty()||score_ui==0||sex_ui.isEmpty()){QMessageBox::information(this,"提示","漏填数据");return;}//准备sql语句QString sql=QString("insert into myTable(numb,name,score,sex)""values(%1,'%2',%3,'%4')").arg(numb_ui).arg(name_ui).arg(score_ui).arg(sex_ui);//Qstring s1 = Qstring("%61,%2,9%3").arg(526).arg(3.14).arg("I love China");//定义语句执行官QSqlQuery querry;//调用执行者的相关函数执行sql语句if(!querry.exec(sql)){QMessageBox ::information(this,"失败","插入数据失败");}else{QMessageBox ::information(this,"成功","数据录入成功");}}
//展示按钮对应的槽函数
void Widget::on_showbtn_clicked()
{//准备sql语句QString sql = "select * from myTable";//准备语句执行者QSqlQuery querry;//执行sql语句if(!querry.exec(sql)){QMessageBox ::information(this ,"失败","查询失败");return;}//此时,将查找到的所有结果,全部都放在querry对象中了//可以通过next函数不断遍历查询结int i=0; //记录行号while(querry.next()){//遍历的就是任意一组记录:querry.record// querry.record();// qDebug()<<querry .record()//要找到每条记录中的每个数据使用querry.record().value(i)//qDebug()<<querry.record().value(2);// querry.record().value();//返回当前记录对应数据项的个数// querry.record().value(2).toString();//将记录的某一项的数据转变为字符串//将数据库中的表格展示到ui界面//ui->tableWidget->setItem();for(int j=0;j<querry.record().count()-1;j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}
}//查找
void Widget::on_searchbtn_clicked()
{//获取ui界面中要存入数据库中的数据int numb_ui = ui->numEdit->text().toInt(); //获取学号QString name_ui = ui->nameEdit->text(); //获取姓名int score_ui = ui->scoreEdit->text().toInt(); //获取分数QString sex_ui = ui->sexEdit->text(); //获取性别if(numb_ui != 0 ){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where %1 == numb").arg(numb_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(!name_ui.isEmpty()){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where '%2' == name").arg(name_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(score_ui != 0){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where %3 == score").arg(score_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(!sex_ui.isEmpty()){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where '%4' == sex").arg(sex_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}
}//删除
void Widget::on_deletebtn_clicked()
{int numb_ui = ui->numEdit->text().toInt();QString name_ui = ui->nameEdit->text();int score_ui = ui->scoreEdit->text().toInt();QString sex_ui = ui->sexEdit->text();if (numb_ui != 0) {QString sql = QString("delete from myTable where %1 = numb").arg(numb_ui);QSqlQuery query;if (!query.exec(sql)) {QMessageBox::information(this, "失败", "删除失败");return;} else {QMessageBox::information(this, "成功", "删除成功");}} else if (!name_ui.isEmpty()) {QString sql = QString("delete from myTable where '%1' = name").arg(name_ui);QSqlQuery query;if (!query.exec(sql)) {QMessageBox::information(this, "失败", "删除失败");return;} else {QMessageBox::information(this, "成功", "删除成功");}} else if (score_ui != 0) {QString sql = QString("delete from myTable where %1 = score").arg(score_ui);QSqlQuery query;if (!query.exec(sql)) {QMessageBox::information(this, "失败", "删除失败");return;} else {QMessageBox::information(this, "成功", "删除成功");}} else if (!sex_ui.isEmpty()) {QString sql = QString("delete from myTable where '%1' = sex").arg(sex_ui);QSqlQuery query;if (!query.exec(sql)) {QMessageBox::information(this, "失败", "删除失败");return;} else {QMessageBox::information(this, "成功", "删除成功");}}//清空ui->tableWidget->clear();//准备sql语句QString sql = "select * from myTable";//准备语句执行者QSqlQuery querry;querry.exec(sql);int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}
}
服务器:
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给服务器指针实例化对象server=new QTcpServer(this);
}Widget::~Widget()
{delete ui;
}//启动服务器按钮对应的槽函数
void Widget::on_btn1_clicked()
{//获取ui上的端口号quint16 port=ui->portEdit->text().toUInt();if(!server->listen(QHostAddress::Any,port)){QMessageBox::critical(this,"失败","服务器启动失败");return;}else{QMessageBox::information(this,"成功","服务器启动成功");}connect(server,&QTcpServer::newConnection,this,&Widget::newconnection_slot);}//处理newconneion信号的槽函数
void Widget::newconnection_slot()
{qDebug()<<"新的客户端发来连接请求了" ;QTcpSocket *s=server->nextPendingConnection();//将获取的套接字存放到客户端中clientList.push_back(s);connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);}//处理readRead信号的槽函数
void Widget::readyRead_slot()
{for(int i=0;i<clientList.count();i++){if(clientList[i]->state()==0){clientList.removeAt(i);}}for(int i=0;i<clientList.count();i++){if(clientList[i]->bytesAvailable()!=0){QByteArray msg=clientList[i]->readAll();ui->msglist->addItem(QString::fromLocal8Bit(msg));for(int j=0;j<clientList.count();j++){clientList[j]->write(msg);}}}}
客户端
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给客户端指针实例化空间socket=new QTcpSocket(this);connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);}Widget::~Widget()
{delete ui;
}void Widget::on_connectbtn_clicked()
{//获取ui界面的信息userName =ui->usernameEdit->text();//获取用户名QString hostName= ui->ipEdit->text();//获取主机地址quint16 port = ui->portEdit->text().toUInt();//获取端口号socket->connectToHost(hostName,port);
}//关于connected信号的槽函数的定义
void Widget::connected_slot()
{
QMessageBox::information (this,"成功","连接服务器成功");QString msg = userName + ":进入聊天室";socket->write(msg.toLocal8Bit()) ;}//关于readRead信号对应槽函数的实现
void Widget::readyRead_slot()
{//读取该客户端中的数据QByteArray msg = socket-> readAll();//将数据展示在ui界面ui->msglist->addItem(QString::fromLocal8Bit(msg));
}//发送按钮对应的槽函数
void Widget::on_sendbtn_clicked()
{//获取ui界面中的编辑的文本内容QString m=ui->msgEdit->text();//整合要发送的信息QString msg=userName+":"+m;//将消息发送给服务器socket->write(msg.toLocal8Bit());//将消息编辑框中的内容清空ui->msgEdit->clear( ) ;}
//断开服务器按钮对应的槽函数
void Widget::on_disconnectbtn_clicked()
{//准备要发送的信息QString msg=userName+":离开聊天室";socket->write(msg.toLocal8Bit());socket->disconnectFromHost();}//disconnected信号对应的槽函数实现
void Widget::disconnected_slot()
{QMessageBox::information(this,"退出","断开成功");
}