1> 将网络聊天室重新实现一遍
服务器端
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);ser = new QTcpServer(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_startBtn_clicked()
{if(ui->startBtn->text() == "启动"){quint16 port = ui->lineEdit->text().toUInt();if(ser->listen(QHostAddress::Any,port)==true){QMessageBox::information(this,"成功","服务器启动成功");}else{QMessageBox::information(this,"失败","服务器启动失败");}//当启动服务器后,如果有客户端发来连接请求,那么该服务器端就会自动化发送一个newConnection信号//我们可以将信号,连接到对应的槽函数connect(ser,&QTcpServer::newConnection,this,&Widget::newconnection_slot);ui->startBtn->setText("关闭");}else{ser->close();ui->startBtn->setText("启动");}
}
//关于readyread信号对应的槽函数的定义
void Widget::readyread_slots()
{//判断客户端容器中,是否已经有退出的客户端,或者无效的客户端,或者无效的客户端,如果有,将其进行移除for(int i=0;i<cliList.length();i++){if(cliList[i]->state() == QTcpSocket::UnconnectedState){//功能:判断当前套接字的状态//参数:无//返回值:readyread_slots表示无效的客服端cliList.removeAt(i);}}for(int i=0;i<cliList.length();i++){if(cliList[i]->bytesAvailable()!=0){//获取当前套接字中套接字中的待读数据的个数//参数:无//返回值:返回套接字中待读//如果不等于0;表示有数据可读,可以使用readall读取数据QByteArray msg = cliList[i]->readAll();ui->msgWidget->addItem(QString::fromLocal8Bit(msg));for(int j=0;j<cliList.length();j++){if(i!=j){cliList[j]->write(msg);}}}}}
void Widget::newconnection_slot()
{qDebug() << "有客户端发来连接请求";QTcpSocket *socket = ser->nextPendingConnection();cliList.append(socket);//当有客户端向服务器发来数据时候,当前客户端套接字就会自动发送一个readyread信号//我们就可以connect(socket,&QTcpSocket::readyRead,this,&Widget::readyread_slots);}
客户端
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->resize(800,600);this->setFixedSize(800,600);this->setWindowTitle("网络聊天室");list = new QListWidget(this);list->resize(800,400);edtsend = new QLineEdit(this);edtsend->setPlaceholderText("发送消息");edtsend->resize(450,80);edtsend->move(50,420);edtname = new QLineEdit(this);edtname->setPlaceholderText("用户名");edtname->resize(100,50);edtname->move(50,500);edtip = new QLineEdit(this);edtip->setPlaceholderText("ip");edtip->resize(300,50);edtip->move(150,500);edtport = new QLineEdit(this);edtport->setPlaceholderText("port");edtport->resize(300,50);edtport->move(100,550);btnsend = new QPushButton("发送",this);btnsend->resize(100,50);btnsend->move(600,450);btnlink = new QPushButton("连接服务器",this);btnlink->resize(100,50);btnlink->move(600,550);cli = new QTcpSocket(this);connect(cli,&QTcpSocket::connected,this,&Widget::connected_slot);connect(cli,&QTcpSocket::readyRead,this,&Widget::readyread_slot);connect(cli,&QTcpSocket::disconnected,this,&Widget::disconnectes_slot);connect(btnlink,&QPushButton::clicked,this,&Widget::on_connectBtn_clicked);connect(btnsend,&QPushButton::clicked,this,&Widget::on_sendBtn_clicked);// connect(btn,&QPushButton::clicked,this,&Widget::cliked_slot);
}Widget::~Widget()
{delete ui;
}void Widget::connected_slot()
{QMessageBox::information(this,"连接","连接服务器成功");}void Widget::readyread_slot()
{QByteArray msg = cli->readAll();list->addItem(QString::fromLocal8Bit(msg));}void Widget::disconnectes_slot()
{QMessageBox::information(this, "断开", "断开服务器成功");
}
//信息发送按钮对应的槽函数
void Widget::on_sendBtn_clicked()
{//1、将ui界面上的文本信息获取下来QString msg = userName + ":" + edtsend->text();//2、将该消息发送给服务器cli->write(msg.toLocal8Bit());//3、清空航编辑器中的内容edtsend->clear();//将消息放入ui界面中QListWidgetItem *item = new QListWidgetItem(msg);item->setTextAlignment(Qt::AlignRight);list->addItem(item);
}
//连接服务器按钮对应的槽函数
void Widget::on_connectBtn_clicked()
{if(btnlink->text() == "连接服务器"){userName = edtname->text(); //用户名QString ip = edtip->text(); //ip地址quint16 port = edtport->text().toUInt(); //端口号cli->connectToHost(ip, port);btnlink->setText("断开服务器");}else{QString msg = userName + ": 离开聊天室";cli->write(msg.toLocal8Bit());cli->disconnectFromHost();btnlink->setText("连接服务器");}
}
2> 将数据库管理系统,进行完善
#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;}//程序执行至此,数据库打开成功//准备sql语句QString sql = "create table if not exists Stu(numb int , name char , sex char , score double)";//实例化一个sql语句的的执行者QSqlQuery query;if(!query.exec(sql)){QMessageBox::information(this,"提示","创建数据表失败");return;}}Widget::~Widget()
{delete ui;
}//添加学院信息按钮对应的槽函数
void Widget::on_pushButton_clicked()
{//将ui界面上的相关信息获取下来int ui_numb = ui->lineEdit->text().toUInt();QString ui_name = ui->lineEdit_2->text();QString ui_sex = ui->lineEdit_3->text();double ui_score = ui->lineEdit_4->text().toDouble();if(ui_sex==NULL || ui_numb==0 || ui_score==0 || ui_name==NULL){QMessageBox::information(this,"提示","请将信息填写完整");return;}//准备sql语句QString sql = QString("insert into Stu values(%1,'%2','%3',%4)").arg(ui_numb).arg(ui_name).arg(ui_sex).arg(ui_score);//执行sql语句QSqlQuery query;if(!query.exec(sql)){QMessageBox::information(this,"提示","添加数据表失败");return;}else{QMessageBox::information(this,"提示","添加数据表成功");}
}
//删除按钮对应的槽函数
void Widget::on_pushButton_2_clicked()
{QString sql;if(ui->lineEdit_2->text() == NULL){sql = "delete * from Stu";}else{sql = QString("delete from Stu where name='%1'").arg(ui->lineEdit_2->text());}//执行sql语句QSqlQuery query;if(!query.exec(sql)){QMessageBox::information(this,"提示","删除数据表失败");return;}else{QMessageBox::information(this,"提示","删除数据表成功");}}
//修改
void Widget::on_pushButton_3_clicked()
{QString sql;if(ui->lineEdit_2->text() == NULL){sql = "delete * from Stu";}else{sql = QString("update stu set name='%1' from Stu where numb='%2'").arg(ui->lineEdit_2->text()).arg(ui->lineEdit->text());}//执行sql语句QSqlQuery query;if(!query.exec(sql)){QMessageBox::information(this,"提示","更新数据表失败");return;}else{QMessageBox::information(this,"提示","更新数据表成功");}}
//查找按钮对应的槽函数
void Widget::on_pushButton_4_clicked()
{QString sql;if(ui->lineEdit_2->text() == NULL){sql = "select * from Stu";}else{sql = QString("select * from Stu where name='%1'").arg(ui->lineEdit_2->text());}//执行sql语句QSqlQuery query;if(!query.exec(sql)){QMessageBox::information(this,"提示","查询数据表失败");return;}else{QMessageBox::information(this,"提示","查询数据表成功");}//将查询结果展示到ui上int i = 0;while(query.next()){//这里面是任意一个查询的结果。query对象中保存的是当前的一个记录//可以通过成员函数recordqDebug() << query.record().value(1).toString(); //每一次转变成字符串的值//通过条用Qsqlrecord类的成员函数:valuefor(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界面中ui->tableWidget->setItem(i,j,item);}i++;}
}