1、聊天室
服务器端
cpp
//头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QListWidget>
#include <QMessageBox>
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
QListWidget *wid;
QLineEdit *Edit;
QPushButton *Btn;
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_clicked();
void newConnect_slot();
void readyRead_slot();
private:
Ui::Widget *ui;
QTcpServer *ser;
QList<QTcpSocket*> cliList;
};
#endif // WIDGET_H
//源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->wid=new QListWidget(this);
wid->resize(800,500);
wid->setFixedSize(760,400);
wid->move(20,20);
this->Edit=new QLineEdit(this);
Edit->resize(500,50);
Edit->move(20,440);
this->Btn=new QPushButton("启动",this);
Btn->resize(200,50);
Btn->move(580,440);
ser=new QTcpServer(this);
QObject::connect(Btn,&QPushButton::clicked,this,&Widget::on_clicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_clicked()
{
if(this->Btn->text()=="启动")
{
quint16 port=this->Edit->text().toUInt();
if(ser->listen(QHostAddress::Any, port) == true)
{
QMessageBox::information(this, "成功", "服务器启动成功");
}else
{
QMessageBox::information(this, "失败", "服务器启动失败");
}
connect(ser, &QTcpServer::newConnection, this, &Widget::newConnect_slot);
//将文本内容更改成 关闭
this->Btn->setText("关闭");
}else
{
//关闭服务器
ser->close(); //关闭监听
//将文件内容更改成 启动
this->Btn->setText("启动");
}
}
void Widget::newConnect_slot()
{
qDebug () <<"有新客户端发来连接请求了,请尽快处理";
//获取最新连接的客户端套接字,并将最新连接的套接字地址返回
QTcpSocket* socket = ser->nextPendingConnection();
//将该客户端套接字放入到客户端容器中
cliList.append(socket);
//当有客户端向服务器发来数据时,当前这个客户端套接字就会自动发射一个readyRead信号
//我们可以将该信号连接到对应的信号处理函数中,处理相关数据
connect(socket, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
}
void Widget::readyRead_slot()
{
//判断客户端容器中,是否有已经退出的客户端,或无效的客户端,如果有,将其进行移除
for(int i=0; i<cliList.length(); i++)
{
if(cliList[i]->state() == QTcpSocket::UnconnectedState)
{//功能:判断当前套接字的状态
//参数:无
//返回值:readyRead_slot表示无效的套接字
cliList.removeAt(i); //将下标为i的客户端套接字从容器中移除
}
}
//再将客户端容器遍历一遍,判断哪个客户端中有数据待读
for(int i=0; i<cliList.length(); i++)
{
if(cliList[i]->bytesAvailable() != 0)
{
//功能:获取当前套接字中待读数据的个数
//参数:无
//返回值:返回套接字中待读数据的个数,如果为0,表示没有数据可读
//如果不等于0,表示有数据可读,可以使用readAll读取数据
QByteArray msg = cliList[i]->readAll();
//将该消息,展示到ui界面上
this->wid->addItem(QString::fromLocal8Bit(msg));
//将该消息转发给所有客户端
for(int j=0; j<cliList.length(); j++)
{
if(i!=j)
{
cliList[j]->write(msg);
}
}
}
}
}
客户端
cpp
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void connnected_slot();
void readyRead_slot();
void disconnectes_slot();
void on_pushButton_clicked();
void on_connectBtn_clicked();
private:
Ui::Widget *ui;
QTcpSocket *cli;
QString userName;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
cli=new QTcpSocket(this);
connect(cli,&QTcpSocket::connected,this,&Widget::connnected_slot);
connect(cli,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
connect(cli,&QTcpSocket::disconnected,this,&Widget::disconnectes_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::connnected_slot()
{
QMessageBox::information(this,"连接","连接成功!");
}
void Widget::readyRead_slot()
{
//将客户端套接字中的消息读取出来
QByteArray msg=cli->readAll();
ui->listWidget->addItem(msg);
}
void Widget::disconnectes_slot()
{
QMessageBox::information(this,"断开","断开服务器");
}
void Widget::on_pushButton_clicked()
{
QString msg=ui->lineEdit->text();
cli->write(msg.toLocal8Bit());
ui->lineEdit->clear();
QListWidgetItem *item=new QListWidgetItem(msg);
item->setTextAlignment(Qt::AlignRight);
ui->listWidget->addItem(item);
}
void Widget::on_connectBtn_clicked()
{
if(ui->connectBtn->text()=="连接服务器")
{
userName = ui->userNameEdit->text();
QString ip=ui->ipEdit->text();
quint16 port =ui->portEdit->text().toUInt();
cli->connectToHost(ip,port);
ui->connectBtn->setText("断开服务器");
}else {
cli->disconnectFromHost();
ui->connectBtn->setText("连接服务器");
}
}
2、完善数据库管理系统
cpp
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_addBtn_clicked();
void on_searchBtn_clicked();
private:
Ui::Widget *ui;
QSqlDatabase db;
};
#endif // WIDGET_H
源文件
#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 double)";
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","创建数据表失败");
return;
} else
{
QMessageBox::information(this,"提示","创建数据表成功");
return;
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_addBtn_clicked()
{
int ui_numb=ui->numbEdit->text().toUInt();
QString ui_name=ui->nameEdit->text();
QString ui_sex=ui->sexEdit->text();
double ui_score=ui->scoreEdit->text().toDouble();
if(ui_sex==NULL||ui_numb==0||ui_score==0||ui_name==NULL)
{
QMessageBox::information(this,"提示","请将信息填写完整");
return;
}else
{
}
//准备sql语句
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,"提示","创建数据表失败");
return;
}else
{
QMessageBox::information(this,"提示","创建数据表成功");
return;
}
}
//查找按钮对应的槽函数
void Widget::on_searchBtn_clicked()
{
QString sql;
if(ui->nameEdit->text()==NULL)
{
sql="select * from Stu";
}else
{
sql=QString("select %1 from Stu").arg(ui->nameEdit->text());
}
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","查询失败");
return;
}
int i=0;
while(query.next())
{
//qDebug()<<query.record();
for(int j=0;j<query.record().count();j++)
{
QTableWidgetItem *item=new QTableWidgetItem(query.record().value(j).toString());
item->setTextAlignment(Qt::AlignCenter);
ui->tableWidget->setItem(i,j,item);
}
i++;
}
}
3、思维导图