更新内容

  • 成功配置并连接MYSQL数据库:实现登录与注册功能

    #include "register.h"
    #include "ui_register.h"
    Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
    {
    ui->setupUi(this);
    setWindowTitle("注册界面");

    }

    Register::~Register()
    {
    delete ui;
    }

    void Register::on_pushButton_clicked()
    {
    //连接数据库并检测是否连接成功
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setDatabaseName("chatroom");
    db.setPassword("root");
    db.setPort(3306);
    if(!db.open())
    {
    QString msg = "数据库加载失败: " + db.lastError().text();
    QMessageBox::information(this, "加载数据库", msg);
    }

    //获取并插入用户输入信息
    QString name = ui->name->text();
    QString pass = ui->password->text();
    QSqlQuery query;
    QString sql = "select ID from user";
    query.exec(sql);
    int id=0;
    while(query.next()) id = query.value(0).toInt();
    sql = QString("insert into user(ID,Name,Pass) values('%1','%2','%3')").arg(id+1).arg(name).arg(pass);

    //对数据库数据进行保护
    db.transaction();
    bool flag = query.exec(sql);
    if(!flag)
    {
    db.rollback();
    QString msg = "注册失败: " + db.lastError().text();
    QMessageBox::information(this, "信息", msg);
    }
    else
    {
    db.commit();
    QString msg = "注册成功!";
    QMessageBox::information(this, "信息", msg);
    this->close();
    }

    db.close();
    }

    #include "login.h"
    #include "ui_login.h"
    #include "register.h"
    #include "mainwindow.h"
    Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
    {
    ui->setupUi(this);
    setWindowTitle("登录界面");

    }

    Login::~Login()
    {
    delete ui;
    }

    void Login::on_reg_btn_clicked()
    {
    Register *reg = new Register;
    reg->show();

    }

    void Login::on_login_btn_clicked()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setDatabaseName("chatroom");
    db.setPassword("root");
    db.setPort(3306);
    if(!db.open())
    {
    QString msg = "数据库加载失败: " + db.lastError().text();
    QMessageBox::information(this, "加载数据库", msg);
    }
    QString name = ui->name->text();
    QString pass = ui->password->text();
    QSqlQuery query("select * from user");
    query.exec();
    while(query.next())
    {
    if((name==query.value(1).toString())&&(pass==query.value(2).toString())) //输入正确
    {
    QMessageBox MBox;//定义登陆状态的窗口
    MBox.setWindowTitle("登录状态");
    MBox.setText("登录成功!");
    MBox.addButton("确定", QMessageBox::AcceptRole);
    MBox.exec();
    this->close();
    MainWindow *mw = new MainWindow;
    mw->show();
    db.close();
    return ;
    }
    }
    QMessageBox MBox;//定义登陆状态的窗口
    MBox.setWindowTitle("登录状态");
    MBox.setText("账号或密码错误!");
    MBox.addButton("确定", QMessageBox::AcceptRole);
    MBox.exec();
    db.close();

    }
  • 添加了每次发消息后,输入框内容会自动清空的功能

    ui->msg->clear();
  • 增加了每次发送消息前,对Socket连接状态判断的功能

    //发送信息
    void MainWindow::on_sendMsg_clicked()
    {
    if(!status_flag) //增加了布尔类型的数据成员status_flag,通过断开与连接操作来改变逻辑值
    {
    ui->record->append("消息发送失败,未有客户端连接至服务器....");
    }
    else
    {
    QString msg = ui->msg->toPlainText();
    msg_tcp->write(msg.toUtf8());
    ui->record->append("服务器说:"+ msg);

    }
    }
    //服务器端代码如上所示,客户端代码同理