QT += core gui sql network
/*****************************************************************/
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include <QIcon>
#include <QMessageBox>
#include "second.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
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_btn1_clicked();
void on_btn2_clicked();
void on_le1_cursorPositionChanged(int arg1, int arg2);
void on_le2_cursorPositionChanged(int arg1, int arg2);
void on_btn3_clicked();
signals:
void jump(); //定义一个跳转到其他界面的信号
private:
Ui::Widget *ui;
Second *s1;
QSqlDatabase db; //定义一个数据库的类对象
};
#endif // WIDGET_H
/*****************************************************************/
#ifndef SECOND_H
#define SECOND_H
#include <QDialog>
#include <QTcpSocket>
#include <QMessageBox>
namespace Ui {
class Second;
}
class Second : public QDialog
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
public slots:
void jump_slot(); //自定义了一个槽函数
private slots:
void on_pushButton_2_clicked();
void connected_slot();
void readyRead_slot();
void on_send_clicked();
void on_duankai_clicked();
void disconnected_slot();
private:
Ui::Second *ui;
//定义一个客户端指针
QTcpSocket *socket;
//定义字符串用于接收用户名
QString username;
};
#endif // SECOND_H
/*****************************************************************/
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
s1 = new Second;
connect(this,&Widget::jump,s1,&Second::jump_slot); //将jump信号绑定到Second的槽函数
//添加一个数据库
if(!db.contains("id_passwd.db"))
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("id_passwd.db");
}
//打开数据库
if(!db.open())
{
QMessageBox::information(this,"失败","数据库打开失败");
return;
}
//创建表格,准备sql语句
QString sql = "create table if not exists id_passwd("
"id varchar(10) primary key,"
"passwd varchar(20))";
//准备语句执行者
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","创建表失败");
}
}
Widget::~Widget()
{
delete ui;
}
//登录按钮
void Widget::on_btn1_clicked()
{
QString username = ui->le1->text();
QString password = ui->le2->text();
QString sql = QString("select * from id_passwd where id = '%1' "
"and passwd = '%2'").arg(username).arg(password);
QSqlQuery q;
//登录成功
if (q.exec(sql)&&q.next())
{
QMessageBox::information(this,"登录成功","登录成功");
//跳转到其他界面的代码
emit jump(); //使用关键字emit发射自定义的信号
this->hide();
}
else //登录失败
{
//静态版
int ret = QMessageBox::critical(this,"登录失败",
"账号密码不匹配,是否重新登录",
QMessageBox::Ok|QMessageBox::Cancel);
if(ret==QMessageBox::Ok)
{
//清楚密码框内容
ui->le2->clear();
}
else if(ret==QMessageBox::Cancel)
{
//关闭界面
close();
}
}
}
//取消按钮
void Widget::on_btn2_clicked()
{
//对象版
QMessageBox box(QMessageBox::Question,"退出登录","是否确定退出登录?",
QMessageBox::Yes|QMessageBox::No,
this);
int ret = box.exec();
if(ret == QMessageBox::Yes)
{
//关闭界面
close();
}
}
//输入账号
void Widget::on_le1_cursorPositionChanged(int arg1, int arg2)
{
ui->le1->setPlaceholderText("账号");
}
//输入密码
void Widget::on_le2_cursorPositionChanged(int arg1, int arg2)
{
ui->le2->setPlaceholderText("密码");
ui->le2->setEchoMode(QLineEdit::Password);
ui->le2->setMaxLength(12);
}
//注册按钮对应的槽函数
void Widget::on_btn3_clicked()
{
QString id = ui->le1->text();
QString passwd = ui->le2->text();
if(id.isEmpty()||passwd.isEmpty())
{
QMessageBox::information(this,"提示","填写完整账号或者密码");
return;
}
//准备sql语句
QString sql = QString("insert into id_passwd(id,passwd)"
"values('%1','%2')").arg(id).arg(passwd);
//准备语句的执行者
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","注册失败");
return;
}
else
{
QMessageBox::information(this,"成功","注册成功");
}
}
/*****************************************************************/
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QDialog(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
//给客户端指针实例化对象
socket = new QTcpSocket(this);
connect(socket,&QTcpSocket::connected,this,&Second::connected_slot);
connect(socket,&QTcpSocket::readyRead,this,&Second::readyRead_slot);
connect(socket,&QTcpSocket::disconnected,this,&Second::disconnected_slot);
}
Second::~Second()
{
delete ui;
}
void Second::jump_slot()
{
this->show(); //当接收到信号,展示自己的界面
}
//连接按钮对应的槽函数
void Second::on_pushButton_2_clicked()
{
//获取UI界面上的相关信息
username = ui->le_usr->text();
QString ip = ui->le_ip->text();
quint16 port = ui->le_port->text().toUInt();
//将客户端连接到指定的服务器
socket->connectToHost(ip,port);
//连接成功的话客户端会自动发送一个connected的信号
}
void Second::connected_slot()
{
QMessageBox::information(this,"成功","连接服务器成功");
QString msg = username + ":进入聊天室";
socket->write(msg.toLocal8Bit()); //将数据发给服务器
}
void Second::readyRead_slot()
{
//读取服务器发来的信息
QByteArray msg = socket->readAll();
//将数据展示到界面
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
//发送按钮对应的槽函数
void Second::on_send_clicked()
{
//读取UI界面中输入的内容
QString msg = username + ": " + ui->text->text();
//发送给服务器
socket->write(msg.toLocal8Bit());
ui->text->clear();
}
//断开按钮对应的槽函数
void Second::on_duankai_clicked()
{
QString msg = username + " 离开服务器";
socket->write(msg.toLocal8Bit());
//断开连接
socket->disconnectFromHost();
//此时,客户端会自动发射一个disconnected信号
}
void Second::disconnected_slot()
{
QMessageBox::information(this,"提示","退出成功");
}