QT完成登录注册功能实现页面跳转

思路

1.首先,绘制两个简单的ui界面

2.创建槽函数,创建数据库,数据表

zhuce.h

#ifndef ZHUCE_H
#define ZHUCE_H

#include <QWidget>

namespace Ui {
class ZhuCe;
}

class ZhuCe : public QWidget
{
    Q_OBJECT

public:
    explicit ZhuCe(QWidget *parent = 0);
    ~ZhuCe();

private slots:
    void on_pushButton_clicked();

private:
    Ui::ZhuCe *ui;
};

#endif // ZHUCE_H

zhuce.cpp

#include "zhuce.h"
#include "ui_zhuce.h"
#include "widget.h"
#include<QString>
ZhuCe::ZhuCe(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ZhuCe)
{
    ui->setupUi(this);
}

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

void ZhuCe::on_pushButton_clicked()
{
        sqlite_Init();
        QString username = ui->lineEdit_username->text();
        QString password = ui->lineEdit_passwd->text();
        QString surepass = ui->lineEdit_surepasswd->text();//确认密码
        //判断密码是否一致
        if(password == surepass)
        {
            QString sql=QString("insert into user(username,password) values('%1','%2');")
                    .arg(username).arg(password);
            //创建执行语句对象
            QSqlQuery query;
            //判断执行结果
            if(!query.exec(sql))
            {
                qDebug()<<"insert into error";
                QMessageBox::information(this,"注册认证","插入失败!");
            }
            else
            {
                qDebug()<<"insert into success";
                QMessageBox::information(this,"注册认证","插入成功!");
                Widget *w = new Widget;
                w->show();
                this->close();
            }

        }else{
            QMessageBox::information(this,"注册认证","两次密码输入不一致");
        }

}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include<QtSql/QSqlDatabase> //数据驱动
#include <QtSql/QSqlQuery> //数据库执行语句
#include <QMessageBox>//消息盒子
#include <QDebug>
#include <QWidget>
#include<home.h>
#include<zhuce.h>
#include<caozuo.h>
void sqlite_Init();
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
    ZhuCe *newZhuCe;
signals:
    void mousePressed(const QString &type, const QString &time);

public slots:
    void loginFun();//登录
    void hometoWidget();//返回的槽函数
    void exitFun();//退出


private slots:
    void on_SingUp_clicked();//注册
    void on_clearbtn_clicked();//清除

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include<QString>
#include<QDebug>
#include<QTimer>
#include<QDateTime>
#include <QSqlError>
#include <QMouseEvent>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //登录
    connect(ui->login,SIGNAL(clicked(bool)),this,SLOT(loginFun()));
    //退出应用
    connect(ui->exit,SIGNAL(clicked(bool)),this,SLOT(exitFun()));
    //在构造方法中实例化主界面
    //newHome = new Home;
    //主窗口信号监听,并再次发起
    //connect(newHome,SIGNAL(mySignal()),this,SLOT(hometoWidget()));
    //注册
    newZhuCe = new ZhuCe;
    connect(ui->SingUp,SIGNAL(clicked(bool)),this,SLOT(on_SingUp_clicked()));

    ui->lineEditUser->installEventFilter(this);//安装过滤器
    ui->lineEditPassword->installEventFilter(this);

}

Widget::~Widget()
{
    delete ui;
}
//登录功能
void Widget::loginFun()
{
/*
    QString user=ui->lineEditUser->text();//获取用户名
    QString pwd=ui->lineEditPassword->text();//获取密码
    if(user=="zs"&&pwd=="123"){
        //跳转到主界面
        this->hide();
        newHome->show();
    }else{
        qDebug()<<"登录失败"<<endl;
    }
  */

        sqlite_Init();
        QString username = ui->lineEditUser->text();
        QString password = ui->lineEditPassword->text();
        QString sql=QString("select * from user where username='%1' and password='%2'")
                .arg(username).arg(password);
        //创建执行语句对象
        QSqlQuery query(sql);
        //判断执行结果
        if(!query.next())
        {
            qDebug()<<"Login error";
            QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");
        }
        else
        {
            qDebug()<<"Login success";
            QMessageBox::information(this,"登录认证","登录成功");
            //登录成功后可以跳转到其他页面
            QWidget *w = new QWidget;
            newHome->show();
            this->close();
        }

}

void Widget::hometoWidget()
{
    this->show();
    newHome->hide();

}
//退出功能
void Widget::exitFun()
{
    close();
}

//创建数据库
void sqlite_Init()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
        db.setDatabaseName("user.db");//本地的db文件
        db.setUserName("root");
        db.setPassword("1602251825");
        if(!db.open())
        {
            qDebug()<<"open error";
        }
        //create excle
        QString createsql=QString("create table if not exists user(id integer primary key autoincrement,"
                            "username ntext unique not NULL,"
                            "password ntext not NULL)");
        //创建密码表
        QSqlQuery query;
            query.exec("create table password(pwd varchar primary key)");
            query.exec(QString("insert into password values('123')"));

        if(!query.exec(createsql)){
            qDebug()<<"table create error";
        }
        else{
            qDebug()<<"table create success";
        }



        db.setDatabaseName("F:/c++/ceshi/MyDataBase.db");

}


//注册功能
void Widget::on_SingUp_clicked()
{
    this->hide();
    newZhuCe->show();
}

bool Widget::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->lineEditUser && event->type() == QEvent::MouseButtonPress)
       {
           QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
           QString type = "输入用户名";
           QDateTime currentDateTime = QDateTime::currentDateTime();
           QString time = currentDateTime.toString("yyyy.MM.dd \n hh:mm:ss");
           emit mousePressed(type, time);
        //qDebug() << "Mouse Press Type:" << type << "Time:" << time;
           return true;
       }


    if (obj == ui->lineEditPassword && event->type() == QEvent::MouseButtonPress)
       {
           QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
           QString type = "输入密码";
           QDateTime currentDateTime = QDateTime::currentDateTime();
           QString time = currentDateTime.toString("yyyy.MM.dd \n hh:mm:ss");

           emit mousePressed(type, time);
        //qDebug() << "Mouse Press Type:" << type << "Time:" << time;
           return true;
       }
       return QWidget::eventFilter(obj, event);
}



//清除功能
void Widget::on_clearbtn_clicked()
{
    ui->lineEditUser->clear();
    ui->lineEditPassword->clear();
}

运行截图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

残念惭忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值