注册功能:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include
//数据库管理类 - #include
//执行sql语句的类 - #include
//数据库记录的类 -
- QT_BEGIN_NAMESPACE
- namespace Ui {class Widget;}
- QT_END_NAMESPACE
-
-
-
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
-
- signals:
- void btn1_signal();
-
- public slots:
- void btn1_slot();
-
- void btn2_slot();
-
- void btn3_clicked();
-
- private:
-
- Ui::Widget *ui;
- QPushButton *btn1;
- QPushButton *btn2;
- QPushButton *btn3;
- QLineEdit *edit1;
- QLineEdit *edit2;
- QLabel *lab1;
- QLabel *lab2;
- QLabel *lab3;
-
- QSqlDatabase db;
-
-
-
- };
- #endif // WIDGET_H
- #include "widget.h"
- #include "ui_widget.h"
- #include
- #include
-
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- ,ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //判断自己的数据库对象中,是否包含了要处理的数据库,如果没有包含则添加一个数据库,如果包含了,就可以打开了
- if(!db.contains("mydatabase.db"))
- {
-
- db = QSqlDatabase::addDatabase("QSQLITE");
-
- //设置数据库的名字
- db.setDatabaseName("mydatabase.db");
- }
-
- //此时已经有一个名为mydatabase.db的数据库
- //打开数据库
- if(!db.open())
- {
- QMessageBox::information(this,"失败","数据库打开失败");
- return;
- }
-
- //需要使用sql语句进行创建表的操作
- //准备sql语句
- QString sql = "create table if not exists usr(" //创建表
- "usrname varchar(10) primary key,"
- "password varchar(10))";
-
- //准备语句执行者
- QSqlQuery querry;
-
- if(!querry.exec(sql))
- {
- QMessageBox::information(this, "失败", "创建表失败");
- return;
- }
-
-
-
-
- //将图形化界面的名字改成登录界面
- this->setWindowTitle("LOL");
- //将图标改成自定义图片内容
- this->setWindowIcon(QIcon(":/userName.jpg"));
- //设置ui界面的大小为合适的大小
- this->setFixedSize(QSize(400,300));
-
- //插入一个图标
- lab1 = new QLabel(this);
- lab1->resize(QSize(400,135));
- lab1->move(0,0);
- //内容要是一张图片
- lab1->setPixmap(QPixmap(":/bizhi.png"));
- //设置图片填充
- lab1->setScaledContents(true);
-
- //插入两个行编辑器
- edit1 = new QLineEdit(this);
- edit1->resize(QSize(240,40));
- edit1->move(110,150);
- edit1->setPlaceholderText("账号");
- //输入密码
- edit2 = new QLineEdit(this);
- edit2->resize(QSize(240,40));
- edit2->move(edit1->x(),edit1->y()+55);
- edit2->setPlaceholderText("密码");
- //回显模式是密码模式
- edit2->setEchoMode(QLineEdit::Password);
-
-
- lab2 = new QLabel(this);
- lab2->resize(50,40);
- lab2->setPixmap(QPixmap(":/userName.jpg"));
- lab2->setScaledContents(true);
- lab2->move(edit1->x()-60,edit1->y());
-
- lab3 = new QLabel(this);
- lab3->resize(50,40);
- lab3->setPixmap(QPixmap(":/passwd.jpg"));
- lab3->setScaledContents(true);
- lab3->move(edit2->x()-60,edit2->y());
-
- //两个按钮
- QPushButton *btn1 = new QPushButton("登录",this);
- QPushButton *btn2 = new QPushButton("取消",this);
- QPushButton *btn3 = new QPushButton("注册",this);
- //重设尺寸
- btn1->resize(QSize(60,30));
- btn2->resize(btn1->size());
- btn3->resize(btn1->size());
- //移动位置
- btn1->move(120,edit2->y()+55);
- btn2->move(btn1->x()+80,edit2->y()+55);
- btn3->move(btn1->x()+160,edit2->y()+55);
-
- btn1->setIcon(QIcon(":/login.png"));
- btn2->setIcon(QIcon(":/cancel.png"));
- btn3->setIcon(QIcon(":/mima.png"));
-
- //将两个按钮连到自己定义的槽函数中
-
- connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slot);
- connect(btn2,&QPushButton::clicked,this,&Widget::btn2_slot);
- connect(btn3,&QPushButton::clicked,this,&Widget::btn3_clicked);
- // connect(this, &Widget::btn1_signal,s1,&Second::jump_slot1);
-
- }
-
- void Widget::btn1_slot(){
-
- //获取ui界面中要登录的数据
- QString user =edit1->text();
- QString key = edit2->text();
- int flag=0;
- //准备sql语句
- QString sql = QString("select * from usr where usrname=='%1' and password=='%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_slot(){
-
- int ret =QMessageBox::warning(
- this,
- "LOL",
- "是否退出?",
- QMessageBox::Yes|QMessageBox::No,
- QMessageBox::Yes);
-
- if(ret == QMessageBox::No){
- this->close();
- }
- }
-
-
- void Widget::btn3_clicked(){
- //获取ui界面中要录入的数据
-
- QString usr = edit1->text();
- QString pass = edit2->text();
-
-
- //要确保每个编辑器中都有数据
- if(usr.isEmpty() || pass.isEmpty())
- {
- QMessageBox::information(this,"提示","请将信息填写完整");
- return;
- }
-
- //准备sql语句
- QString sql = QString("insert into usr(usrname,password) "
- "values('%1', '%2')").arg(usr).arg(pass);
-
- //qDebug()<
- //准备语句执行者
- QSqlQuery querry;
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"失败", "添加失败");
- return;
- }else
- {
- QMessageBox::information(this,"成功", "添加成功");
- }
-
- }
- Widget::~Widget()
- {
- }