完成登录界面的注册功能
头文件
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QApplication>
#include <iostream>
#include <QMessageBox>
#include <QtDebug>
#include <QIcon>
#include<QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <string.h>
#include <QFileDialog>
#include<QSqlDatabase> //数据库管理类
#include<QSqlQuery> //执行sql语句的类
#include<QSqlRecord> //数据库记录的类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
signals:
void my_signal();
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void btn2_clicked();
void btn1_clicked();
void btn3_clicked();
private:
Ui::Widget *ui;
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QLineEdit *edit1;
QLineEdit *edit2;
QSqlDatabase db; //定义一个数据库的类对象
};
#endif // WIDGET_H
源文件:
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置固定尺寸
this->setFixedSize(640,480);
//给程序框重命名并添加图片
this->setWindowTitle("Widget");
this->setWindowIcon(QIcon(":/C:/Users/LENOVO/Desktop/icon/wodepeizhenshi.png"));
//设置登录按钮
btn1=new QPushButton(QIcon(":/C:/Users/LENOVO/Desktop/icon/login.png"),"登录",this);
btn1->setParent(this);
btn1->resize(100,50);
btn1->move(220,400);
//设置注册按钮
btn3=new QPushButton("注册",this);
btn3->setParent(this);
btn3->resize(100,50);
btn3->move(btn1->x()-120,btn1->y());
//设置取消按钮
btn2=new QPushButton(QIcon(":/C:/Users/LENOVO/Desktop/icon/cancel.png"),"取消",this);
btn2->setParent(this);
btn2->resize(btn1->size());
btn2->move(btn1->x()+120,btn1->y());
//添加账号图片
QLabel *lab1=new QLabel(this);
lab1->setPixmap(QPixmap(":/C:/Users/LENOVO/Desktop/icon/userName.jpg"));
lab1->setScaledContents(true);
lab1->resize(50,50);
lab1->move(80,250);
//添加密码图片
QLabel *lab2=new QLabel(this);
lab2->setPixmap(QPixmap(":/C:/Users/LENOVO/Desktop/icon/passwd.jpg"));
lab2->setScaledContents(true);
lab2->resize(50,50);
lab2->move(lab1->x(),lab1->y()+60);
//添加账号文本框
edit1=new QLineEdit(this);
edit1->resize(400,50);
edit1->move(lab1->x()+60,lab1->y());
edit1->setMaxLength(11);
edit1->setPlaceholderText("账号/手机/邮箱");
//添加密码文本框
edit2=new QLineEdit(this);
edit2->resize(400,50);
edit2->move(lab2->x()+60,lab2->y());
edit2->setEchoMode(QLineEdit::Password);
edit2->setMaxLength(11);
//添加顶层图片
QLabel* lab3=new QLabel(this);
lab3->setPixmap(QPixmap(":/C:/Users/LENOVO/Desktop/icon/logo.png"));
lab3->setScaledContents(true);
lab3->resize(640,220);
//判断自己的数据库对象中,是否包含了要处理的数据库,如果没有包含则添加一个数据库,如果包含了,就可以打开了
if(!db.contains("mydata.db"))
{
db = QSqlDatabase::addDatabase("QSQLITE");
//设置数据库的名字
db.setDatabaseName("mydata.db");
}
//此时已经有一个名为mydata.db的数据库
//打开数据库
if(!db.open())
{
QMessageBox::information(this,"失败","数据库打开失败");
return;
}
//需要使用sql语句进行创建表的操作
//准备sql语句
QString sql = "create table if not exists user_reg(" //创建表
"user varchar(10) primary key," //用户
"key varchar(10))"; //密码
//准备语句执行者
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this, "失败", "创建表失败");
return;
}
//判断账户密码
connect(btn1, &QPushButton::clicked, this, &Widget::btn1_clicked);
connect(btn2, &QPushButton::clicked, this, &Widget::btn2_clicked);
connect(btn3, &QPushButton::clicked, this, &Widget::btn3_clicked);
}
Widget::~Widget()
{
delete ui;
}
//注册槽函数
void Widget::btn3_clicked()
{
//获取ui界面中要录入的数据
QString user =edit1->text();
QString key = edit2->text();
//要确保每个编辑器中都有数据
if(user.isEmpty() || key.isEmpty())
{
QMessageBox::information(this,"提示","请将信息填写完整");
return;
}
//准备sql语句
QString sql = QString("insert into user_reg(user,key) "
"values('%1', '%2')").arg(user).arg(key);
//准备语句执行者
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败", "添加失败");
return;
}else
{
QMessageBox::information(this,"成功", "添加成功");
}
}
//登录槽函数
void Widget::btn1_clicked()
{
//获取ui界面中要登录的数据
QString user =edit1->text();
QString key = edit2->text();
int flag=0;
//准备sql语句
QString sql = QString("select * from user_reg where user=='%1' and key=='%2';").arg(user).arg(key);
//准备语句执行者
QSqlQuery querry;
querry.exec(sql);
while(querry.next())
{
flag=1;
}
if(!flag)
{
QMessageBox box(QMessageBox::Critical,
"出错",
"账号密码不匹配,是否重新登录",
QMessageBox::Ok | QMessageBox::Cancel,
this);
int ret=box.exec();
if(ret==QMessageBox::Ok)
{
edit2->clear();
}else
{
this->close();
}
}
else
{
//信息对话框
QMessageBox box(QMessageBox::Information,
"信息对话框",
"登录成功",
QMessageBox::Yes,
this);
box.setButtonText(QMessageBox::Yes,"ok");
int ret=box.exec();
if(ret==QMessageBox::Yes)
{
this->close();
}
}
}
//取消槽函数
void Widget::btn2_clicked()
{
QMessageBox box(QMessageBox::Question,
"问题对话框",
"是否确定要退出登录",
QMessageBox::Yes | QMessageBox::No,
this);
int ret=box.exec();
if(ret==QMessageBox::Yes)
{
this->close();
}
}
测试文件:
cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}