数据库完成登入注册
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include
- #include
- #include
- #include
- #include
- #include
- #include
//消息对话框 //输出函数对应的头文件 - #include
- #include"form.h"
- /*******数据库***********/
- #include
//数据库 - #include
- #include
- #include
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- signals:
- void jump();//自定义跳转信号函数
- void jump2();//自定义跳转信号函数
- private slots:
- void on_btn1_clicked();
- void on_btn2_clicked();
-
- void on_btn3_clicked();
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- private:
- Ui::MainWindow *ui;
- QPushButton *btn1;
- QPushButton *btn2;
- QPushButton *btn3;
- QLineEdit *edit1;
- QLineEdit *edit2;
- QSqlDatabase mydb;//数据库
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- this->setFixedSize(600,400);//设置固定尺寸
- this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
- this->setWindowTitle("易碗浆糊");//窗口的名字
- //this->setStyleSheet("background-color:pink;");//背景颜色
- QLabel *lab1=new QLabel(this);//实例化一个标签
- lab1->resize(600,170);//重新设置尺寸
- lab1->setStyleSheet("background-color:yellow");
- lab1->setAlignment(Qt::AlignCenter);
- lab1->setPixmap(QPixmap(":/new/prefix1/tuku/fjh.png"));
- lab1->setScaledContents(true);
- this->setStyleSheet("background-color:white;");//背景颜色
-
- //用户名框
- edit1=new QLineEdit(this);
- edit1->resize(230,45);
- edit1->move(lab1->x()+200,lab1->y()+200);
- edit1->setPlaceholderText("用户名");//设置占位文本
- //密码框
- edit2=new QLineEdit(this);
- edit2->resize(230,45);
- edit2->move(edit1->x(),edit1->y()+75);
- edit2->setPlaceholderText("密码");//设置占位文本
- edit2->setEchoMode(QLineEdit::Password);//设置回显模式
- //实例化一个标签
- QLabel *lab2=new QLabel(this);
- lab2->resize(45,45);//重新设置尺寸
- lab2->setAlignment(Qt::AlignCenter);
- lab2->setPixmap(QPixmap(":/new/prefix1/tuku/wxn.png"));
- lab2->setScaledContents(true);
- lab2->move(lab1->x()+140,lab1->y()+200);
- //实例化一个标签
- QLabel *lab3=new QLabel(this);
- lab3->resize(45,45);//重新设置尺寸
- lab3->setAlignment(Qt::AlignCenter);
- lab3->setPixmap(QPixmap(":/new/prefix1/tuku/wxnnn.png"));
- lab3->setScaledContents(true);
- lab3->move(lab1->x()+140,lab1->y()+275);
- //实例化一个按钮
- //QPushButton *btn1=new QPushButton(this);
- this->btn1=new QPushButton("btn1",this);
- btn1->setText("登录");
- btn1->resize(70,40);
- btn1->move(lab3->x()+80,lab3->y()+70);
- btn1->setIcon(QIcon(":/new/prefix1/tuku/wq.png"));
- connect(btn1,&QPushButton::clicked,this,&MainWindow::on_btn1_clicked);
-
- // btn1->setStyleSheet("background-color:white;border-radius:10px;");//设置组件背景色
-
- //实例化一个按钮
- // QPushButton *btn2=new QPushButton(this);
- this->btn2=new QPushButton("btn2",this);
- btn2->setText("取消");
- btn2->resize(70,40);
- btn2->move(btn1->x()+130,btn1->y());
- // btn2->setStyleSheet("background-color:white;border-radius:10px;");//设置组件背景色
- btn2->setIcon(QIcon(":/new/prefix1/tuku/wq2.png"));
- connect(btn2,&QPushButton::clicked,this,&MainWindow::on_btn2_clicked);
- //实例化一个按钮
- this->btn3=new QPushButton("btn2",this);
- btn3->setText("注册");
- btn3->resize(70,45);
- btn3->move(edit1->x()+235,edit1->y());
- connect(btn3,&QPushButton::clicked,this,&MainWindow::on_btn3_clicked);
- /************************************************/
- if(!mydb.contains("mydatabase.db"))
- {
- mydb=QSqlDatabase::addDatabase("QSQLITE");
- //设置数据库的名字
- mydb.setDatabaseName("mydatabase.db");
- }
- if(!mydb.open())
- {
- QMessageBox::information(this,"失败","数据库打开失败");
- return;
- }
- //准备sql语句
- QString sql="create table if not exists user_info("
- "name varchar(10),"//用户名
- "mm integer)";//密码
-
-
-
-
- }
- void MainWindow::on_btn1_clicked()
- {
-
- //准备sql语句
- QString sql="select *from user_info";
- //准备语句执行者
- QSqlQuery querry;
- //执行sql语句
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"提示","执行失败");
- return;
- }
- int flg=0;
- QSqlRecord rec = querry.record();//总行
- while(querry.next())
- {
-
- for(int i=0;i
count();i++) - { if(edit1->text()==querry.record().value(0).toString() &&edit2->text()==querry.record().value(1).toString()){
- flg=1;
-
- break;
- }
-
- }
- }
- if(flg==1)//登入成功
- {
- emit jump();
- this->hide();
- }
- else if(flg==0)//数据库没有相应的密码和用户
- {
-
- QMessageBox box(QMessageBox::Critical,"密码错误","账号密码不匹配,是否重新登录",
- QMessageBox::Yes|QMessageBox::No,
- this);
- box.setButtonText(QMessageBox::Yes,"OK");
- box.setButtonText(QMessageBox::No,"cancel");
- int ret=box.exec();
- if(ret==QMessageBox::Yes)
- {
- edit1->clear();//清空
- edit2->clear();//清空
- }else if(ret==QMessageBox::No)
- {
- this->close();
-
- }
- }
-
- if(edit1->text()=="admin" &&edit2->text()=="12345")
- {
-
- emit jump();
- this->hide();
- }
-
-
- }
- void MainWindow::on_btn2_clicked()
- {
-
-
- int ret1= QMessageBox::question(this,"问题","是否确定要退出登录",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
- //对用户选中的按钮进行判断
- if(ret1==QMessageBox::Yes)
- {
- this->close();
- }else if(ret1==QMessageBox::No)
- {
- edit1->clear();//清空
- edit2->clear();//清空
- }
-
- }
-
- //注册按钮的槽函数
- void MainWindow::on_btn3_clicked()
- {
- emit jump2();
- this->hide();
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
-
from.h
- #ifndef FORM_H
- #define FORM_H
- #include
//消息对话框 - #include
- #include
- namespace Ui {
- class Form;
- }
-
- class Form : public QWidget
- {
- Q_OBJECT
-
- public slots:
- void jump_slot();
- void on_bt3_clicked();
- public:
- explicit Form(QWidget *parent = nullptr);
- ~Form();
-
-
-
- private:
- Ui::Form *ui;
-
- };
-
- #endif // FORM_H
form.cpp
- #include "form.h"
- #include "ui_form.h"
-
- Form::Form(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Form)
- {
- ui->setupUi(this);
- this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
- this->setWindowTitle("成功");//窗口的名字
-
- }
-
- Form::~Form()
- {
- delete ui;
- }
- //跳转
- void Form::jump_slot()
- {
-
- this->show();
-
- }
-
-
- void Form::on_bt3_clicked()
- {
- close();
- }
zcfrom.h
- #ifndef ZCFROM_H
- #define ZCFROM_H
- #include
//消息对话框 - #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
//消息对话框 //输出函数对应的头文件 - #include
- #include"form.h"
- /*******数据库***********/
- #include
//数据库 - #include
- #include
- #include
- namespace Ui {
- class zcfrom;
- }
-
- class zcfrom : public QWidget
- {
-
- Q_OBJECT
- signals:
- void jump3();//自定义跳转信号函数
- public slots:
- void jump2_slot();
- public:
- explicit zcfrom(QWidget *parent = nullptr);
- ~zcfrom();
-
- private slots:
- void on_zcBttn_clicked();
-
- void on_fhbttn_clicked();
-
- private:
- Ui::zcfrom *ui;
- QSqlDatabase mydb;//数据库
- };
-
- #endif // ZCFROM_H
zcfrom.cpp
- #include "zcfrom.h"
- #include "ui_zcfrom.h"
-
- zcfrom::zcfrom(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::zcfrom)
- {
- ui->setupUi(this);
- this->setWindowTitle("注册");//窗口的名字
- this->setWindowIcon(QIcon(":/new/prefix1/tuku/yh.png"));//改变左上角图标的位
- if(!mydb.contains("mydatabase.db"))
- {
- mydb=QSqlDatabase::addDatabase("QSQLITE");
- //设置数据库的名字
- mydb.setDatabaseName("mydatabase.db");
- }
- if(!mydb.open())
- {
- QMessageBox::information(this,"失败","数据库打开失败");
- return;
- }
- //准备sql语句
- QString sql="create table if not exists user_info("
- "name varchar(10),"//用户名
- "mm integer)";//密码
- //准备语句执行者
- QSqlQuery querry;
- //让语句执行者指向sql语句
- //成功true失败false
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"失败","失败");
- return;
- }
- }
-
- zcfrom::~zcfrom()
- {
- delete ui;
- }
- void zcfrom::jump2_slot()
- {
-
- this->show();
-
- }
- //确定
- void zcfrom::on_zcBttn_clicked()
- {
-
- QString name=ui->nameEdit->text();
- int mm=ui->mmEdit->text().toInt();
- if(name.isEmpty()||mm==0)
- {
- QMessageBox::information(this,"提示","数据未填写完整");
- return;
- }
- //准备sql语句
- QString sql=QString("insert into user_info(name,mm)"
- "values('%1',%4)").arg(name).arg(mm);
- QSqlQuery querry;
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"失败","注册失败");
- return;
- }else
- {
- ui->nameEdit->clear();
- ui->mmEdit->clear();
- QMessageBox::information(this,"成功","注册成功");
- //此界面消失
- emit jump3();
- this->hide();
- }
- }
- //返回登录按钮的槽函数
-
- void zcfrom::on_fhbttn_clicked()
- {
- emit jump3();
- this->hide();
- }
main.cpp
- #include "mainwindow.h"
- #include "form.h"
- #include
- #include "zcfrom.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- Form s;//定义第二个界面
- QObject::connect(&w,&MainWindow::jump,&s,&Form::jump_slot);
- zcfrom s2;//定义第二个界面
- QObject::connect(&w,&MainWindow::jump2,&s2,&zcfrom::jump2_slot);
-
- QObject::connect(&s2,&zcfrom::jump3,&w,&MainWindow::show);//定义第二个界面
-
- return a.exec();
- }
03dljm.pro
- QT += core gui sql
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++11
-
- # The following define makes your compiler emit warnings if you use
- # any Qt feature that has been marked deprecated (the exact warnings
- # depend on your compiler). Please consult the documentation of the
- # deprecated API in order to know how to port your code away from it.
- DEFINES += QT_DEPRECATED_WARNINGS
-
- # You can also make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- # You can also select to disable deprecated APIs only up to a certain version of Qt.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
-
- SOURCES += \
- form.cpp \
- main.cpp \
- mainwindow.cpp \
- zcfrom.cpp
-
- HEADERS += \
- form.h \
- mainwindow.h \
- zcfrom.h
-
- FORMS += \
- form.ui \
- mainwindow.ui \
- zcfrom.ui
-
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
-
- RESOURCES += \
- ttp.qrc
-
- INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include
- INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv
- INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv2
- LIBS += D:/opencv/opencv3.4-qt-intall/install/x86/mingw/lib/libopencv_*.a
-
智能指针思维导图