思路
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();
}