将注册功能连接到数据库
#include "mywnd.h"
#include
#include
#include
#include
#include
MyWnd::MyWnd(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(400,350);
this->setWindowTitle("Widget");
this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
lab1 = new QLabel(this);
lab1->resize(400,150);
lab1->setPixmap(QPixmap(":/icon/logo.png"));
lab1->setScaledContents(true);
lab2 = new QLabel(this);
lab2->resize(60,55);
lab2->move(75,160);
lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
lab2->setScaledContents(true);
lab3 = new QLabel(this);
lab3->resize(60,55);
lab3->move(lab2->x(),lab2->y()+65);
lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
lab3->setScaledContents(true);
//账号
edit1 = new QLineEdit(this);
this->edit1->setPlaceholderText("账号名");
edit1->resize(200,55);
edit1->move(lab2->x()+65,lab2->y());
//密码
edit2 = new QLineEdit(this);
this->edit2->setPlaceholderText("******");
edit2->resize(200,55);
edit2->move(lab3->x()+65,lab3->y());
edit2->setEchoMode(QLineEdit::Password);
//登录
bton1 = new QPushButton(QIcon(":/icon/login.png"),"登录",this);
bton1->resize(90,55);
bton1->move(edit2->x()+20,edit2->y()+65);
//取消
bton2 = new QPushButton(QIcon(":/icon/cancel.png"),"取消",this);
bton2->resize(90,55);
bton2->move(bton1->x()+105,bton1->y());
//注册
registerBtn = new QPushButton("注册",this);
registerBtn->resize(90,55);
registerBtn->move(bton1->x()-105,bton1->y());
//按钮连接对应的槽函数
connect(bton1,&QPushButton::clicked,this,&MyWnd::loginbton1_clicked);
connect(bton2,&QPushButton::clicked,this,&MyWnd::cancelbton2_clicked);
connect(registerBtn,&QPushButton::clicked,this,&MyWnd::register_slot);
connect(registerBtn,&QPushButton::clicked,this,&MyWnd::registerBtn_clicked);
if(!db.contains("mydatabase.db")){
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("F:\\C++\\hqyj\\mydatabase.db");
}
//打开数据库
if(!db.open()){
QMessageBox::information(this,"失败","数据库打开失败");
return ;
}
//需要使用Sql语句创建表
QString sql = "create table if not exists stu_info("
"account varchar(10) ,"
"passwd integer)";
//准备语句执行者
QSqlQuery query;
if(!query.exec(sql)){
QMessageBox::information(this,"失败","创建表失败");
return ;
}
}
void MyWnd::loginbton1_clicked()
{
if(edit1->text()=="admin"&&edit2->text()=="123456"){
QMessageBox sucbox(QMessageBox::Information,
"登录",
"登录成功",
QMessageBox::Ok,
this);
int ret = sucbox.exec();
if(ret == QMessageBox::Ok){
this->close();
// emit jumpsignal();
}
}else{
QMessageBox errbox(QMessageBox::Critical,
"登录",
"账号密码不匹配,是否重新登录",
QMessageBox::Ok|QMessageBox::Cancel,
this);
int ret = errbox.exec();
if(ret == QMessageBox::Ok){
edit2->clear();
}else if(ret == QMessageBox::Cancel){
this->close();
}
}
}
void MyWnd::cancelbton2_clicked()
{
int ret = QMessageBox::question(this,
"取消",
"是否确定要退出登录",
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes);
if(ret == QMessageBox::Yes){
this->close();
}
}
void MyWnd::registerBtn_clicked()
{
emit jumpsignal();
}
//注册连接数据库
void MyWnd::register_slot(){
//获取注册要录入的数据
QString account = this->edit1->text();
int passwd = this->edit2->text().toUInt();
//确保每个编辑器都有数据
if(account.isEmpty() || passwd == 0){
QMessageBox::information(this,"提示","请将信息填写完整");
return ;
}
//准备sql语句
QString sql = QString("insert into stu_info(account,passwd)"
"values('%1', %2)").arg(account).arg(passwd);
QSqlQuery query;
if(!query.exec(sql)){
QMessageBox::information(this,"失败","注册失败");
return ;
}else{
QMessageBox::information(this,"成功","注册成功");
}
}
MyWnd::~MyWnd()
{
}