注册
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include <QMessageBox>
#include<QString>
#include<QSqlDatabase> //数据库管理类
#include<QSqlQuery> //执行sql语句的类
#include<QSqlRecord> //数据库记录的类
#include "form.h"
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QLineEdit *zh;
QLineEdit *mm;
Form *f1;
QSqlDatabase db;
signals:
void my_sig();
void jump();
public slots:
void btn1_slot();
void btn2_slot();
void btn3_slot();
};
#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(400,300);
//设置窗口标题
this->setWindowTitle("HUAQI");
//设置窗口图标
this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
//构建两个按钮
QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
btn1->resize(100,40);
btn1->move(150,250);
QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
btn2->resize(btn1->size());
btn2->move(btn1->x()+125,btn1->y());
connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));
QPushButton *btn3=new QPushButton("注册",this);
btn3->resize(btn1->size());
btn3->move(btn1->x()-125,btn1->y());
connect(btn3,SIGNAL(clicked()),this,SLOT(btn3_slot()));
//构建两个行编辑器
QLineEdit *edit1=new QLineEdit(this);
edit1->resize(200,30);
edit1->setEchoMode(QLineEdit::Password);
edit1->setText("123456");
edit1->move(125,btn1->y()-50);
QLineEdit *edit2=new QLineEdit(this);
edit2->resize(200,30);
edit2->setText("admin");
edit2->move(125,edit1->y()-50);
//构建三个标签
QLabel *lab1=new QLabel(this);
lab1->resize(30,30);
lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
lab1->setScaledContents(true);
lab1->move(edit1->x()-40,edit1->y());
QLabel *lab2=new QLabel(this);
lab2->resize(lab1->size());
lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
lab2->setScaledContents(true);
lab2->move(edit2->x()-40,edit2->y());
QLabel *lab3=new QLabel(this);
lab3->resize(400,120);
lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
lab3->setScaledContents(true);
connect(btn1,&QPushButton::clicked,this,&MainWindow::btn1_slot);
zh=edit2;
mm=edit1;
//设置数据库
if(!db.contains("mydb.db"))
{
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mydb.db");
}
//打开数据库
if(!db.open())
{
QMessageBox::information(this,"失败","数据库打开失败");
return;
}
//创建表
QString sql="create table if not exists stu_info("
"zhanghao varchar(20) primary key,"
"mima varchar(20))";
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","创建表失败");
return;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::btn1_slot()
{
QString i=zh->text();
QString j=mm->text();
qDebug()<<i;
qDebug()<<j;
QString sql=QString("select * from stu_info");
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","查询失败");
return;
}
while(querry.next())
{
qDebug()<<querry.record().value(0).toString();
qDebug()<<querry.record().value(1).toString();
if(querry.record().value(0).toString()==i&&querry.record().value(1).toString()==j)
{
QMessageBox box(QMessageBox::NoIcon,
"成功",
"登陆成功",
QMessageBox::Ok,
this);
int ret=box.exec();
if(ret==QMessageBox::Ok)
{
emit jump();
}
return;
}
}
QMessageBox box(QMessageBox::Warning,
"错误",
"账号或密码出错",
QMessageBox::Yes|QMessageBox::No,
this);
box.setButtonText(QMessageBox::Yes,"继续");
box.setButtonText(QMessageBox::No,"取消");
int ret=box.exec();
if(ret==QMessageBox::Yes)
{
}else
{
this->close();
}
return;
}
void MainWindow::btn2_slot()
{
QMessageBox box(QMessageBox::Question,
"退出",
"确定要退出吗?",
QMessageBox::Yes|QMessageBox::No,
this);
int ret=box.exec();
if(ret==QMessageBox::Yes)
{
this->close();
}
}
void MainWindow::btn3_slot()
{
QString zhanghao=zh->text();
QString mima=mm->text();
if(zhanghao.isEmpty()||mima.isEmpty())
{
QMessageBox::information(this,"提示","信息不完整");
return;
}
QString sql=QString("insert into stu_info(zhanghao,mima)"
"values('%1','%2')").arg(zhanghao).arg(mima);
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","添加失败");
return;
}else
{
QMessageBox::information(this,"成功","添加成功");
}
}
效果图
添加
登录