QT-day5

1、添加注册功能到数据库

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMessageBox>      //消息对话框类头文件
#include <QDebug>
#include <QPushButton>
#include <QSqlDatabase>     //数据库管理类
#include <QSqlQuery>        //执行sql语句的类
#include <QSqlRecord>       //数据库记录的类
#include "second.h"


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

signals:
    void my_signal();       //定义一个无参无返回值的信号函数
    void jump();            //自定义跳转信号函数

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:

    void my_slot();         //自定义无参无返回值槽函数

    void on_dengBtn_clicked();

    void on_quBtn_clicked();

    void on_zhuBtn_clicked();

private:
    Ui::Widget *ui;

    second *s1;

    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);
    s1 = new second;
    //使用qt5版本,把登录、注册、取消按钮连接
    connect(ui->dengBtn, &QPushButton::clicked, this, &Widget::my_slot);
    connect(ui->quBtn, &QPushButton::clicked, this, &Widget::my_slot);
    connect(ui->zhuBtn, &QPushButton::clicked, this, &Widget::my_slot);
    connect(this, &Widget::jump, s1, &second::jump_slot);

    this->setWindowTitle("灵能事务所");     //设置窗口标题
    this->setWindowIcon(QIcon(":/pic/biao.jpg"));

    //判断自己的数据库对象中是否包含了要处理的数据库,如果没有包含,则添加一个数据库,如果包含了,就可以打开
    if(!db.contains("mydatabase.db"))
    {
        //添加一个数据库,调用该类中的静态成员函数addDatabase

        db = QSqlDatabase::addDatabase("QSQLITE");

        //设置数据库的名字
        db.setDatabaseName("mydatabase.db");
    }

    //此时已经有一个名为mydatabase.db的数据库
    // 打开数据库
    if(!db.open())
    {
        QMessageBox::information(this, "失败", "打开失败");
        return;
    }

    //需要使用sql语句进行创建表的操作
    //1、准备sql语句
    QString sql = "create table if not exists stu_info("    //创建表
                  "zczh integer primary key,"               //注册账号,主键
                  "zcmm integer)";                         //注册密码
    //准备语句执行者
    QSqlQuery querry;

    //让语句执行者执行sql语句

    if(!querry.exec(sql))
    {
        QMessageBox::information(this, "失败", "创建表失败");
        return;
    }
}

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

void Widget::my_slot()
{
    //emit jump();
}

//登录按钮对应的槽函数
void Widget::on_dengBtn_clicked()
{
    //传入账号密码
    QString zczh = ui->lineEdit1->text();
    QString zcmm = ui->lineEdit2->text();

    QString sql = QString("select * from stu_info where zczh = '%1' and zcmm = '%2' ").arg(zczh).arg(zcmm);

    //准备语句执行者
    QSqlQuery querry;
    if(!querry.exec(sql))
    {

        //1、调用构造函数实例化对象
        QMessageBox::information(this, "失败", "登录失败");


        //3、对结果进行判断
        if(QMessageBox::Ok)
        {
            ui->lineEdit1->clear();
            ui->lineEdit2->clear();
            qDebug()<<"继续登录";
        }else if(QMessageBox::Cancel)
        {
            close();
        }
    }else
    {
        //1、调用构造函数实例化对象
        QMessageBox::information(this, "成功", "登录成功");

        //3、对结果进行判断
        if(QMessageBox::Ok)
        {
            emit jump();
        }
    }

}

//取消
void Widget::on_quBtn_clicked()
{
    //1、调用静态函数实例化对象
    int ret = QMessageBox::information(this,              //父组件
                         "问题",              //对话框标题
                         "是否确定退出登录",            //对话框文本内容
                         QMessageBox::Yes | QMessageBox::No,    //对话框提供的按钮
                         QMessageBox::No);          //默认选中的按钮

    //3、对结果进行判断
    if(ret == QMessageBox::Yes)
    {
        close();
    }
}

//注册
void Widget::on_zhuBtn_clicked()
{
    //获取ui界面中要录入的数据
    QString zczh = ui->lineEdit1->text();
    QString zcmm = ui->lineEdit2->text();

    //要确保每个编辑器中都有数据
    if(zczh.isEmpty() || zcmm.isEmpty())
    {
        QMessageBox::information(this, "提示", "账号或密码为空,请重新输入");
        return ;
    }

    //准备sql语句
    QString sql = QString("insert into stu_info(zczh, zcmm)"
                  "values('%1', '%2')").arg(zczh).arg(zcmm);

    //准备语句执行者
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this, "失败", "注册失败");
        return;
    }else
    {
        QMessageBox::information(this, "成功", "注册成功");
    }
}

ui界面:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值