实现把注册的信息导入数据库中
second.h
- #ifndef SECOND_H
- #define SECOND_H
-
- #include
- #include
- namespace Ui {
- class Second;
- }
-
- class Second : public QWidget
- {
- Q_OBJECT
- public:
- void newslot();
- public:
- explicit Second(QWidget *parent = nullptr);
- ~Second();
-
- private:
- Ui::Second *ui;
- };
-
- #endif // SECOND_H
widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #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 mysignals();
-
-
- public slots:
- void userLogin();
- void userExit();
- void userregister();
-
- private:
- QPushButton *btn1;
-
- QPushButton *btn2;
- QPushButton *btn3;
- QLabel *lab1;
- QLabel *lab2;
- QLabel *lab3;
- QLineEdit *edit1;
- QLineEdit *edit2;
-
- private:
- Ui::Widget *ui;
- QSqlDatabase db;
- };
- #endif // WIDGET_H
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- //判断自己的数据库对象中,是否包含了要处理的数据库,如果没有包含则添加一个数据库,如果包含了,就可以打开了
- if(!db.contains("mydatabase.db"))
- {
- //添加一个数据库,调用该类中的静态成员函数addDatabase
- 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 stu_info(" //创建表
- " userName varchar(20) primary key," //账号,主键
- "password varchar(20))"; //密码
-
- //准备语句执行者
- QSqlQuery querry;
-
- //让语句执行者执行sql语句
- //函数原型:bool exec(const QString& query);
- //参数:要执行的sql语句
- //返回值:成功执行返回true,失败返回false
- if(!querry.exec(sql))
- {
- QMessageBox::information(this, "失败", "创建表失败");
- return;
- }
-
-
-
- //构造一个登录按钮,并指定父组件,图标,和文本内容
- btn1=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\login.png"),"登录",this);
- //设置固定长度
- this->setFixedSize(400,300);
- //设置窗口标题
- this->setWindowTitle("Widget");
- //设置窗口图标
- this->setWindowIcon(QIcon("C:\\Users\\wuhuiwu\\Desktop\\wodepeizhenshi.png"));
- //设置窗口尺寸
- btn1->setFixedSize(70,40);
- //移动按钮
- btn1->move(150,250);
- //再构造一个按钮,并给定父组件,图标,文本内容
- btn2=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\cancel.png"),"取消",this);
- //设置按钮尺寸
- btn2->setFixedSize(70,40);
- //移动按钮
- btn2->move(btn1->x()+70,btn1->y());
- //再构造一个注册按钮,并给定父组件,图标,文本内容
- btn3=new QPushButton(QIcon("C:\\Users\\wuhuiwu\\Desktop\\register.png"),"注册",this);
- //设置按钮尺寸
- btn3->setFixedSize(70,40);
- //移动按钮
- btn3->move(btn2->x()+70,btn1->y());
- //实例化一个标签并指定父组件
- lab1=new QLabel(this);
- //设置尺寸
- lab1->resize(400,150);
- //设置图片
- lab1->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\logo.png"));
- lab1->setScaledContents(true);
- //实例化一个标签并指定父组件
- lab2=new QLabel(this);
- //设置图片
- lab2->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\userName.jpg"));
- //设置尺寸
- lab2->resize(40,30);
- //引动图标
- lab2->move(120,160);
- lab2->setScaledContents(true);
- //实例化一个标签并指定父组件
- lab3=new QLabel(this);
- //设置图片
- lab3->setPixmap(QPixmap("C:\\Users\\wuhuiwu\\Desktop\\passwd.jpg"));
- //设置尺寸
- lab3->resize(40,30);
- //移动图标
- lab3->move(lab2->x(),lab2->y()+50);
- lab3->setScaledContents(true);
- //构建一个文本编辑器
- edit1=new QLineEdit(this);
- //设置尺寸
- edit1->resize(100,30);
- edit1->move(lab2->x()+60,lab2->y());
- //设置占位文本
- edit1->setPlaceholderText("账号名");
-
- //构建一个文本编辑器
- edit2=new QLineEdit(this);
- //设置尺寸
- edit2->resize(100,30);
- edit2->move(edit1->x(),edit1->y()+50);
- //设置回显模式
- edit2->setEchoMode(QLineEdit::Password);
-
- //登录
- connect(this->btn1,&QPushButton::clicked,this,&Widget::userLogin);
- //退出
- connect(this->btn2,&QPushButton::clicked,this,&Widget::userExit);
-
- //注册
- connect(this->btn3,&QPushButton::clicked,this,&Widget::userregister);
-
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::userLogin()
- {
- QString accout=this->edit1->text();
- QString password=this->edit2->text();
- if(accout=="admin"&&password=="123456")
- {
- qDebug()<<"匹配成功";
-
- QMessageBox box(QMessageBox::NoIcon,"success","登录成功",
- QMessageBox::Ok);
- int res=box.exec();
- if(res==QMessageBox::Ok)
- {
- this->close();
- emit mysignals();
- }
- }else
- {
- qDebug()<<"账户密码不匹配,是否重新登录";
- QMessageBox box1(QMessageBox::Critical,"error","账户密码错误",
- QMessageBox::Ok|QMessageBox::Cancel);
- int res=box1.exec();
- if(res==QMessageBox::Ok)
- {
- this->edit2->clear();
- }
- else if(res==QMessageBox::Cancel)
- {
-
- this->close();
- }
-
- }
-
- }
- void Widget::userExit()
- {
- QMessageBox box2(QMessageBox::Warning,"退出","要退出吗?",
- QMessageBox::Yes|QMessageBox::No);
- int res=box2.exec();
- if(res==QMessageBox::Yes)
-
- {
- this->close();
- }
- else if(res==QMessageBox::No)
- {
- this->edit1->clear();
- this->edit2->clear();
- }
- }
-
- void Widget::userregister()
- {
- //获取ui界面中要录入的数据
- QString userName = this->edit1->text();
- QString password=this->edit2->text();
-
-
- //要确保每个编辑器中都有数据
- if(userName.isEmpty() || password.isEmpty())
- {
- QMessageBox::information(this,"提示","请将信息填写完整");
- return;
- }
- QString sql = QString("insert into stu_info(userName,password) "
- "values('%1', '%2')").arg(userName).arg(password);
- QSqlQuery querry;
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"失败", "添加失败");
- return;
- }else
- {
- QMessageBox::information(this,"成功", "添加成功");
- }
-
- }
second.cpp
- #include "second.h"
- #include "ui_second.h"
-
- Second::Second(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Second)
- {
- ui->setupUi(this);
- }
- void Second::newslot()
- {
- this->show(); //将自己界面进行展示
- }
-
- Second::~Second()
- {
- delete ui;
- }
main.cpp
- #include "widget.h"
- #include "second.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
-
- Second s;
- QObject::connect(&w,&Widget::mysignals,&s,&Second::newslot);
-
- return a.exec();
- }

思维导图
